Skip to content

fix(plugin-postgresql): resolve effective schema when public does not exist#1665

Merged
datlechin merged 3 commits into
mainfrom
fix/1662-pg-default-schema
Jun 12, 2026
Merged

fix(plugin-postgresql): resolve effective schema when public does not exist#1665
datlechin merged 3 commits into
mainfrom
fix/1662-pg-default-schema

Conversation

@datlechin

Copy link
Copy Markdown
Member

Fixes #1662

A PostgreSQL database with no public schema showed no tables and no schema selector. The user had to create an empty public schema to get the picker to appear.

Two defects combined:

  • LibPQDriverCore.currentSchema was hardcoded to public and only updated when SELECT current_schema() returned non-NULL. PostgreSQL returns NULL there when neither public nor a $user schema exists (default search_path is "$user", public), so every metadata query ran against a schema that does not exist.
  • The schema picker only rendered with more than one schema, so a single-schema database left no way to switch.

Changes:

  • connect() now falls back when current_schema() is NULL: first current_schemas(false)[1] (the first existing search_path entry, which honors ALTER ROLE/DATABASE SET search_path), then the first user schema alphabetically. On fallback it issues SET search_path so editor queries resolve to the same schema the sidebar shows. Runs before onPostConnect, so the metadata connection pool inherits the right schema.
  • The schema picker shows whenever at least one schema is known, still gated to schema-switching drivers.
  • setSearchPath no longer appends , public when switching schemas. Note: unqualified calls to extension functions installed in public (uuid-ossp, PostGIS) now require qualification after switching to another schema.
  • The database list table count now counts all user schemas instead of only public.

CockroachDB and Redshift share LibPQDriverCore but always have a public schema, so the fallback never triggers for them. No PluginKit types touched, no ABI change.

Tests: PostgreSQLSearchPathTests (updated for the new SET SQL), new PostgreSQLDefaultSchemaFallbackTests and SchemaPickerVisibilityTests. All pass locally; swiftlint lint --strict clean.

DBeaver had this same bug and fixed it the same way in 7.1.4 (reading the effective search_path instead of assuming public).

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0e6eff53f7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return "SET search_path TO \(quotedIdentifier)"
}
return "SET search_path TO \(quotedIdentifier), public"
return "SET search_path TO \(quotedIdentifier)"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve PostGIS lookup for spatial rendering

When a user switches to a non-public schema on a database where PostGIS is installed in public, this removes public from the session search path. The driver's own spatial rendering path still runs unqualified conversion SQL (PostGISSpatialRewrite.geometryConversionQuery / geographyConversionQuery use ST_AsEWKT(t::geometry)), so those internal PQexecParams conversions fail and the app falls back to raw EWKB hex for geometry/geography columns. Please either keep the extension schema available for these internal conversions or qualify/probe the PostGIS functions/types before dropping public.

Useful? React with 👍 / 👎.

…rch path and use USAGE-filtered fallback on Redshift
@datlechin

Copy link
Copy Markdown
Member Author

Follow-up from self-review (4f922c8):

  • Schema probe now distinguishes a failed query from a NULL result. A transient error on SELECT current_schema() keeps the prior schema instead of triggering the fallback chain and overwriting the search path.
  • Fallback queries are injected per driver: Redshift uses its USAGE-filtered pg_namespace list, so the chosen default is always readable by the connected role. PostgreSQL and CockroachDB keep the information_schema.schemata list.
  • CHANGELOG: the search-path behavior change moved to its own Changed entry with the extension-function caveat spelled out, separate from the Fixed entry.
  • New tests: PostgreSQLSchemaProbeTests (5 cases for the failure/NULL/text mapping) and Redshift fallback ordering. All targeted suites pass, swiftlint strict clean.

Signed-off-by: Ngô Quốc Đạt <datlechin@gmail.com>
@datlechin datlechin merged commit 91a9cb0 into main Jun 12, 2026
3 checks passed
@datlechin datlechin deleted the fix/1662-pg-default-schema branch June 12, 2026 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No database shown if schema "public" does not exist

1 participant