Change IndexAmRoutines to be statically-allocated structs. Up to now, index amhandlers were expected to produce a new, palloc'd struct on each call. That requires palloc/pfree overhead, and creates a risk of memory leaks if the caller fails to pfree, and the time taken to fill such a large structure isn't nil. Moreover, we were storing these things in the relcache, eating several hundred bytes for each cached index. There is not anything in these structs that needs to vary at runtime, so let's change the definition so that an amhandler can return a pointer to a "static const" struct of which there's only one copy per index AM. Mark all the core code's IndexAmRoutine pointers const so that we catch anyplace that might still try to change or pfree one.
(This is similar to the way we were already handling TableAmRoutine structs. This commit does fix one comment that was infelicitously copied-and-pasted into tableamapi.c.) This commit needs to be called out in the v19 release notes as an API change for extension index AMs. An un-updated AM will still work (as of now, anyway) but it risks memory leaks and will be slower than necessary. Author: Matthias van de Meent <[email protected]> Reviewed-by: Tom Lane <[email protected]> Discussion: https://postgr.es/m/CAEoWx2=vApYk2LRu8R0DdahsPNEhWUxGBZ=rbZo1EXE=ua+...@mail.gmail.com Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/bc6374cd76abb2e6a48c4b57c0b5a7baa5babd67 Modified Files -------------- contrib/bloom/blutils.c | 111 +++++++++++----------- doc/src/sgml/indexam.sgml | 12 ++- src/backend/access/brin/brin.c | 113 ++++++++++++----------- src/backend/access/gin/ginutil.c | 109 +++++++++++----------- src/backend/access/gist/gist.c | 113 ++++++++++++----------- src/backend/access/hash/hash.c | 113 ++++++++++++----------- src/backend/access/index/amapi.c | 18 ++-- src/backend/access/nbtree/nbtree.c | 113 ++++++++++++----------- src/backend/access/spgist/spgutils.c | 113 ++++++++++++----------- src/backend/access/table/tableamapi.c | 5 +- src/backend/catalog/index.c | 4 +- src/backend/commands/indexcmds.c | 5 +- src/backend/commands/opclasscmds.c | 10 +- src/backend/executor/execAmi.c | 3 +- src/backend/optimizer/util/plancat.c | 2 +- src/backend/utils/adt/amutils.c | 4 +- src/backend/utils/adt/ruleutils.c | 2 +- src/backend/utils/cache/lsyscache.c | 35 ++----- src/backend/utils/cache/relcache.c | 21 ++--- src/include/access/amapi.h | 8 +- src/include/utils/rel.h | 2 +- src/test/modules/dummy_index_am/dummy_index_am.c | 101 ++++++++++---------- 22 files changed, 501 insertions(+), 516 deletions(-)
