On Mon, Dec 06, 2021 at 09:08:56AM -0600, Justin Pryzby wrote:
> I raised this issue a few years ago.
> https://www.postgresql.org/message-id/20181217175841.GS13019%40telsasoft.com
> 
> |[pryzbyj@database ~]$ psql -v VERBOSITY=terse ts -xtc 'ONE' -c "SELECT 
> 'TWO'"; echo "exit status $?"
> |ERROR:  syntax error at or near "ONE" at character 1
> |?column? | TWO
> |
> |exit status 0
> 
> The documentation doen't say what the exit status should be in this case:
> | psql returns 0 to the shell if it finished normally, 1 if a fatal error of 
> its own occurs (e.g., out of memory, file not found), 2 if the connection to 
> the server went bad and the session was not interactive, and 3 if an error 
> occurred in a script and the variable ON_ERROR_STOP was set.
> 
> It returns 1 if the final command fails, even though it's not a "fatal error"
> (it would've happily kept running more commands).
> 
> | x=`some_command_that_fails`
> | rm -fr "$x/$y # removes all your data
> 
> | psql -c "begin; C REATE TABLE newtable(LIKE oldtable) INSERT INTO newtable 
> SELECT * FROM oldtable; commit" -c "DROP TABLE oldtable
> | psql -v VERBOSITY=terse ts -xtc '0CREATE TABLE newtbl(i int)' -c 'INSERT 
> INTO newtbl SELECT * FROM tbl' -c 'DROP TABLE IF EXISTS tbl' -c 'ALTER TABLE 
> newtbl RENAME TO tbl'; echo ret=$?
> 
> David J suggested to change the default value of ON_ERROR_STOP.  The exit
> status in the non-default case would have to be documented.  That's one
> solution, and allows the old behavior if anybody wants it.  That probably does
> what most people want, too.  This is more likely to expose a real problem that
> someone would have missed than to break a legitimate use.  That doesn't appear
> to break regression tests at all.

I was wrong - the regression tests specifically exercise failure cases, so the
scripts must continue after errors.

I think the current behavior of the regression test SQL scripts is exactly the
opposite of what's desirable for almost all other scripts.  The attached makes
ON_ERROR_STOP the default, and runs the regression tests with ON_ERROR_STOP=0.

Is it viable to consider changing this ?
>From 5a54d265bff7565bd311f5e0d4115efb615f172f Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryz...@telsasoft.com>
Date: Wed, 8 Dec 2021 22:30:21 -0600
Subject: [PATCH] psql: default to ON_ERROR_STOP

---
 src/bin/psql/startup.c             | 1 +
 src/test/regress/pg_regress_main.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index f7ea4ce3d46..47cc37e6bc1 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -200,6 +200,7 @@ main(int argc, char *argv[])
 
 	/* Default values for variables (that don't match the result of \unset) */
 	SetVariableBool(pset.vars, "AUTOCOMMIT");
+	SetVariableBool(pset.vars, "ON_ERROR_STOP");
 	SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
 	SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
 	SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
diff --git a/src/test/regress/pg_regress_main.c b/src/test/regress/pg_regress_main.c
index 1524676f3b3..8b1f456a674 100644
--- a/src/test/regress/pg_regress_main.c
+++ b/src/test/regress/pg_regress_main.c
@@ -82,7 +82,7 @@ psql_start_test(const char *testname,
 					   bindir ? bindir : "",
 					   bindir ? "/" : "",
 					   dblist->str,
-					   "-v HIDE_TABLEAM=on -v HIDE_TOAST_COMPRESSION=on",
+					   "-v HIDE_TABLEAM=on -v HIDE_TOAST_COMPRESSION=on -v ON_ERROR_STOP=no",
 					   infile,
 					   outfile);
 	if (offset >= sizeof(psql_cmd))
-- 
2.17.0

Reply via email to