diff --git a/internal/diff/function.go b/internal/diff/function.go index 72f2cf1ba..614f2d880 100644 --- a/internal/diff/function.go +++ b/internal/diff/function.go @@ -52,7 +52,7 @@ func generateModifyFunctionsSQL(diffs []*functionDiff, targetSchema string, coll continue } - // Check if only LEAKPROOF or PARALLEL attributes changed (not the function body/definition) + // Check if only LEAKPROOF, PARALLEL or ROWS attributes changed (not the function body/definition) onlyAttributesChanged := functionsEqualExceptAttributes(oldFunc, newFunc) if onlyAttributesChanged { @@ -97,6 +97,27 @@ func generateModifyFunctionsSQL(diffs []*functionDiff, targetSchema string, coll collector.collect(context, stmt) } + // Check ROWS changes; an empty Rows means the default estimate (1000) + if oldFunc.Rows != newFunc.Rows { + rows := newFunc.Rows + if rows == "" { + rows = "1000" + } + stmt := fmt.Sprintf("ALTER FUNCTION %s(%s) ROWS %s;", + qualifyEntityName(newFunc.Schema, newFunc.Name, targetSchema), + newFunc.GetArguments(), + rows) + + context := &diffContext{ + Type: DiffTypeFunction, + Operation: DiffOperationAlter, + Path: fmt.Sprintf("%s.%s", newFunc.Schema, newFunc.Name), + Source: diff, + CanRunInTransaction: true, + } + collector.collect(context, stmt) + } + // Check if comment also changed alongside attributes if oldFunc.Comment != newFunc.Comment { generateFunctionComment(newFunc, targetSchema, DiffTypeFunction, DiffOperationAlter, collector) @@ -263,6 +284,11 @@ func generateFunctionSQL(function *ir.Function, targetSchema string, qualifySche } // Note: Don't output PARALLEL UNSAFE (it's the default) + // Add ROWS estimate for set-returning functions when not the default (1000) + if function.Rows != "" { + stmt.WriteString(fmt.Sprintf("\nROWS %s", function.Rows)) + } + // Add SET clauses from pg_proc.proconfig. // Each entry is a raw "key=value" pair (e.g., "search_path=public", "TimeZone=UTC"). // Render them in sorted order for deterministic output. @@ -401,7 +427,7 @@ func formatFunctionParameter(param *ir.Parameter, includeDefault bool, targetSch return part } -// functionsEqualExceptAttributes compares two functions ignoring LEAKPROOF and PARALLEL attributes +// functionsEqualExceptAttributes compares two functions ignoring the LEAKPROOF, PARALLEL and ROWS attributes // Used to determine if ALTER FUNCTION can be used instead of CREATE OR REPLACE func functionsEqualExceptAttributes(old, new *ir.Function) bool { if old.Schema != new.Schema { @@ -431,7 +457,7 @@ func functionsEqualExceptAttributes(old, new *ir.Function) bool { if !setConfigEqual(old.SetConfig, new.SetConfig) { return false } - // Note: We intentionally do NOT compare IsLeakproof or Parallel here + // Note: We intentionally do NOT compare IsLeakproof, Parallel or Rows here // That's the whole point - we want to detect when only those attributes changed // Compare using normalized Parameters array @@ -466,6 +492,9 @@ func functionsEqual(old, new *ir.Function) bool { if old.IsSecurityDefiner != new.IsSecurityDefiner { return false } + if old.Rows != new.Rows { + return false + } if old.IsLeakproof != new.IsLeakproof { return false } @@ -515,6 +544,9 @@ func functionsEqualExceptComment(old, new *ir.Function) bool { if old.IsSecurityDefiner != new.IsSecurityDefiner { return false } + if old.Rows != new.Rows { + return false + } if old.IsLeakproof != new.IsLeakproof { return false } diff --git a/ir/inspector.go b/ir/inspector.go index 0c2ae8a32..092b1d0f1 100644 --- a/ir/inspector.go +++ b/ir/inspector.go @@ -1321,6 +1321,16 @@ func (i *Inspector) buildFunctions(ctx context.Context, schema *IR, targetSchema parallelMode = "UNSAFE" // Defensive default } + // ROWS estimate: pg_proc.prorows is 0 for non-set-returning functions and + // 1000 by default for set-returning ones; like pg_dump, keep it only when + // it differs from the default. + rows := "" + if fn.ReturnsSet { + if r := i.safeInterfaceToString(fn.ResultRows); r != "" && r != "0" && r != "1000" { + rows = r + } + } + // Parse parameters from the complete signature provided by pg_get_function_arguments() // This signature includes all parameter information including modes, names, types, and defaults parameters := i.parseParametersFromSignature(signature, schemaName) @@ -1343,6 +1353,7 @@ func (i *Inspector) buildFunctions(ctx context.Context, schema *IR, targetSchema IsSecurityDefiner: isSecurityDefiner, IsLeakproof: isLeakproof, Parallel: parallelMode, + Rows: rows, SetConfig: setConfig, } diff --git a/ir/ir.go b/ir/ir.go index 858cb2053..f57c64992 100644 --- a/ir/ir.go +++ b/ir/ir.go @@ -244,6 +244,7 @@ type Function struct { IsSecurityDefiner bool `json:"is_security_definer,omitempty"` // SECURITY DEFINER IsLeakproof bool `json:"is_leakproof,omitempty"` // LEAKPROOF Parallel string `json:"parallel,omitempty"` // SAFE, UNSAFE, RESTRICTED + Rows string `json:"rows,omitempty"` // ROWS n planner estimate of a set-returning function; empty when default (1000) or not set-returning SetConfig []string `json:"set_config,omitempty"` // SET clauses from pg_proc.proconfig (e.g., "search_path=public", "TimeZone=UTC") Dependencies []string `json:"dependencies,omitempty"` // Function keys (name(args)) this function depends on TypeDependencies []string `json:"-"` // schema.name of the types this function depends on per pg_depend (signature, SQL-standard body, defaults); ordering data, kept out of the fingerprint diff --git a/ir/queries/queries.sql b/ir/queries/queries.sql index c3a397555..0baca4413 100644 --- a/ir/queries/queries.sql +++ b/ir/queries/queries.sql @@ -1757,6 +1757,9 @@ SELECT p.prosecdef AS is_security_definer, p.proleakproof AS is_leakproof, p.proparallel AS parallel_mode, + p.proretset AS returns_set, + -- Planner row estimate for set-returning functions (ROWS n); text so it round-trips as pg_dump prints it + p.prorows::text AS result_rows, COALESCE(p.proconfig, '{}') AS set_config FROM information_schema.routines r LEFT JOIN pg_proc p ON p.proname = r.routine_name diff --git a/ir/queries/queries.sql.go b/ir/queries/queries.sql.go index 99de9ef82..8d5cdc64a 100644 --- a/ir/queries/queries.sql.go +++ b/ir/queries/queries.sql.go @@ -2161,6 +2161,9 @@ SELECT p.prosecdef AS is_security_definer, p.proleakproof AS is_leakproof, p.proparallel AS parallel_mode, + p.proretset AS returns_set, + -- Planner row estimate for set-returning functions (ROWS n); text so it round-trips as pg_dump prints it + p.prorows::text AS result_rows, COALESCE(p.proconfig, '{}') AS set_config FROM information_schema.routines r LEFT JOIN pg_proc p ON p.proname = r.routine_name @@ -2205,6 +2208,8 @@ type GetFunctionsForSchemaRow struct { IsSecurityDefiner bool `db:"is_security_definer" json:"is_security_definer"` IsLeakproof bool `db:"is_leakproof" json:"is_leakproof"` ParallelMode interface{} `db:"parallel_mode" json:"parallel_mode"` + ReturnsSet bool `db:"returns_set" json:"returns_set"` + ResultRows sql.NullString `db:"result_rows" json:"result_rows"` SetConfig []string `db:"set_config" json:"set_config"` } @@ -2234,6 +2239,8 @@ func (q *Queries) GetFunctionsForSchema(ctx context.Context, dollar_1 sql.NullSt &i.IsSecurityDefiner, &i.IsLeakproof, &i.ParallelMode, + &i.ReturnsSet, + &i.ResultRows, pq.Array(&i.SetConfig), ); err != nil { return nil, err diff --git a/testdata/diff/create_function/alter_function_same_signature/diff.sql b/testdata/diff/create_function/alter_function_same_signature/diff.sql index da4a0d01e..29b3bedb0 100644 --- a/testdata/diff/create_function/alter_function_same_signature/diff.sql +++ b/testdata/diff/create_function/alter_function_same_signature/diff.sql @@ -1,3 +1,18 @@ +ALTER FUNCTION expand_order(integer) ROWS 1; + +CREATE OR REPLACE FUNCTION order_items( + order_id integer +) +RETURNS SETOF integer +LANGUAGE sql +VOLATILE +ROWS 10 +AS $$ + SELECT order_id + 1; +$$; + +ALTER FUNCTION order_lines(integer) ROWS 1000; + CREATE OR REPLACE FUNCTION process_order( order_id integer, discount_percent numeric DEFAULT 0, diff --git a/testdata/diff/create_function/alter_function_same_signature/new.sql b/testdata/diff/create_function/alter_function_same_signature/new.sql index 77862d68e..9a53c27b1 100644 --- a/testdata/diff/create_function/alter_function_same_signature/new.sql +++ b/testdata/diff/create_function/alter_function_same_signature/new.sql @@ -20,4 +20,30 @@ BEGIN SELECT price INTO base_price FROM products WHERE id = order_id; RETURN base_price * (1 - discount_percent / 100) * (1 + tax_rate); END; -$$; \ No newline at end of file +$$; + +-- Same body, but with a planner row estimate; issue #702 +CREATE FUNCTION expand_order(order_id integer) +RETURNS TABLE (line_id integer) +LANGUAGE sql +ROWS 1 +AS $$ + SELECT order_id; +$$; + +-- Estimate dropped, back to the default of 1000; issue #702 +CREATE FUNCTION order_lines(order_id integer) +RETURNS SETOF integer +LANGUAGE sql +AS $$ + SELECT order_id; +$$; + +-- Body and estimate both change, so CREATE OR REPLACE carries ROWS; issue #702 +CREATE FUNCTION order_items(order_id integer) +RETURNS SETOF integer +LANGUAGE sql +ROWS 10 +AS $$ + SELECT order_id + 1; +$$; diff --git a/testdata/diff/create_function/alter_function_same_signature/old.sql b/testdata/diff/create_function/alter_function_same_signature/old.sql index 1646d18b7..5df07e407 100644 --- a/testdata/diff/create_function/alter_function_same_signature/old.sql +++ b/testdata/diff/create_function/alter_function_same_signature/old.sql @@ -17,4 +17,29 @@ BEGIN SELECT amount INTO total FROM orders WHERE id = order_id; RETURN total - (total * discount_percent / 100); END; -$$; \ No newline at end of file +$$; + +-- Set-returning function with the default ROWS estimate (1000); issue #702 +CREATE FUNCTION expand_order(order_id integer) +RETURNS TABLE (line_id integer) +LANGUAGE sql +AS $$ + SELECT order_id; +$$; + +-- Set-returning function with a custom estimate that goes back to the default; issue #702 +CREATE FUNCTION order_lines(order_id integer) +RETURNS SETOF integer +LANGUAGE sql +ROWS 5 +AS $$ + SELECT order_id; +$$; + +-- Set-returning function whose body and estimate both change; issue #702 +CREATE FUNCTION order_items(order_id integer) +RETURNS SETOF integer +LANGUAGE sql +AS $$ + SELECT order_id; +$$; diff --git a/testdata/diff/create_function/alter_function_same_signature/plan.json b/testdata/diff/create_function/alter_function_same_signature/plan.json index 13dfe2524..bd528beb3 100644 --- a/testdata/diff/create_function/alter_function_same_signature/plan.json +++ b/testdata/diff/create_function/alter_function_same_signature/plan.json @@ -3,11 +3,29 @@ "pgschema_version": "1.14.1", "created_at": "1970-01-01T00:00:00Z", "source_fingerprint": { - "hash": "fc335bb328a0b47f89b922eadc006adbeadd205c59890bb5523a57bb00e854b9" + "hash": "1db813e004230e88ebf2743783a6c8b7bf85c967112961b8525f5b2c131f74b4" }, "groups": [ { "steps": [ + { + "sql": "ALTER FUNCTION expand_order(integer) ROWS 1;", + "type": "function", + "operation": "alter", + "path": "public.expand_order" + }, + { + "sql": "CREATE OR REPLACE FUNCTION order_items(\n order_id integer\n)\nRETURNS SETOF integer\nLANGUAGE sql\nVOLATILE\nROWS 10\nAS $$\n SELECT order_id + 1;\n$$;", + "type": "function", + "operation": "alter", + "path": "public.order_items" + }, + { + "sql": "ALTER FUNCTION order_lines(integer) ROWS 1000;", + "type": "function", + "operation": "alter", + "path": "public.order_lines" + }, { "sql": "CREATE OR REPLACE FUNCTION process_order(\n order_id integer,\n discount_percent numeric DEFAULT 0,\n status order_status DEFAULT 'pending'::order_status,\n priority utils.priority_level DEFAULT 'medium'::utils.priority_level\n)\nRETURNS numeric\nLANGUAGE plpgsql\nSTABLE\nAS $$\nDECLARE\n base_price numeric;\n tax_rate numeric := 0.08;\nBEGIN\n -- Different logic: calculate with tax instead of just discount\n -- Status and priority parameters are available but not used in this simplified version\n SELECT price INTO base_price FROM products WHERE id = order_id;\n RETURN base_price * (1 - discount_percent / 100) * (1 + tax_rate);\nEND;\n$$;", "type": "function", diff --git a/testdata/diff/create_function/alter_function_same_signature/plan.sql b/testdata/diff/create_function/alter_function_same_signature/plan.sql index da4a0d01e..29b3bedb0 100644 --- a/testdata/diff/create_function/alter_function_same_signature/plan.sql +++ b/testdata/diff/create_function/alter_function_same_signature/plan.sql @@ -1,3 +1,18 @@ +ALTER FUNCTION expand_order(integer) ROWS 1; + +CREATE OR REPLACE FUNCTION order_items( + order_id integer +) +RETURNS SETOF integer +LANGUAGE sql +VOLATILE +ROWS 10 +AS $$ + SELECT order_id + 1; +$$; + +ALTER FUNCTION order_lines(integer) ROWS 1000; + CREATE OR REPLACE FUNCTION process_order( order_id integer, discount_percent numeric DEFAULT 0, diff --git a/testdata/diff/create_function/alter_function_same_signature/plan.txt b/testdata/diff/create_function/alter_function_same_signature/plan.txt index 77eb4b5e9..5102a07f9 100644 --- a/testdata/diff/create_function/alter_function_same_signature/plan.txt +++ b/testdata/diff/create_function/alter_function_same_signature/plan.txt @@ -1,14 +1,32 @@ -Plan: 1 to modify. +Plan: 4 to modify. Summary by type: - functions: 1 to modify + functions: 4 to modify Functions: + ~ expand_order + ~ order_items + ~ order_lines ~ process_order DDL to be executed: -------------------------------------------------- +ALTER FUNCTION expand_order(integer) ROWS 1; + +CREATE OR REPLACE FUNCTION order_items( + order_id integer +) +RETURNS SETOF integer +LANGUAGE sql +VOLATILE +ROWS 10 +AS $$ + SELECT order_id + 1; +$$; + +ALTER FUNCTION order_lines(integer) ROWS 1000; + CREATE OR REPLACE FUNCTION process_order( order_id integer, discount_percent numeric DEFAULT 0, diff --git a/testdata/diff/dependency/issue_300_function_table_composite_type/diff.sql b/testdata/diff/dependency/issue_300_function_table_composite_type/diff.sql index 807cef7e4..15233c319 100644 --- a/testdata/diff/dependency/issue_300_function_table_composite_type/diff.sql +++ b/testdata/diff/dependency/issue_300_function_table_composite_type/diff.sql @@ -21,5 +21,6 @@ CREATE OR REPLACE FUNCTION get_actor( RETURNS SETOF actor LANGUAGE sql STABLE +ROWS 1 AS $$ SELECT actor.* FROM actor WHERE actor.id = activity.author_id $$; diff --git a/testdata/diff/dependency/issue_300_function_table_composite_type/plan.json b/testdata/diff/dependency/issue_300_function_table_composite_type/plan.json index 6b0b04c37..d163b1e4b 100644 --- a/testdata/diff/dependency/issue_300_function_table_composite_type/plan.json +++ b/testdata/diff/dependency/issue_300_function_table_composite_type/plan.json @@ -27,7 +27,7 @@ "path": "public.actor" }, { - "sql": "CREATE OR REPLACE FUNCTION get_actor(\n activity activity\n)\nRETURNS SETOF actor\nLANGUAGE sql\nSTABLE\nAS $$ SELECT actor.* FROM actor WHERE actor.id = activity.author_id\n$$;", + "sql": "CREATE OR REPLACE FUNCTION get_actor(\n activity activity\n)\nRETURNS SETOF actor\nLANGUAGE sql\nSTABLE\nROWS 1\nAS $$ SELECT actor.* FROM actor WHERE actor.id = activity.author_id\n$$;", "type": "function", "operation": "create", "path": "public.get_actor" diff --git a/testdata/diff/dependency/issue_300_function_table_composite_type/plan.sql b/testdata/diff/dependency/issue_300_function_table_composite_type/plan.sql index 807cef7e4..15233c319 100644 --- a/testdata/diff/dependency/issue_300_function_table_composite_type/plan.sql +++ b/testdata/diff/dependency/issue_300_function_table_composite_type/plan.sql @@ -21,5 +21,6 @@ CREATE OR REPLACE FUNCTION get_actor( RETURNS SETOF actor LANGUAGE sql STABLE +ROWS 1 AS $$ SELECT actor.* FROM actor WHERE actor.id = activity.author_id $$; diff --git a/testdata/diff/dependency/issue_300_function_table_composite_type/plan.txt b/testdata/diff/dependency/issue_300_function_table_composite_type/plan.txt index 82e426288..13187f57a 100644 --- a/testdata/diff/dependency/issue_300_function_table_composite_type/plan.txt +++ b/testdata/diff/dependency/issue_300_function_table_composite_type/plan.txt @@ -41,5 +41,6 @@ CREATE OR REPLACE FUNCTION get_actor( RETURNS SETOF actor LANGUAGE sql STABLE +ROWS 1 AS $$ SELECT actor.* FROM actor WHERE actor.id = activity.author_id $$;