postgresql/src/common
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
..
unicode Remove distprep 2023-11-06 15:18:04 +01:00
.gitignore Replace the data structure used for keyword lookup. 2019-01-06 17:02:57 -05:00
Makefile Remove distprep 2023-11-06 15:18:04 +01:00
archive.c Revert refactoring of restore command code to shell_restore.c 2023-02-06 08:28:42 +09:00
base64.c Fix small overestimation of base64 encoding output length. 2023-06-08 11:24:31 -04:00
binaryheap.c Add function for removing arbitrary nodes in binaryheap. 2023-09-18 14:06:08 -07:00
checksum_helper.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
compression.c Message style improvements 2023-05-19 18:45:29 +02:00
config_info.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
controldata_utils.c Try to handle torn reads of pg_control in frontend. 2023-10-16 17:33:08 +13:00
cryptohash.c Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
cryptohash_openssl.c Make ResourceOwners more easily extensible. 2023-11-08 13:30:50 +02:00
d2s.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
d2s_full_table.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
d2s_intrinsics.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
digit_table.h Change floating-point output format for improved performance. 2019-02-13 15:20:33 +00:00
encnames.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
exec.c Implement find_my_exec()'s path normalization using realpath(3). 2023-03-23 18:17:49 -04:00
f2s.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
fe_memutils.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
file_perm.c More consistent behavior of GetDataDirectoryCreatePerm on Windows 2023-11-05 21:59:04 +01:00
file_utils.c Add support for syncfs() in frontend support functions. 2023-09-06 16:27:00 -07:00
hashfn.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
hmac.c Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
hmac_openssl.c Make ResourceOwners more easily extensible. 2023-11-08 13:30:50 +02:00
ip.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
jsonapi.c Add trailing commas to enum definitions 2023-10-26 09:20:54 +02:00
keywords.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
kwlookup.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
link-canary.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
logging.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
md5.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
md5_common.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
md5_int.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
meson.build Additional unicode primitive functions. 2023-11-01 22:47:06 -07:00
percentrepl.c Fix error message wordings 2023-05-17 21:33:47 +02:00
pg_get_line.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
pg_lzcompress.c Improve pglz_decompress's defenses against corrupt compressed data. 2023-10-18 20:43:27 -04:00
pg_prng.c Invent random_normal() to provide normally-distributed random numbers. 2023-01-09 12:44:00 -05:00
pgfnames.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
protocol_openssl.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
psprintf.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
relpath.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
restricted_token.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
rmtree.c Avoid memory leak in rmtree() when path cannot be opened 2023-07-31 11:36:44 +09:00
ryu_common.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
saslprep.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
scram-common.c Make SCRAM iteration count configurable 2023-03-27 09:46:29 +02:00
sha1.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
sha1_int.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
sha2.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
sha2_int.h Update copyright for 2023 2023-01-02 15:00:37 -05:00
sprompt.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
string.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
stringinfo.c Introduce the concept of read-only StringInfos 2023-10-26 16:31:48 +13:00
unicode_category.c Additional unicode primitive functions. 2023-11-01 22:47:06 -07:00
unicode_norm.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
username.c Update copyright for 2023 2023-01-02 15:00:37 -05:00
wait_error.c Add SHELL_ERROR and SHELL_EXIT_CODE magic variables to psql. 2023-03-21 13:03:56 -04:00
wchar.c Update copyright for 2023 2023-01-02 15:00:37 -05:00