On Wed, Jul 06, 2022 at 02:26:00PM +0200, Matthias Apitz wrote: > El d??a Mittwoch, Juli 06, 2022 a las 11:45:14 +0200, Karsten Hilbert escribi??: > > > Am Wed, Jul 06, 2022 at 08:18:42AM +0200 schrieb Matthias Apitz: > > [skipped] > WHERE-clause of its interest ("WHERE d01status=4"), here all books which > are lent to patrons. This gives in the DB layer a CURSOR of say 100.000 > rows of the 3.000.000 in the table. Now the application fetches row > by row and see if something should be done with the row. If so, the > DB layer must LOCK the row for update. It does so using the CTID. Of > course there is a key in the row (d01gsi, the signature of the book), > but this is not uniqu and can't be used to lock exactly this row for update. Then add the primary key to the result of your function. The primary key for a table could easily be found in pg_constraint. You could even collapse it into one field and name it CTID in the resultset, if it is strictly necessary by your logic.