| From: | Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com> |
|---|---|
| To: | shihao zhong <zhong950419(at)gmail(dot)com> |
| Cc: | pgsql-hackers(at)lists(dot)postgresql(dot)org, Kirill Reshke <reshkekirill(at)gmail(dot)com>, Jeff Davis <pgsql(at)j-davis(dot)com> |
| Subject: | Re: DROP USER MAPPING doesn't acquire lock on its server |
| Date: | 2026-12-04 09:06:57 |
| Message-ID: | CAN4CZFPZA1KK7X4sN2-q0n15=mx7H8vUco2JD+_peKPc5cr_uA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Fri, 04 Dec 2026, shihao zhong <zhong950419(at)gmail(dot)com> wrote:
> Zsolt, I tried a single syscache lookup and CREATE FOREIGN TABLE and
> CREATE SUBSCRIPTION still fail at the next lookup by OID. So I think
> the lock is needed, as the comment above dependencyLockAndCheckObject()
> also says. That check is already back-patched and stops orphaned rows,
> so maybe this can be master only.
You are right, we do need locking, I was wrong about that part. But I
still think
(1) fixing GetForeignServerByName so it isn't racy by itself is worth
it, that would also help dblink and extensions.
(2) introducing GetForeignServerByNameLocked isn't the ideal approach,
and the objectaddress.c location seems to be fixed by (1) unless I
missed something.
> CREATE SERVER
> racing with DROP FOREIGN DATA WRAPPER still fails, so maybe lock the
> FDW lookup too and add a test.
We also have other races (ALTER USER MAPPING vs DROP USER MAPPING, two
concurrent DROP USER MAPPING)
And there are a few different issues:
1. Shouldn't we release the lock?
-- Setup: CREATE FOREIGN DATA WRAPPER w; CREATE SERVER s FOREIGN DATA WRAPPER w;
-- Session 1:
BEGIN;
DROP USER MAPPING IF EXISTS FOR PUBLIC SERVER s;
-- NOTICE: user mapping for "public" does not exist for server "s", skipping
-- Session 2:
ALTER SERVER s RENAME TO s_ren;
-- master: ALTER SERVER (succeeds immediately)
-- patched: blocks waiting for AccessExclusiveLock on
pg_foreign_server until Session 1 commits
2. CREATE/ALTER/DROP USER MAPPING resolve role before waiting on
server lock, acting on the wrong user
-- Setup: CREATE ROLE a_u1 LOGIN; CREATE SERVER s_alter FOREIGN DATA WRAPPER w;
-- CREATE USER MAPPING FOR a_u1 SERVER s_alter OPTIONS (user 'orig_u1');
-- Session 1:
BEGIN;
DROP SERVER s_alter CASCADE;
ALTER ROLE a_u1 RENAME TO a_u2;
CREATE ROLE a_u1 LOGIN;
CREATE SERVER s_alter FOREIGN DATA WRAPPER w;
CREATE USER MAPPING FOR a_u1 SERVER s_alter OPTIONS (user 'new_u1');
CREATE USER MAPPING FOR a_u2 SERVER s_alter OPTIONS (user 'new_u2');
-- Session 2 (blocks waiting for AccessShareLock on s_alter):
ALTER USER MAPPING FOR a_u1 SERVER s_alter OPTIONS (SET user 'HIJACKED');
-- Session 1:
COMMIT;
-- Result on patched:
-- a_u1 | {user=new_u1}
-- a_u2 | {user=HIJACKED}
-- Result on master:
-- ERROR: tuple concurrently deleted (both a_u1 and a_u2 untouched)
3. It deadlocks with ALTER SERVER
-- Setup: CREATE FOREIGN DATA WRAPPER w; CREATE SERVER s1 FOREIGN DATA
WRAPPER w;
-- Session 1:
BEGIN;
ALTER SERVER s1 VERSION '2.0';
-- Session 2 (acquires AccessExclusiveLock on s1, blocks on S1's tuple XID):
DROP SERVER s1;
-- Session 1 (now blocks on AccessShareLock on s1):
DROP USER MAPPING IF EXISTS FOR unpriv SERVER s1;
-- Result on patched:
-- ERROR: deadlock detected
-- DETAIL: Process ... waits for ShareLock on transaction ...
-- Process ... waits for AccessShareLock on object ... of class 1417
of database ...
-- Result on master:
-- S1 commits cleanly with NOTICE; S2 gets ERROR: tuple concurrently
updated (no deadlock)
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Hayato Kuroda (Fujitsu) | 2026-12-04 09:10:09 | RE: Wrong relation in apply worker "not updatable" errors PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>for partitions; relcache leak in check_and_init_gencol() |
| Previous Message | Ajit Awekar | 2026-12-04 09:03:07 | [PATCH] Don't write ldapbindpasswd to the server log on authentication failure |