#!/usr/bin/env python3 """tenants.py PREFIX N OUT.tsv [PORT] Tenant sign-up scenario. Starts from a fresh cluster of the install at PREFIX and adds N tenants one after another. One sign-up = one transaction: CREATE SCHEMA tN; CREATE TABLE tN.a1 .. tN.a10 (id int PRIMARY KEY, v text); GRANT SELECT ON ALL TABLES IN SCHEMA tN TO reader; ALTER PUBLICATION pub ADD TABLES IN SCHEMA tN; COMMIT; Writes one row per sign-up: i, total ms, and ms for the GRANT and for the ALTER PUBLICATION (both measured client-side, inside the same transaction). The last line is a comment with pg_class size at the end. The cluster is created under ~/.cache/ygg-tenants/ and removed afterwards. GUCs: fsync=off, synchronous_commit=off, autovacuum=off (AV=on to enable), so the number is CPU in the catalog, not disk; shared_buffers=1GB (SB=... to change). """ import os, shutil, subprocess, sys, time import psycopg prefix, n, out = sys.argv[1], int(sys.argv[2]), sys.argv[3] port = int(sys.argv[4]) if len(sys.argv) > 4 else 61437 base = os.path.expanduser(f"~/.cache/ygg-tenants/{port}") data = os.path.join(base, "data") bindir = os.path.join(prefix, "bin") def run(*a): subprocess.run(a, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) shutil.rmtree(base, ignore_errors=True) os.makedirs(base) run(f"{bindir}/initdb", "-D", data, "-A", "trust", "-U", "postgres", "--no-sync", "-E", "UTF8", "--locale=C") with open(os.path.join(data, "postgresql.conf"), "a") as f: f.write(f""" port = {port} listen_addresses = '' unix_socket_directories = '{base}' fsync = off synchronous_commit = off full_page_writes = off autovacuum = {os.environ.get('AV', 'off')} shared_buffers = {os.environ.get('SB', '1GB')} wal_level = logical max_wal_size = 8GB log_min_messages = warning """) run(f"{bindir}/pg_ctl", "-D", data, "-l", os.path.join(base, "log"), "-w", "start") try: conn = psycopg.connect(host=base, port=port, user="postgres", dbname="postgres", autocommit=True) cur = conn.cursor() cur.execute("CREATE ROLE reader") cur.execute("CREATE PUBLICATION pub") # empty; schemas are added one by one cur.execute("SELECT reltuples, relpages FROM pg_class WHERE oid = 'pg_class'::regclass") initial_catalog_stats = cur.fetchone() conn.autocommit = False tables = "".join(f"CREATE TABLE {{s}}.a{k} (id int PRIMARY KEY, v text);" for k in range(1, 11)) rows = [] for i in range(1, n + 1): s = f"t{i}" t0 = time.perf_counter() cur.execute(f"CREATE SCHEMA {s}") cur.execute(tables.format(s=s)) t1 = time.perf_counter() cur.execute(f"GRANT SELECT ON ALL TABLES IN SCHEMA {s} TO reader") t2 = time.perf_counter() cur.execute(f"ALTER PUBLICATION pub ADD TABLES IN SCHEMA {s}") t3 = time.perf_counter() conn.commit() t4 = time.perf_counter() rows.append((i, (t4 - t0) * 1e3, (t2 - t1) * 1e3, (t3 - t2) * 1e3)) conn.autocommit = True cur.execute("SELECT count(*) FROM pg_class") nclass = cur.fetchone()[0] cur.execute("SELECT reltuples, relpages, pg_relation_size(oid) / current_setting('block_size')::int " "FROM pg_class WHERE oid = 'pg_class'::regclass") final_catalog_stats = cur.fetchone() # sanity: every tenant table is granted and published cur.execute("SELECT count(*) FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace " "WHERE n.nspname ~ '^t[0-9]+$' AND c.relkind = 'r' " "AND has_table_privilege('reader', c.oid, 'SELECT')") granted = cur.fetchone()[0] cur.execute("SELECT count(*) FROM pg_publication_tables WHERE pubname = 'pub'") published = cur.fetchone()[0] conn.close() finally: subprocess.run([f"{bindir}/pg_ctl", "-D", data, "-m", "fast", "-w", "stop"], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) shutil.rmtree(base, ignore_errors=True) ok = granted == 10 * n and published == 10 * n with open(out, "w") as f: f.write("i\tms\tgrant_ms\tpub_ms\n") for r in rows: f.write("%d\t%.3f\t%.3f\t%.3f\n" % r) f.write(f"# pg_class stats initial={initial_catalog_stats} final={final_catalog_stats}\n") f.write(f"# pg_class={nclass} granted={granted} published={published} ok={ok}\n") print(f"{out}: n={n} total_ms={sum(r[1] for r in rows):.0f} pg_class={nclass} ok={ok}") sys.exit(0 if ok else 3)