On 18/05/2026 00:43, Michael Paquier wrote:
> On Sun, May 17, 2026 at 08:03:15AM +0200, Andrei Lepikhov wrote:
>> Right now, awaiting this feature, I use a nextval hook. But it is just to
>> minimise the number of core lines that need to be changed. Neither hook nor
>> callback is a good idea here - sequence source might be only one for a
>> specific
>> table; \d should show an unequivocal definition of a table.
>> Also, the AM machinery makes the dump/restore use cases clear. Logical
>> replication plugins also benefit from it: pgactive, pglogical, and spock all
>> include Auto-DDL solutions that simplify the management of sequence
>> generation
>> methods across instances.
>
> There was zero feedback from other core developers, so it's really
> hard to weigh about its acceptance. My guess is that nobody really
> cares about this thread, which is just the way it is on -hackers for
> some things. FWIW, I still like what I've done in this patch and this
> design.
Ok. So let me just leave the idea of avoiding unnecessary cache lookups here.
--
regards, Andrei Lepikhov,
pgEdge
diff --git a/src/backend/utils/cache/relcache.c
b/src/backend/utils/cache/relcache.c
index 2a8e64c7279..1c219ce319d 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -1872,12 +1872,24 @@ RelationInitSequenceAccessMethod(Relation relation)
Oid tableam_handler;
Assert(RELKIND_HAS_SEQUENCE_AM(relation->rd_rel->relkind));
+ Assert(relation->rd_rel->relam != InvalidOid);
/*
- * Look up the sequence access method, save the OID of its handler
- * function.
+ * Fast path for the built-in "seqlocal" AM: avoid two syscache lookups
+ * and a name-based pg_am scan on every cold-cache open of a sequence
+ * using the default access method.
+ *
+ * This mirrors the catalog-relation fast path in
+ * RelationInitTableAccessMethod() above.
*/
- Assert(relation->rd_rel->relam != InvalidOid);
+ if (relation->rd_rel->relam == LOCAL_SEQUENCE_AM_OID)
+ {
+ relation->rd_amhandler = F_SEQ_LOCAL_SEQUENCEAM_HANDLER;
+ relation->rd_sequenceam =
GetSequenceAmRoutine(relation->rd_amhandler);
+ relation->rd_tableam =
GetTableAmRoutine(F_HEAP_TABLEAM_HANDLER);
+ return;
+ }
+
relation->rd_amhandler =
GetSequenceAmRoutineId(relation->rd_rel->relam);
/*