pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid deadlock during orphan temp table removal.
Date: Tue, 02 Apr 2024 18:59:48 +0000
Message-ID: <E1rrjMJ-000ARV-FK@gemulon.postgresql.org> (raw)

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
------
REL_13_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/4afc2c219d70ee7d12116aecda03d91c21ad6141

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(-)



view thread (6+ messages)

Message-ID: <E1rrjMJ-000ARV-FK@gemulon.postgresql.org>
Permalink:  ../E1rrjMJ-000ARV-FK@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1rrjMJ-000ARV-FK@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Avoid deadlock during orphan temp table removal.
  In-Reply-To: <E1rrjMJ-000ARV-FK@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox