Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

Поиск
Список
Период
Сортировка
Искать

Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Вложения:
> > However, that fix seems to have the same kind of layering
> > violation that Andres complained about for the first version of the
> > patch on this thread -- predicate locking should be in heapam code,
> > not in execIndexing.c.
>
> It's not just the layering violation, that solution also has a leftover issue, similarly to my first patch in this thread. When I looked at this my initial conclusion was that we can't fix the two at the same place without layering violations / remaining issues, so my idea was to apply the same fix as I did for SELECT to those paths, but I'll take another look at it.

And I was wrong, this approach is simple and works for all reported
cases, I attached v4 which uses this approach.

Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
Hi Zsolt,

On 10 Sep 2026, Zsolt Parragi wrote:
> this approach is simple and works for all reported cases

I reviewed v4 and it looks correct to me. I also replaced the MVCC fetch
with just PredicateLockTID(): the ordinary isolation test still passed,
but all three probe-window permutations allowed both transactions to
commit. The new test catches the race in my June approach.

I checked the DO NOTHING and filtered DO UPDATE cases on REL_14_STABLE
too, using the same code change without the injection point. All three
permutations allow both transactions to commit without the fix, and
produce the expected serialization failures with it.

Thank you!


Best regards, Andrey Borodin.



Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
Вложения:
On Sun, 13 Sept 2026 at 16:14, Andrey Borodin  wrote:
>
> +1 to v4
>

OK, I've pushed v4. Thank you both.

Regards,
Dean


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Вложения:
Unfortunately I realized a bit late that v4 can cause a logical
replication crash because the apply worker doesn't have es_snapshot
set. This seems to be an oversight as CreateExecutorState documents
that the caller has to initialize es_snapshot, but
create_edata_for_relation never does so in worker.c

The attached patch sets it and adds a simple test case that shows the problem.

Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
Hi Zsolt,

The snapshot change looks correct to me.

Two small points about the test:

The return value of poll_query_until() should be checked. On timeout it
returns false, so currently the test can continue without confirming
that the old worker has stopped.

ALTER DATABASE ... RESET does not change an existing worker's setting.
A worker starting between TRUNCATE and RESET can retain SERIALIZABLE
for the next test. I'd disable the subscription and wait for the worker
to stop, then truncate, reset the setting, and re-enable it.

Thank you!


Best regards, Andrey Borodin.



Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
On Wed, 23 Sept 2026 at 12:22, Andrey Borodin  wrote:
>
> The snapshot change looks correct to me.

Hmm, I'm not so sure.

I'm thinking now that making ExecCheckIndexConstraints() require
estate->es_snapshot was actually an unintended change of the kind that
probably wasn't safe for back-patching, because it might affect
extension code.

Perhaps, instead, we should make it only do the re-fetch if
estate->es_snapshot is not InvalidSnapshot. For logical replication,
it actually seems unnecessary anyway, since it's only replaying an
update that has already been successfully committed by the publisher.

I wonder if it was actually intentional that logical replication
workers don't set es_snapshot on the EState, since they don't use it
to scan tables, so estate->es_snapshot is not needed.

Regards,
Dean


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
On Wed, 23 Sep 2026, Dean Rasheed  wrote:
> I'm thinking now that making ExecCheckIndexConstraints() require
> estate->es_snapshot was actually an unintended change of the kind that
> probably wasn't safe for back-patching, because it might affect
> extension code.

I forgot to mention this, but the issue I mentioned only exists in 18
and 19, earlier branches are not affected (at least without
extensions).

> Perhaps, instead, we should make it only do the re-fetch if
> estate->es_snapshot is not InvalidSnapshot. For logical replication,
> it actually seems unnecessary anyway, since it's only replaying an
> update that has already been successfully committed by the publisher.

That would work in 18 and 19, but is it the good solution for later
with subscriber side conflict resolution?

> I wonder if it was actually intentional that logical replication
> workers don't set es_snapshot on the EState, since they don't use it
> to scan tables, so estate->es_snapshot is not needed.

In that case, we should add least add a comment there, because right
now it contradicts the comment in CreateExecutorState without any
explanation


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
On Wed, 23 Sept 2026 at 13:51, Zsolt Parragi  wrote:
>
> On Wed, 23 Sep 2026, Dean Rasheed  wrote:
> > I'm thinking now that making ExecCheckIndexConstraints() require
> > estate->es_snapshot was actually an unintended change of the kind that
> > probably wasn't safe for back-patching, because it might affect
> > extension code.
>
> I forgot to mention this, but the issue I mentioned only exists in 18
> and 19, earlier branches are not affected (at least without
> extensions).

Grepping on PGXN, I see no use of ExecCheckIndexConstraints(), so it's
probably your fix is OK.

Regards,
Dean


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
Hi Dean,

On 23 Sep 2026, Dean Rasheed wrote:
> Grepping on PGXN, I see no use of ExecCheckIndexConstraints(), so it's
> probably your fix is OK.

I found a caller in pg_lake [0] that creates its own EState without
setting es_snapshot. It pushes an active snapshot, but passes the
unchanged EState to ExecCheckIndexConstraints(). Your proposed
InvalidSnapshot guard would cover this too.

Thank you!


Best regards, Andrey Borodin.

[0] https://github.com/Snowflake-Labs/pg_lake/blob/44befc068dda9346bca2b0982929d99508d43fdf/pg_lake_table/src/fdw/update_tracking.c#L105-L284



Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Вложения:
> I found a caller in pg_lake [0] that creates its own EState without
> setting es_snapshot. It pushes an active snapshot, but passes the
> unchanged EState to ExecCheckIndexConstraints(). Your proposed
> InvalidSnapshot guard would cover this too.

Maybe we should open a bug report to pg_lake about this? Even if we
allow for this here, the documentation still requires es_snapshot to
be set.

I attached v2 based on Dean's suggestion, if we want to go that route.
I would still be leaning towards including both code changes instead
(or if we do not want that, adjusting the documentation about this),
but for now this is a simple alternative patch that only relaxes the
original fix when we use es_snapshot.

Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:


> On 1 Oct 2026, at 13:28, Zsolt Parragi  wrote:
> 
> Maybe we should open a bug report to pg_lake about this? Even if we
> allow for this here, the documentation still requires es_snapshot to
> be set.

It's the cost of breaking old pg_lake users and possible unknown buggy users
against cost of extra runtime check for all others.

I don't have personal preferences in this choice. But I suspect that project
policy is that Postgres works. Without much surprises.


Best regards, Andrey Borodin.


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
David Christensen <david+pg@pgguru.net>
Дата:
On Thu, Oct 1, 2026 at 9:18 AM Andrey Borodin  wrote:
> > On 1 Oct 2026, at 13:28, Zsolt Parragi  wrote:
> >
> > Maybe we should open a bug report to pg_lake about this? Even if we
> > allow for this here, the documentation still requires es_snapshot to
> > be set.
>
> It's the cost of breaking old pg_lake users and possible unknown buggy users
> against cost of extra runtime check for all others.
>
> I don't have personal preferences in this choice. But I suspect that project
> policy is that Postgres works. Without much surprises.

One of the pg_lake maintainers here; so the basic issue is that we
didn't populate the es_snapshot field in the Estate, so just setting
that to the active current snapshot before calling
ExecCheckIndexConstraints() is the fix?

TL;DR; this is only an 18/19 issue, or it needs backported all the way down?

Thanks,.

David


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
> One of the pg_lake maintainers here; so the basic issue is that we
> didn't populate the es_snapshot field in the Estate, so just setting
> that to the active current snapshot before calling
> ExecCheckIndexConstraints() is the fix?

Yes.

> TL;DR; this is only an 18/19 issue, or it needs backported all the way down?

If we go with the second version of the fix, which is more defensive,
there will be no issues in any branches even if you pg_lake doesn't
set it.
If we go with only the first version that fixes logical replication in
18/19, the problematic code will be there on all branches, so 14+.

But regardless to this, CreateExecutorState documents on all branches
that callers are expected to set es_snapshot:

	/*
	 * Initialize all fields of the Executor State structure
	 */
	estate->es_direction = ForwardScanDirection;
	estate->es_snapshot = InvalidSnapshot;	/* caller must initialize this */
	estate->es_crosscheck_snapshot = InvalidSnapshot;	/* no crosscheck */

So I think it would be good practice to set it, independently to this bug.


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
David Christensen <david+pg@pgguru.net>
Дата:
On Thu, Oct 1, 2026 at 10:26 AM Zsolt Parragi  wrote:
> But regardless to this, CreateExecutorState documents on all branches
> that callers are expected to set es_snapshot:
>
>         /*
>          * Initialize all fields of the Executor State structure
>          */
>         estate->es_direction = ForwardScanDirection;
>         estate->es_snapshot = InvalidSnapshot;  /* caller must initialize this */
>         estate->es_crosscheck_snapshot = InvalidSnapshot;       /* no crosscheck */
>
> So I think it would be good practice to set it, independently to this bug.

Agreed, thanks for finding this; the PR for this issue is in flight.

David


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
Hi Dean, Zsolt,

The apply-worker crash is still unfixed in master and 18/19 branches.
Are there any remaining objections to the InvalidSnapshot guard?

Minor releases are approachnig...

Thank you!


Best regards, Andrey Borodin.


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
On Fri, 30 Oct 2026 at 16:53, Andrey Borodin  wrote:
>
> The apply-worker crash is still unfixed in master and 18/19 branches.
> Are there any remaining objections to the InvalidSnapshot guard?
>
> Minor releases are approachnig...

Thanks for the reminder. I'll take care of this using an InvalidSnapshot guard.

Regards,
Dean


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
> The apply-worker crash is still unfixed in master and 18/19 branches.
> Are there any remaining objections to the InvalidSnapshot guard?

Not from me, I attached a version using that upthread in[1]

[1]: https://www.postgresql.org/message-id/CAN4CZFPzwmTjbsu_hVu2bFENCssmycf4d1etjOn%3DmNq7dHwcoQ%40mail.gmail.com


Re: SSI: ON CONFLICT DO SELECT takes no predicate lock on the returned row

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
On Tue, 3 Nov 2026 at 12:14, Dean Rasheed  wrote:
>
> Thanks for the reminder. I'll take care of this using an InvalidSnapshot guard.

OK, I've pushed and back-patched that fix down to v14, so no extension
code will need changing.

I didn't bother with the test case, since it only applies to v18
onwards, and it didn't seem worth carrying that extra test code just
for this.

Regards,
Dean


FAQ