pgsql: Make ResourceOwners more easily extensible.

Поиск
Список
Период
Сортировка
От Heikki Linnakangas
Тема pgsql: Make ResourceOwners more easily extensible.
Дата
Msg-id E1r0grf-004TbG-Jx@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
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

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/b8bff07daa85c837a2747b4d35cd5a27e73fb7b2

Modified Files
--------------
src/backend/access/common/tupdesc.c                |   51 +-
src/backend/access/transam/xact.c                  |   14 +
src/backend/jit/jit.c                              |    2 -
src/backend/jit/llvm/llvmjit.c                     |   45 +-
src/backend/storage/buffer/bufmgr.c                |  164 ++-
src/backend/storage/buffer/localbuf.c              |   21 +-
src/backend/storage/file/fd.c                      |   57 +-
src/backend/storage/ipc/dsm.c                      |   53 +-
src/backend/storage/lmgr/lock.c                    |    2 +-
src/backend/utils/cache/catcache.c                 |  125 +-
src/backend/utils/cache/plancache.c                |   50 +-
src/backend/utils/cache/relcache.c                 |   64 +-
src/backend/utils/resowner/README                  |  114 +-
src/backend/utils/resowner/resowner.c              | 1545 +++++++-------------
src/backend/utils/time/snapmgr.c                   |   47 +-
src/common/cryptohash_openssl.c                    |   49 +-
src/common/hmac_openssl.c                          |   47 +-
src/include/storage/buf_internals.h                |   28 +
src/include/storage/bufmgr.h                       |    4 +-
src/include/utils/catcache.h                       |    3 -
src/include/utils/plancache.h                      |    2 +
src/include/utils/resowner.h                       |   94 +-
src/pl/plpgsql/src/pl_exec.c                       |    2 +-
src/pl/plpgsql/src/pl_handler.c                    |    6 +-
src/test/modules/Makefile                          |    1 +
src/test/modules/meson.build                       |    1 +
src/test/modules/test_resowner/.gitignore          |    4 +
src/test/modules/test_resowner/Makefile            |   24 +
.../test_resowner/expected/test_resowner.out       |  197 +++
src/test/modules/test_resowner/meson.build         |   34 +
.../modules/test_resowner/sql/test_resowner.sql    |   25 +
.../modules/test_resowner/test_resowner--1.0.sql   |   30 +
.../modules/test_resowner/test_resowner.control    |    4 +
.../modules/test_resowner/test_resowner_basic.c    |  211 +++
.../modules/test_resowner/test_resowner_many.c     |  296 ++++
src/tools/pgindent/typedefs.list                   |    6 +-
36 files changed, 2278 insertions(+), 1144 deletions(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Peter Eisentraut
Дата:
Сообщение: pgsql: Don't install ldap_password_func in meson
Следующее
От: Alvaro Herrera
Дата:
Сообщение: pgsql: Call pqPipelineFlush from PQsendFlushRequest