Repository navigation
fix: keep schema qualifier on routine types shadowed by pg_catalog (#701) - #727
Conversation
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
[Medium impact] The change reaches routine dumps, migrations, and plan comparisons. Fix the two desired-state rewrite failures before merging. FindingsSummaryKeeps schema qualifiers on routine types whose names also exist in
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Desired SQL] --> B[Find created names shared with pg_catalog]
B --> C[Point matching qualifiers at temporary schema]
C --> D[Strip remaining target schema qualifiers]
D --> E[External provider extracts created names again]
E --> F[Rewrite selected function body references]
F --> G[Apply SQL and inspect]
Reviews (1) · Last reviewed commit: "fix: keep schema qualifier on routine ty..." · Reviewed by Greptile |
| result.WriteString(mapCodePreservingStrings(seg.text, func(code string) string { | ||
| code = rewrite(sr.re1, code, 1, quoted) | ||
| code = rewrite(sr.re2, code, 1, quoted) | ||
| code = rewrite(sr.re3, code, 2, unquoted) | ||
| return rewrite(sr.re4, code, 2, unquoted) |
There was a problem hiding this comment.
qualifyShadowedTypeReferences also changes column references whose table alias matches the managed schema. With CREATE TABLE public.name(id int), the valid view query SELECT public.name FROM (SELECT 1 AS name) public becomes SELECT "tmp".name FROM (SELECT 1 AS name) public. There is no table alias named tmp, so planning fails. The old stripping pass produced the valid unqualified column name. Keep column references out of this rewrite.
| schemaAgnosticSQL := qualifyShadowedTypeReferences(sql, schema, ed.tempSchema, catalogTypes) | ||
| schemaAgnosticSQL = stripSchemaQualifications(schemaAgnosticSQL, schema) |
There was a problem hiding this comment.
The new pass turns CREATE TABLE public.name into CREATE TABLE "<tmp>".name. The later extractCreatedObjectNames call reads only the first identifier after TABLE, so it records the temporary schema instead of name. This makes qualifyFunctionBodiesWithTempSchema leave public.name unchanged inside function bodies.
If the external plan database lacks public.name, an immutable SQL function reading that table can fail when a materialized view calls it during planning. The table exists only in the temporary schema. Collect the created names before adding qualifiers, or teach the extractor to read qualified names.
Co-Authored-By: Tianzhou <t@bytebase.com>
Co-Authored-By: Tianzhou <t@bytebase.com>
There was a problem hiding this comment.
Note
Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.
Devin Review found 4 findings. Devin fixed 3 findings; 1 finding is left for you to triage below.
Fixed by Devin (3)
- Broken routine references in external plans — createdObjectRe now skips an optional schema qualifier, so CREATE TABLE .name records 'name'. Unit test added.
- Premature revokes block function migrations — normalizeSchemaNames now maps Function.IdentityArguments from the temp schema, so functionPrivilegeObjectName matches the privilege ObjectName; unit test added.
- Restricted overload retains public execution — Privilege signatures (inspector identity args, privilege object names, normalizePrivilegeObjectName) now keep the qualifier on shadowed types; plan maps it from the temp schema. Regression: overload nc_core(name)/nc_core(public.name) with REVOKE in dump and plan fixtures.
| // Add parameter name and type | ||
| // Strip schema prefix from data type if it matches the target schema | ||
| dataType := stripSchemaPrefixMode(param.DataType, targetSchema, qualifySchema) | ||
| dataType := stripTypeSchemaPrefix(param.DataType, targetSchema, qualifySchema, shadowedTypes) |
There was a problem hiding this comment.
🔴 Wrong type casts in routine defaults
For an enum parameter of type public.name, formatFunctionParameter preserves the declaration but emits 'main'::name as the default. normalizeDefaultValue strips the cast qualifier for functions and procedures. PostgreSQL rejects the built-in name default for the enum parameter.
Devin left this for you to triage: Real but narrow: needs an enum/domain named like a pg_catalog type used as a routine parameter with a default; the '::public.name' cast is stripped by normalizeDefaultValue and the pgschema_tmp_ cast regex. Recommend a follow-up that threads shadowedTypes into default-value normalization rather than widening this PR.
Learn more
The parameter declaration and its default expression travel through different normalization paths. normalizeFunction and normalizeProcedure both call normalizeDefaultValue, which unconditionally strips same-schema qualifiers from casts. normalizeSchemaNames also strips qualifiers from parameter defaults when planning, and function rendering performs another cast-stripping pass. The declaration now preserves its enum type while the default still resolves to the built-in type.
Example: Declare CREATE TYPE public.name AS ENUM ('main') and a function with n public.name DEFAULT 'main'::public.name. The generated declaration has n public.name DEFAULT 'main'::name. PostgreSQL resolves the default cast to pg_catalog.name, which is not implicitly coercible to the enum.
Recommended fix: Preserve casts to shadowed types through inspection, temporary-to-target remapping, and SQL rendering for both functions and procedures. Verify replay and a subsequent no-op plan for a routine with a typed enum default.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Devin Review found 1 finding. It is left for you to triage in Devin Review.
Summary
Fixes #701. A user type that shares its name with a
pg_catalogtype (e.g. the row type ofCREATE TABLE name) needs its schema qualifier.pg_catalogis always searched first, sonm public.namestripped tonm namereplays aspg_catalog.name. pgschema stripped same-schema qualifiers unconditionally in four places. Each one now keeps the qualifier for these names:buildShadowedTypesrecords type names in the target/managed schema thatpg_catalogalso has, inMetadata.ShadowedTypes.stripSameSchemaPrefixkeeps the qualifier for those names. That covers parameters,RETURNS,SETOF, arrays andTABLE(...)columns.stripSchemaFromReturnType/stripSchemaPrefixskip shadowed names (ir.IsShadowedTypeName).stripTypeSchemaPrefix, using the union of both IRs'ShadowedTypes.stripSchemaQualifications,qualifyShadowedTypeReferencesrewritespublic.Xto"<tmp>".Xfor each object the SQL creates whose name is apg_catalogtype. Without this,public.namebecame barenamein the temp schema and bound topg_catalog.name, so the plan said "No changes".normalizeSchemaNamesnow also maps the temp schema in theFunctions/Proceduresmap keys, because a qualified argument type is part of the key (nc_core(pgschema_tmp_….name)).Built-in references such as
nc_builtin(n name) RETURNS namestay unqualified.Tests
issue_701_shadowed_type_paramcovers parameters, return types,SETOF, arrays,TABLE(...), procedures and the built-innametype.create_function/issue_701_shadowed_type_paramchecks two things. Moving fromnm nametonm public.namedrops and recreates the function. Applying the same desired state a second time plans no changes.TestQualifyShadowedTypeReferences../ir,./internal/postgres,./internal/diff,./cmd/dump,./cmd/plan, andTestPlanAndApplyforcreate_function/,create_procedure/,create_aggregate/,create_type/,dependency/andprivilege/.Link to Devin session: https://app.devin.ai/sessions/2160d3f08fd1454bb91db03645ac0427
Open in Devin Desktop: https://app.devin.ai/desktop/session/2160d3f08fd1454bb91db03645ac0427?variant=devin
Requested by: @tianzhou
👀 1 finding for you to triage · Follow Devin's work