Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>plpgsql function and COPY</h3> <p>One way would be to put the SQL script into a <a href="http://www.postgresql.org/docs/current/interactive/plpgsql-overview.html#PLPGSQL-ADVANTAGES" rel="nofollow noreferrer">plpgsql function</a>, where you can write the individual return values to files with <a href="http://www.postgresql.org/docs/current/interactive/sql-copy.html" rel="nofollow noreferrer"><code>COPY</code></a> and compile a report from intermediary results just like you need it.</p> <p>This has additional effect that may or may not be desirable. Like, you can grant or revoke permission to the whole function to arbitrary roles. Read about <a href="http://www.postgresql.org/docs/current/interactive/sql-createfunction.html#SQL-CREATEFUNCTION-SECURITY" rel="nofollow noreferrer"><code>SECURITY DEFINER</code></a> in the manual. And the syntax will be verified when you save the function - however, only superficially (there are plans to change that in the future). More details in this <a href="https://dba.stackexchange.com/questions/8119/postgresql-stored-procedure-performance/8189#8189">answer on dba.SE</a>.</p> <p>Basic example:</p> <pre><code>CREATE OR REPLACE FUNCTION func() RETURNS void AS $BODY$ BEGIN COPY (SELECT * FROM tbl WHERE foo) TO '/path/to/my/file/tbl.csv'; COPY (SELECT * FROM tbl2 WHERE NOT bar) TO '/path/to/my/file/tbl2.csv'; END; $BODY$ LANGUAGE plpgsql; </code></pre> <p>Of course, you need to have the necessary privileges in the database and in the file system.<br> Call it from the shell:</p> <pre><code>psql mydb -c 'SELECT func();' </code></pre> <hr> <h3>psql switching between meta commands and SQL</h3> <pre><code>#!/bin/sh BASEDIR='/var/lib/postgresql/outfiles/' echo " \\o $OUTDIR/file1.txt \\\\\\\\ SELECT * FROM tbl1; \\o $OUTDIR/file2.txt \\\\\\\\ SELECT * FROM tbl2; \\o $OUTDIR/file3.txt \\\\\\\\ SELECT * FROM tbl3;" | psql event -p 5432 -t -A </code></pre> <p>That's right, 8 backslashes. Results from a double backslash that gets interpreted two times, so you have to double them two times.</p> <p>I quote <a href="http://www.postgresql.org/docs/current/interactive/app-psql.html" rel="nofollow noreferrer">the manual</a> about the meta-commands <code>\o</code>:</p> <blockquote> <p>Saves future query results to the file filename or ...</p> </blockquote> <p>and <code>\\</code>:</p> <blockquote> <p>command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single backslash command. Thus you cannot mix SQL and psql meta-commands with this option. To achieve that, you could pipe the string into psql, like this: echo '\x \\ SELECT * FROM foo;' | psql. (\\ is the separator meta-command.)</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload