Re: ON CONFLICT DO SELECT (take 3)

From: Viktor Holmberg <v(at)viktorh(dot)net>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, Marko Tiikkaja <marko(at)joh(dot)to>, Andreas Karlsson <andreas(at)proxel(dot)se>
Subject: Re: ON CONFLICT DO SELECT (take 3)
Date: 2025-12-16 15:14:06
Message-ID: 10ce9553-c345-4792-a894-25e205f441bc@Spark
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 12 Dec 2025 at 07:15 +0100, jian he <jian(dot)universality(at)gmail(dot)com>, wrote:
> On Sat, Nov 29, 2025 at 6:02 AM Viktor Holmberg <v(at)viktorh(dot)net> wrote:
> >
> > Attaching v18 with the above changes. Thanks your continued reviews Jian!
> >
> hi.
> I had some minor comments with doc, comments.
Thanks, all changed now.
> + if (lockStrength == LCS_NONE)
> + {
> + if (!table_tuple_fetch_row_version(relation, conflictTid,
> SnapshotAny, existing))
> + /* The pre-existing tuple was deleted */
> + return false;
> + }
> i think this part should be
> ```
> if (!table_tuple_fetch_row_version(rel, conflictTid, SnapshotAny, existing))
> elog(ERROR, "failed to fetch conflicting tuple for ON CONFLICT");
> ```
>
> say we have a conflict for values (1)
> ``insert into tsa values (1,3) on conflict(a) do select returning *;``
>
> set a GDB breakpoint at ExecOnConflictSelect, let another process do
> ``delete from tsa; vacuum tsa;``
> then let GDB continue.
>
> table_tuple_fetch_row_version can still fetch the tuple.
> so I think this is an unlikely scenario.
Ok, I find this change slightly scary, but I’ve now changed this to assert that table_tuple_fetch_row_version is true. You say “unlikely” but having looked at it for a while I can’t see any case where it’d happen. Hence an assert seems most appropriate. I was thinking about asserting it even before the if as I believe the tuple should always be physically present, but I didn’t dare to. If anyone can think of a case where it’d happen I’d love to hear it!

/Viktor

Attachment Content-Type Size
v19-0001-ON-CONFLICT-DO-SELECT.patch application/octet-stream 131.8 KB
v19-0002-DO-SELECT-Fixes-after-Jians-review-of-v-17.patch application/octet-stream 3.0 KB
v19-0003-rowsecurity-tests-for-ON-CONFLICT-DO-SELECT-FOR-.patch application/octet-stream 4.4 KB
v19-0004-Fixes-after-Jian-s-review-on-the-12th-dec.patch application/octet-stream 15.8 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tomas Vondra 2025-12-16 15:17:51 Re: failed NUMA pages inquiry status: Operation not permitted
Previous Message Adrien Nayrat 2025-12-16 15:08:26 Re: Queries that should be canceled will get stuck on secure_write function