pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Change IndexAmRoutines to be statically-allocated structs.
Date: Tue, 30 Dec 2025 23:26:29 +0000
Message-ID: <E1vaj6i-003YXn-02@gemulon.postgresql.org> (raw)

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

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(-)



Message-ID: <E1vaj6i-003YXn-02@gemulon.postgresql.org>
Permalink:  ../E1vaj6i-003YXn-02@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1vaj6i-003YXn-02@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Change IndexAmRoutines to be statically-allocated structs.
  In-Reply-To: <E1vaj6i-003YXn-02@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox