pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Reduce planning roundtrips for remote databases

Open mikkelam opened this issue 1 month ago • 2 comments

We're using pgschema 1.13.0 against Supabase, and planning is becoming a significant bottleneck in CI.

Our setup:

  • Separate public and private schema invocations.
  • The remote target database also serves as the external planning database, to provide our extensions, roles, and cross-schema dependencies.
  • Supabase session pooler, with default_query_exec_mode=simple_protocol.
  • The public schema currently contains approximately 48 tables, 79 functions, and 74 RLS policies.
  • Estimated latency to the database is around 200ms; we haven't measured it from the CI runner.

Across several CI runs, reconciliation takes roughly 9–15 minutes per environment. In one representative run:

  • The private schema reported no changes after about 38 seconds.
  • The public schema then took approximately 11m15s to produce a plan.
  • The plan contained just five CREATE POLICY statements.
  • From “Applying changes…” to “Changes applied successfully!” took about 1.5 seconds.

We suspect many sequential database roundtrips during introspection or preparation of the external planning schema, but haven't profiled this yet.

Could planning reduce roundtrips through batching queries/statements or fetching metadata for multiple objects together? Is there an existing configuration or recommended setup that would improve this?

We can reproduce against staging with --debug. What diagnostics would be most useful to identify where the time goes?

Possibly related: #285?

Btw, I love your tool. It is super nice.

mikkelam avatar Oct 15 '26 13:10 mikkelam

Perf is our priority. Let me take a look

tianzhou avatar Oct 15 '26 13:10 tianzhou

Thanks for the detailed report. This is the same root cause as #644 (and #285 that you linked), so I'm closing this one as a duplicate and will track the fix there.

What's happening: the inspector resolves grantee names one row at a time (SELECT rolname FROM pg_roles WHERE oid = $1 per aclexplode row) and looks up constraint column positions one column at a time. On Supabase every table gets grants to anon, authenticated, service_role and postgres, so a single table yields ~28 privilege rows and each function 4. For 48 tables and 79 functions that's roughly 1,700+ round trips per inspection, and plan inspects twice (target database + external plan database, both on the same remote host). At ~200ms that comes out to ~12 minutes, which matches your 11m15s.

The fix is to return the role name and column position from the already-batched queries (see the proposal in #644). No configuration change will help in the meantime, though using the embedded plan database instead of --plan-host would roughly halve the time if your schema can tolerate it.

Correction: --debug doesn't currently log the inspector's catalog queries, only connection and apply statements, so it won't show these lookups. If you want to confirm on your side, enable log_statement = 'all' on the plan database (or watch pg_stat_activity / pg_stat_statements) during a plan and look for SELECT rolname FROM pg_roles WHERE oid = $1.

tianzhou avatar Oct 15 '26 14:10 tianzhou