December 14th, 2015 by depesz | Tags: , , | No comments »
Did it help? If yes - maybe you can help me?

On 8th of December, Robert Haas committed patch:

psql: Support multiple -c and -f options, and allow mixing them.
 
To support this, we must reconcile some historical anomalies in the
behavior of -c.  In particular, as a backward-incompatibility, -c no
longer implies --no-psqlrc.
 
Pavel Stehule (code) and Catalin Iacob (documentation).  Review by
Michael Paquier and myself.  Proposed behavior per Tom Lane.

This is really, really great. psql -c/-f support was always “tricky". For example – -c assumed -X (–no-psqlrc), and it support for multiple commands was “tricky".

Consider:

=$ psql -c 'select 1; select 2;'
 ?column? 
----------
        2
(1 row)

So it looks like it ran only 2nd command. In reality it ran both, but returned only last resultset.

Now, we can forget about it – since we can have multiple -c options, we can easily:

=$ psql -c 'select 1' -c 'select 2'
 ?column? 
----------
        1
(1 row)
 
 ?column? 
----------
        2
(1 row)

(though putting both selects in single -c, will still yield only result from 2nd select).

Now, with multiple -f, I can, for example:

=$ head *.sql
==> end.sql <==
SELECT 'Ending ...', now();
rollback;
 
==> start.sql <==
BEGIN;
    SELECT 'Started transaction', now();
 
==> work.sql <==
SELECT version(), now();
 
=$ psql -f start.sql -f work.sql -f end.sql 
BEGIN
      ?column?       |              now              
---------------------+-------------------------------
 Started transaction | 2015-12-14 20:09:20.075231+01
(1 row)
 
                                                   version                                                   |              now              
-------------------------------------------------------------------------------------------------------------+-------------------------------
 PostgreSQL 9.6devel on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010, 64-bit | 2015-12-14 20:09:20.075231+01
(1 row)
 
  ?column?  |              now              
------------+-------------------------------
 Ending ... | 2015-12-14 20:09:20.075231+01
(1 row)
 
ROLLBACK

All commands from all 3 files were run, in given order. Of course you can mix and match -c and -f too:

=$ psql -qtA -f start.sql -c "select 'first c'" -f work.sql -c "select 'second c'" -f end.sql 
BEGIN
Started transaction|2015-12-14 20:11:24.628256+01
first c
PostgreSQL 9.6devel on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010, 64-bit|2015-12-14 20:11:24.628256+01
second c
Ending ...|2015-12-14 20:11:24.628256+01
ROLLBACK

I, for one, welcome this change whole-heartedly, as it will spare me tricks with printf ‘….; …' | psql.

Thanks a lot 🙂

Leave a comment