Re: Making psql error out on output failures

David Zhang <david.zhang@highgo.ca>

From: David Zhang <david.zhang@highgo.ca>
To: pgsql-hackers@lists.postgresql.org
Cc: Daniel Vérité <daniel@manitou-mail.org>
Date: 2020-01-15T18:35:38Z
Lists: pgsql-hackers
Right, the file difference is caused by "-At". 

On the other side, in order to keep the output message more consistent with other tools, I did a litter bit more investigation on pg_dump to see how it handles this situation. Here is my findings.
pg_dump using WRITE_ERROR_EXIT to throw the error message when "(bytes_written != size * nmemb)", where WRITE_ERROR_EXIT calls fatal("could not write to output file: %m") and then "pg_log_generic(PG_LOG_ERROR, __VA_ARGS__)". After ran a quick test in the same situation, I got message like below,
$ pg_dump -h localhost -p 5432  -d postgres -t psql_error -f /mnt/ramdisk/file
pg_dump: error: could not write to output file: No space left on device

If I change the error log message like below, where "%m" is used to pass the value of strerror(errno), "could not write to output file:" is copied from function "WRITE_ERROR_EXIT". 
-			pg_log_error("Error printing tuples");
+			pg_log_error("could not write to output file: %m");
then the output message is something like below, which, I believe, is more consistent with pg_dump.
$ psql -d postgres  -t -c "select repeat('111', 1000000)" -o /mnt/ramdisk/file
could not write to output file: No space left on device
$ psql -d postgres  -t -c "select repeat('111', 1000000)" > /mnt/ramdisk/file
could not write to output file: No space left on device

Hope the information will help.

David
---
Highgo Software Inc. (Canada)
www.highgo.ca

Commits

  1. psql: Catch and report errors while printing result table

  2. Fix base backup with database OIDs larger than INT32_MAX