This patch adds support for opening multiple independent connections to different Postgres databases in a single psql client. This allows the implementation of regression tests that test the interaction between multiple connections such as locking and transaction semantics.
The original inspiration for the user interface came from the traditional shell job control where & allows the user to start commands asynchronously and assigns each job a sequential number.
The patch adds a suite of new commands to psql to create new connections, switch between connections, and issue SQL commands asynchronously (the choice of specific names are open for discussion):
\connect& (or just \c&) behaves exactly like \connect or \c except that it keeps the existing connection active, adding the new connection as an additional connection. It prints out the connection number of the new connection which can be used later to refer to it in other commands.
Instructs psql to issue the following SQL command without waiting for the result. Instead the psql prompt will return immediately. The results will be displayed when they are ready.
Note however that results are only checked for in specific points in time: Just before printing the prompt, and only for the current connection. This is so that regression tests behave consistently.
Waits for the specified number of seconds (default: 0) for output from the server from a command issued with \cnowait.
For regression tests it's necessary to use this in a few specific cases for the tests to be effective such as immediately following every \cnowait command which is expected to block to verify that it does in fact block and doesn't continue incorrectly.
Switches to the numbered connection n.
Lists the current connections and their connection numbers
Disconnects from the current connection without quitting psql. This leaves psql in the same state as when the backend disconnnects unexpectedly. No further commands can be issued except \connect to open a new connection or \cswitch to switch to an existing intact connection.
regression=> drop table test; DROP TABLE regression=> create table test (i integer); CREATE TABLE regression=> insert into test values (0); INSERT 0 1 regression=> begin; BEGIN regression=> select * from test for update; i --- 0 (1 row) regression=> savepoint x; SAVEPOINT regression=> update test set i = 1; UPDATE 1 regression=> rollback to x; ROLLBACK regression=> \c&  You are now connected to database "regression". regression=> \cnowait regression=> update test set i = 2; UPDATE 1 regression=> \cwait 1 regression=> \cswitch 1  You are now connected to database "regression" regression=> rollback; ROLLBACK regression=> \cswitch 2  You are now connected to database "regression"