public inbox for [email protected]  
help / color / mirror / Atom feed
From: CharSyam <[email protected]>
To: [email protected]
Subject: [PATCH] Reduce pg_class scans in GRANT/REVOKE ON ALL TABLES IN SCHEMA
Date: Sun, 12 Apr 2026 16:22:24 +0900
Message-ID: <CAMrLSE7bZ70eGaTMH3-g=TA_sfp3kTDOrHVNYw1=1XL9krapqQ@mail.gmail.com> (raw)

Hi hackers,

  While reading aclchk.c I noticed that objectsInSchemaToOids(), used
  by GRANT/REVOKE ... ON ALL TABLES IN SCHEMA, calls
  getRelationsInNamespace() five times for the OBJECT_TABLE case --
  once per relkind (RELATION, VIEW, MATVIEW, FOREIGN_TABLE,
  PARTITIONED_TABLE):

      case OBJECT_TABLE:
          objs = getRelationsInNamespace(namespaceId, RELKIND_RELATION);
          objects = list_concat(objects, objs);
          objs = getRelationsInNamespace(namespaceId, RELKIND_VIEW);
          ...
          objs = getRelationsInNamespace(namespaceId,
RELKIND_PARTITIONED_TABLE);
          objects = list_concat(objects, objs);
          break;

  pg_class does have an index on (relname, relnamespace), but there
  is no index matching (relnamespace, relkind), so each of those
  per-relkind calls falls back to a full heap scan via
  table_beginscan_catalog().  The work is just repeated five times.

  The attached patch introduces a small helper
  getRelationsInNamespaceMulti() that performs a single heap scan
  filtered by relnamespace and distributes matching tuples into
  per-relkind buckets supplied by the caller.  Relkind filtering is
  done in C after each tuple is read, which is trivially cheap.  The
  OBJECT_TABLE case uses the helper; OBJECT_SEQUENCE and
  OBJECT_PROPGRAPH are left on the original getRelationsInNamespace()
  helper because they only need a single relkind and benefit from the
  second ScanKey.

  Correctness / order preservation
  --------------------------------
   * Result order is identical.  The underlying pg_class heap (and
     thus its physical scan order) is the same regardless of how we
     filter, so each bucket ends up holding the same OIDs in the same
     relative order as a separate per-relkind heap scan would have
     produced.  Concatenating the buckets in the original relkind
     order reproduces the previous list tuple-for-tuple.

     I verified this empirically.  On a schema with interleaved
     relkinds (tables, views, matviews, partitioned tables) I ran two
     equivalent SQL formulations while forcing seq scans on pg_class:

       OLD-path model: UNION ALL of five
         "SELECT oid FROM pg_class
            WHERE relnamespace = X AND relkind = Y
            ORDER BY ctid"
         queries, one per relkind, in the same group order the code
         uses.

       NEW-path model: a single
         "SELECT oid FROM pg_class
            WHERE relnamespace = X
            ORDER BY ctid"
         bucketed by relkind and concatenated in the same group
         order.

     The two formulations produced identical OID sequences, element
     by element.  A positional FULL JOIN between them returned zero
     rows.

   * MVCC semantics are, if anything, a bit stricter.  The old code
     took five separate catalog scans, so in principle concurrent DDL
     could commit between scan N and scan N+1 and be visible to one
     but not another.  With a single scan everything is collected
     under one catalog snapshot.

   * Locking is unchanged in kind: AccessShareLock on pg_class is
     still taken, just once instead of five times.

  Benchmark
  ---------
  This is a targeted micro-optimization, not a dramatic speedup.
  With 10,000 tables in a single schema (pg_class ~10,452 rows),
  running GRANT/REVOKE SELECT ON ALL TABLES IN SCHEMA in a loop
  (6 iterations, first dropped as warmup), I measured a consistent
  ~15% reduction in end-to-end time:

                       baseline    patched     delta
      GRANT  (avg)     88.2 ms     75.9 ms    -14%
      REVOKE (avg)    134.9 ms    115.7 ms    -14%

  Per-iteration numbers (ms):

      baseline GRANT : 92, 87, 87, 85, 89
      patched  GRANT : 77, 72, 72, 76, 79, 79
      baseline REVOKE: 145, 144, 132, 128, 130, 128
      patched  REVOKE: 114, 117, 112, 120, 112, 119

  The absolute savings are small because most of the time in these
  commands is spent updating per-relation ACL tuples, not scanning
  pg_class.  For schemas with only a handful of relations the effect
  is not measurable.  The change is aimed at multi-tenant /
  partition-heavy installations that regularly issue
  "... ON ALL TABLES IN SCHEMA ..." statements over large catalogs.

  Testing
  -------
  Both `make check` and `make check-world` pass cleanly with the
  patch applied on top of current master (all suites green, no new
  failures).  TAP tests were not exercised (tree configured without
  --enable-tap-tests); I can rerun with TAP enabled if that is useful.

  The patch is attached (against master).  Feedback and review
  welcome -- in particular I'd like to know if anyone sees a
  correctness concern I missed, or prefers a different shape for the
  helper (e.g. returning a single flat list rather than per-relkind
  buckets).

  Thanks,
  charsyam


Attachments:

  [application/octet-stream] 0001-Reduce-pg_class-scans-in-GRANT-REVOKE-ON-ALL-TABLES-.patch (6.8K, 3-0001-Reduce-pg_class-scans-in-GRANT-REVOKE-ON-ALL-TABLES-.patch)
  download | inline diff:
From 32e86de0e36a9b9544426ab91c6af57d029d078c Mon Sep 17 00:00:00 2001
From: charsyam <[email protected]>
Date: Sun, 12 Apr 2026 16:20:35 +0900
Subject: [PATCH] Reduce pg_class scans in GRANT/REVOKE ON ALL TABLES IN SCHEMA

When processing GRANT/REVOKE ... ON ALL TABLES IN SCHEMA,
objectsInSchemaToOids() called getRelationsInNamespace() five times,
once per relkind (RELATION, VIEW, MATVIEW, FOREIGN_TABLE,
PARTITIONED_TABLE).  pg_class does have an index on
(relname, relnamespace), but there is no index matching
(relnamespace, relkind), so each of those per-relkind calls falls
back to a full heap scan via table_beginscan_catalog() -- i.e. the
catalog is scanned five times in total.

Introduce getRelationsInNamespaceMulti(), which performs a single
heap scan filtered by relnamespace and distributes matching tuples
into per-relkind buckets supplied by the caller.  Relkind filtering
is done in C after each tuple is read, which is trivially cheap.
The OBJECT_TABLE case uses the helper; OBJECT_SEQUENCE and
OBJECT_PROPGRAPH keep calling getRelationsInNamespace() unchanged
because they only need a single relkind and benefit from the second
ScanKey.

Behavior is preserved:

* Result order is identical.  The underlying pg_class heap (and
  therefore its physical scan order) is the same regardless of how
  we filter, so each bucket ends up holding exactly the OIDs that
  the corresponding per-relkind heap scan would have produced, in
  the same order.  Concatenating the buckets in the original
  relkind order reproduces the previous list tuple-for-tuple.
  This was verified empirically by comparing, on a schema with
  mixed relkinds, the OID sequence produced by the old UNION-ALL
  pattern against the new single-scan + bucketed pattern; the
  sequences are identical element by element.

* MVCC semantics are, if anything, a bit stricter: all relkinds
  are now collected under a single catalog snapshot rather than
  five.

* Locking is unchanged in kind -- AccessShareLock on pg_class is
  still taken, just once instead of five times.

A simple benchmark (10,000 tables in one schema, pg_class ~10,452
rows) shows a consistent ~15% reduction in end-to-end time of
GRANT/REVOKE SELECT ON ALL TABLES IN SCHEMA:

  GRANT : 88.2 ms -> 75.9 ms
  REVOKE: 134.9 ms -> 115.7 ms

The absolute savings are small because the bulk of the time in
these commands is spent updating per-relation ACL tuples, not
scanning pg_class.  For schemas with only a handful of relations
the effect is not measurable.  The change is therefore a targeted
improvement for deployments with very large catalogs
(multi-tenant / partition-heavy systems) that frequently run ALL
TABLES IN SCHEMA grants.
---
 src/backend/catalog/aclchk.c | 90 ++++++++++++++++++++++++++++++++----
 1 file changed, 80 insertions(+), 10 deletions(-)

diff --git a/src/backend/catalog/aclchk.c b/src/backend/catalog/aclchk.c
index 67424fe3b0c..a68f0a989d4 100644
--- a/src/backend/catalog/aclchk.c
+++ b/src/backend/catalog/aclchk.c
@@ -125,6 +125,11 @@ static List *objectNamesToOids(ObjectType objtype, List *objnames,
 							   bool is_grant);
 static List *objectsInSchemaToOids(ObjectType objtype, List *nspnames);
 static List *getRelationsInNamespace(Oid namespaceId, char relkind);
+/* Single-scan helper over pg_class for multiple relkinds in one namespace */
+static void getRelationsInNamespaceMulti(Oid namespaceId,
+										 const char *relkinds,
+										 int nkinds,
+										 List **buckets);
 static void expand_col_privileges(List *colnames, Oid table_oid,
 								  AclMode this_privileges,
 								  AclMode *col_privileges,
@@ -797,16 +802,25 @@ objectsInSchemaToOids(ObjectType objtype, List *nspnames)
 		switch (objtype)
 		{
 			case OBJECT_TABLE:
-				objs = getRelationsInNamespace(namespaceId, RELKIND_RELATION);
-				objects = list_concat(objects, objs);
-				objs = getRelationsInNamespace(namespaceId, RELKIND_VIEW);
-				objects = list_concat(objects, objs);
-				objs = getRelationsInNamespace(namespaceId, RELKIND_MATVIEW);
-				objects = list_concat(objects, objs);
-				objs = getRelationsInNamespace(namespaceId, RELKIND_FOREIGN_TABLE);
-				objects = list_concat(objects, objs);
-				objs = getRelationsInNamespace(namespaceId, RELKIND_PARTITIONED_TABLE);
-				objects = list_concat(objects, objs);
+				{
+					const char kinds[] = {
+						RELKIND_RELATION,
+						RELKIND_VIEW,
+						RELKIND_MATVIEW,
+						RELKIND_FOREIGN_TABLE,
+						RELKIND_PARTITIONED_TABLE
+					};
+					List *buckets[lengthof(kinds)];
+					int i;
+
+					for (i = 0; i < (int) lengthof(kinds); i++)
+						buckets[i] = NIL;
+
+					getRelationsInNamespaceMulti(namespaceId, kinds, lengthof(kinds), buckets);
+
+					for (i = 0; i < (int) lengthof(kinds); i++)
+						objects = list_concat(objects, buckets[i]);
+				}
 				break;
 			case OBJECT_SEQUENCE:
 				objs = getRelationsInNamespace(namespaceId, RELKIND_SEQUENCE);
@@ -907,6 +921,62 @@ getRelationsInNamespace(Oid namespaceId, char relkind)
 	return relations;
 }
 
+/*
+ * getRelationsInNamespaceMulti
+ *
+ * Perform a single heap scan over pg_class for the given namespace, and
+ * distribute matching tuples into per-relkind buckets provided by the
+ * caller.  There is no pg_class index matching (relnamespace, relkind),
+ * so the previous per-relkind variant also resorted to a full heap scan;
+ * this helper simply collapses N such scans into one.
+ *
+ * Order preservation: within each bucket, entries appear in the order
+ * they were encountered during the heap scan.  Because the underlying
+ * heap (and thus its physical scan order) is the same regardless of
+ * how we filter, each bucket ends up holding the same OIDs in the same
+ * relative order as a separate per-relkind heap scan would have
+ * produced.  Concatenating the buckets in the caller's requested
+ * relkind order therefore reproduces the list that the previous code
+ * built from N separate getRelationsInNamespace() calls, tuple for
+ * tuple.
+ */
+static void
+getRelationsInNamespaceMulti(Oid namespaceId, const char *relkinds, int nkinds, List **buckets)
+{
+	ScanKeyData key;
+	Relation	rel;
+	TableScanDesc scan;
+	HeapTuple	tuple;
+	int			i;
+
+	/* Open pg_class once and scan by namespace; filter relkind in-code */
+	ScanKeyInit(&key,
+				Anum_pg_class_relnamespace,
+				BTEqualStrategyNumber, F_OIDEQ,
+				ObjectIdGetDatum(namespaceId));
+
+	rel = table_open(RelationRelationId, AccessShareLock);
+	scan = table_beginscan_catalog(rel, 1, &key);
+
+	while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
+	{
+		Form_pg_class cform = (Form_pg_class) GETSTRUCT(tuple);
+		char		rk = cform->relkind;
+
+		for (i = 0; i < nkinds; i++)
+		{
+			if (rk == relkinds[i])
+			{
+				buckets[i] = lappend_oid(buckets[i], cform->oid);
+				break;
+			}
+		}
+	}
+
+	table_endscan(scan);
+	table_close(rel, AccessShareLock);
+}
+
 
 /*
  * ALTER DEFAULT PRIVILEGES statement
-- 
2.50.1 (Apple Git-155)



view thread (3+ messages)  latest in thread

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]
  Subject: Re: [PATCH] Reduce pg_class scans in GRANT/REVOKE ON ALL TABLES IN SCHEMA
  In-Reply-To: <CAMrLSE7bZ70eGaTMH3-g=TA_sfp3kTDOrHVNYw1=1XL9krapqQ@mail.gmail.com>

* 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