2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2012, PostgreSQL Global Development Group
6 * src/bin/psql/common.c
8 #include "postgres_fe.h"
14 #include <unistd.h> /* for write() */
16 #include <io.h> /* for _write() */
20 #include "portability/instr_time.h"
31 static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec);
32 static bool command_no_begin(const char *query);
33 static bool is_select_command(const char *query);
36 * "Safe" wrapper around strdup()
39 pg_strdup(const char *string)
45 fprintf(stderr, _("%s: pg_strdup: cannot duplicate null pointer (internal error)\n"),
52 psql_error("out of memory\n");
59 pg_malloc(size_t size)
66 psql_error("out of memory\n");
73 pg_malloc_zero(size_t size)
77 tmp = pg_malloc(size);
83 pg_calloc(size_t nmemb, size_t size)
87 tmp = calloc(nmemb, size);
90 psql_error("out of memory\n");
98 * -- handler for -o command line option and \o command
100 * Tries to open file fname (or pipe if fname starts with '|')
101 * and stores the file handle in pset)
102 * Upon failure, sets stdout and returns false.
105 setQFout(const char *fname)
109 /* Close old file/pipe */
110 if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
112 if (pset.queryFoutPipe)
113 pclose(pset.queryFout);
115 fclose(pset.queryFout);
118 /* If no filename, set stdout */
119 if (!fname || fname[0] == '\0')
121 pset.queryFout = stdout;
122 pset.queryFoutPipe = false;
124 else if (*fname == '|')
126 pset.queryFout = popen(fname + 1, "w");
127 pset.queryFoutPipe = true;
131 pset.queryFout = fopen(fname, "w");
132 pset.queryFoutPipe = false;
135 if (!(pset.queryFout))
137 psql_error("%s: %s\n", fname, strerror(errno));
138 pset.queryFout = stdout;
139 pset.queryFoutPipe = false;
145 pqsignal(SIGPIPE, pset.queryFoutPipe ? SIG_IGN : SIG_DFL);
154 * Error reporting for scripts. Errors should look like
155 * psql:filename:lineno: message
159 psql_error(const char *fmt,...)
164 if (pset.queryFout != stdout)
165 fflush(pset.queryFout);
168 fprintf(stderr, "%s:%s:" UINT64_FORMAT ": ", pset.progname, pset.inputfile, pset.lineno);
170 vfprintf(stderr, _(fmt), ap);
177 * for backend Notice messages (INFO, WARNING, etc)
180 NoticeProcessor(void *arg, const char *message)
182 (void) arg; /* not used */
183 psql_error("%s", message);
189 * Code to support query cancellation
191 * Before we start a query, we enable the SIGINT signal catcher to send a
192 * cancel request to the backend. Note that sending the cancel directly from
193 * the signal handler is safe because PQcancel() is written to make it
194 * so. We use write() to report to stderr because it's better to use simple
195 * facilities in a signal handler.
197 * On win32, the signal canceling happens on a separate thread, because
198 * that's how SetConsoleCtrlHandler works. The PQcancel function is safe
199 * for this (unlike PQrequestCancel). However, a CRITICAL_SECTION is required
200 * to protect the PGcancel structure against being changed while the signal
201 * thread is using it.
203 * SIGINT is supposed to abort all long-running psql operations, not only
204 * database queries. In most places, this is accomplished by checking
205 * cancel_pressed during long-running loops. However, that won't work when
206 * blocked on user input (in readline() or fgets()). In those places, we
207 * set sigint_interrupt_enabled TRUE while blocked, instructing the signal
208 * catcher to longjmp through sigint_interrupt_jmp. We assume readline and
209 * fgets are coded to handle possible interruption. (XXX currently this does
210 * not work on win32, so control-C is less useful there)
212 volatile bool sigint_interrupt_enabled = false;
214 sigjmp_buf sigint_interrupt_jmp;
216 static PGcancel *volatile cancelConn = NULL;
219 static CRITICAL_SECTION cancelConnLock;
222 #define write_stderr(str) write(fileno(stderr), str, strlen(str))
228 handle_sigint(SIGNAL_ARGS)
230 int save_errno = errno;
234 /* if we are waiting for input, longjmp out of it */
235 if (sigint_interrupt_enabled)
237 sigint_interrupt_enabled = false;
238 siglongjmp(sigint_interrupt_jmp, 1);
241 /* else, set cancel flag to stop any long-running loops */
242 cancel_pressed = true;
244 /* and send QueryCancel if we are processing a database query */
245 if (cancelConn != NULL)
247 if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
249 rc = write_stderr("Cancel request sent\n");
250 (void) rc; /* ignore errors, nothing we can do here */
254 rc = write_stderr("Could not send cancel request: ");
255 (void) rc; /* ignore errors, nothing we can do here */
256 rc = write_stderr(errbuf);
257 (void) rc; /* ignore errors, nothing we can do here */
261 errno = save_errno; /* just in case the write changed it */
265 setup_cancel_handler(void)
267 pqsignal(SIGINT, handle_sigint);
272 consoleHandler(DWORD dwCtrlType)
276 if (dwCtrlType == CTRL_C_EVENT ||
277 dwCtrlType == CTRL_BREAK_EVENT)
280 * Can't longjmp here, because we are in wrong thread :-(
283 /* set cancel flag to stop any long-running loops */
284 cancel_pressed = true;
286 /* and send QueryCancel if we are processing a database query */
287 EnterCriticalSection(&cancelConnLock);
288 if (cancelConn != NULL)
290 if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
291 write_stderr("Cancel request sent\n");
294 write_stderr("Could not send cancel request: ");
295 write_stderr(errbuf);
298 LeaveCriticalSection(&cancelConnLock);
303 /* Return FALSE for any signals not being handled */
308 setup_cancel_handler(void)
310 InitializeCriticalSection(&cancelConnLock);
312 SetConsoleCtrlHandler(consoleHandler, TRUE);
319 * Returns whether our backend connection is still there.
324 return PQstatus(pset.db) != CONNECTION_BAD;
331 * Verify that we still have a good connection to the backend, and if not,
332 * see if it can be restored.
334 * Returns true if either the connection was still there, or it could be
335 * restored successfully; false otherwise. If, however, there was no
336 * connection and the session is non-interactive, this will exit the program
337 * with a code of EXIT_BADCONN.
340 CheckConnection(void)
347 if (!pset.cur_cmd_interactive)
349 psql_error("connection to server was lost\n");
353 fputs(_("The connection to the server was lost. Attempting reset: "), stderr);
358 fputs(_("Failed.\n"), stderr);
365 fputs(_("Succeeded.\n"), stderr);
376 * Set cancelConn to point to the current database connection.
381 PGcancel *oldCancelConn;
384 EnterCriticalSection(&cancelConnLock);
387 /* Free the old one if we have one */
388 oldCancelConn = cancelConn;
389 /* be sure handle_sigint doesn't use pointer while freeing */
392 if (oldCancelConn != NULL)
393 PQfreeCancel(oldCancelConn);
395 cancelConn = PQgetCancel(pset.db);
398 LeaveCriticalSection(&cancelConnLock);
406 * Free the current cancel connection, if any, and set to NULL.
409 ResetCancelConn(void)
411 PGcancel *oldCancelConn;
414 EnterCriticalSection(&cancelConnLock);
417 oldCancelConn = cancelConn;
418 /* be sure handle_sigint doesn't use pointer while freeing */
421 if (oldCancelConn != NULL)
422 PQfreeCancel(oldCancelConn);
425 LeaveCriticalSection(&cancelConnLock);
433 * Checks whether a result is valid, giving an error message if necessary;
434 * and ensures that the connection to the backend is still up.
436 * Returns true for valid result, false for error state.
439 AcceptResult(const PGresult *result)
446 switch (PQresultStatus(result))
448 case PGRES_COMMAND_OK:
449 case PGRES_TUPLES_OK:
450 case PGRES_EMPTY_QUERY:
453 /* Fine, do nothing */
457 case PGRES_BAD_RESPONSE:
458 case PGRES_NONFATAL_ERROR:
459 case PGRES_FATAL_ERROR:
465 psql_error("unexpected PQresultStatus (%d)",
466 PQresultStatus(result));
472 const char *error = PQerrorMessage(pset.db);
475 psql_error("%s", error);
488 * This is the way to send "backdoor" queries (those not directly entered
489 * by the user). It is subject to -E but not -e.
491 * In autocommit-off mode, a new transaction block is started if start_xact
492 * is true; nothing special is done when start_xact is false. Typically,
493 * start_xact = false is used for SELECTs and explicit BEGIN/COMMIT commands.
495 * Caller is responsible for handling the ensuing processing if a COPY
498 * Note: we don't bother to check PQclientEncoding; it is assumed that no
499 * caller uses this path to issue "SET CLIENT_ENCODING".
502 PSQLexec(const char *query, bool start_xact)
508 psql_error("You are currently not connected to a database.\n");
512 if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
514 printf(_("********* QUERY **********\n"
516 "**************************\n\n"), query);
520 fprintf(pset.logfile,
521 _("********* QUERY **********\n"
523 "**************************\n\n"), query);
524 fflush(pset.logfile);
527 if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
535 PQtransactionStatus(pset.db) == PQTRANS_IDLE)
537 res = PQexec(pset.db, "BEGIN");
538 if (PQresultStatus(res) != PGRES_COMMAND_OK)
540 psql_error("%s", PQerrorMessage(pset.db));
548 res = PQexec(pset.db, query);
552 if (!AcceptResult(res))
564 * PrintNotifications: check for asynchronous notifications, and print them out
567 PrintNotifications(void)
571 while ((notify = PQnotifies(pset.db)))
573 /* for backward compatibility, only show payload if nonempty */
574 if (notify->extra[0])
575 fprintf(pset.queryFout, _("Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
576 notify->relname, notify->extra, notify->be_pid);
578 fprintf(pset.queryFout, _("Asynchronous notification \"%s\" received from server process with PID %d.\n"),
579 notify->relname, notify->be_pid);
580 fflush(pset.queryFout);
587 * PrintQueryTuples: assuming query result is OK, print its tuples
589 * Returns true if successful, false otherwise.
592 PrintQueryTuples(const PGresult *results)
594 printQueryOpt my_popt = pset.popt;
596 /* write output to \g argument, if any */
599 /* keep this code in sync with ExecQueryUsingCursor */
600 FILE *queryFout_copy = pset.queryFout;
601 bool queryFoutPipe_copy = pset.queryFoutPipe;
603 pset.queryFout = stdout; /* so it doesn't get closed */
606 if (!setQFout(pset.gfname))
608 pset.queryFout = queryFout_copy;
609 pset.queryFoutPipe = queryFoutPipe_copy;
613 printQuery(results, &my_popt, pset.queryFout, pset.logfile);
615 /* close file/pipe, restore old setting */
618 pset.queryFout = queryFout_copy;
619 pset.queryFoutPipe = queryFoutPipe_copy;
625 printQuery(results, &my_popt, pset.queryFout, pset.logfile);
632 * ProcessResult: utility function for use by SendQuery() only
634 * When our command string contained a COPY FROM STDIN or COPY TO STDOUT,
635 * PQexec() has stopped at the PGresult associated with the first such
636 * command. In that event, we'll marshal data for the COPY and then cycle
637 * through any subsequent PGresult objects.
639 * When the command string contained no affected COPY command, this function
640 * degenerates to an AcceptResult() call.
642 * Changes its argument to point to the last PGresult of the command string,
643 * or NULL if that result was for a COPY FROM STDIN or COPY TO STDOUT.
645 * Returns true on complete success, false otherwise. Possible failure modes
646 * include purely client-side problems; check the transaction status for the
647 * server-side opinion.
650 ProcessResult(PGresult **results)
652 PGresult *next_result;
654 bool first_cycle = true;
658 ExecStatusType result_status;
661 if (!AcceptResult(*results))
664 * Failure at this point is always a server-side failure or a
665 * failure to submit the command string. Either way, we're
666 * finished with this command string.
672 result_status = PQresultStatus(*results);
673 switch (result_status)
675 case PGRES_EMPTY_QUERY:
676 case PGRES_COMMAND_OK:
677 case PGRES_TUPLES_OK:
687 /* AcceptResult() should have caught anything else. */
689 psql_error("unexpected PQresultStatus (%d)", result_status);
696 * Marshal the COPY data. Either subroutine will get the
697 * connection out of its COPY state, then call PQresultStatus()
698 * once and report any error.
701 if (result_status == PGRES_COPY_OUT)
702 success = handleCopyOut(pset.db, pset.queryFout) && success;
704 success = handleCopyIn(pset.db, pset.cur_cmd_source,
705 PQbinaryTuples(*results)) && success;
709 * Call PQgetResult() once more. In the typical case of a
710 * single-command string, it will return NULL. Otherwise, we'll
711 * have other results to process that may include other COPYs.
714 *results = next_result = PQgetResult(pset.db);
716 else if (first_cycle)
717 /* fast path: no COPY commands; PQexec visited all results */
719 else if ((next_result = PQgetResult(pset.db)))
721 /* non-COPY command(s) after a COPY: keep the last one */
723 *results = next_result;
727 } while (next_result);
729 /* may need this to recover from conn loss during COPY */
730 if (!first_cycle && !CheckConnection())
738 * PrintQueryStatus: report command status as required
740 * Note: Utility function for use by PrintQueryResults() only.
743 PrintQueryStatus(PGresult *results)
749 if (pset.popt.topt.format == PRINT_HTML)
751 fputs("<p>", pset.queryFout);
752 html_escaped_print(PQcmdStatus(results), pset.queryFout);
753 fputs("</p>\n", pset.queryFout);
756 fprintf(pset.queryFout, "%s\n", PQcmdStatus(results));
760 fprintf(pset.logfile, "%s\n", PQcmdStatus(results));
762 snprintf(buf, sizeof(buf), "%u", (unsigned int) PQoidValue(results));
763 SetVariable(pset.vars, "LASTOID", buf);
768 * PrintQueryResults: print out query results as required
770 * Note: Utility function for use by SendQuery() only.
772 * Returns true if the query executed successfully, false otherwise.
775 PrintQueryResults(PGresult *results)
778 const char *cmdstatus;
783 switch (PQresultStatus(results))
785 case PGRES_TUPLES_OK:
786 /* print the data ... */
787 success = PrintQueryTuples(results);
788 /* if it's INSERT/UPDATE/DELETE RETURNING, also print status */
789 cmdstatus = PQcmdStatus(results);
790 if (strncmp(cmdstatus, "INSERT", 6) == 0 ||
791 strncmp(cmdstatus, "UPDATE", 6) == 0 ||
792 strncmp(cmdstatus, "DELETE", 6) == 0)
793 PrintQueryStatus(results);
796 case PGRES_COMMAND_OK:
797 PrintQueryStatus(results);
801 case PGRES_EMPTY_QUERY:
807 /* nothing to do here */
811 case PGRES_BAD_RESPONSE:
812 case PGRES_NONFATAL_ERROR:
813 case PGRES_FATAL_ERROR:
819 psql_error("unexpected PQresultStatus (%d)",
820 PQresultStatus(results));
824 fflush(pset.queryFout);
831 * SendQuery: send the query string to the backend
832 * (and print out results)
834 * Note: This is the "front door" way to send a query. That is, use it to
835 * send queries actually entered by the user. These queries will be subject to
837 * To send "back door" queries (generated by slash commands, etc.) in a
838 * controlled way, use PSQLexec().
840 * Returns true if the query executed successfully, false otherwise.
843 SendQuery(const char *query)
846 PGTransactionStatusType transaction_status;
847 double elapsed_msec = 0;
849 on_error_rollback_savepoint = false;
850 static bool on_error_rollback_warning = false;
854 psql_error("You are currently not connected to a database.\n");
862 printf(_("***(Single step mode: verify command)*******************************************\n"
864 "***(press return to proceed or enter x and return to cancel)********************\n"),
867 if (fgets(buf, sizeof(buf), stdin) != NULL)
871 else if (pset.echo == PSQL_ECHO_QUERIES)
879 fprintf(pset.logfile,
880 _("********* QUERY **********\n"
882 "**************************\n\n"), query);
883 fflush(pset.logfile);
888 transaction_status = PQtransactionStatus(pset.db);
890 if (transaction_status == PQTRANS_IDLE &&
892 !command_no_begin(query))
894 results = PQexec(pset.db, "BEGIN");
895 if (PQresultStatus(results) != PGRES_COMMAND_OK)
897 psql_error("%s", PQerrorMessage(pset.db));
903 transaction_status = PQtransactionStatus(pset.db);
906 if (transaction_status == PQTRANS_INTRANS &&
907 pset.on_error_rollback != PSQL_ERROR_ROLLBACK_OFF &&
908 (pset.cur_cmd_interactive ||
909 pset.on_error_rollback == PSQL_ERROR_ROLLBACK_ON))
911 if (on_error_rollback_warning == false && pset.sversion < 80000)
913 fprintf(stderr, _("The server (version %d.%d) does not support savepoints for ON_ERROR_ROLLBACK.\n"),
914 pset.sversion / 10000, (pset.sversion / 100) % 100);
915 on_error_rollback_warning = true;
919 results = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
920 if (PQresultStatus(results) != PGRES_COMMAND_OK)
922 psql_error("%s", PQerrorMessage(pset.db));
928 on_error_rollback_savepoint = true;
932 if (pset.fetch_count <= 0 || !is_select_command(query))
934 /* Default fetch-it-all-and-print mode */
939 INSTR_TIME_SET_CURRENT(before);
941 results = PQexec(pset.db, query);
943 /* these operations are included in the timing result: */
945 OK = ProcessResult(&results);
949 INSTR_TIME_SET_CURRENT(after);
950 INSTR_TIME_SUBTRACT(after, before);
951 elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
954 /* but printing results isn't: */
956 OK = PrintQueryResults(results);
960 /* Fetch-in-segments mode */
961 OK = ExecQueryUsingCursor(query, &elapsed_msec);
963 results = NULL; /* PQclear(NULL) does nothing */
966 /* If we made a temporary savepoint, possibly release/rollback */
967 if (on_error_rollback_savepoint)
969 const char *svptcmd = NULL;
971 transaction_status = PQtransactionStatus(pset.db);
973 switch (transaction_status)
975 case PQTRANS_INERROR:
976 /* We always rollback on an error */
977 svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
981 /* If they are no longer in a transaction, then do nothing */
984 case PQTRANS_INTRANS:
987 * Do nothing if they are messing with savepoints themselves:
988 * If the user did RELEASE or ROLLBACK, our savepoint is gone.
989 * If they issued a SAVEPOINT, releasing ours would remove
993 (strcmp(PQcmdStatus(results), "SAVEPOINT") == 0 ||
994 strcmp(PQcmdStatus(results), "RELEASE") == 0 ||
995 strcmp(PQcmdStatus(results), "ROLLBACK") == 0))
998 svptcmd = "RELEASE pg_psql_temporary_savepoint";
1001 case PQTRANS_ACTIVE:
1002 case PQTRANS_UNKNOWN:
1005 /* PQTRANS_UNKNOWN is expected given a broken connection. */
1006 if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1007 psql_error("unexpected transaction status (%d)\n",
1008 transaction_status);
1016 svptres = PQexec(pset.db, svptcmd);
1017 if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1019 psql_error("%s", PQerrorMessage(pset.db));
1032 /* Possible microtiming output */
1034 printf(_("Time: %.3f ms\n"), elapsed_msec);
1036 /* check for events that may occur during query execution */
1038 if (pset.encoding != PQclientEncoding(pset.db) &&
1039 PQclientEncoding(pset.db) >= 0)
1041 /* track effects of SET CLIENT_ENCODING */
1042 pset.encoding = PQclientEncoding(pset.db);
1043 pset.popt.topt.encoding = pset.encoding;
1044 SetVariable(pset.vars, "ENCODING",
1045 pg_encoding_to_char(pset.encoding));
1048 PrintNotifications();
1055 * ExecQueryUsingCursor: run a SELECT-like query using a cursor
1057 * This feature allows result sets larger than RAM to be dealt with.
1059 * Returns true if the query executed successfully, false otherwise.
1061 * If pset.timing is on, total query time (exclusive of result-printing) is
1062 * stored into *elapsed_msec.
1065 ExecQueryUsingCursor(const char *query, double *elapsed_msec)
1069 PQExpBufferData buf;
1070 printQueryOpt my_popt = pset.popt;
1071 FILE *queryFout_copy = pset.queryFout;
1072 bool queryFoutPipe_copy = pset.queryFoutPipe;
1073 bool started_txn = false;
1074 bool did_pager = false;
1083 /* initialize print options for partial table output */
1084 my_popt.topt.start_table = true;
1085 my_popt.topt.stop_table = false;
1086 my_popt.topt.prior_records = 0;
1089 INSTR_TIME_SET_CURRENT(before);
1091 /* if we're not in a transaction, start one */
1092 if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
1094 results = PQexec(pset.db, "BEGIN");
1095 OK = AcceptResult(results) &&
1096 (PQresultStatus(results) == PGRES_COMMAND_OK);
1103 /* Send DECLARE CURSOR */
1104 initPQExpBuffer(&buf);
1105 appendPQExpBuffer(&buf, "DECLARE _psql_cursor NO SCROLL CURSOR FOR\n%s",
1108 results = PQexec(pset.db, buf.data);
1109 OK = AcceptResult(results) &&
1110 (PQresultStatus(results) == PGRES_COMMAND_OK);
1112 termPQExpBuffer(&buf);
1118 INSTR_TIME_SET_CURRENT(after);
1119 INSTR_TIME_SUBTRACT(after, before);
1120 *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1123 snprintf(fetch_cmd, sizeof(fetch_cmd),
1124 "FETCH FORWARD %d FROM _psql_cursor",
1127 /* prepare to write output to \g argument, if any */
1130 /* keep this code in sync with PrintQueryTuples */
1131 pset.queryFout = stdout; /* so it doesn't get closed */
1133 /* open file/pipe */
1134 if (!setQFout(pset.gfname))
1136 pset.queryFout = queryFout_copy;
1137 pset.queryFoutPipe = queryFoutPipe_copy;
1143 /* clear any pre-existing error indication on the output stream */
1144 clearerr(pset.queryFout);
1149 INSTR_TIME_SET_CURRENT(before);
1151 /* get FETCH_COUNT tuples at a time */
1152 results = PQexec(pset.db, fetch_cmd);
1156 INSTR_TIME_SET_CURRENT(after);
1157 INSTR_TIME_SUBTRACT(after, before);
1158 *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1161 if (PQresultStatus(results) != PGRES_TUPLES_OK)
1163 /* shut down pager before printing error message */
1166 ClosePager(pset.queryFout);
1167 pset.queryFout = queryFout_copy;
1168 pset.queryFoutPipe = queryFoutPipe_copy;
1172 OK = AcceptResult(results);
1178 ntuples = PQntuples(results);
1180 if (ntuples < pset.fetch_count)
1182 /* this is the last result set, so allow footer decoration */
1183 my_popt.topt.stop_table = true;
1185 else if (pset.queryFout == stdout && !did_pager)
1188 * If query requires multiple result sets, hack to ensure that
1189 * only one pager instance is used for the whole mess
1191 pset.queryFout = PageOutput(100000, my_popt.topt.pager);
1195 printQuery(results, &my_popt, pset.queryFout, pset.logfile);
1199 /* after the first result set, disallow header decoration */
1200 my_popt.topt.start_table = false;
1201 my_popt.topt.prior_records += ntuples;
1204 * Make sure to flush the output stream, so intermediate results are
1205 * visible to the client immediately. We check the results because if
1206 * the pager dies/exits/etc, there's no sense throwing more data at
1209 flush_error = fflush(pset.queryFout);
1212 * Check if we are at the end, if a cancel was pressed, or if there
1213 * were any errors either trying to flush out the results, or more
1214 * generally on the output stream at all. If we hit any errors
1215 * writing things to the stream, we presume $PAGER has disappeared and
1216 * stop bothering to pull down more data.
1218 if (ntuples < pset.fetch_count || cancel_pressed || flush_error ||
1219 ferror(pset.queryFout))
1223 /* close \g argument file/pipe, restore old setting */
1226 /* keep this code in sync with PrintQueryTuples */
1229 pset.queryFout = queryFout_copy;
1230 pset.queryFoutPipe = queryFoutPipe_copy;
1237 ClosePager(pset.queryFout);
1238 pset.queryFout = queryFout_copy;
1239 pset.queryFoutPipe = queryFoutPipe_copy;
1244 INSTR_TIME_SET_CURRENT(before);
1247 * We try to close the cursor on either success or failure, but on failure
1248 * ignore the result (it's probably just a bleat about being in an aborted
1251 results = PQexec(pset.db, "CLOSE _psql_cursor");
1254 OK = AcceptResult(results) &&
1255 (PQresultStatus(results) == PGRES_COMMAND_OK);
1261 results = PQexec(pset.db, OK ? "COMMIT" : "ROLLBACK");
1262 OK &= AcceptResult(results) &&
1263 (PQresultStatus(results) == PGRES_COMMAND_OK);
1269 INSTR_TIME_SET_CURRENT(after);
1270 INSTR_TIME_SUBTRACT(after, before);
1271 *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1279 * Advance the given char pointer over white space and SQL comments.
1282 skip_white_space(const char *query)
1284 int cnestlevel = 0; /* slash-star comment nest level */
1288 int mblen = PQmblen(query, pset.encoding);
1291 * Note: we assume the encoding is a superset of ASCII, so that for
1292 * example "query[0] == '/'" is meaningful. However, we do NOT assume
1293 * that the second and subsequent bytes of a multibyte character
1294 * couldn't look like ASCII characters; so it is critical to advance
1295 * by mblen, not 1, whenever we haven't exactly identified the
1296 * character we are skipping over.
1298 if (isspace((unsigned char) *query))
1300 else if (query[0] == '/' && query[1] == '*')
1305 else if (cnestlevel > 0 && query[0] == '*' && query[1] == '/')
1310 else if (cnestlevel == 0 && query[0] == '-' && query[1] == '-')
1315 * We have to skip to end of line since any slash-star inside the
1316 * -- comment does NOT start a slash-star comment.
1325 query += PQmblen(query, pset.encoding);
1328 else if (cnestlevel > 0)
1331 break; /* found first token */
1339 * Check whether a command is one of those for which we should NOT start
1340 * a new transaction block (ie, send a preceding BEGIN).
1342 * These include the transaction control statements themselves, plus
1343 * certain statements that the backend disallows inside transaction blocks.
1346 command_no_begin(const char *query)
1351 * First we must advance over any whitespace and comments.
1353 query = skip_white_space(query);
1356 * Check word length (since "beginx" is not "begin").
1359 while (isalpha((unsigned char) query[wordlen]))
1360 wordlen += PQmblen(&query[wordlen], pset.encoding);
1363 * Transaction control commands. These should include every keyword that
1364 * gives rise to a TransactionStmt in the backend grammar, except for the
1365 * savepoint-related commands.
1367 * (We assume that START must be START TRANSACTION, since there is
1368 * presently no other "START foo" command.)
1370 if (wordlen == 5 && pg_strncasecmp(query, "abort", 5) == 0)
1372 if (wordlen == 5 && pg_strncasecmp(query, "begin", 5) == 0)
1374 if (wordlen == 5 && pg_strncasecmp(query, "start", 5) == 0)
1376 if (wordlen == 6 && pg_strncasecmp(query, "commit", 6) == 0)
1378 if (wordlen == 3 && pg_strncasecmp(query, "end", 3) == 0)
1380 if (wordlen == 8 && pg_strncasecmp(query, "rollback", 8) == 0)
1382 if (wordlen == 7 && pg_strncasecmp(query, "prepare", 7) == 0)
1384 /* PREPARE TRANSACTION is a TC command, PREPARE foo is not */
1387 query = skip_white_space(query);
1390 while (isalpha((unsigned char) query[wordlen]))
1391 wordlen += PQmblen(&query[wordlen], pset.encoding);
1393 if (wordlen == 11 && pg_strncasecmp(query, "transaction", 11) == 0)
1399 * Commands not allowed within transactions. The statements checked for
1400 * here should be exactly those that call PreventTransactionChain() in the
1403 if (wordlen == 6 && pg_strncasecmp(query, "vacuum", 6) == 0)
1405 if (wordlen == 7 && pg_strncasecmp(query, "cluster", 7) == 0)
1407 /* CLUSTER with any arguments is allowed in transactions */
1410 query = skip_white_space(query);
1412 if (isalpha((unsigned char) query[0]))
1413 return false; /* has additional words */
1414 return true; /* it's CLUSTER without arguments */
1417 if (wordlen == 6 && pg_strncasecmp(query, "create", 6) == 0)
1421 query = skip_white_space(query);
1424 while (isalpha((unsigned char) query[wordlen]))
1425 wordlen += PQmblen(&query[wordlen], pset.encoding);
1427 if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
1429 if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
1432 /* CREATE [UNIQUE] INDEX CONCURRENTLY isn't allowed in xacts */
1433 if (wordlen == 6 && pg_strncasecmp(query, "unique", 6) == 0)
1437 query = skip_white_space(query);
1440 while (isalpha((unsigned char) query[wordlen]))
1441 wordlen += PQmblen(&query[wordlen], pset.encoding);
1444 if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
1448 query = skip_white_space(query);
1451 while (isalpha((unsigned char) query[wordlen]))
1452 wordlen += PQmblen(&query[wordlen], pset.encoding);
1454 if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
1462 * Note: these tests will match DROP SYSTEM and REINDEX TABLESPACE, which
1463 * aren't really valid commands so we don't care much. The other four
1464 * possible matches are correct.
1466 if ((wordlen == 4 && pg_strncasecmp(query, "drop", 4) == 0) ||
1467 (wordlen == 7 && pg_strncasecmp(query, "reindex", 7) == 0))
1471 query = skip_white_space(query);
1474 while (isalpha((unsigned char) query[wordlen]))
1475 wordlen += PQmblen(&query[wordlen], pset.encoding);
1477 if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
1479 if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
1481 if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
1486 /* DISCARD ALL isn't allowed in xacts, but other variants are allowed. */
1487 if (wordlen == 7 && pg_strncasecmp(query, "discard", 7) == 0)
1491 query = skip_white_space(query);
1494 while (isalpha((unsigned char) query[wordlen]))
1495 wordlen += PQmblen(&query[wordlen], pset.encoding);
1497 if (wordlen == 3 && pg_strncasecmp(query, "all", 3) == 0)
1507 * Check whether the specified command is a SELECT (or VALUES).
1510 is_select_command(const char *query)
1515 * First advance over any whitespace, comments and left parentheses.
1519 query = skip_white_space(query);
1520 if (query[0] == '(')
1527 * Check word length (since "selectx" is not "select").
1530 while (isalpha((unsigned char) query[wordlen]))
1531 wordlen += PQmblen(&query[wordlen], pset.encoding);
1533 if (wordlen == 6 && pg_strncasecmp(query, "select", 6) == 0)
1536 if (wordlen == 6 && pg_strncasecmp(query, "values", 6) == 0)
1544 * Test if the current user is a database superuser.
1546 * Note: this will correctly detect superuserness only with a protocol-3.0
1547 * or newer backend; otherwise it will always say "false".
1557 val = PQparameterStatus(pset.db, "is_superuser");
1559 if (val && strcmp(val, "on") == 0)
1567 * Test if the current session uses standard string literals.
1569 * Note: With a pre-protocol-3.0 connection this will always say "false",
1570 * which should be the right answer.
1573 standard_strings(void)
1580 val = PQparameterStatus(pset.db, "standard_conforming_strings");
1582 if (val && strcmp(val, "on") == 0)
1590 * Return the session user of the current connection.
1592 * Note: this will correctly detect the session user only with a
1593 * protocol-3.0 or newer backend; otherwise it will return the
1597 session_username(void)
1604 val = PQparameterStatus(pset.db, "session_authorization");
1608 return PQuser(pset.db);
1614 * substitute '~' with HOME or '~username' with username's home dir
1618 expand_tilde(char **filename)
1620 if (!filename || !(*filename))
1624 * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
1625 * for short versions of long file names, though the tilde is usually
1626 * toward the end, not at the beginning.
1630 /* try tilde expansion */
1631 if (**filename == '~')
1637 char home[MAXPGPATH];
1643 while (*p != '/' && *p != '\0')
1649 if (*(fn + 1) == '\0')
1650 get_home_path(home); /* ~ or ~/ only */
1651 else if ((pw = getpwnam(fn + 1)) != NULL)
1652 strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
1655 if (strlen(home) != 0)
1659 newfn = pg_malloc(strlen(home) + strlen(p) + 1);
1660 strcpy(newfn, home);