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 <robertmh...@gmail.com>
Discussion: 
https://www.postgresql.org/message-id/ca%2bhukgj8ntvqlhz6dqbqnt2c8xcki4r2qvxjbqcxpvwxty_...@mail.gmail.com

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 via email to