Avoid looping over all type cache entries in TypeCacheRelCallback()
authorAlexander Korotkov <akorotkov@postgresql.org>
Thu, 24 Oct 2024 11:35:52 +0000 (14:35 +0300)
committerAlexander Korotkov <akorotkov@postgresql.org>
Thu, 24 Oct 2024 11:35:52 +0000 (14:35 +0300)
commitb85a9d046efdd27775cbe7db9e92aad96aab4ada
tree63f9cb3fafa222f54c3e0e0342ff0b8a6d2eb501
parentc1500a1ba7e16ac9e98c6baf792f0be64a48e839
Avoid looping over all type cache entries in TypeCacheRelCallback()

Currently, when a single relcache entry gets invalidated,
TypeCacheRelCallback() has to loop over all type cache entries to find
appropriate typentry to invalidate.  Unfortunately, using the syscache here
is impossible, because this callback could be called outside a transaction
and this makes impossible catalog lookups.  This is why present commit
introduces RelIdToTypeIdCacheHash to map relation OID to its composite type
OID.

We are keeping RelIdToTypeIdCacheHash entry while corresponding type cache
entry have something to clean.  Therefore, RelIdToTypeIdCacheHash shouldn't
get bloat in the case of temporary tables flood.

There are many places in lookup_type_cache() where syscache invalidation,
user interruption, or even error could occur.  In order to handle this, we
keep an array of in-progress type cache entries.  In the case of
lookup_type_cache() interruption this array is processed to keep
RelIdToTypeIdCacheHash in a consistent state.

Discussion: https://postgr.es/m/5812a6e5-68ae-4d84-9d85-b443176966a1%40sigaev.ru
Author: Teodor Sigaev
Reviewed-by: Aleksander Alekseev, Tom Lane, Michael Paquier, Roman Zharkov
Reviewed-by: Andrei Lepikhov, Pavel Borisov, Jian He, Alexander Lakhin
Reviewed-by: Artur Zakirov
src/backend/access/transam/xact.c
src/backend/utils/cache/typcache.c
src/include/utils/typcache.h
src/test/modules/Makefile
src/test/modules/meson.build
src/test/modules/typcache/.gitignore [new file with mode: 0644]
src/test/modules/typcache/Makefile [new file with mode: 0644]
src/test/modules/typcache/expected/typcache_rel_type_cache.out [new file with mode: 0644]
src/test/modules/typcache/meson.build [new file with mode: 0644]
src/test/modules/typcache/sql/typcache_rel_type_cache.sql [new file with mode: 0644]
src/tools/pgindent/typedefs.list