Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nwOgA-0007Qt-UP for pgsql-general@arkaria.postgresql.org; Wed, 01 Jun 2022 13:46:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nwOg7-0008Ck-QN for pgsql-general@arkaria.postgresql.org; Wed, 01 Jun 2022 13:46:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nwOg7-0008Aq-FX for pgsql-general@lists.postgresql.org; Wed, 01 Jun 2022 13:46:27 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nwOg0-0004WK-CK for pgsql-general@lists.postgresql.org; Wed, 01 Jun 2022 13:46:26 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 251DkHpi551143; Wed, 1 Jun 2022 09:46:18 -0400 From: Tom Lane To: Matthias Apitz cc: pgsql-general@lists.postgresql.org Subject: Re: function currtid2() in SQL and ESQL/C to get the new CTID of a row In-reply-to: <20220601105945.GA18@sh4-5.1blu.de> References: <20220601105945.GA18@sh4-5.1blu.de> Comments: In-reply-to Matthias Apitz message dated "Wed, 01 Jun 2022 12:59:45 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <551139.1654091177.1@sss.pgh.pa.us> Date: Wed, 01 Jun 2022 09:46:17 -0400 Message-ID: <551140.1654091177@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Matthias Apitz 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