Dear PostgreSQL developers,

Plese find attached a patch so that:

    Make "psql -1 < file.sql" work as with "-f"

    Make psql --single-transaction option work on a non-interactive
    standard input as well, so that "psql -1 < input.sql" behaves as
    "psql -1 -f input.sql".

This saner/less error-prone behavior was discussed in this thread back in June:

        http://archives.postgresql.org/pgsql-hackers/2012-06/msg00785.php

I have tested it manually and it works for me. I'm not sure this is the best possible implementation, but it is a small diff one. I haven't found a place in the regression tests where "psql" could be tested with different options. Did I miss something?

--
Fabien.
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index b6bf6a3..dc3c97e 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -512,10 +512,10 @@ PostgreSQL documentation
       <listitem>
        <para>
         When <application>psql</application> executes a script with the
-        <option>-f</> option, adding this option wraps
-        <command>BEGIN</>/<command>COMMIT</> around the script to execute it
-        as a single transaction.  This ensures that either all the commands
-        complete successfully, or no changes are applied.
+        <option>-f</> option or from a non-interactive standard input, adding
+        this option wraps <command>BEGIN</>/<command>COMMIT</> around the script
+        to execute it as a single transaction.  This ensures that either all
+        the commands complete successfully, or no changes are applied.
        </para>
 
        <para>
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 3ebf7cc..e56fd39 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -97,7 +97,8 @@ usage(void)
 	printf(_("  -V, --version            output version information, then exit\n"));
 	printf(_("  -X, --no-psqlrc          do not read startup file (~/.psqlrc)\n"));
 	printf(_("  -1 (\"one\"), --single-transaction\n"
-			 "                           execute command file as a single transaction\n"));
+    "                           execute command file or non-interactive stdin\n"
+    "                           as a single transaction\n"));
 	printf(_("  -?, --help               show this help, then exit\n"));
 
 	printf(_("\nInput and output options:\n"));
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index c907fa0..06d2407 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -76,6 +76,7 @@ typedef struct _psqlSettings
 
 	bool		notty;			/* stdin or stdout is not a tty (as determined
 								 * on startup) */
+	bool        stdin_notty;    /* stdin is not a tty (on startup) */
 	enum trivalue getPassword;	/* prompt the user for a username and password */
 	FILE	   *cur_cmd_source; /* describe the status of the current main
 								 * loop */
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 9a6306b..fb417ce 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -133,7 +133,8 @@ main(int argc, char *argv[])
 	/* We must get COLUMNS here before readline() sets it */
 	pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
 
-	pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
+	pset.stdin_notty = !isatty(fileno(stdin));
+	pset.notty = (pset.stdin_notty || !isatty(fileno(stdout)));
 
 	pset.getPassword = TRI_DEFAULT;
 
@@ -314,7 +315,12 @@ main(int argc, char *argv[])
 		if (!pset.notty)
 			initializeInput(options.no_readline ? 0 : 1);
 
-		successResult = MainLoop(stdin);
+		if (options.single_txn && pset.stdin_notty)
+			/* stdin is NOT a tty and -1, process as a file */
+			successResult = process_file("-", true, false);
+		else
+			/* no single transation, process as if interative */
+			successResult = MainLoop(stdin);
 	}
 
 	/* clean up */
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to