I have below plpgsql function –
CREATE OR REPLACE FUNCTION public.end_to_end_query(part_id integer)
LANGUAGE plpgsql IMMUTABLE
AS $function$
DECLARE
query_pw varchar := format(
$dynsql$
ALTER TABLE metric_events_%s SET (parallel_workers = 16)
$dynsql$, part_id
);
BEGIN
RAISE NOTICE 'Value: %', query;
QUERY EXECUTE 'set enable_parallel_append = off';
QUERY EXECUTE "SET work_mem TO '1GB'";
QUERY EXECUTE query_pw;
QUERY EXECUTE "SET max_parallel_workers_per_gather = 16";
QUERY EXECUTE "set max_worker_processes=8";
END $function$;
When I run the above function, I get the error –
ERROR: syntax error at or near "QUERY"
LINE 13: QUERY EXECUTE ‘set enable_parallel_append = off’;
^SQL state: 42601
Character: 1419
How can I fix this error?
2
Answers
query execute
is not a valid SQL command, you just need to callexecute
:The PL/pgSQL command to execute dynamic query strings is
EXECUTE
, notQUERY EXECUTE
. (Also available in the combined formRETURN QUERY EXECUTE
to actually return the results of a dynamic query.)But this is only one of multiple grave errors in the function. This could work:
You cannot create a function without declaring the return type, typically with a
RETURNS
clause. You may want aPROCEDURE
instead. See:Double-quotes are not for string literals. See:
You do not need
EXECUTE
for plain, staticSET
commands.You cannot set
max_worker_processes
in a function like this. It requires a server restart.It is nonsense to set
max_worker_processes
lower thanmax_parallel_workers_per_gather
.The function is
VOLATILE
, notIMMUTABLE
.You must defend against
NULL
input, or the dynamic query string either raises an exception or does something unexpected.Mixing those general
SET
commands with setting a table option seems odd. Are you sure, you want to package that in a single function?