Format specifier for integer variables in format() for EXECUTE?

This would be shorter, faster and safer: CREATE OR REPLACE FUNCTION get_parent_ltree(parent_id bigint, tbl_name regclass , OUT parent_ltree ltree) LANGUAGE plpgsql AS $func$ BEGIN EXECUTE format(‘SELECT l_tree FROM %s WHERE id = $1′, tbl_name) INTO parent_ltree USING parent_id; END $func$; Why? Most importantly, use the USING clause of EXECUTE for parameter values. Don’t convert them … Read more

plpgsql error “RETURN NEXT cannot have a parameter in function with OUT parameters” in table-returning function

RETURN NEXT just returns what output parameters currently hold. The manual: If you declared the function with output parameters, write just RETURN NEXT with no expression. You object: There are no OUT parameters. Output parameters are declared among function parameters with the keyword OUT or INOUT, or implicitly in your RETURNS clause: RETURNS TABLE(column1 integer, … Read more

Function with SQL query has no destination for result data

Do it as plain SQL CREATE OR REPLACE FUNCTION tst_dates_func() RETURNS TABLE( date_value date, date_id int, date_desc varchar) as $BODY$ select a.date_value, a.date_id, a.date_desc from dates_tbl a; $BODY$ LANGUAGE sql; If you really need plpgsql use return query CREATE OR REPLACE FUNCTION tst_dates_func() RETURNS TABLE( date_value date, date_id int, date_desc varchar) as $BODY$ BEGIN perform … Read more