From: | ISHAN CHHANGANI <f20200230h(at)alumni(dot)bits-pilani(dot)ac(dot)in> |
---|---|
To: | "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Extract constants from EXECUTE queries |
Date: | 2024-11-12 11:28:24 |
Message-ID: | SEYPR01MB5313B860CF3CFD5A52753217A7592@SEYPR01MB5313.apcprd01.prod.exchangelabs.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
Hi hackers, I wanted to extract constants hard coded within prepared statements.
ex:-
PREPARE stmt(text, int) as SELECT * FROM test_table WHERE a = $1 AND b = 99 AND c = $2;
EXECUTE stmt('abc', 1);
I can easily get the parameter values ('abc' and 1) from queryDesc->params, but I need to also extract the constant value (99) from within the queryDesc structure during EXECUTE.
I've tried traversing the plan tree like this:
List *constants = NIL;
extract_constants_from_plan(queryDesc->plannedstmt->planTree, &constants);
ListCell *lc;
foreach(lc, constants)
{
Const *c = (Const *) lfirst(lc);
if (!c->constisnull)
{
char *valueStr = NULL;
switch (c->consttype)
{
case BOOLOID:
valueStr = DatumGetBool(c->constvalue) ? "true" : "false";
break;
case INT2OID:
valueStr = psprintf("%d", DatumGetInt16(c->constvalue));
break;
case INT4OID:
valueStr = psprintf("%d", DatumGetInt32(c->constvalue));
break;
case INT8OID:
valueStr = psprintf("%ld", DatumGetInt64(c->constvalue));
break;
case FLOAT4OID:
valueStr = psprintf("%f", DatumGetFloat4(c->constvalue));
break;
case FLOAT8OID:
valueStr = psprintf("%f", DatumGetFloat8(c->constvalue));
break;
case TEXTOID:
case VARCHAROID:
case BPCHAROID:
valueStr = TextDatumGetCString(c->constvalue);
break;
default:
/* For unknown types, try to convert to string using output function */
valueStr = OidOutputFunctionCall(c->consttype, c->constvalue);
break;
}
FILE* fptr = fopen("/Users/abc/test.txt","a");
fprintf(fptr, "Constant value: %s\n", valueStr);
fclose(fptr);
}
}
But this does not seems to work for select statements, though this works for fine for prepared insert statements. Is there a general/ simpler way to do this?
--
The information contained in this electronic communication is intended
solely for the individual(s) or entity to which it is addressed. It may
contain proprietary, confidential and/or legally privileged information.
Any review, retransmission, dissemination, printing, copying or other use
of, or taking any action in reliance on the contents of this information by
person(s) or entities other than the intended recipient is strictly
prohibited and may be unlawful. If you have received this communication in
error, please notify us by responding to this email or telephone and
immediately and permanently delete all copies of this message and any
attachments from your system(s). The contents of this message do not
necessarily represent the views or policies of BITS Pilani.
From | Date | Subject | |
---|---|---|---|
Next Message | Tomas Vondra | 2024-11-12 12:00:08 | Re: Commit Timestamp and LSN Inversion issue |
Previous Message | Tomas Vondra | 2024-11-12 11:25:22 | Re: logical replication: restart_lsn can go backwards (and more), seems broken since 9.4 |