public inbox for [email protected]  
help / color / mirror / Atom feed
From: Andrei Lepikhov <[email protected]>
To: Michael Paquier <[email protected]>
Cc: Postgres hackers <[email protected]>
Cc: Xuneng Zhou <[email protected]>
Cc: Chao Li <[email protected]>
Cc: Peter Eisentraut <[email protected]>
Cc: Kirill Reshke <[email protected]>
Cc: Peter Smith <[email protected]>
Subject: Re: Sequence Access Methods, round two
Date: Mon, 18 May 2026 20:42:51 +0200
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>
	<[email protected]>

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);
 
 	/*


Attachments:

  [text/plain] wire_local_seq.diff (1.2K, 2-wire_local_seq.diff)
  download | inline diff:
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);
 
 	/*


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: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
  Subject: Re: Sequence Access Methods, round two
  In-Reply-To: <[email protected]>

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

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