Report
Apply worker fails with "tuple concurrently updated" (and disable_on_error disables the subscription) when it stops or resumes dead-tuple retention (max_retention_duration) while ALTER SUBSCRIPTION holds the subscription lock, as UpdateDeadTupleRetentionStatus() reads the row before locking
- Severity
- 25
- Blamed commit
- a850be2fe653b3b529969946c1cefe0fd9e34a8d REL_19_STABLE, master
Summary
When an apply worker of a subscription with retain_dead_tuples = true and max_retention_duration > 0 decides to stop (or resume) retaining conflict information, it updates pg_subscription.subretentionactive through UpdateDeadTupleRetentionStatus(). That function fetches the pg_subscription tuple from the syscache first and only then takes AccessShareLock on the subscription object. If an ALTER SUBSCRIPTION of the same subscription holds AccessExclusiveLock at that moment, the worker waits for it, and after the ALTER commits it updates the stale tuple version and fails with
ERROR: tuple concurrently updated
With disable_on_error = true the subscription is then disabled ("has been disabled because of an error") and stays disabled until it is enabled manually; otherwise the apply worker exits and is restarted, and the retention status change it attempted is lost for that cycle.
Reproduction
Two clusters: publisher (wal_level = logical) on port 5432 and subscriber (track_commit_timestamp = on) on port 5433, both with database postgres.
Publisher:
CREATE TABLE t (id int PRIMARY KEY);
CREATE TABLE slow (id int PRIMARY KEY);
CREATE PUBLICATION pb FOR TABLE t, slow;
Subscriber (the trigger only creates apply lag):
CREATE TABLE t (id int PRIMARY KEY);
CREATE TABLE slow (id int PRIMARY KEY);
CREATE FUNCTION slowf() RETURNS trigger LANGUAGE plpgsql
AS $$BEGIN PERFORM pg_sleep(3); RETURN NEW; END$$;
CREATE TRIGGER slowt BEFORE INSERT ON slow
FOR EACH ROW EXECUTE FUNCTION slowf();
ALTER TABLE slow ENABLE ALWAYS TRIGGER slowt;
CREATE SUBSCRIPTION sb
CONNECTION 'port=5432 dbname=postgres' PUBLICATION pb
WITH (retain_dead_tuples = true, max_retention_duration = 1000,
disable_on_error = true);
Wait a few seconds for the initial sync, then run, in this order:
-- subscriber
SELECT txid_current();
-- publisher: ten separate transactions (about 30 s of apply lag)
INSERT INTO slow VALUES (1);
...
INSERT INTO slow VALUES (10);
-- subscriber, immediately, in a second session
BEGIN;
ALTER SUBSCRIPTION sb SET (max_retention_duration = 1000);
SELECT pg_sleep(40); -- outlast the apply lag
COMMIT;
-- subscriber
SELECT subenabled, subretentionactive FROM pg_subscription;
Expected: when the lag clears, the worker logs "has stopped retaining the information for detecting conflicts" (after waiting for the ALTER), and the subscription stays enabled.
Actual: the worker waits for AccessShareLock on the subscription while the ALTER transaction is open, then fails as soon as it commits:
LOG: process N still waiting for AccessShareLock on object 16398
of class 6100 of database 0 after 1000.223 ms
ERROR: tuple concurrently updated
LOG: subscription "sb" has been disabled because of an error
and the query returns subenabled = f, subretentionactive = t. With disable_on_error = false the same ERROR is logged and the worker is restarted. The pg_sleep only widens the window; any ALTER SUBSCRIPTION of the subscription that commits while the worker is between the tuple fetch and the lock (for example one that has to contact the publisher) hits the same error.
Analysis
stop_conflict_info_retention() and resume_conflict_info_retention() in src/backend/replication/logical/worker.c call update_retention_status(), which calls UpdateDeadTupleRetentionStatus() in src/backend/catalog/pg_subscription.c:
tup = SearchSysCacheCopy1(SUBSCRIPTIONOID, ...);
...
LockSharedObject(SubscriptionRelationId, subid, 0,
AccessShareLock);
...
CatalogTupleUpdate(rel, &tup->t_self, tup);
AlterSubscription() takes AccessExclusiveLock on the same object and updates the same pg_subscription row. The worker's copy of the tuple is taken before the lock wait and is not refetched afterwards, so once the ALTER commits, simple_heap_update() finds the old version already updated and raises "tuple concurrently updated". The error is raised inside the apply loop, so it is handled like an apply error (disable_on_error applies).
UpdateDeadTupleRetentionStatus() and the fetch-then-lock order were added by commit a850be2fe653 ("Add max_retention_duration option to subscriptions."); the resume path that uses it was added later.
Notes
Tested on master ea39d58e4b8242ec5da211364b20ffd3d4000a51 with assertions enabled; reproduced in 3 of 3 runs, with disable_on_error both on and off.
Branches: master and REL_19_STABLE (commit a850be2fe653 is not in REL_18_STABLE or older).