Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions internal/diff/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Comment thread
tianzhou marked this conversation as resolved.
Resolved
}

// 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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions ir/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -1343,6 +1353,7 @@ func (i *Inspector) buildFunctions(ctx context.Context, schema *IR, targetSchema
IsSecurityDefiner: isSecurityDefiner,
IsLeakproof: isLeakproof,
Parallel: parallelMode,
Rows: rows,
SetConfig: setConfig,
}

Expand Down
1 change: 1 addition & 0 deletions ir/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions ir/queries/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions ir/queries/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
$$;
$$;

-- 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;
$$;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,29 @@ BEGIN
SELECT amount INTO total FROM orders WHERE id = order_id;
RETURN total - (total * discount_percent / 100);
END;
$$;
$$;

-- 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;
$$;
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
$$;
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
$$;
Original file line number Diff line number Diff line change
Expand Up @@ -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
$$;
Loading