]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Use direct hash lookup in logicalrep_partmap_invalidate_cb()
authorMichael Paquier <michael@paquier.xyz>
Mon, 27 Jul 2026 00:42:50 +0000 (09:42 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 27 Jul 2026 00:42:50 +0000 (09:42 +0900)
This replaces an O(N) hash_seq_search() loop by an O(1) lookup, removing
a TODO item, making the invalidation callback faster when dealing with
many relations.  This can work because LogicalRepPartMap is keyed by a
partition OID, and a relmapentry's localreloid matches with it.

An assertion is added in logicalrep_partition_open() to enforce the fact
that localreloid matches with the hash key.

Author: DaeMyung Kang <charsyam@gmail.com>
Discussion: https://postgr.es/m/20260417174450.4158878-1-charsyam@gmail.com

src/backend/replication/logical/relation.c

index 296cbaede3018cb782196f83b1a33f37fae44c93..8749826425648b715319bed24ed82fbaeaeb742b 100644 (file)
@@ -543,20 +543,14 @@ logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid)
 
        if (reloid != InvalidOid)
        {
-               HASH_SEQ_STATUS status;
-
-               hash_seq_init(&status, LogicalRepPartMap);
-
-               /* TODO, use inverse lookup hashtable? */
-               while ((entry = (LogicalRepPartMapEntry *) hash_seq_search(&status)) != NULL)
-               {
-                       if (entry->relmapentry.localreloid == reloid)
-                       {
-                               entry->relmapentry.localrelvalid = false;
-                               hash_seq_term(&status);
-                               break;
-                       }
-               }
+               /*
+                * LogicalRepPartMap is keyed by partition OID, matching with
+                * entry->relmapentry.localreloid (see logicalrep_partition_open), so
+                * we can invalidate via a direct hash lookup.
+                */
+               entry = hash_search(LogicalRepPartMap, &reloid, HASH_FIND, NULL);
+               if (entry != NULL)
+                       entry->relmapentry.localrelvalid = false;
        }
        else
        {
@@ -675,6 +669,7 @@ logicalrep_partition_open(LogicalRepRelMapEntry *root,
         */
        if (found && entry->localrelvalid)
        {
+               Assert(entry->localreloid == partOid);
                entry->localrel = partrel;
                return entry;
        }