2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2016, PostgreSQL Global Development Group
6 * src/bin/psql/common.c
8 #include "postgres_fe.h"
16 #include <unistd.h> /* for write() */
18 #include <io.h> /* for _write() */
22 #include "fe_utils/string_utils.h"
23 #include "portability/instr_time.h"
28 #include "crosstabview.h"
29 #include "fe_utils/mbprint.h"
32 static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec);
33 static bool command_no_begin(const char *query);
34 static bool is_select_command(const char *query);
38 * openQueryOutputFile --- attempt to open a query output file
40 * fname == NULL selects stdout, else an initial '|' selects a pipe,
43 * Returns output file pointer into *fout, and is-a-pipe flag into *is_pipe.
44 * Caller is responsible for adjusting SIGPIPE state if it's a pipe.
46 * On error, reports suitable error message and returns FALSE.
49 openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
51 if (!fname || fname[0] == '\0')
56 else if (*fname == '|')
58 *fout = popen(fname + 1, "w");
63 *fout = fopen(fname, "w");
69 psql_error("%s: %s\n", fname, strerror(errno));
78 * -- handler for -o command line option and \o command
80 * On success, updates pset with the new output file and returns true.
81 * On failure, returns false without changing pset state.
84 setQFout(const char *fname)
89 /* First make sure we can open the new output file/pipe */
90 if (!openQueryOutputFile(fname, &fout, &is_pipe))
93 /* Close old file/pipe */
94 if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
96 if (pset.queryFoutPipe)
97 pclose(pset.queryFout);
99 fclose(pset.queryFout);
102 pset.queryFout = fout;
103 pset.queryFoutPipe = is_pipe;
105 /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
106 set_sigpipe_trap_state(is_pipe);
107 restore_sigpipe_trap();
114 * Variable-fetching callback for flex lexer
116 * If the specified variable exists, return its value as a string (malloc'd
117 * and expected to be freed by the caller); else return NULL.
119 * If "escape" is true, return the value suitably quoted and escaped,
120 * as an identifier or string literal depending on "as_ident".
121 * (Failure in escaping should lead to returning NULL.)
124 psql_get_variable(const char *varname, bool escape, bool as_ident)
129 value = GetVariable(pset.vars, varname);
139 psql_error("cannot escape without active connection\n");
145 PQescapeIdentifier(pset.db, value, strlen(value));
148 PQescapeLiteral(pset.db, value, strlen(value));
150 if (escaped_value == NULL)
152 const char *error = PQerrorMessage(pset.db);
154 psql_error("%s", error);
159 * Rather than complicate the lexer's API with a notion of which
160 * free() routine to use, just pay the price of an extra strdup().
162 result = pg_strdup(escaped_value);
163 PQfreemem(escaped_value);
166 result = pg_strdup(value);
173 * Error reporting for scripts. Errors should look like
174 * psql:filename:lineno: message
177 psql_error(const char *fmt,...)
182 if (pset.queryFout && pset.queryFout != stdout)
183 fflush(pset.queryFout);
186 fprintf(stderr, "%s:%s:" UINT64_FORMAT ": ", pset.progname, pset.inputfile, pset.lineno);
188 vfprintf(stderr, _(fmt), ap);
195 * for backend Notice messages (INFO, WARNING, etc)
198 NoticeProcessor(void *arg, const char *message)
200 (void) arg; /* not used */
201 psql_error("%s", message);
207 * Code to support query cancellation
209 * Before we start a query, we enable the SIGINT signal catcher to send a
210 * cancel request to the backend. Note that sending the cancel directly from
211 * the signal handler is safe because PQcancel() is written to make it
212 * so. We use write() to report to stderr because it's better to use simple
213 * facilities in a signal handler.
215 * On win32, the signal canceling happens on a separate thread, because
216 * that's how SetConsoleCtrlHandler works. The PQcancel function is safe
217 * for this (unlike PQrequestCancel). However, a CRITICAL_SECTION is required
218 * to protect the PGcancel structure against being changed while the signal
219 * thread is using it.
221 * SIGINT is supposed to abort all long-running psql operations, not only
222 * database queries. In most places, this is accomplished by checking
223 * cancel_pressed during long-running loops. However, that won't work when
224 * blocked on user input (in readline() or fgets()). In those places, we
225 * set sigint_interrupt_enabled TRUE while blocked, instructing the signal
226 * catcher to longjmp through sigint_interrupt_jmp. We assume readline and
227 * fgets are coded to handle possible interruption. (XXX currently this does
228 * not work on win32, so control-C is less useful there)
230 volatile bool sigint_interrupt_enabled = false;
232 sigjmp_buf sigint_interrupt_jmp;
234 static PGcancel *volatile cancelConn = NULL;
237 static CRITICAL_SECTION cancelConnLock;
241 * Write a simple string to stderr --- must be safe in a signal handler.
242 * We ignore the write() result since there's not much we could do about it.
243 * Certain compilers make that harder than it ought to be.
245 #define write_stderr(str) \
247 const char *str_ = (str); \
249 rc_ = write(fileno(stderr), str_, strlen(str_)); \
257 handle_sigint(SIGNAL_ARGS)
259 int save_errno = errno;
262 /* if we are waiting for input, longjmp out of it */
263 if (sigint_interrupt_enabled)
265 sigint_interrupt_enabled = false;
266 siglongjmp(sigint_interrupt_jmp, 1);
269 /* else, set cancel flag to stop any long-running loops */
270 cancel_pressed = true;
272 /* and send QueryCancel if we are processing a database query */
273 if (cancelConn != NULL)
275 if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
276 write_stderr("Cancel request sent\n");
279 write_stderr("Could not send cancel request: ");
280 write_stderr(errbuf);
284 errno = save_errno; /* just in case the write changed it */
288 setup_cancel_handler(void)
290 pqsignal(SIGINT, handle_sigint);
295 consoleHandler(DWORD dwCtrlType)
299 if (dwCtrlType == CTRL_C_EVENT ||
300 dwCtrlType == CTRL_BREAK_EVENT)
303 * Can't longjmp here, because we are in wrong thread :-(
306 /* set cancel flag to stop any long-running loops */
307 cancel_pressed = true;
309 /* and send QueryCancel if we are processing a database query */
310 EnterCriticalSection(&cancelConnLock);
311 if (cancelConn != NULL)
313 if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
314 write_stderr("Cancel request sent\n");
317 write_stderr("Could not send cancel request: ");
318 write_stderr(errbuf);
321 LeaveCriticalSection(&cancelConnLock);
326 /* Return FALSE for any signals not being handled */
331 setup_cancel_handler(void)
333 InitializeCriticalSection(&cancelConnLock);
335 SetConsoleCtrlHandler(consoleHandler, TRUE);
342 * Returns whether our backend connection is still there.
347 return PQstatus(pset.db) != CONNECTION_BAD;
354 * Verify that we still have a good connection to the backend, and if not,
355 * see if it can be restored.
357 * Returns true if either the connection was still there, or it could be
358 * restored successfully; false otherwise. If, however, there was no
359 * connection and the session is non-interactive, this will exit the program
360 * with a code of EXIT_BADCONN.
363 CheckConnection(void)
370 if (!pset.cur_cmd_interactive)
372 psql_error("connection to server was lost\n");
376 psql_error("The connection to the server was lost. Attempting reset: ");
381 psql_error("Failed.\n");
388 psql_error("Succeeded.\n");
399 * Set cancelConn to point to the current database connection.
404 PGcancel *oldCancelConn;
407 EnterCriticalSection(&cancelConnLock);
410 /* Free the old one if we have one */
411 oldCancelConn = cancelConn;
412 /* be sure handle_sigint doesn't use pointer while freeing */
415 if (oldCancelConn != NULL)
416 PQfreeCancel(oldCancelConn);
418 cancelConn = PQgetCancel(pset.db);
421 LeaveCriticalSection(&cancelConnLock);
429 * Free the current cancel connection, if any, and set to NULL.
432 ResetCancelConn(void)
434 PGcancel *oldCancelConn;
437 EnterCriticalSection(&cancelConnLock);
440 oldCancelConn = cancelConn;
441 /* be sure handle_sigint doesn't use pointer while freeing */
444 if (oldCancelConn != NULL)
445 PQfreeCancel(oldCancelConn);
448 LeaveCriticalSection(&cancelConnLock);
456 * Checks whether a result is valid, giving an error message if necessary;
457 * and ensures that the connection to the backend is still up.
459 * Returns true for valid result, false for error state.
462 AcceptResult(const PGresult *result)
469 switch (PQresultStatus(result))
471 case PGRES_COMMAND_OK:
472 case PGRES_TUPLES_OK:
473 case PGRES_EMPTY_QUERY:
476 /* Fine, do nothing */
480 case PGRES_BAD_RESPONSE:
481 case PGRES_NONFATAL_ERROR:
482 case PGRES_FATAL_ERROR:
488 psql_error("unexpected PQresultStatus: %d\n",
489 PQresultStatus(result));
495 const char *error = PQerrorMessage(pset.db);
498 psql_error("%s", error);
510 * If the result represents an error, remember it for possible display by
511 * \errverbose. Otherwise, just PQclear() it.
514 ClearOrSaveResult(PGresult *result)
518 switch (PQresultStatus(result))
520 case PGRES_NONFATAL_ERROR:
521 case PGRES_FATAL_ERROR:
522 if (pset.last_error_result)
523 PQclear(pset.last_error_result);
524 pset.last_error_result = result;
536 * Print microtiming output. Always print raw milliseconds; if the interval
537 * is >= 1 second, also break it down into days/hours/minutes/seconds.
540 PrintTiming(double elapsed_msec)
547 if (elapsed_msec < 1000.0)
549 /* This is the traditional (pre-v10) output format */
550 printf(_("Time: %.3f ms\n"), elapsed_msec);
555 * Note: we could print just seconds, in a format like %06.3f, when the
556 * total is less than 1min. But that's hard to interpret unless we tack
557 * on "s" or otherwise annotate it. Forcing the display to include
558 * minutes seems like a better solution.
560 seconds = elapsed_msec / 1000.0;
561 minutes = floor(seconds / 60.0);
562 seconds -= 60.0 * minutes;
565 printf(_("Time: %.3f ms (%02d:%06.3f)\n"),
566 elapsed_msec, (int) minutes, seconds);
570 hours = floor(minutes / 60.0);
571 minutes -= 60.0 * hours;
574 printf(_("Time: %.3f ms (%02d:%02d:%06.3f)\n"),
575 elapsed_msec, (int) hours, (int) minutes, seconds);
579 days = floor(hours / 24.0);
580 hours -= 24.0 * days;
581 printf(_("Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"),
582 elapsed_msec, days, (int) hours, (int) minutes, seconds);
589 * This is the way to send "backdoor" queries (those not directly entered
590 * by the user). It is subject to -E but not -e.
592 * Caller is responsible for handling the ensuing processing if a COPY
595 * Note: we don't bother to check PQclientEncoding; it is assumed that no
596 * caller uses this path to issue "SET CLIENT_ENCODING".
599 PSQLexec(const char *query)
605 psql_error("You are currently not connected to a database.\n");
609 if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
611 printf(_("********* QUERY **********\n"
613 "**************************\n\n"), query);
617 fprintf(pset.logfile,
618 _("********* QUERY **********\n"
620 "**************************\n\n"), query);
621 fflush(pset.logfile);
624 if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
630 res = PQexec(pset.db, query);
634 if (!AcceptResult(res))
636 ClearOrSaveResult(res);
647 * This function is used for \watch command to send the query to
648 * the server and print out the results.
650 * Returns 1 if the query executed successfully, 0 if it cannot be repeated,
651 * e.g., because of the interrupt, -1 on error.
654 PSQLexecWatch(const char *query, const printQueryOpt *opt)
657 double elapsed_msec = 0;
663 psql_error("You are currently not connected to a database.\n");
670 INSTR_TIME_SET_CURRENT(before);
672 res = PQexec(pset.db, query);
676 if (!AcceptResult(res))
678 ClearOrSaveResult(res);
684 INSTR_TIME_SET_CURRENT(after);
685 INSTR_TIME_SUBTRACT(after, before);
686 elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
690 * If SIGINT is sent while the query is processing, the interrupt will be
691 * consumed. The user's intention, though, is to cancel the entire watch
692 * process, so detect a sent cancellation request and exit in this case.
700 switch (PQresultStatus(res))
702 case PGRES_TUPLES_OK:
703 printQuery(res, opt, pset.queryFout, false, pset.logfile);
706 case PGRES_COMMAND_OK:
707 fprintf(pset.queryFout, "%s\n%s\n\n", opt->title, PQcmdStatus(res));
710 case PGRES_EMPTY_QUERY:
711 psql_error(_("\\watch cannot be used with an empty query\n"));
717 case PGRES_COPY_BOTH:
718 psql_error(_("\\watch cannot be used with COPY\n"));
723 psql_error(_("unexpected result status for \\watch\n"));
730 fflush(pset.queryFout);
732 /* Possible microtiming output */
734 PrintTiming(elapsed_msec);
741 * PrintNotifications: check for asynchronous notifications, and print them out
744 PrintNotifications(void)
748 while ((notify = PQnotifies(pset.db)))
750 /* for backward compatibility, only show payload if nonempty */
751 if (notify->extra[0])
752 fprintf(pset.queryFout, _("Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
753 notify->relname, notify->extra, notify->be_pid);
755 fprintf(pset.queryFout, _("Asynchronous notification \"%s\" received from server process with PID %d.\n"),
756 notify->relname, notify->be_pid);
757 fflush(pset.queryFout);
764 * PrintQueryTuples: assuming query result is OK, print its tuples
766 * Returns true if successful, false otherwise.
769 PrintQueryTuples(const PGresult *results)
771 printQueryOpt my_popt = pset.popt;
773 /* write output to \g argument, if any */
779 if (!openQueryOutputFile(pset.gfname, &fout, &is_pipe))
782 disable_sigpipe_trap();
784 printQuery(results, &my_popt, fout, false, pset.logfile);
789 restore_sigpipe_trap();
795 printQuery(results, &my_popt, pset.queryFout, false, pset.logfile);
802 * StoreQueryTuple: assuming query result is OK, save data into variables
804 * Returns true if successful, false otherwise.
807 StoreQueryTuple(const PGresult *result)
811 if (PQntuples(result) < 1)
813 psql_error("no rows returned for \\gset\n");
816 else if (PQntuples(result) > 1)
818 psql_error("more than one row returned for \\gset\n");
825 for (i = 0; i < PQnfields(result); i++)
827 char *colname = PQfname(result, i);
831 /* concate prefix and column name */
832 varname = psprintf("%s%s", pset.gset_prefix, colname);
834 if (!PQgetisnull(result, 0, i))
835 value = PQgetvalue(result, 0, i);
838 /* for NULL value, unset rather than set the variable */
842 if (!SetVariable(pset.vars, varname, value))
844 psql_error("could not set variable \"%s\"\n", varname);
859 * ExecQueryTuples: assuming query result is OK, execute each query
860 * result field as a SQL statement
862 * Returns true if successful, false otherwise.
865 ExecQueryTuples(const PGresult *result)
868 int nrows = PQntuples(result);
869 int ncolumns = PQnfields(result);
874 * We must turn off gexec_flag to avoid infinite recursion. Note that
875 * this allows ExecQueryUsingCursor to be applied to the individual query
876 * results. SendQuery prevents it from being applied when fetching the
877 * queries-to-execute, because it can't handle recursion either.
879 pset.gexec_flag = false;
881 for (r = 0; r < nrows; r++)
883 for (c = 0; c < ncolumns; c++)
885 if (!PQgetisnull(result, r, c))
887 const char *query = PQgetvalue(result, r, c);
889 /* Abandon execution if cancel_pressed */
894 * ECHO_ALL mode should echo these queries, but SendQuery
895 * assumes that MainLoop did that, so we have to do it here.
897 if (pset.echo == PSQL_ECHO_ALL && !pset.singlestep)
903 if (!SendQuery(query))
905 /* Error - abandon execution if ON_ERROR_STOP */
907 if (pset.on_error_stop)
917 * Restore state. We know gexec_flag was on, else we'd not be here. (We
918 * also know it'll get turned off at end of command, but that's not ours
921 pset.gexec_flag = true;
923 /* Return true if all queries were successful */
929 * ProcessResult: utility function for use by SendQuery() only
931 * When our command string contained a COPY FROM STDIN or COPY TO STDOUT,
932 * PQexec() has stopped at the PGresult associated with the first such
933 * command. In that event, we'll marshal data for the COPY and then cycle
934 * through any subsequent PGresult objects.
936 * When the command string contained no such COPY command, this function
937 * degenerates to an AcceptResult() call.
939 * Changes its argument to point to the last PGresult of the command string,
940 * or NULL if that result was for a COPY TO STDOUT. (Returning NULL prevents
941 * the command status from being printed, which we want in that case so that
942 * the status line doesn't get taken as part of the COPY data.)
944 * Returns true on complete success, false otherwise. Possible failure modes
945 * include purely client-side problems; check the transaction status for the
946 * server-side opinion.
949 ProcessResult(PGresult **results)
952 bool first_cycle = true;
956 ExecStatusType result_status;
958 PGresult *next_result;
960 if (!AcceptResult(*results))
963 * Failure at this point is always a server-side failure or a
964 * failure to submit the command string. Either way, we're
965 * finished with this command string.
971 result_status = PQresultStatus(*results);
972 switch (result_status)
974 case PGRES_EMPTY_QUERY:
975 case PGRES_COMMAND_OK:
976 case PGRES_TUPLES_OK:
986 /* AcceptResult() should have caught anything else. */
988 psql_error("unexpected PQresultStatus: %d\n", result_status);
995 * Marshal the COPY data. Either subroutine will get the
996 * connection out of its COPY state, then call PQresultStatus()
997 * once and report any error.
999 * If pset.copyStream is set, use that as data source/sink,
1000 * otherwise use queryFout or cur_cmd_source as appropriate.
1002 FILE *copystream = pset.copyStream;
1003 PGresult *copy_result;
1006 if (result_status == PGRES_COPY_OUT)
1009 copystream = pset.queryFout;
1010 success = handleCopyOut(pset.db,
1012 ©_result) && success;
1015 * Suppress status printing if the report would go to the same
1016 * place as the COPY data just went. Note this doesn't
1017 * prevent error reporting, since handleCopyOut did that.
1019 if (copystream == pset.queryFout)
1021 PQclear(copy_result);
1028 copystream = pset.cur_cmd_source;
1029 success = handleCopyIn(pset.db,
1031 PQbinaryTuples(*results),
1032 ©_result) && success;
1037 * Replace the PGRES_COPY_OUT/IN result with COPY command's exit
1038 * status, or with NULL if we want to suppress printing anything.
1041 *results = copy_result;
1043 else if (first_cycle)
1045 /* fast path: no COPY commands; PQexec visited all results */
1050 * Check PQgetResult() again. In the typical case of a single-command
1051 * string, it will return NULL. Otherwise, we'll have other results
1052 * to process that may include other COPYs. We keep the last result.
1054 next_result = PQgetResult(pset.db);
1059 *results = next_result;
1060 first_cycle = false;
1063 /* may need this to recover from conn loss during COPY */
1064 if (!first_cycle && !CheckConnection())
1072 * PrintQueryStatus: report command status as required
1074 * Note: Utility function for use by PrintQueryResults() only.
1077 PrintQueryStatus(PGresult *results)
1083 if (pset.popt.topt.format == PRINT_HTML)
1085 fputs("<p>", pset.queryFout);
1086 html_escaped_print(PQcmdStatus(results), pset.queryFout);
1087 fputs("</p>\n", pset.queryFout);
1090 fprintf(pset.queryFout, "%s\n", PQcmdStatus(results));
1094 fprintf(pset.logfile, "%s\n", PQcmdStatus(results));
1096 snprintf(buf, sizeof(buf), "%u", (unsigned int) PQoidValue(results));
1097 SetVariable(pset.vars, "LASTOID", buf);
1102 * PrintQueryResults: print out (or store or execute) query results as required
1104 * Note: Utility function for use by SendQuery() only.
1106 * Returns true if the query executed successfully, false otherwise.
1109 PrintQueryResults(PGresult *results)
1112 const char *cmdstatus;
1117 switch (PQresultStatus(results))
1119 case PGRES_TUPLES_OK:
1120 /* store or execute or print the data ... */
1121 if (pset.gset_prefix)
1122 success = StoreQueryTuple(results);
1123 else if (pset.gexec_flag)
1124 success = ExecQueryTuples(results);
1125 else if (pset.crosstab_flag)
1126 success = PrintResultsInCrosstab(results);
1128 success = PrintQueryTuples(results);
1129 /* if it's INSERT/UPDATE/DELETE RETURNING, also print status */
1130 cmdstatus = PQcmdStatus(results);
1131 if (strncmp(cmdstatus, "INSERT", 6) == 0 ||
1132 strncmp(cmdstatus, "UPDATE", 6) == 0 ||
1133 strncmp(cmdstatus, "DELETE", 6) == 0)
1134 PrintQueryStatus(results);
1137 case PGRES_COMMAND_OK:
1138 PrintQueryStatus(results);
1142 case PGRES_EMPTY_QUERY:
1146 case PGRES_COPY_OUT:
1148 /* nothing to do here */
1152 case PGRES_BAD_RESPONSE:
1153 case PGRES_NONFATAL_ERROR:
1154 case PGRES_FATAL_ERROR:
1160 psql_error("unexpected PQresultStatus: %d\n",
1161 PQresultStatus(results));
1165 fflush(pset.queryFout);
1172 * SendQuery: send the query string to the backend
1173 * (and print out results)
1175 * Note: This is the "front door" way to send a query. That is, use it to
1176 * send queries actually entered by the user. These queries will be subject to
1178 * To send "back door" queries (generated by slash commands, etc.) in a
1179 * controlled way, use PSQLexec().
1181 * Returns true if the query executed successfully, false otherwise.
1184 SendQuery(const char *query)
1187 PGTransactionStatusType transaction_status;
1188 double elapsed_msec = 0;
1191 bool on_error_rollback_savepoint = false;
1192 static bool on_error_rollback_warning = false;
1196 psql_error("You are currently not connected to a database.\n");
1197 goto sendquery_cleanup;
1200 if (pset.singlestep)
1205 printf(_("***(Single step mode: verify command)*******************************************\n"
1207 "***(press return to proceed or enter x and return to cancel)********************\n"),
1210 if (fgets(buf, sizeof(buf), stdin) != NULL)
1212 goto sendquery_cleanup;
1214 goto sendquery_cleanup;
1216 else if (pset.echo == PSQL_ECHO_QUERIES)
1224 fprintf(pset.logfile,
1225 _("********* QUERY **********\n"
1227 "**************************\n\n"), query);
1228 fflush(pset.logfile);
1233 transaction_status = PQtransactionStatus(pset.db);
1235 if (transaction_status == PQTRANS_IDLE &&
1237 !command_no_begin(query))
1239 results = PQexec(pset.db, "BEGIN");
1240 if (PQresultStatus(results) != PGRES_COMMAND_OK)
1242 psql_error("%s", PQerrorMessage(pset.db));
1243 ClearOrSaveResult(results);
1245 goto sendquery_cleanup;
1247 ClearOrSaveResult(results);
1248 transaction_status = PQtransactionStatus(pset.db);
1251 if (transaction_status == PQTRANS_INTRANS &&
1252 pset.on_error_rollback != PSQL_ERROR_ROLLBACK_OFF &&
1253 (pset.cur_cmd_interactive ||
1254 pset.on_error_rollback == PSQL_ERROR_ROLLBACK_ON))
1256 if (on_error_rollback_warning == false && pset.sversion < 80000)
1260 psql_error("The server (version %s) does not support savepoints for ON_ERROR_ROLLBACK.\n",
1261 formatPGVersionNumber(pset.sversion, false,
1262 sverbuf, sizeof(sverbuf)));
1263 on_error_rollback_warning = true;
1267 results = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1268 if (PQresultStatus(results) != PGRES_COMMAND_OK)
1270 psql_error("%s", PQerrorMessage(pset.db));
1271 ClearOrSaveResult(results);
1273 goto sendquery_cleanup;
1275 ClearOrSaveResult(results);
1276 on_error_rollback_savepoint = true;
1280 if (pset.fetch_count <= 0 || pset.gexec_flag ||
1281 pset.crosstab_flag || !is_select_command(query))
1283 /* Default fetch-it-all-and-print mode */
1288 INSTR_TIME_SET_CURRENT(before);
1290 results = PQexec(pset.db, query);
1292 /* these operations are included in the timing result: */
1294 OK = ProcessResult(&results);
1298 INSTR_TIME_SET_CURRENT(after);
1299 INSTR_TIME_SUBTRACT(after, before);
1300 elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1303 /* but printing results isn't: */
1305 OK = PrintQueryResults(results);
1309 /* Fetch-in-segments mode */
1310 OK = ExecQueryUsingCursor(query, &elapsed_msec);
1312 results = NULL; /* PQclear(NULL) does nothing */
1315 if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1316 psql_error("STATEMENT: %s\n", query);
1318 /* If we made a temporary savepoint, possibly release/rollback */
1319 if (on_error_rollback_savepoint)
1321 const char *svptcmd = NULL;
1323 transaction_status = PQtransactionStatus(pset.db);
1325 switch (transaction_status)
1327 case PQTRANS_INERROR:
1328 /* We always rollback on an error */
1329 svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1333 /* If they are no longer in a transaction, then do nothing */
1336 case PQTRANS_INTRANS:
1339 * Do nothing if they are messing with savepoints themselves:
1340 * If the user did RELEASE or ROLLBACK, our savepoint is gone.
1341 * If they issued a SAVEPOINT, releasing ours would remove
1345 (strcmp(PQcmdStatus(results), "SAVEPOINT") == 0 ||
1346 strcmp(PQcmdStatus(results), "RELEASE") == 0 ||
1347 strcmp(PQcmdStatus(results), "ROLLBACK") == 0))
1350 svptcmd = "RELEASE pg_psql_temporary_savepoint";
1353 case PQTRANS_ACTIVE:
1354 case PQTRANS_UNKNOWN:
1357 /* PQTRANS_UNKNOWN is expected given a broken connection. */
1358 if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1359 psql_error("unexpected transaction status (%d)\n",
1360 transaction_status);
1368 svptres = PQexec(pset.db, svptcmd);
1369 if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1371 psql_error("%s", PQerrorMessage(pset.db));
1372 ClearOrSaveResult(svptres);
1377 goto sendquery_cleanup;
1383 ClearOrSaveResult(results);
1385 /* Possible microtiming output */
1387 PrintTiming(elapsed_msec);
1389 /* check for events that may occur during query execution */
1391 if (pset.encoding != PQclientEncoding(pset.db) &&
1392 PQclientEncoding(pset.db) >= 0)
1394 /* track effects of SET CLIENT_ENCODING */
1395 pset.encoding = PQclientEncoding(pset.db);
1396 pset.popt.topt.encoding = pset.encoding;
1397 SetVariable(pset.vars, "ENCODING",
1398 pg_encoding_to_char(pset.encoding));
1401 PrintNotifications();
1403 /* perform cleanup that should occur after any attempted query */
1407 /* reset \g's output-to-filename trigger */
1414 /* reset \gset trigger */
1415 if (pset.gset_prefix)
1417 free(pset.gset_prefix);
1418 pset.gset_prefix = NULL;
1421 /* reset \gexec trigger */
1422 pset.gexec_flag = false;
1424 /* reset \crosstabview trigger */
1425 pset.crosstab_flag = false;
1426 for (i = 0; i < lengthof(pset.ctv_args); i++)
1428 pg_free(pset.ctv_args[i]);
1429 pset.ctv_args[i] = NULL;
1437 * ExecQueryUsingCursor: run a SELECT-like query using a cursor
1439 * This feature allows result sets larger than RAM to be dealt with.
1441 * Returns true if the query executed successfully, false otherwise.
1443 * If pset.timing is on, total query time (exclusive of result-printing) is
1444 * stored into *elapsed_msec.
1447 ExecQueryUsingCursor(const char *query, double *elapsed_msec)
1451 PQExpBufferData buf;
1452 printQueryOpt my_popt = pset.popt;
1455 bool is_pager = false;
1456 bool started_txn = false;
1466 /* initialize print options for partial table output */
1467 my_popt.topt.start_table = true;
1468 my_popt.topt.stop_table = false;
1469 my_popt.topt.prior_records = 0;
1472 INSTR_TIME_SET_CURRENT(before);
1474 /* if we're not in a transaction, start one */
1475 if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
1477 results = PQexec(pset.db, "BEGIN");
1478 OK = AcceptResult(results) &&
1479 (PQresultStatus(results) == PGRES_COMMAND_OK);
1480 ClearOrSaveResult(results);
1486 /* Send DECLARE CURSOR */
1487 initPQExpBuffer(&buf);
1488 appendPQExpBuffer(&buf, "DECLARE _psql_cursor NO SCROLL CURSOR FOR\n%s",
1491 results = PQexec(pset.db, buf.data);
1492 OK = AcceptResult(results) &&
1493 (PQresultStatus(results) == PGRES_COMMAND_OK);
1494 ClearOrSaveResult(results);
1495 termPQExpBuffer(&buf);
1501 INSTR_TIME_SET_CURRENT(after);
1502 INSTR_TIME_SUBTRACT(after, before);
1503 *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1507 * In \gset mode, we force the fetch count to be 2, so that we will throw
1508 * the appropriate error if the query returns more than one row.
1510 if (pset.gset_prefix)
1513 fetch_count = pset.fetch_count;
1515 snprintf(fetch_cmd, sizeof(fetch_cmd),
1516 "FETCH FORWARD %d FROM _psql_cursor",
1519 /* prepare to write output to \g argument, if any */
1522 if (!openQueryOutputFile(pset.gfname, &fout, &is_pipe))
1528 disable_sigpipe_trap();
1532 fout = pset.queryFout;
1533 is_pipe = false; /* doesn't matter */
1536 /* clear any pre-existing error indication on the output stream */
1542 INSTR_TIME_SET_CURRENT(before);
1544 /* get fetch_count tuples at a time */
1545 results = PQexec(pset.db, fetch_cmd);
1549 INSTR_TIME_SET_CURRENT(after);
1550 INSTR_TIME_SUBTRACT(after, before);
1551 *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1554 if (PQresultStatus(results) != PGRES_TUPLES_OK)
1556 /* shut down pager before printing error message */
1563 OK = AcceptResult(results);
1565 ClearOrSaveResult(results);
1569 if (pset.gset_prefix)
1571 /* StoreQueryTuple will complain if not exactly one row */
1572 OK = StoreQueryTuple(results);
1573 ClearOrSaveResult(results);
1577 /* Note we do not deal with \gexec or \crosstabview modes here */
1579 ntuples = PQntuples(results);
1581 if (ntuples < fetch_count)
1583 /* this is the last result set, so allow footer decoration */
1584 my_popt.topt.stop_table = true;
1586 else if (fout == stdout && !is_pager)
1589 * If query requires multiple result sets, hack to ensure that
1590 * only one pager instance is used for the whole mess
1592 fout = PageOutput(INT_MAX, &(my_popt.topt));
1596 printQuery(results, &my_popt, fout, is_pager, pset.logfile);
1598 ClearOrSaveResult(results);
1600 /* after the first result set, disallow header decoration */
1601 my_popt.topt.start_table = false;
1602 my_popt.topt.prior_records += ntuples;
1605 * Make sure to flush the output stream, so intermediate results are
1606 * visible to the client immediately. We check the results because if
1607 * the pager dies/exits/etc, there's no sense throwing more data at
1610 flush_error = fflush(fout);
1613 * Check if we are at the end, if a cancel was pressed, or if there
1614 * were any errors either trying to flush out the results, or more
1615 * generally on the output stream at all. If we hit any errors
1616 * writing things to the stream, we presume $PAGER has disappeared and
1617 * stop bothering to pull down more data.
1619 if (ntuples < fetch_count || cancel_pressed || flush_error ||
1626 /* close \g argument file/pipe */
1630 restore_sigpipe_trap();
1637 /* close transient pager */
1643 INSTR_TIME_SET_CURRENT(before);
1646 * We try to close the cursor on either success or failure, but on failure
1647 * ignore the result (it's probably just a bleat about being in an aborted
1650 results = PQexec(pset.db, "CLOSE _psql_cursor");
1653 OK = AcceptResult(results) &&
1654 (PQresultStatus(results) == PGRES_COMMAND_OK);
1655 ClearOrSaveResult(results);
1662 results = PQexec(pset.db, OK ? "COMMIT" : "ROLLBACK");
1663 OK &= AcceptResult(results) &&
1664 (PQresultStatus(results) == PGRES_COMMAND_OK);
1665 ClearOrSaveResult(results);
1670 INSTR_TIME_SET_CURRENT(after);
1671 INSTR_TIME_SUBTRACT(after, before);
1672 *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1680 * Advance the given char pointer over white space and SQL comments.
1683 skip_white_space(const char *query)
1685 int cnestlevel = 0; /* slash-star comment nest level */
1689 int mblen = PQmblen(query, pset.encoding);
1692 * Note: we assume the encoding is a superset of ASCII, so that for
1693 * example "query[0] == '/'" is meaningful. However, we do NOT assume
1694 * that the second and subsequent bytes of a multibyte character
1695 * couldn't look like ASCII characters; so it is critical to advance
1696 * by mblen, not 1, whenever we haven't exactly identified the
1697 * character we are skipping over.
1699 if (isspace((unsigned char) *query))
1701 else if (query[0] == '/' && query[1] == '*')
1706 else if (cnestlevel > 0 && query[0] == '*' && query[1] == '/')
1711 else if (cnestlevel == 0 && query[0] == '-' && query[1] == '-')
1716 * We have to skip to end of line since any slash-star inside the
1717 * -- comment does NOT start a slash-star comment.
1726 query += PQmblen(query, pset.encoding);
1729 else if (cnestlevel > 0)
1732 break; /* found first token */
1740 * Check whether a command is one of those for which we should NOT start
1741 * a new transaction block (ie, send a preceding BEGIN).
1743 * These include the transaction control statements themselves, plus
1744 * certain statements that the backend disallows inside transaction blocks.
1747 command_no_begin(const char *query)
1752 * First we must advance over any whitespace and comments.
1754 query = skip_white_space(query);
1757 * Check word length (since "beginx" is not "begin").
1760 while (isalpha((unsigned char) query[wordlen]))
1761 wordlen += PQmblen(&query[wordlen], pset.encoding);
1764 * Transaction control commands. These should include every keyword that
1765 * gives rise to a TransactionStmt in the backend grammar, except for the
1766 * savepoint-related commands.
1768 * (We assume that START must be START TRANSACTION, since there is
1769 * presently no other "START foo" command.)
1771 if (wordlen == 5 && pg_strncasecmp(query, "abort", 5) == 0)
1773 if (wordlen == 5 && pg_strncasecmp(query, "begin", 5) == 0)
1775 if (wordlen == 5 && pg_strncasecmp(query, "start", 5) == 0)
1777 if (wordlen == 6 && pg_strncasecmp(query, "commit", 6) == 0)
1779 if (wordlen == 3 && pg_strncasecmp(query, "end", 3) == 0)
1781 if (wordlen == 8 && pg_strncasecmp(query, "rollback", 8) == 0)
1783 if (wordlen == 7 && pg_strncasecmp(query, "prepare", 7) == 0)
1785 /* PREPARE TRANSACTION is a TC command, PREPARE foo is not */
1788 query = skip_white_space(query);
1791 while (isalpha((unsigned char) query[wordlen]))
1792 wordlen += PQmblen(&query[wordlen], pset.encoding);
1794 if (wordlen == 11 && pg_strncasecmp(query, "transaction", 11) == 0)
1800 * Commands not allowed within transactions. The statements checked for
1801 * here should be exactly those that call PreventTransactionChain() in the
1804 if (wordlen == 6 && pg_strncasecmp(query, "vacuum", 6) == 0)
1806 if (wordlen == 7 && pg_strncasecmp(query, "cluster", 7) == 0)
1808 /* CLUSTER with any arguments is allowed in transactions */
1811 query = skip_white_space(query);
1813 if (isalpha((unsigned char) query[0]))
1814 return false; /* has additional words */
1815 return true; /* it's CLUSTER without arguments */
1818 if (wordlen == 6 && pg_strncasecmp(query, "create", 6) == 0)
1822 query = skip_white_space(query);
1825 while (isalpha((unsigned char) query[wordlen]))
1826 wordlen += PQmblen(&query[wordlen], pset.encoding);
1828 if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
1830 if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
1833 /* CREATE [UNIQUE] INDEX CONCURRENTLY isn't allowed in xacts */
1834 if (wordlen == 6 && pg_strncasecmp(query, "unique", 6) == 0)
1838 query = skip_white_space(query);
1841 while (isalpha((unsigned char) query[wordlen]))
1842 wordlen += PQmblen(&query[wordlen], pset.encoding);
1845 if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
1849 query = skip_white_space(query);
1852 while (isalpha((unsigned char) query[wordlen]))
1853 wordlen += PQmblen(&query[wordlen], pset.encoding);
1855 if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
1862 if (wordlen == 5 && pg_strncasecmp(query, "alter", 5) == 0)
1866 query = skip_white_space(query);
1869 while (isalpha((unsigned char) query[wordlen]))
1870 wordlen += PQmblen(&query[wordlen], pset.encoding);
1872 /* ALTER SYSTEM isn't allowed in xacts */
1873 if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
1880 * Note: these tests will match DROP SYSTEM and REINDEX TABLESPACE, which
1881 * aren't really valid commands so we don't care much. The other four
1882 * possible matches are correct.
1884 if ((wordlen == 4 && pg_strncasecmp(query, "drop", 4) == 0) ||
1885 (wordlen == 7 && pg_strncasecmp(query, "reindex", 7) == 0))
1889 query = skip_white_space(query);
1892 while (isalpha((unsigned char) query[wordlen]))
1893 wordlen += PQmblen(&query[wordlen], pset.encoding);
1895 if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
1897 if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
1899 if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
1902 /* DROP INDEX CONCURRENTLY isn't allowed in xacts */
1903 if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
1907 query = skip_white_space(query);
1910 while (isalpha((unsigned char) query[wordlen]))
1911 wordlen += PQmblen(&query[wordlen], pset.encoding);
1913 if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
1922 /* DISCARD ALL isn't allowed in xacts, but other variants are allowed. */
1923 if (wordlen == 7 && pg_strncasecmp(query, "discard", 7) == 0)
1927 query = skip_white_space(query);
1930 while (isalpha((unsigned char) query[wordlen]))
1931 wordlen += PQmblen(&query[wordlen], pset.encoding);
1933 if (wordlen == 3 && pg_strncasecmp(query, "all", 3) == 0)
1943 * Check whether the specified command is a SELECT (or VALUES).
1946 is_select_command(const char *query)
1951 * First advance over any whitespace, comments and left parentheses.
1955 query = skip_white_space(query);
1956 if (query[0] == '(')
1963 * Check word length (since "selectx" is not "select").
1966 while (isalpha((unsigned char) query[wordlen]))
1967 wordlen += PQmblen(&query[wordlen], pset.encoding);
1969 if (wordlen == 6 && pg_strncasecmp(query, "select", 6) == 0)
1972 if (wordlen == 6 && pg_strncasecmp(query, "values", 6) == 0)
1980 * Test if the current user is a database superuser.
1982 * Note: this will correctly detect superuserness only with a protocol-3.0
1983 * or newer backend; otherwise it will always say "false".
1993 val = PQparameterStatus(pset.db, "is_superuser");
1995 if (val && strcmp(val, "on") == 0)
2003 * Test if the current session uses standard string literals.
2005 * Note: With a pre-protocol-3.0 connection this will always say "false",
2006 * which should be the right answer.
2009 standard_strings(void)
2016 val = PQparameterStatus(pset.db, "standard_conforming_strings");
2018 if (val && strcmp(val, "on") == 0)
2026 * Return the session user of the current connection.
2028 * Note: this will correctly detect the session user only with a
2029 * protocol-3.0 or newer backend; otherwise it will return the
2033 session_username(void)
2040 val = PQparameterStatus(pset.db, "session_authorization");
2044 return PQuser(pset.db);
2050 * substitute '~' with HOME or '~username' with username's home dir
2054 expand_tilde(char **filename)
2056 if (!filename || !(*filename))
2060 * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2061 * for short versions of long file names, though the tilde is usually
2062 * toward the end, not at the beginning.
2066 /* try tilde expansion */
2067 if (**filename == '~')
2073 char home[MAXPGPATH];
2079 while (*p != '/' && *p != '\0')
2085 if (*(fn + 1) == '\0')
2086 get_home_path(home); /* ~ or ~/ only */
2087 else if ((pw = getpwnam(fn + 1)) != NULL)
2088 strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2091 if (strlen(home) != 0)
2095 newfn = psprintf("%s%s", home, p);
2106 * Checks if connection string starts with either of the valid URI prefix
2109 * Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
2111 * XXX This is a duplicate of the eponymous libpq function.
2114 uri_prefix_length(const char *connstr)
2116 /* The connection URI must start with either of the following designators: */
2117 static const char uri_designator[] = "postgresql://";
2118 static const char short_uri_designator[] = "postgres://";
2120 if (strncmp(connstr, uri_designator,
2121 sizeof(uri_designator) - 1) == 0)
2122 return sizeof(uri_designator) - 1;
2124 if (strncmp(connstr, short_uri_designator,
2125 sizeof(short_uri_designator) - 1) == 0)
2126 return sizeof(short_uri_designator) - 1;
2132 * Recognized connection string either starts with a valid URI prefix or
2133 * contains a "=" in it.
2135 * Must be consistent with parse_connection_string: anything for which this
2136 * returns true should at least look like it's parseable by that routine.
2138 * XXX This is a duplicate of the eponymous libpq function.
2141 recognized_connection_string(const char *connstr)
2143 return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;