pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Matthias Apitz <guru@unixarea.de>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: function currtid2() in SQL and ESQL/C to get the new CTID of a row
Date: Wed, 01 Jun 2022 09:46:17 -0400
Message-ID: <551140.1654091177@sss.pgh.pa.us> (raw)
In-Reply-To: <20220601105945.GA18@sh4-5.1blu.de>
References: <20220601105945.GA18@sh4-5.1blu.de>

Matthias Apitz <guru@unixarea.de> writes:
> So far so good, but we do need this in ESQL/C. There the code looks as:

>      EXEC SQL BEGIN DECLARE SECTION;
>      char    stmt[255];
>      static char newCTID[80];
>      EXEC SQL END DECLARE SECTION;
               
>      memset(stmt, 0, sizeof(stmt));
>      sprintf(stmt, "currtid2('%s'::text, '%s'::tid)", table, oldCTID);
>      fprintf(stderr, stmt); 
>      fprintf(stderr, "\n"); 

>      EXEC SQL SELECT :stmt INTO :newCTID;
     
>      sprintf(stmt, "table %s oldCTID %s newCTID %s\n",
>                       table, oldCTID, newCTID);
>      fprintf(stderr, stmt);

> The code runs fine but the content of the host variable is the statement
> itself 'currtid2('dbctest'::text, '(0,13)'::tid)' like the SELECT was
> just an echo function.

Indeed.

> Is this function currtid2() not meant to be used in ESQL/C? Or did we
> something wrong in ESQL/C?

This is not about currtid2, this is a fundamental misunderstanding
of how ECPG works.  You can only inject data values into ordinary
EXEC SQL commands.  I think you could handle this as

EXEC SQL SELECT currtid2(:table ::text, :oldCTID ::tid) INTO :newCTID;

If you want full-on dynamic SQL, that's also possible but you'd need
PREPARE/EXECUTE, and it wouldn't look much like this fragment.
I don't see a need for that here, though.

			regards, tom lane





view thread (4+ messages)  latest in thread

Message-ID: <551140.1654091177@sss.pgh.pa.us>
Permalink:  ../551140.1654091177@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/551140.1654091177@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, guru@unixarea.de
  Subject: Re: function currtid2() in SQL and ESQL/C to get the new CTID of a row
  In-Reply-To: <551140.1654091177@sss.pgh.pa.us>
  References: <20220601105945.GA18@sh4-5.1blu.de> <551140.1654091177@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox