Skip to content

Enhance SQL query structure and formatting#425

Closed
Rooffeell wants to merge 1 commit intopgplex:mainfrom
Rooffeell:main
Closed

Enhance SQL query structure and formatting#425
Rooffeell wants to merge 1 commit intopgplex:mainfrom
Rooffeell:main

Conversation

@Rooffeell
Copy link
Copy Markdown
Contributor

Refactor SQL queries for improved clarity and consistency.

Closes #422

Refactor SQL queries for improved clarity and consistency.
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Apr 30, 2026

Greptile Summary

This PR modifies ir/queries/queries.sql to remove several columns and change type-resolution logic, but queries.sql.go (the sqlc-generated file) was not regenerated. The SQL changes have no effect at runtime today, and running sqlc generate after merging will produce a Go file that fails to compile due to inspector.go still referencing the removed fields.

Beyond the missing regeneration, several of the SQL changes themselves are semantically incorrect:

  • typelem <> 0 replaces typcategory = 'A' for array detection — the original code included a comment explaining exactly why this condition is wrong (the name type has a non-zero typelem but is not an array).
  • generate_series(1, idx.indnatts) is used for column_directions and column_opclasses instead of indnkeyatts, so INCLUDE columns (which have no sort order or operator class) are incorrectly included.
  • relpersistence, no_inherit, nulls_not_distinct, and include_columns are removed from their respective queries while inspector.go still depends on all four to detect UNLOGGED tables, NO INHERIT checks, NULLS NOT DISTINCT unique constraints, and index INCLUDE columns.

Confidence Score: 1/5

Not safe to merge — changes are currently inert but will cause compilation failures and semantic regressions when the generated code is updated.

A P0 (generated code not regenerated, guaranteed compilation break on sqlc generate) combined with multiple P1s (array detection regression, INCLUDE column corruption, removed fields breaking active inspector features) places this well below the P0 ceiling of 2/5.

ir/queries/queries.sql — every changed section has a blocking issue; ir/queries/queries.sql.go and ir/inspector.go must also be updated in tandem.

Important Files Changed

Filename Overview
ir/queries/queries.sql Multiple critical fields removed from SQL queries (relpersistence, no_inherit, nulls_not_distinct, include_columns, num_key_columns); array detection condition regressed from typcategory='A' to typelem<>0; generated Go file not regenerated — changes are inert and will break compilation when sqlc is run

Reviews (1): Last reviewed commit: "Enhance SQL query structure and formatti..." | Re-trigger Greptile

Comment thread ir/queries/queries.sql
Comment on lines +25 to +29
SELECT
t.table_schema,
t.table_name,
t.table_type,
COALESCE(d.description, '') AS table_comment,
c.relpersistence::text AS relpersistence
COALESCE(d.description, '') AS table_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.

P0 Generated Go code not regenerated after SQL changes

queries.sql.go was not re-generated with sqlc generate after this PR's changes. This has two consequences: (1) the SQL changes here are entirely inert at runtime — the app still executes the old queries from the embedded string constants in the generated file, and (2) running sqlc generate after merging will produce a Go file that fails to compile because inspector.go still references fields that are being removed:

  • inspector.go:264table.Relpersistence (removed from GetTables/GetTablesForSchema)
  • inspector.go:495constraint.NoInherit (removed from both constraints queries)
  • inspector.go:497constraint.NullsNotDistinct.Bool (removed from GetConstraintsForSchema)
  • inspector.go:773indexRow.IncludeColumns (removed from GetIndexesForSchema)

queries.sql.go must be regenerated and inspector.go must be updated to stop referencing the removed fields before this PR is mergeable.

Comment thread ir/queries/queries.sql
ELSE dn.nspname || '.' || format_type(dt.oid, NULL)
END
WHEN dt.typtype = 'b' AND dt.typcategory = 'A' THEN
WHEN dt.typtype = 'b' AND dt.typelem <> 0 THEN
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Array detection condition is a documented regression

The condition was changed from dt.typcategory = 'A' to dt.typelem <> 0, but the original code had an explicit comment explaining exactly why typelem <> 0 is wrong:

Use typcategory = 'A' rather than typelem <> 0; the latter is true for non-array fixed-length types like name (typelem points to char).

The name type (pg_catalog.name) has typelem = 18 (pointing to char), but typcategory = 'S' (string), not 'A'. With this change, columns of type name would hit the array branch and be misidentified as char[]. The same incorrect condition appears at line 198 in GetColumnsForSchema.

Suggested change
WHEN dt.typtype = 'b' AND dt.typelem <> 0 THEN
WHEN dt.typtype = 'b' AND dt.typcategory = 'A' THEN

Comment thread ir/queries/queries.sql
Comment on lines +450 to 458
FROM generate_series(1, idx.indnatts) k
) as column_directions,
ARRAY(
SELECT CASE
WHEN opc.opcdefault THEN '' -- Omit default operator classes
ELSE COALESCE(opc.opcname, '')
END
FROM generate_series(1, idx.indnkeyatts) k
FROM generate_series(1, idx.indnatts) k
LEFT JOIN pg_opclass opc ON opc.oid = idx.indclass[k-1]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 INCLUDE columns incorrectly included in direction and opclass arrays

column_directions and column_opclasses now iterate over indnatts (all columns including INCLUDE columns) instead of indnkeyatts (key columns only). INCLUDE columns have no sort order and no operator class — they are not indexing key columns, just stored values. For an index like CREATE INDEX ON t (a) INCLUDE (b), this will generate ['ASC', 'ASC'] for directions and look up an indclass entry that is 0/undefined for the INCLUDE column slot.

The indoption and indclass arrays are only defined for the first indnkeyatts positions; entries beyond that point are meaningless. The include_columns array (now removed) was the correct place to handle the INCLUDE columns separately.

Comment thread ir/queries/queries.sql
c.convalidated AS is_valid,
COALESCE((to_jsonb(c) ->> 'conperiod')::boolean, false) AS is_period,
c.connoinherit AS no_inherit
COALESCE((to_jsonb(c) ->> 'conperiod')::boolean, false) AS is_period
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Removal of no_inherit and nulls_not_distinct drops active features

Both GetConstraints (line 341) and GetConstraintsForSchema (line 920) have had c.connoinherit AS no_inherit removed. Additionally GetConstraintsForSchema lost nulls_not_distinct and its LEFT JOIN pg_index i. These fields drive active behaviour:

  • inspector.go:495 populates Constraint.NoInherit — removing this silently makes all CHECK constraints appear inheritable, producing incorrect DDL for any schema that uses NO INHERIT checks.
  • inspector.go:497 populates Constraint.NullsNotDistinct (PG15+ UNIQUE NULLS NOT DISTINCT) — removing this will generate incorrect migration diffs for unique constraints that use NULLS NOT DISTINCT.

Both fields need to be retained, or their consumers in inspector.go must be updated accordingly.

Comment thread ir/queries/queries.sql
t.table_type,
COALESCE(d.description, '') AS table_comment,
c.relpersistence::text AS relpersistence
COALESCE(d.description, '') AS table_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.

P1 relpersistence removal breaks UNLOGGED table detection

Removing c.relpersistence::text AS relpersistence from both GetTables and GetTablesForSchema will drop the ability to detect UNLOGGED tables. inspector.go:264 reads:

Unlogged: table.Relpersistence.Valid && table.Relpersistence.String == "u",

Without this column, Relpersistence would always be the zero value and every table would appear as a regular (logged) table, causing UNLOGGED tables to lose their UNLOGGED modifier in dumps and diffs.

@tianzhou tianzhou closed this Apr 30, 2026
@tianzhou
Copy link
Copy Markdown
Contributor

Do no accept mere formatting changes

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.

Bug: Schema with quoted mixed-case custom types cannot be round-tripped

2 participants