pgsql: Avoid deadlock during orphan temp table removal.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Avoid deadlock during orphan temp table removal.
Date: 2024-04-02 18:59:47
Message-ID: E1rrjMJ-000ARN-CG@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Avoid deadlock during orphan temp table removal.

If temp tables have dependencies (such as sequences) then it's
possible for autovacuum's cleanup of orphan temp tables to deadlock
against an incoming backend that's trying to clean out the temp
namespace for its own use. That can happen because RemoveTempRelations'
performDeletion call can visit objects within the namespace in
an order different from the order in which a per-table deletion
will visit them.

To fix, observe that performDeletion will begin by taking an exclusive
lock on the temp namespace (even though it won't actually delete it).
So, if we can get a shared lock on the namespace, we can be sure we're
not running concurrently with RemoveTempRelations, while also not
conflicting with ordinary use of the namespace. This requires
introducing a conditional version of LockDatabaseObject, but that's no
big deal. (It's surprising we've got along without that this long.)

Report and patch by Mikhail Zhilin. Back-patch to all supported
branches.

Discussion: https://postgr.es/m/c43ce028-2bc2-4865-9b89-3f706246eed5@postgrespro.ru

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/6faca9ae2878c8f642a2e5748d2dbb2b91341bec

Modified Files
--------------
src/backend/postmaster/autovacuum.c | 21 +++++++++++++++++++-
src/backend/storage/lmgr/lmgr.c | 38 +++++++++++++++++++++++++++++++++++++
src/include/storage/lmgr.h | 2 ++
3 files changed, 60 insertions(+), 1 deletion(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2024-04-02 18:59:48 pgsql: Avoid deadlock during orphan temp table removal.
Previous Message Nathan Bossart 2024-04-02 15:36:13 pgsql: Avoid function call overhead of pg_popcount() in syslogger.c.