postgresql/src/include/storage
Heikki Linnakangas b8bff07daa Make ResourceOwners more easily extensible.
Instead of having a separate array/hash for each resource kind, use a
single array and hash to hold all kinds of resources. This makes it
possible to introduce new resource "kinds" without having to modify
the ResourceOwnerData struct. In particular, this makes it possible
for extensions to register custom resource kinds.

The old approach was to have a small array of resources of each kind,
and if it fills up, switch to a hash table. The new approach also uses
an array and a hash, but now the array and the hash are used at the
same time. The array is used to hold the recently added resources, and
when it fills up, they are moved to the hash. This keeps the access to
recent entries fast, even when there are a lot of long-held resources.

All the resource-specific ResourceOwnerEnlarge*(),
ResourceOwnerRemember*(), and ResourceOwnerForget*() functions have
been replaced with three generic functions that take resource kind as
argument. For convenience, we still define resource-specific wrapper
macros around the generic functions with the old names, but they are
now defined in the source files that use those resource kinds.

The release callback no longer needs to call ResourceOwnerForget on
the resource being released. ResourceOwnerRelease unregisters the
resource from the owner before calling the callback. That needed some
changes in bufmgr.c and some other files, where releasing the
resources previously always called ResourceOwnerForget.

Each resource kind specifies a release priority, and
ResourceOwnerReleaseAll releases the resources in priority order. To
make that possible, we have to restrict what you can do between
phases. After calling ResourceOwnerRelease(), you are no longer
allowed to remember any more resources in it or to forget any
previously remembered resources by calling ResourceOwnerForget.  There
was one case where that was done previously. At subtransaction commit,
AtEOSubXact_Inval() would handle the invalidation messages and call
RelationFlushRelation(), which temporarily increased the reference
count on the relation being flushed. We now switch to the parent
subtransaction's resource owner before calling AtEOSubXact_Inval(), so
that there is a valid ResourceOwner to temporarily hold that relcache
reference.

Other end-of-xact routines make similar calls to AtEOXact_Inval()
between release phases, but I didn't see any regression test failures
from those, so I'm not sure if they could reach a codepath that needs
remembering extra resources.

There were two exceptions to how the resource leak WARNINGs on commit
were printed previously: llvmjit silently released the context without
printing the warning, and a leaked buffer io triggered a PANIC. Now
everything prints a WARNING, including those cases.

Add tests in src/test/modules/test_resowner.

Reviewed-by: Aleksander Alekseev, Michael Paquier, Julien Rouhaud
Reviewed-by: Kyotaro Horiguchi, Hayato Kuroda, Álvaro Herrera, Zhihong Yu
Reviewed-by: Peter Eisentraut, Andres Freund
Discussion: https://www.postgresql.org/message-id/cbfabeb0-cd3c-e951-a572-19b365ed314d%40iki.fi
2023-11-08 13:30:50 +02:00
..
.gitignore When trace_lwlocks is used, identify individual lwlocks by name. 2015-09-11 14:01:39 -04:00
backendid.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
barrier.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
block.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
buf.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
buf_internals.h Make ResourceOwners more easily extensible. 2023-11-08 13:30:50 +02:00
buffile.h Add BufFileRead variants with short read and EOF detection 2023-01-16 11:01:31 +01:00
bufmgr.h Make ResourceOwners more easily extensible. 2023-11-08 13:30:50 +02:00
bufpage.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
checksum.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
checksum_impl.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
condition_variable.h De-pessimize ConditionVariableCancelSleep(). 2023-08-15 10:23:47 +12:00
copydir.h Constify the arguments of copydir.h functions 2023-01-18 08:55:26 +09:00
dsm.h Avoid type cheats for invalid dsa_handles and dshash_table_handles. 2023-01-25 11:48:38 -05:00
dsm_impl.h Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
fd.h Make enum for sync methods available to frontend code. 2023-09-06 16:26:39 -07:00
fileset.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
freespace.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
fsm_internals.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
indexfsm.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
ipc.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
item.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
itemid.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
itemptr.h Consolidate ItemPointer to Datum conversion functions 2023-02-13 09:57:15 +01:00
large_object.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
latch.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
lmgr.h Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
lock.h Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
lockdefs.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
lwlock.h Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
md.h Add smgrzeroextend(), FileZero(), FileFallocate() 2023-04-05 10:06:39 -07:00
meson.build Update copyright for 2023 2023-01-02 15:00:37 -05:00
off.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
pg_sema.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
pg_shmem.h Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
pmsignal.h Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
predicate.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
predicate_internals.h Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
proc.h Pre-beta mechanical code beautification. 2023-05-19 17:24:48 -04:00
procarray.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
proclist.h Constify proclist.h 2023-01-19 09:45:34 +01:00
proclist_types.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
procsignal.h Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
reinit.h Refactor parse_filename_for_nontemp_relation to parse more. 2023-10-23 15:08:53 -04:00
relfilelocator.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
s_lock.h Fix some typos and some incorrectly duplicated words 2023-04-18 14:03:49 +12:00
sharedfileset.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
shm_mq.h Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
shm_toc.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
shmem.h Remove SHM_QUEUE 2023-01-19 18:55:51 -08:00
sinval.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
sinvaladt.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
smgr.h Remove bogus #include added by d4e71df6d7. 2023-04-26 10:43:53 +12:00
spin.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
standby.h Remove vacuum_defer_cleanup_age 2023-04-24 12:21:02 -07:00
standbydefs.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
sync.h Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00