I want to display the results of psql queries on a file.In the log just the name DO for the anonymous Bloc is being displayed.
The following is being executed from a shell script file :
psql --host=${HOST} --port=${PORT_IP} --dbname=${DATABASE_NAME} --username=${USERNAME} -f $scriptSQL -v v1=$fichierLog -v v2=$annee
and here is the content of scriptSQL :
\o :v1
\timing on
DO $$
declare
-- programme de lancement de la purge des redevances
-- date creation : 20/02/09
annee varchar(4); -- annee a purger
begin
annee := ':v2';
-- lancement de la fonction de purge
SELECT pckg_activ.purgeRedevance(annee);
end $$;
\timing off
\o
\q
nothing is being displayed apart from DO.
Is the DO Anonymous Bloc the reason why nothing is displayed on v1 (the output file) ?
Any solutions ?
Thanks
Best Answer
RAISE NOTICE
must be used to display values in a DO block, as inResult:
If you do just a SELECT, here's what happens:
Since it's an error, it doesn't go into the file designated by
\o :v1
The output of
\timing
also won't go into that file.The documentation says about
\o
:To capture all the output, use redirection at the shell:
Another problem is that the
:v2
variable cannot be expanded in the dollar-dollar-quoted string that constitutes the DO block (and it should be written as:'v2'
, not':v2'
, but anyway it doesn't matter here because there's no variable expansion in$$
-quoted strings )Both input parameters and output results are sadly difficult to manage with DO blocks.
For the input side, custom session parameters can be used, as shown below: