Change IndexAmRoutines to be statically-allocated structs. master github/master
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 30 Dec 2025 23:26:23 +0000 (18:26 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 30 Dec 2025 23:26:23 +0000 (18:26 -0500)
commitbc6374cd76abb2e6a48c4b57c0b5a7baa5babd67
treed90c3ca10ec7cbc70d538e64a7d5205532e2ed03
parent736f754eed01ca81198b6cd7421321088cbe5ded
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 <boekewurm+postgres@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAEoWx2=vApYk2LRu8R0DdahsPNEhWUxGBZ=rbZo1EXE=uA+opQ@mail.gmail.com
22 files changed:
contrib/bloom/blutils.c
doc/src/sgml/indexam.sgml
src/backend/access/brin/brin.c
src/backend/access/gin/ginutil.c
src/backend/access/gist/gist.c
src/backend/access/hash/hash.c
src/backend/access/index/amapi.c
src/backend/access/nbtree/nbtree.c
src/backend/access/spgist/spgutils.c
src/backend/access/table/tableamapi.c
src/backend/catalog/index.c
src/backend/commands/indexcmds.c
src/backend/commands/opclasscmds.c
src/backend/executor/execAmi.c
src/backend/optimizer/util/plancat.c
src/backend/utils/adt/amutils.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/cache/lsyscache.c
src/backend/utils/cache/relcache.c
src/include/access/amapi.h
src/include/utils/rel.h
src/test/modules/dummy_index_am/dummy_index_am.c