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 1o8yN9-0007Qa-OJ for pgsql-general@arkaria.postgresql.org; Wed, 06 Jul 2022 06:18:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o8yN8-0003kl-AR for pgsql-general@arkaria.postgresql.org; Wed, 06 Jul 2022 06:18:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o8yN7-0003kb-VV for pgsql-general@lists.postgresql.org; Wed, 06 Jul 2022 06:18:49 +0000 Received: from ms-10.1blu.de ([178.254.4.101]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o8yN5-0006UE-2D for pgsql-general@lists.postgresql.org; Wed, 06 Jul 2022 06:18:48 +0000 Received: from [188.174.58.154] (helo=localhost.unixarea.de) by ms-10.1blu.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o8yN1-0000UP-ER; Wed, 06 Jul 2022 08:18:43 +0200 Received: from localhost.my.domain (localhost [127.0.0.1]) by localhost.unixarea.de (8.16.1/8.14.9) with ESMTPS id 2666Ig1p001493 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 6 Jul 2022 08:18:42 +0200 (CEST) (envelope-from guru@unixarea.de) Received: (from guru@localhost) by localhost.my.domain (8.16.1/8.14.9/Submit) id 2666Ig45001492; Wed, 6 Jul 2022 08:18:42 +0200 (CEST) (envelope-from guru@unixarea.de) X-Authentication-Warning: localhost.my.domain: guru set sender to guru@unixarea.de using -f Date: Wed, 6 Jul 2022 08:18:42 +0200 From: Matthias Apitz To: Christophe Pettus Cc: Laurenz Albe , pgsql-general@lists.postgresql.org Subject: Re: lifetime of the old CTID Message-ID: Reply-To: Matthias Apitz References: <20220705075119.GA23@sh4-5.1blu.de> <03ca00a881de408016a907e31d832175b04aeac7.camel@cybertec.at> <20220705102201.GA9@sh4-5.1blu.de> <1c03b94b606f493bc6448b8559c4cccf806d3607.camel@cybertec.at> <9B418AE6-EA48-4493-B1D1-566708ADC9AC@thebuild.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <9B418AE6-EA48-4493-B1D1-566708ADC9AC@thebuild.com> 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.58.154 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk El día martes, julio 05, 2022 a las 10:44:23p. m. -0700, Christophe Pettus escribió: > > > > On Jul 5, 2022, at 22:35, Matthias Apitz wrote: > > Internally, in the DB layer, the read_where() builds the row list matching > > the WHERE clause as a SCROLLED CURSOR of > > > > SELECT ctid, * FROM d01buch WHERE ... > > > > and each fetch() delivers the next row from this cursor. The functions > > start_transaction() and end_transaction() do what their names suggest and > > rewrite_actual_row() does a new SELECT based on the ctid of the actual row > > > > SELECT * FROM d01buch WHERE ctid = ... FOR UPDATE > > ... > > UPDATE ... > > On first glance, it appears that you are using the ctid as a primary key for a row, and that's highly not-recommended. The ctid is never intended to be stable in the database, as you have discovered. There are really no particular guarantees about ctid values being retained. > > I'd suggest having a proper primary key column on the table, and using that instead. Ofc, each table has its own primary key(s), used for example for the SELECT ctid, * FROM d01buch WHERE ... As I said, we came to PostgreSQL from Sybase (and Oracle) and Sybase has for each table a so called SYB_IDENTITY_COLUMN which is static for the table and its value does not change. When we would add now to some 400 tables an additional INTEGER column (and triggers to fill this on INSERT) this would be a big change in our DB layer and migration of databases in the field. Your suggesting (thanks for it in any case) is not that easy to implement, and no option at the moment. At the moment the DB layer informs the application layer correctly when the row can not be found by the ctid for an UPDATE and the application must handle this situation correctly (logging, 2nd pass through with a new CURSOR etc.). If I understand Laurenz correctly, there seems to be a way to keep the tuple old-ctid : new-ctid for some time, at least until the ney autovacuum. matthias -- Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045 Public GnuPG key: http://www.unixarea.de/key.pub