psql --echo-queries does not echo all queries

Started by Gavin Wahl6 months ago3 messagesbugs
Jump to latest
#1Gavin Wahl
gavinwahl@gmail.com

The -e/--echo-queries psql option is documented to "Copy all SQL
commands sent to the server to standard output as well." However, it
does not show queries sent to the server by -1/--single-transaction or
by ON_ERROR_ROLLBACK.

For example:

$ psql -e -1 -c 'SELECT 1'
SELECT 1
?column?
----------
1
(1 row)

While the server log reports:
2025-10-24 12:01:57.937 MDT [1970796] LOG: duration: 0.035 ms statement: BEGIN
2025-10-24 12:01:57.937 MDT [1970796] LOG: duration: 0.085 ms
statement: SELECT 1
2025-10-24 12:01:57.937 MDT [1970796] LOG: duration: 0.004 ms
statement: COMMIT

The BEGIN/END are not reported by psql.

$ psql -e -v ON_ERROR_ROLLBACK=on
psql (16.9)
Type "help" for help.

# BEGIN;
BEGIN;
BEGIN
# SELECT 1;
SELECT 1;
?column?
----------
1
(1 row)

While the server log reports:
2025-10-24 12:12:21.517 MDT [1974726] LOG: duration: 0.077 ms
statement: BEGIN;
2025-10-24 12:12:25.757 MDT [1974726] LOG: duration: 0.048 ms
statement: SAVEPOINT pg_psql_temporary_savepoint
2025-10-24 12:12:25.757 MDT [1974726] LOG: duration: 0.156 ms
statement: SELECT 1;
2025-10-24 12:12:25.757 MDT [1974726] LOG: duration: 0.012 ms
statement: RELEASE pg_psql_temporary_savepoint

The savepoints are not reported by psql.

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Gavin Wahl (#1)
Re: psql --echo-queries does not echo all queries

Gavin Wahl <gavinwahl@gmail.com> writes:

The -e/--echo-queries psql option is documented to "Copy all SQL
commands sent to the server to standard output as well." However, it
does not show queries sent to the server by -1/--single-transaction or
by ON_ERROR_ROLLBACK.

Did you try

-E, --echo-hidden display queries that internal commands generate

?

regards, tom lane

#3Gavin Wahl
gavinwahl@gmail.com
In reply to: Tom Lane (#2)
Re: psql --echo-queries does not echo all queries

I tried with -E and it does echo the BEGIN/END used to implement
-1/--single-transaction, but not the SAVEPOINT queries for
ON_ERROR_ROLLBACK. It looks like SendQuery is calling PQexec directly
instead of calling the PSQLexec wrapper function that does the
logging. I happened to notice that the queries emitted by \watch, nor
the BEGIN for autocommit, aren't logged by either option as well.

I think this may be a documentation bug as well? I would expect -e to
be a superset of -E:

-E
--echo-hidden
Echo the actual queries generated by \d and other backslash commands.

-e
--echo-queries
Copy *ALL* [emphasis mine] SQL commands sent to the server to
standard output as well