From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Give SMgrRelation pointers a well-defined lifetime.
Date: Wed, 31 Jan 2024 11:02:23 +0000
Message-ID: <E1rV8MJ-003yw6-5E@gemulon.postgresql.org> (raw)
Give SMgrRelation pointers a well-defined lifetime.
After calling smgropen(), it was not clear how long you could continue
to use the result, because various code paths including cache
invalidation could call smgrclose(), which freed the memory.
Guarantee that the object won't be destroyed until the end of the
current transaction, or in recovery, the commit/abort record that
destroys the underlying storage.
smgrclose() is now just an alias for smgrrelease(). It closes files
and forgets all state except the rlocator, but keeps the SMgrRelation
object valid.
A new smgrdestroy() function is used by rare places that know there
should be no other references to the SMgrRelation.
The short version:
* smgrclose() is now just an alias for smgrrelease(). It releases
resources, but doesn't destroy until EOX
* smgrdestroy() now frees memory, and should rarely be used.
Existing code should be unaffected, but it is now possible for code that
has an SMgrRelation object to use it repeatedly during a transaction as
long as the storage hasn't been physically dropped. Such code would
normally hold a lock on the relation.
This also replaces the "ownership" mechanism of SMgrRelations with a
pin counter. An SMgrRelation can now be "pinned", which prevents it
from being destroyed at end of transaction. There can be multiple pins
on the same SMgrRelation. In practice, the pin mechanism is only used
by the relcache, so there cannot be more than one pin on the same
SMgrRelation. Except with swap_relation_files XXX
Author: Thomas Munro, Heikki Linnakangas
Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Discussion: https://www.postgresql.org/message-id/CA%2BhUKGJ8NTvqLHz6dqbQnt2c8XCki4r2QvXjBQcXpVwxTY_pvA@mail.gma...
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/21d9c3ee4ef74e2229341d39811c97f85071c90a
Modified Files
--------------
src/backend/access/transam/xlogutils.c | 15 +-
src/backend/commands/cluster.c | 19 ---
src/backend/postmaster/bgwriter.c | 8 +-
src/backend/postmaster/checkpointer.c | 15 +-
src/backend/storage/buffer/bufmgr.c | 32 ++--
src/backend/storage/freespace/freespace.c | 9 +-
src/backend/storage/smgr/smgr.c | 233 ++++++++++++++++--------------
src/backend/utils/cache/inval.c | 2 +-
src/backend/utils/cache/relcache.c | 21 +--
src/include/storage/smgr.h | 36 ++---
src/include/utils/rel.h | 18 +--
src/include/utils/relcache.h | 2 -
12 files changed, 183 insertions(+), 227 deletions(-)
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: heikki.linnakangas@iki.fi, pgsql-committers@lists.postgresql.org
Subject: Re: pgsql: Give SMgrRelation pointers a well-defined lifetime.
In-Reply-To: <E1rV8MJ-003yw6-5E@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