| From: | Manu <manuelreyesbravo(at)gmail(dot)com> |
|---|---|
| To: | pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Cc: | CharSyam <charsyam(at)gmail(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz> |
| Subject: | [PATCH] Enumerate schema members through pg_depend |
| Date: | 2026-12-05 06:58:55 |
| Message-ID: | 179645393515.1049625.15148239006009831884@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
In a database with one schema per tenant, granting access to the
objects of one tenant can scan the catalogs for every tenant. REINDEX
SCHEMA also scans pg_class to find its relations. For a publication
covering several schemas, enumerating its tables can scan pg_class
once per schema, including on CREATE SUBSCRIPTION and REFRESH
PUBLICATION.
I propose using pg_depend_reference_index to look up the candidate
objects of a schema, then checking each candidate's namespace in
pg_class or pg_proc. The catalog scan remains available for pinned
schemas and when candidates exceed 1/64 of the estimated catalog
rows.
Before choosing a scan, we apply the last measured tuple density to
the current page count. A catalog can grow substantially between
ANALYZE runs. We scan the member catalog directly when
ignore_system_indexes is enabled, to avoid a heap scan of pg_depend
during operations such as REINDEX SCHEMA.
I measured 5,000 tenant sign-ups in a fresh cluster. Each transaction
creates a schema and ten tables with primary keys, grants SELECT
on all tables in that schema, and adds the schema to a publication.
With autovacuum and fsync disabled, the median total went from 135.2 s
on master (five runs) to 16.9 s with the series before the repair-mode
change (three runs). The builds were measured in separate batches.
In the last 500 sign-ups, median GRANT time went from 40.7 ms to 0.14 ms,
and median ALTER PUBLICATION time from 8.31 ms to 0.074 ms. Each run
checked that all 50,000 tables were granted and published.
After creating and dropping 100,000 tables in the target schema
without vacuuming, the first GRANT took 14.0 ms with the lookup
and 8.2 ms on master. The next four took 0.06-0.16 ms with the
lookup and 1.93-3.50 ms on master.
On the final series, REINDEX SCHEMA for a schema with two indexes
in a cluster with 100,433 pg_class rows took 0.197 ms in normal mode
versus 3.289 ms on master (30 runs per build). In repair mode the
final series matched master's catalog scan path and rebuilt the same
two indexes. Median latency was 20.223 ms versus 18.084 ms on master.
The main correctness concern is a missing namespace dependency. For
schema publications, that can silently leave a table out of the set
the subscriber synchronizes. DROP SCHEMA CASCADE already relies on
these dependencies. Patch 0001 adds an oidjoins check for missing
entries and cross-checks the dependency path against the catalog scan
in assertion builds. Both checks detected deliberately omitted
dependencies in testing.
The attached series covers sequence and routine GRANT and REINDEX
SCHEMA in 0001, and schema publications in 0002. Patch 0003 credits
CharSyam's idea for GRANT/REVOKE ON ALL TABLES, collecting all five
relation kinds in one pass.
https://postgr.es/m/CAMrLSE7bZ70eGaTMH3-g=TA_sfp3kTDOrHVNYw1=1XL9krapqQ@mail.gmail.com
The series is based on 458c77d74d2. check-world passed on the base
and on each prefix in assertion-enabled builds.
The attachments also include the tenant benchmark script, per-run
tenant totals, and timings and scan counts for the repair-mode test.
Regards,
Manu
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-namespace-members-from-pg_depend.patch | text/x-patch | 26.9 KB |
| v1-0002-publications-schema-members.patch | text/x-patch | 2.9 KB |
| v1-0003-grant-on-all-tables-one-call.patch | text/x-patch | 3.4 KB |
| tenants.py.txt | text/plain | 4.4 KB |
| tenant-runs.txt | text/plain | 1.8 KB |
| repair-runs.txt | text/plain | 3.8 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | shihao zhong | 2026-12-05 05:32:43 | Re: POC: Unlocked path for GetSnapshotDataReuse |