postgresql/src/backend
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
..
access Make ResourceOwners more easily extensible. 2023-11-08 13:30:50 +02:00
archive Redesign archive modules 2023-02-17 14:26:42 +09:00
backup Change struct tablespaceinfo's oid member from 'char *' to 'Oid' 2023-10-23 15:17:26 -04:00
bootstrap Remove distprep 2023-11-06 15:18:04 +01:00
catalog Remove distprep 2023-11-06 15:18:04 +01:00
commands Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
executor Fix intra-query memory leak when a SRF returns zero rows. 2023-10-28 14:05:01 -04:00
foreign Expand some more uses of "deleg" to "delegation" or "delegated". 2023-05-21 10:55:18 -04:00
jit Make ResourceOwners more easily extensible. 2023-11-08 13:30:50 +02:00
lib Make binaryheap available to frontend code. 2023-09-18 12:18:33 -07:00
libpq Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
main Remove obsolete defense against strxfrm() bugs. 2023-04-20 13:20:14 +12:00
nodes Remove distprep 2023-11-06 15:18:04 +01:00
optimizer Fix allocation of UniqueRelInfo 2023-11-06 10:04:01 +02:00
parser Compute aggregate argument types correctly in transformAggregateCall(). 2023-11-06 10:38:00 -05:00
partitioning Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
po Translation updates 2023-08-07 12:39:30 +02:00
port Remove distprep 2023-11-06 15:18:04 +01:00
postmaster pgindent run to fix commits de64268561 and 5ae2087202 2023-10-30 14:52:35 -04:00
regex Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
replication Make use of initReadOnlyStringInfo() in more places 2023-11-07 11:16:43 +13:00
rewrite Add TupleDescGetDefault() 2023-09-27 18:52:40 +01:00
snowball Remove distprep 2023-11-06 15:18:04 +01:00
statistics Take pg_attribute out of VacAttrStats 2023-07-03 07:18:57 +02:00
storage Make ResourceOwners more easily extensible. 2023-11-08 13:30:50 +02:00
tcop Introduce the concept of read-only StringInfos 2023-10-26 16:31:48 +13:00
tsearch Limit to_tsvector_byid's initial array allocation to something sane. 2023-09-25 11:50:28 -04:00
utils Make ResourceOwners more easily extensible. 2023-11-08 13:30:50 +02:00
.gitignore
Makefile Remove distprep 2023-11-06 15:18:04 +01:00
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
meson.build Add win32ver data to meson-built postgres.exe. 2023-06-12 07:40:38 -07:00
nls.mk Remove distprep 2023-11-06 15:18:04 +01:00