pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matthias Apitz <guru@unixarea.de>
To: Christophe Pettus <xof@thebuild.com>
Cc: Laurenz Albe <laurenz.albe@cybertec.at>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: lifetime of the old CTID
Date: Wed, 6 Jul 2022 08:18:42 +0200
Message-ID: <YsUpQsTgf2rv1hk+@c720-r368166> (raw)
In-Reply-To: <9B418AE6-EA48-4493-B1D1-566708ADC9AC@thebuild.com>
References: <20220705075119.GA23@sh4-5.1blu.de>
	<03ca00a881de408016a907e31d832175b04aeac7.camel@cybertec.at>
	<20220705102201.GA9@sh4-5.1blu.de>
	<1c03b94b606f493bc6448b8559c4cccf806d3607.camel@cybertec.at>
	<YsReJL8fvHK0D8On@c720-r368166>
	<f363b79595205d42764ad7485aa46ab534f2d318.camel@cybertec.at>
	<YsUfHqpk4Ll1Dej3@c720-r368166>
	<9B418AE6-EA48-4493-B1D1-566708ADC9AC@thebuild.com>

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 <guru@unixarea.de> 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





view thread (25+ messages)  latest in thread

Message-ID: <YsUpQsTgf2rv1hk+@c720-r368166>
Permalink:  ../YsUpQsTgf2rv1hk+@c720-r368166/
Also on:    postgresql.org/message-id/YsUpQsTgf2rv1hk+@c720-r368166

 · 

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: guru@unixarea.de, xof@thebuild.com, laurenz.albe@cybertec.at
  Subject: Re: lifetime of the old CTID
  In-Reply-To: <YsUpQsTgf2rv1hk+@c720-r368166>
  References: <20220705075119.GA23@sh4-5.1blu.de> <03ca00a881de408016a907e31d832175b04aeac7.camel@cybertec.at> <20220705102201.GA9@sh4-5.1blu.de> <1c03b94b606f493bc6448b8559c4cccf806d3607.camel@cybertec.at> <YsReJL8fvHK0D8On@c720-r368166> <f363b79595205d42764ad7485aa46ab534f2d318.camel@cybertec.at> <YsUfHqpk4Ll1Dej3@c720-r368166> <9B418AE6-EA48-4493-B1D1-566708ADC9AC@thebuild.com> <YsUpQsTgf2rv1hk+@c720-r368166>

* 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