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 1o3u2c-0002Q2-Gj for pgsql-general@arkaria.postgresql.org; Wed, 22 Jun 2022 06:40:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o3u1c-0007YC-Kb for pgsql-general@arkaria.postgresql.org; Wed, 22 Jun 2022 06:39:40 +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 1o3u1c-0007Xu-AL for pgsql-general@lists.postgresql.org; Wed, 22 Jun 2022 06:39:40 +0000 Received: from ms-10.1blu.de ([178.254.4.101]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o3u1X-0002DE-UR for pgsql-general@lists.postgresql.org; Wed, 22 Jun 2022 06:39:39 +0000 Received: from [188.174.59.72] (helo=pureos) by ms-10.1blu.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3u1W-0004oB-3y; Wed, 22 Jun 2022 08:39:34 +0200 Date: Wed, 22 Jun 2022 08:39:31 +0200 From: Matthias Apitz To: Tom Lane Cc: pgsql-general@lists.postgresql.org Subject: Re: function currtid2() in SQL and ESQL/C to get the new CTID of a row Message-ID: Reply-To: Matthias Apitz References: <20220601105945.GA18@sh4-5.1blu.de> <551140.1654091177@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <551140.1654091177@sss.pgh.pa.us> X-Operating-System: FreeBSD 13.0-CURRENT r368166 (amd64) X-message-flag: Mails containing HTML will not be read! Please send only plain text. X-Con-Id: 51246 X-Con-U: 0-guru X-Originating-IP: 188.174.59.72 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk El día Wednesday, June 01, 2022 a las 09:46:17AM -0400, Tom Lane escribió: > ... > > > 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; > > ... Hello Tom, We came accross cases where the above SELECT returns as :newCTID the same as the :oldCTID. The :oldCTID was picked up with FETCH from the CURSOR and before locking/updating the row in question we're now checking if its CTID has changed meanwhile we're cycling though the CURSOR. In some cases the CTID is returned as unchanged but a SELECT for UPDATE fails with the CTID. I have here an example of the ESQL/C log: ecpg_execute on line 3480: query: select currtid2 ( 'd01buch' :: text , $1 :: tid ); with 1 parameter(s) on connection sisis ecpg_execute on line 3480: using PQexecParams ecpg_free_params on line 3480: parameter 1 = (671803,22) ecpg_process_output on line 3480: correctly got 1 tuples with 1 fields ecpg_get_data on line 3480: RESULT: (671803,22) offset: 80; array: no ecpg_execute on line 2535: query: declare hc_d01buch cursor for SELECT * FROM d01buch WHERE ctid = $1 FOR UPDATE; with 1 parameter(s) on connection sisis ecpg_execute on line 2535: using PQexecParams ecpg_free_params on line 2535: parameter 1 = (671803,22) ecpg_process_output on line 2535: OK: DECLARE CURSOR ecpg_execute on line 2540: query: fetch hc_d01buch; with 0 parameter(s) on connection sisis ecpg_execute on line 2540: using PQexec ecpg_process_output on line 2540: correctly got 0 tuples with 78 fields raising sqlcode 100 on line 2540: no data found on line 2540 Why is currtid2() returning the old CTID? Looking from another SQL session the CITD of the row is indeed (671803,23), i.e. changed. Thanks matthias -- Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045 Public GnuPG key: http://www.unixarea.de/key.pub