Skip to content

fix: dump and diff the ROWS estimate of set-returning functions (#702) - #706

Merged
tianzhou merged 3 commits into
mainfrom
fix/issue-702-function-rows
Nov 9, 2026
Merged

tianzhou merged 3 commits into
mainfrom
fix/issue-702-function-rows

Conversation

@tianzhou

@tianzhou tianzhou commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Summary

pg_proc.prorows was never inspected, so the ROWS n planner estimate on a set-returning function was dropped on dump (replaying with the default of 1000) and a change to it produced an empty plan.

  • GetFunctionsForSchema now reads proretset and prorows; ir.Function gains Rows, kept only when it differs from the default (same rule as pg_dump: skip 0 and 1000).
  • CREATE OR REPLACE FUNCTION emits ROWS n after the PARALLEL clause.
  • functionsEqual, functionsEqualExceptAttributes and functionsEqualExceptComment compare Rows, so a changed estimate is applied with CREATE OR REPLACE.

Note: the issue says COST is already handled, but procost is not inspected either. Left out of this PR to keep it scoped to ROWS.

Fixes #702

Test plan

Folded into testdata/diff/create_function/alter_function_same_signature: a RETURNS TABLE function with the default estimate in old.sql and ROWS 1 in new.sql, expecting a CREATE OR REPLACE ... ROWS 1. The integration test's idempotency check confirms the estimate round-trips through the inspector.

PGSCHEMA_TEST_FILTER="create_function/" go test ./internal/diff -run TestDiffFromFiles
PGSCHEMA_TEST_FILTER="create_function/alter_function_same_signature" go test ./cmd -run TestPlanAndApply

🤖 Generated with Claude Code

pg_proc.prorows was never inspected, so a function created with ROWS 1
was dumped without it (replaying with the default of 1000) and a change
to the estimate produced no plan.

Read proretset/prorows in GetFunctionsForSchema, keep the estimate on
ir.Function when it differs from the default (as pg_dump does), emit
ROWS n after PARALLEL in CREATE OR REPLACE FUNCTION, and compare it in
the function equality checks so a change triggers CREATE OR REPLACE.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 9, 2026 13:38
@greptile-apps

greptile-apps Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

Preserves custom ROWS estimates when dumping and comparing set-returning functions.

  • Function dumps and diffs now keep non-default ROWS estimates.

Reviews (1) · Last reviewed commit: "fix: dump and diff the ROWS estimate of ..." · Reviewed by Greptile

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A ROWS-only change uses CREATE OR REPLACE without preserving a nondefault COST, causing an existing COST 5 to reset to 100.

1 open issue
1 file not reviewed
  • ir/queries/queries.sql.go: Generated file
What changed in this PR

Adds support for preserving and comparing set-returning function ROWS estimates in pgschema, addressing #702 while leaving COST support outside this PR’s scope.

Changes:

  • Reads row estimates from PostgreSQL and stores nondefault values in the function representation.
  • Emits ROWS in function DDL and detects estimate changes during comparison.
  • Extends the existing function migration fixture to cover changing the default estimate to ROWS 1.
File Description
internal/​diff/​function.go Emits and compares function row estimates.
ir/​inspector.go Captures nondefault estimates during inspection.
ir/​ir.go Adds the function Rows field.
ir/​queries/​queries.sql Queries set-returning status and row estimates.
ir/​queries/​queries.sql.go Updates generated query fields and scanning.
testdata/​diff/​create_function/​alter_function_same_signature/​old.sql Adds a function with the default estimate.
testdata/​diff/​create_function/​alter_function_same_signature/​new.sql Sets the desired estimate to ROWS 1.
testdata/​diff/​create_function/​alter_function_same_signature/​diff.sql Adds expected function replacement DDL.
testdata/​diff/​create_function/​alter_function_same_signature/​plan.json Updates the fingerprint and planned steps.
testdata/​diff/​create_function/​alter_function_same_signature/​plan.sql Adds the planned row-estimate change.
testdata/​diff/​create_function/​alter_function_same_signature/​plan.txt Updates the readable plan and modification count.

Review effort: Balanced

Comment thread internal/diff/function.go Resolved
A ROWS-only change now goes through the ALTER FUNCTION attribute path
like LEAKPROOF and PARALLEL, so the body is not re-sent and attributes
pgschema does not track (such as COST) are left untouched. Dropping
the estimate emits ROWS 1000, the default.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new ROWS emission leaves four golden files for the existing get_actor dependency fixture outdated, causing the full diff and plan/apply suites to fail despite passing scoped tests.

1 open issue
1 resolved since last review
1 file not reviewed
  • ir/queries/queries.sql.go: Generated file

Review effort: Balanced

Comment thread internal/diff/function.go Resolved
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@tianzhou
tianzhou merged commit 6377a12 into main Nov 9, 2026
1 check passed
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.

ROWS estimate on set-returning functions is not dumped or diffed

2 participants