Waiting for 9.6 – psql: Support multiple -c and -f options, and allow mixing them.

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 🙂