patternsqlMinor
Span single transaction over several sequential psql sessions
Viewed 0 times
spanseveralsequentialpsqlsingletransactionoversessions
Problem
I am running PostgreSQL 8.4 database server and psql terminal front-end on Ubuntu 10.04 Lucid Lynx and would like to span a single transaction over several sequential psql sessions.
When I connect to my database with psql a new connection is established and a server backend process for this connection is created. When I disconnect the connection is released and the backend process terminates. A (non-XA*) transaction is bound to the scope of a connection, so obviously there is no straight forward way to span a single transaction over several psql sessions.
What I would like to achieve is that the following sequence of commands can be run within a single transaction and therefore return the same transaction timestamp on each call of
database log:
Clearly this is not what I really want to do. I want to be able to execute several bash scripts that connect to the database and execute SQL statements and scripts with psql within a single transaction.
* Afaik the XA protocol would allow BEGIN TRANSACTION and PREPARE TR
When I connect to my database with psql a new connection is established and a server backend process for this connection is created. When I disconnect the connection is released and the backend process terminates. A (non-XA*) transaction is bound to the scope of a connection, so obviously there is no straight forward way to span a single transaction over several psql sessions.
What I would like to achieve is that the following sequence of commands can be run within a single transaction and therefore return the same transaction timestamp on each call of
now():tscho@test:~$ sudo -u postgres psql -p 5433 --no-align --tuples-only -c "select now()"
2012-02-17 21:25:07.027056+01
tscho@test:~$ sudo -u postgres psql -p 5433 --no-align --tuples-only -c "select now()"
2012-02-17 21:25:09.487601+01database log:
2012-02-17 21:25:07 CET 0- LOG: connection received: host=[local]
2012-02-17 21:25:07 CET 0- LOG: connection authorized: user=postgres database=postgres
2012-02-17 21:25:07 CET 0-2/0 LOG: duration: 0.366 ms statement: select now()
2012-02-17 21:25:07 CET 0- LOG: disconnection: session time: 0:00:00.002 user=postgres database=postgres host=[local]
2012-02-17 21:25:09 CET 0- LOG: connection received: host=[local]
2012-02-17 21:25:09 CET 0- LOG: connection authorized: user=postgres database=postgres
2012-02-17 21:25:09 CET 0-2/0 LOG: duration: 0.347 ms statement: select now()
2012-02-17 21:25:09 CET 0- LOG: disconnection: session time: 0:00:00.002 user=postgres database=postgres host=[local]Clearly this is not what I really want to do. I want to be able to execute several bash scripts that connect to the database and execute SQL statements and scripts with psql within a single transaction.
* Afaik the XA protocol would allow BEGIN TRANSACTION and PREPARE TR
Solution
Maybe it would be easiest to run a psql in the background, with it set to execute stdin, and connect its stdin to a named pipe. Then you can continually push data into that pipe, and finally push "end; \quit". Something like:
Note that you can't simply do
#!/bin/sh
psql_pipe=/tmp/psql$
mkfifo -m 600 $psql_pipe
psql $psql_pipe
psql_pid=$!
echo "> Started psql (pid=$psql_pid) reading from $psql_pipe"
trap '
kill $psql_pid
rm -f $psql_pipe
' EXIT
echo "begin;" >&3
echo "select now();" >&3
sleep 2
echo "select now();" >&3
sleep 2
echo "end; \quit" >&3
wait $psql_pidNote that you can't simply do
echo "sql" >$psql_pipe since the EOF would be transmitted to psql, which would then exit early-- the shell script has to keep its fd open.Code Snippets
#!/bin/sh
psql_pipe=/tmp/psql$$
mkfifo -m 600 $psql_pipe
psql < $psql_pipe &
exec 3>$psql_pipe
psql_pid=$!
echo "> Started psql (pid=$psql_pid) reading from $psql_pipe"
trap '
kill $psql_pid
rm -f $psql_pipe
' EXIT
echo "begin;" >&3
echo "select now();" >&3
sleep 2
echo "select now();" >&3
sleep 2
echo "end; \quit" >&3
wait $psql_pidContext
StackExchange Database Administrators Q#13518, answer score: 7
Revisions (0)
No revisions yet.