Un esempio rapidamente messo insieme usando PLPGSQL
invece di SQL
.
Avvertenza:deve essere creato come superutente.
Sostituisci la query, ecc.
CREATE OR REPLACE FUNCTION copy_out_example ( p_path TEXT, p_filename_prefix TEXT, OUT file_and_path TEXT )
RETURNS TEXT AS
$func$
DECLARE
qry TEXT;
BEGIN
file_and_path := RTRIM(p_path,'/') || '/' || p_filename_prefix || '_' || ceil(random() * 1000000)::TEXT || '.csv';
qry := FORMAT('COPY (select * from pg_catalog.pg_class) TO %L CSV HEADER',file_and_path);
EXECUTE qry;
END;
$func$ LANGUAGE plpgsql STRICT SECURITY DEFINER;
SELECT copy_out_example('/path/to/the/file','some_test_file');
Risulta in un file come '/path/to/the/file/some_test_file_994216.csv'