2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2021, PostgreSQL Global Development Group
6 * src/bin/psql/common.c
8 #include "postgres_fe.h"
15 #include <unistd.h> /* for write() */
17 #include <io.h> /* for _write() */
23 #include "common/logging.h"
25 #include "crosstabview.h"
26 #include "fe_utils/cancel.h"
27 #include "fe_utils/mbprint.h"
28 #include "fe_utils/string_utils.h"
29 #include "portability/instr_time.h"
32 static bool DescribeQuery(const char *query
, double *elapsed_msec
);
33 static bool ExecQueryUsingCursor(const char *query
, double *elapsed_msec
);
34 static bool command_no_begin(const char *query
);
35 static bool is_select_command(const char *query
);
39 * openQueryOutputFile --- attempt to open a query output file
41 * fname == NULL selects stdout, else an initial '|' selects a pipe,
44 * Returns output file pointer into *fout, and is-a-pipe flag into *is_pipe.
45 * Caller is responsible for adjusting SIGPIPE state if it's a pipe.
47 * On error, reports suitable error message and returns false.
50 openQueryOutputFile(const char *fname
, FILE **fout
, bool *is_pipe
)
52 if (!fname
|| fname
[0] == '\0')
57 else if (*fname
== '|')
59 *fout
= popen(fname
+ 1, "w");
64 *fout
= fopen(fname
, "w");
70 pg_log_error("%s: %m", fname
);
79 * -- handler for -o command line option and \o command
81 * On success, updates pset with the new output file and returns true.
82 * On failure, returns false without changing pset state.
85 setQFout(const char *fname
)
90 /* First make sure we can open the new output file/pipe */
91 if (!openQueryOutputFile(fname
, &fout
, &is_pipe
))
94 /* Close old file/pipe */
95 if (pset
.queryFout
&& pset
.queryFout
!= stdout
&& pset
.queryFout
!= stderr
)
97 if (pset
.queryFoutPipe
)
98 pclose(pset
.queryFout
);
100 fclose(pset
.queryFout
);
103 pset
.queryFout
= fout
;
104 pset
.queryFoutPipe
= is_pipe
;
106 /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
107 set_sigpipe_trap_state(is_pipe
);
108 restore_sigpipe_trap();
115 * Variable-fetching callback for flex lexer
117 * If the specified variable exists, return its value as a string (malloc'd
118 * and expected to be freed by the caller); else return NULL.
120 * If "quote" isn't PQUOTE_PLAIN, then return the value suitably quoted and
121 * escaped for the specified quoting requirement. (Failure in escaping
122 * should lead to printing an error and returning NULL.)
124 * "passthrough" is the pointer previously given to psql_scan_set_passthrough.
125 * In psql, passthrough points to a ConditionalStack, which we check to
126 * determine whether variable expansion is allowed.
129 psql_get_variable(const char *varname
, PsqlScanQuoteType quote
,
135 /* In an inactive \if branch, suppress all variable substitutions */
136 if (passthrough
&& !conditional_active((ConditionalStack
) passthrough
))
139 value
= GetVariable(pset
.vars
, varname
);
146 result
= pg_strdup(value
);
148 case PQUOTE_SQL_LITERAL
:
149 case PQUOTE_SQL_IDENT
:
152 * For these cases, we use libpq's quoting functions, which
153 * assume the string is in the connection's client encoding.
159 pg_log_error("cannot escape without active connection");
163 if (quote
== PQUOTE_SQL_LITERAL
)
165 PQescapeLiteral(pset
.db
, value
, strlen(value
));
168 PQescapeIdentifier(pset
.db
, value
, strlen(value
));
170 if (escaped_value
== NULL
)
172 const char *error
= PQerrorMessage(pset
.db
);
174 pg_log_info("%s", error
);
179 * Rather than complicate the lexer's API with a notion of
180 * which free() routine to use, just pay the price of an extra
183 result
= pg_strdup(escaped_value
);
184 PQfreemem(escaped_value
);
187 case PQUOTE_SHELL_ARG
:
190 * For this we use appendShellStringNoError, which is
191 * encoding-agnostic, which is fine since the shell probably
192 * is too. In any case, the only special character is "'",
193 * which is not known to appear in valid multibyte characters.
197 initPQExpBuffer(&buf
);
198 if (!appendShellStringNoError(&buf
, value
))
200 pg_log_error("shell command argument contains a newline or carriage return: \"%s\"",
209 /* No default: we want a compiler warning for missing cases */
217 * for backend Notice messages (INFO, WARNING, etc)
220 NoticeProcessor(void *arg
, const char *message
)
222 (void) arg
; /* not used */
223 pg_log_info("%s", message
);
229 * Code to support query cancellation
231 * Before we start a query, we enable the SIGINT signal catcher to send a
232 * cancel request to the backend.
234 * SIGINT is supposed to abort all long-running psql operations, not only
235 * database queries. In most places, this is accomplished by checking
236 * cancel_pressed during long-running loops. However, that won't work when
237 * blocked on user input (in readline() or fgets()). In those places, we
238 * set sigint_interrupt_enabled true while blocked, instructing the signal
239 * catcher to longjmp through sigint_interrupt_jmp. We assume readline and
240 * fgets are coded to handle possible interruption.
242 * On Windows, currently this does not work, so control-C is less useful
245 volatile bool sigint_interrupt_enabled
= false;
247 sigjmp_buf sigint_interrupt_jmp
;
250 psql_cancel_callback(void)
253 /* if we are waiting for input, longjmp out of it */
254 if (sigint_interrupt_enabled
)
256 sigint_interrupt_enabled
= false;
257 siglongjmp(sigint_interrupt_jmp
, 1);
261 /* else, set cancel flag to stop any long-running loops */
262 cancel_pressed
= true;
266 psql_setup_cancel_handler(void)
268 setup_cancel_handler(psql_cancel_callback
);
274 * Returns whether our backend connection is still there.
279 return PQstatus(pset
.db
) != CONNECTION_BAD
;
286 * Verify that we still have a good connection to the backend, and if not,
287 * see if it can be restored.
289 * Returns true if either the connection was still there, or it could be
290 * restored successfully; false otherwise. If, however, there was no
291 * connection and the session is non-interactive, this will exit the program
292 * with a code of EXIT_BADCONN.
295 CheckConnection(void)
302 if (!pset
.cur_cmd_interactive
)
304 pg_log_fatal("connection to server was lost");
308 fprintf(stderr
, _("The connection to the server was lost. Attempting reset: "));
313 fprintf(stderr
, _("Failed.\n"));
316 * Transition to having no connection; but stash away the failed
317 * connection so that we can still refer to its parameters in a
318 * later \connect attempt. Keep the state cleanup here in sync
322 PQfinish(pset
.dead_conn
);
323 pset
.dead_conn
= pset
.db
;
330 fprintf(stderr
, _("Succeeded.\n"));
333 * Re-sync, just in case anything changed. Keep this in sync with
337 connection_warnings(false); /* Must be after SyncVariables */
350 * Checks whether a result is valid, giving an error message if necessary;
351 * and ensures that the connection to the backend is still up.
353 * Returns true for valid result, false for error state.
356 AcceptResult(const PGresult
*result
)
363 switch (PQresultStatus(result
))
365 case PGRES_COMMAND_OK
:
366 case PGRES_TUPLES_OK
:
367 case PGRES_EMPTY_QUERY
:
370 /* Fine, do nothing */
374 case PGRES_BAD_RESPONSE
:
375 case PGRES_NONFATAL_ERROR
:
376 case PGRES_FATAL_ERROR
:
382 pg_log_error("unexpected PQresultStatus: %d",
383 PQresultStatus(result
));
389 const char *error
= PQerrorMessage(pset
.db
);
392 pg_log_info("%s", error
);
402 * Set special variables from a query result
403 * - ERROR: true/false, whether an error occurred on this query
404 * - SQLSTATE: code of error, or "00000" if no error, or "" if unknown
405 * - ROW_COUNT: how many rows were returned or affected, or "0"
406 * - LAST_ERROR_SQLSTATE: same for last error
407 * - LAST_ERROR_MESSAGE: message of last error
409 * Note: current policy is to apply this only to the results of queries
410 * entered by the user, not queries generated by slash commands.
413 SetResultVariables(PGresult
*results
, bool success
)
417 const char *ntuples
= PQcmdTuples(results
);
419 SetVariable(pset
.vars
, "ERROR", "false");
420 SetVariable(pset
.vars
, "SQLSTATE", "00000");
421 SetVariable(pset
.vars
, "ROW_COUNT", *ntuples
? ntuples
: "0");
425 const char *code
= PQresultErrorField(results
, PG_DIAG_SQLSTATE
);
426 const char *mesg
= PQresultErrorField(results
, PG_DIAG_MESSAGE_PRIMARY
);
428 SetVariable(pset
.vars
, "ERROR", "true");
431 * If there is no SQLSTATE code, use an empty string. This can happen
432 * for libpq-detected errors (e.g., lost connection, ENOMEM).
436 SetVariable(pset
.vars
, "SQLSTATE", code
);
437 SetVariable(pset
.vars
, "ROW_COUNT", "0");
438 SetVariable(pset
.vars
, "LAST_ERROR_SQLSTATE", code
);
439 SetVariable(pset
.vars
, "LAST_ERROR_MESSAGE", mesg
? mesg
: "");
447 * If the result represents an error, remember it for possible display by
448 * \errverbose. Otherwise, just PQclear() it.
450 * Note: current policy is to apply this to the results of all queries,
451 * including "back door" queries, for debugging's sake. It's OK to use
452 * PQclear() directly on results known to not be error results, however.
455 ClearOrSaveResult(PGresult
*result
)
459 switch (PQresultStatus(result
))
461 case PGRES_NONFATAL_ERROR
:
462 case PGRES_FATAL_ERROR
:
463 if (pset
.last_error_result
)
464 PQclear(pset
.last_error_result
);
465 pset
.last_error_result
= result
;
477 * Print microtiming output. Always print raw milliseconds; if the interval
478 * is >= 1 second, also break it down into days/hours/minutes/seconds.
481 PrintTiming(double elapsed_msec
)
488 if (elapsed_msec
< 1000.0)
490 /* This is the traditional (pre-v10) output format */
491 printf(_("Time: %.3f ms\n"), elapsed_msec
);
496 * Note: we could print just seconds, in a format like %06.3f, when the
497 * total is less than 1min. But that's hard to interpret unless we tack
498 * on "s" or otherwise annotate it. Forcing the display to include
499 * minutes seems like a better solution.
501 seconds
= elapsed_msec
/ 1000.0;
502 minutes
= floor(seconds
/ 60.0);
503 seconds
-= 60.0 * minutes
;
506 printf(_("Time: %.3f ms (%02d:%06.3f)\n"),
507 elapsed_msec
, (int) minutes
, seconds
);
511 hours
= floor(minutes
/ 60.0);
512 minutes
-= 60.0 * hours
;
515 printf(_("Time: %.3f ms (%02d:%02d:%06.3f)\n"),
516 elapsed_msec
, (int) hours
, (int) minutes
, seconds
);
520 days
= floor(hours
/ 24.0);
521 hours
-= 24.0 * days
;
522 printf(_("Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"),
523 elapsed_msec
, days
, (int) hours
, (int) minutes
, seconds
);
530 * This is the way to send "backdoor" queries (those not directly entered
531 * by the user). It is subject to -E but not -e.
533 * Caller is responsible for handling the ensuing processing if a COPY
536 * Note: we don't bother to check PQclientEncoding; it is assumed that no
537 * caller uses this path to issue "SET CLIENT_ENCODING".
540 PSQLexec(const char *query
)
546 pg_log_error("You are currently not connected to a database.");
550 if (pset
.echo_hidden
!= PSQL_ECHO_HIDDEN_OFF
)
552 printf(_("********* QUERY **********\n"
554 "**************************\n\n"), query
);
558 fprintf(pset
.logfile
,
559 _("********* QUERY **********\n"
561 "**************************\n\n"), query
);
562 fflush(pset
.logfile
);
565 if (pset
.echo_hidden
== PSQL_ECHO_HIDDEN_NOEXEC
)
569 SetCancelConn(pset
.db
);
571 res
= PQexec(pset
.db
, query
);
575 if (!AcceptResult(res
))
577 ClearOrSaveResult(res
);
588 * This function is used for \watch command to send the query to
589 * the server and print out the results.
591 * Returns 1 if the query executed successfully, 0 if it cannot be repeated,
592 * e.g., because of the interrupt, -1 on error.
595 PSQLexecWatch(const char *query
, const printQueryOpt
*opt
, FILE *printQueryFout
)
598 double elapsed_msec
= 0;
605 pg_log_error("You are currently not connected to a database.");
609 SetCancelConn(pset
.db
);
612 INSTR_TIME_SET_CURRENT(before
);
614 res
= PQexec(pset
.db
, query
);
618 if (!AcceptResult(res
))
620 ClearOrSaveResult(res
);
626 INSTR_TIME_SET_CURRENT(after
);
627 INSTR_TIME_SUBTRACT(after
, before
);
628 elapsed_msec
= INSTR_TIME_GET_MILLISEC(after
);
632 * If SIGINT is sent while the query is processing, the interrupt will be
633 * consumed. The user's intention, though, is to cancel the entire watch
634 * process, so detect a sent cancellation request and exit in this case.
642 fout
= printQueryFout
? printQueryFout
: pset
.queryFout
;
644 switch (PQresultStatus(res
))
646 case PGRES_TUPLES_OK
:
647 printQuery(res
, opt
, fout
, false, pset
.logfile
);
650 case PGRES_COMMAND_OK
:
651 fprintf(fout
, "%s\n%s\n\n", opt
->title
, PQcmdStatus(res
));
654 case PGRES_EMPTY_QUERY
:
655 pg_log_error("\\watch cannot be used with an empty query");
661 case PGRES_COPY_BOTH
:
662 pg_log_error("\\watch cannot be used with COPY");
667 pg_log_error("unexpected result status for \\watch");
676 /* Possible microtiming output */
678 PrintTiming(elapsed_msec
);
685 * PrintNotifications: check for asynchronous notifications, and print them out
688 PrintNotifications(void)
692 PQconsumeInput(pset
.db
);
693 while ((notify
= PQnotifies(pset
.db
)) != NULL
)
695 /* for backward compatibility, only show payload if nonempty */
696 if (notify
->extra
[0])
697 fprintf(pset
.queryFout
, _("Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
698 notify
->relname
, notify
->extra
, notify
->be_pid
);
700 fprintf(pset
.queryFout
, _("Asynchronous notification \"%s\" received from server process with PID %d.\n"),
701 notify
->relname
, notify
->be_pid
);
702 fflush(pset
.queryFout
);
704 PQconsumeInput(pset
.db
);
710 * PrintQueryTuples: assuming query result is OK, print its tuples
712 * Returns true if successful, false otherwise.
715 PrintQueryTuples(const PGresult
*results
)
719 /* write output to \g argument, if any */
725 if (!openQueryOutputFile(pset
.gfname
, &fout
, &is_pipe
))
728 disable_sigpipe_trap();
730 printQuery(results
, &pset
.popt
, fout
, false, pset
.logfile
);
733 pg_log_error("could not print result table: %m");
740 restore_sigpipe_trap();
747 printQuery(results
, &pset
.popt
, pset
.queryFout
, false, pset
.logfile
);
748 if (ferror(pset
.queryFout
))
750 pg_log_error("could not print result table: %m");
760 * StoreQueryTuple: assuming query result is OK, save data into variables
762 * Returns true if successful, false otherwise.
765 StoreQueryTuple(const PGresult
*result
)
769 if (PQntuples(result
) < 1)
771 pg_log_error("no rows returned for \\gset");
774 else if (PQntuples(result
) > 1)
776 pg_log_error("more than one row returned for \\gset");
783 for (i
= 0; i
< PQnfields(result
); i
++)
785 char *colname
= PQfname(result
, i
);
789 /* concatenate prefix and column name */
790 varname
= psprintf("%s%s", pset
.gset_prefix
, colname
);
792 if (VariableHasHook(pset
.vars
, varname
))
794 pg_log_warning("attempt to \\gset into specially treated variable \"%s\" ignored",
799 if (!PQgetisnull(result
, 0, i
))
800 value
= PQgetvalue(result
, 0, i
);
803 /* for NULL value, unset rather than set the variable */
807 if (!SetVariable(pset
.vars
, varname
, value
))
823 * ExecQueryTuples: assuming query result is OK, execute each query
824 * result field as a SQL statement
826 * Returns true if successful, false otherwise.
829 ExecQueryTuples(const PGresult
*result
)
832 int nrows
= PQntuples(result
);
833 int ncolumns
= PQnfields(result
);
838 * We must turn off gexec_flag to avoid infinite recursion. Note that
839 * this allows ExecQueryUsingCursor to be applied to the individual query
840 * results. SendQuery prevents it from being applied when fetching the
841 * queries-to-execute, because it can't handle recursion either.
843 pset
.gexec_flag
= false;
845 for (r
= 0; r
< nrows
; r
++)
847 for (c
= 0; c
< ncolumns
; c
++)
849 if (!PQgetisnull(result
, r
, c
))
851 const char *query
= PQgetvalue(result
, r
, c
);
853 /* Abandon execution if cancel_pressed */
858 * ECHO_ALL mode should echo these queries, but SendQuery
859 * assumes that MainLoop did that, so we have to do it here.
861 if (pset
.echo
== PSQL_ECHO_ALL
&& !pset
.singlestep
)
867 if (!SendQuery(query
))
869 /* Error - abandon execution if ON_ERROR_STOP */
871 if (pset
.on_error_stop
)
881 * Restore state. We know gexec_flag was on, else we'd not be here. (We
882 * also know it'll get turned off at end of command, but that's not ours
885 pset
.gexec_flag
= true;
887 /* Return true if all queries were successful */
893 * ProcessResult: utility function for use by SendQuery() only
895 * When our command string contained a COPY FROM STDIN or COPY TO STDOUT,
896 * PQexec() has stopped at the PGresult associated with the first such
897 * command. In that event, we'll marshal data for the COPY and then cycle
898 * through any subsequent PGresult objects.
900 * When the command string contained no such COPY command, this function
901 * degenerates to an AcceptResult() call.
903 * Changes its argument to point to the last PGresult of the command string,
904 * or NULL if that result was for a COPY TO STDOUT. (Returning NULL prevents
905 * the command status from being printed, which we want in that case so that
906 * the status line doesn't get taken as part of the COPY data.)
908 * Returns true on complete success, false otherwise. Possible failure modes
909 * include purely client-side problems; check the transaction status for the
910 * server-side opinion.
913 ProcessResult(PGresult
**results
)
916 bool first_cycle
= true;
920 ExecStatusType result_status
;
922 PGresult
*next_result
;
924 if (!AcceptResult(*results
))
927 * Failure at this point is always a server-side failure or a
928 * failure to submit the command string. Either way, we're
929 * finished with this command string.
935 result_status
= PQresultStatus(*results
);
936 switch (result_status
)
938 case PGRES_EMPTY_QUERY
:
939 case PGRES_COMMAND_OK
:
940 case PGRES_TUPLES_OK
:
950 /* AcceptResult() should have caught anything else. */
952 pg_log_error("unexpected PQresultStatus: %d", result_status
);
959 * Marshal the COPY data. Either subroutine will get the
960 * connection out of its COPY state, then call PQresultStatus()
961 * once and report any error.
963 * For COPY OUT, direct the output to pset.copyStream if it's set,
964 * otherwise to pset.gfname if it's set, otherwise to queryFout.
965 * For COPY IN, use pset.copyStream as data source if it's set,
966 * otherwise cur_cmd_source.
969 PGresult
*copy_result
;
971 SetCancelConn(pset
.db
);
972 if (result_status
== PGRES_COPY_OUT
)
974 bool need_close
= false;
975 bool is_pipe
= false;
979 /* invoked by \copy */
980 copystream
= pset
.copyStream
;
982 else if (pset
.gfname
)
985 if (openQueryOutputFile(pset
.gfname
,
986 ©stream
, &is_pipe
))
990 disable_sigpipe_trap();
993 copystream
= NULL
; /* discard COPY data entirely */
997 /* fall back to the generic query output stream */
998 copystream
= pset
.queryFout
;
1001 success
= handleCopyOut(pset
.db
,
1005 && (copystream
!= NULL
);
1008 * Suppress status printing if the report would go to the same
1009 * place as the COPY data just went. Note this doesn't
1010 * prevent error reporting, since handleCopyOut did that.
1012 if (copystream
== pset
.queryFout
)
1014 PQclear(copy_result
);
1020 /* close \g argument file/pipe */
1024 restore_sigpipe_trap();
1035 copystream
= pset
.copyStream
? pset
.copyStream
: pset
.cur_cmd_source
;
1036 success
= handleCopyIn(pset
.db
,
1038 PQbinaryTuples(*results
),
1039 ©_result
) && success
;
1044 * Replace the PGRES_COPY_OUT/IN result with COPY command's exit
1045 * status, or with NULL if we want to suppress printing anything.
1048 *results
= copy_result
;
1050 else if (first_cycle
)
1052 /* fast path: no COPY commands; PQexec visited all results */
1057 * Check PQgetResult() again. In the typical case of a single-command
1058 * string, it will return NULL. Otherwise, we'll have other results
1059 * to process that may include other COPYs. We keep the last result.
1061 next_result
= PQgetResult(pset
.db
);
1066 *results
= next_result
;
1067 first_cycle
= false;
1070 SetResultVariables(*results
, success
);
1072 /* may need this to recover from conn loss during COPY */
1073 if (!first_cycle
&& !CheckConnection())
1081 * PrintQueryStatus: report command status as required
1083 * Note: Utility function for use by PrintQueryResults() only.
1086 PrintQueryStatus(PGresult
*results
)
1092 if (pset
.popt
.topt
.format
== PRINT_HTML
)
1094 fputs("<p>", pset
.queryFout
);
1095 html_escaped_print(PQcmdStatus(results
), pset
.queryFout
);
1096 fputs("</p>\n", pset
.queryFout
);
1099 fprintf(pset
.queryFout
, "%s\n", PQcmdStatus(results
));
1103 fprintf(pset
.logfile
, "%s\n", PQcmdStatus(results
));
1105 snprintf(buf
, sizeof(buf
), "%u", (unsigned int) PQoidValue(results
));
1106 SetVariable(pset
.vars
, "LASTOID", buf
);
1111 * PrintQueryResults: print out (or store or execute) query results as required
1113 * Note: Utility function for use by SendQuery() only.
1115 * Returns true if the query executed successfully, false otherwise.
1118 PrintQueryResults(PGresult
*results
)
1121 const char *cmdstatus
;
1126 switch (PQresultStatus(results
))
1128 case PGRES_TUPLES_OK
:
1129 /* store or execute or print the data ... */
1130 if (pset
.gset_prefix
)
1131 success
= StoreQueryTuple(results
);
1132 else if (pset
.gexec_flag
)
1133 success
= ExecQueryTuples(results
);
1134 else if (pset
.crosstab_flag
)
1135 success
= PrintResultsInCrosstab(results
);
1137 success
= PrintQueryTuples(results
);
1138 /* if it's INSERT/UPDATE/DELETE RETURNING, also print status */
1139 cmdstatus
= PQcmdStatus(results
);
1140 if (strncmp(cmdstatus
, "INSERT", 6) == 0 ||
1141 strncmp(cmdstatus
, "UPDATE", 6) == 0 ||
1142 strncmp(cmdstatus
, "DELETE", 6) == 0)
1143 PrintQueryStatus(results
);
1146 case PGRES_COMMAND_OK
:
1147 PrintQueryStatus(results
);
1151 case PGRES_EMPTY_QUERY
:
1155 case PGRES_COPY_OUT
:
1157 /* nothing to do here */
1161 case PGRES_BAD_RESPONSE
:
1162 case PGRES_NONFATAL_ERROR
:
1163 case PGRES_FATAL_ERROR
:
1169 pg_log_error("unexpected PQresultStatus: %d",
1170 PQresultStatus(results
));
1174 fflush(pset
.queryFout
);
1181 * SendQuery: send the query string to the backend
1182 * (and print out results)
1184 * Note: This is the "front door" way to send a query. That is, use it to
1185 * send queries actually entered by the user. These queries will be subject to
1187 * To send "back door" queries (generated by slash commands, etc.) in a
1188 * controlled way, use PSQLexec().
1190 * Returns true if the query executed successfully, false otherwise.
1193 SendQuery(const char *query
)
1196 PGTransactionStatusType transaction_status
;
1197 double elapsed_msec
= 0;
1200 bool on_error_rollback_savepoint
= false;
1201 static bool on_error_rollback_warning
= false;
1205 pg_log_error("You are currently not connected to a database.");
1206 goto sendquery_cleanup
;
1209 if (pset
.singlestep
)
1214 printf(_("***(Single step mode: verify command)*******************************************\n"
1216 "***(press return to proceed or enter x and return to cancel)********************\n"),
1219 if (fgets(buf
, sizeof(buf
), stdin
) != NULL
)
1221 goto sendquery_cleanup
;
1223 goto sendquery_cleanup
;
1225 else if (pset
.echo
== PSQL_ECHO_QUERIES
)
1233 fprintf(pset
.logfile
,
1234 _("********* QUERY **********\n"
1236 "**************************\n\n"), query
);
1237 fflush(pset
.logfile
);
1240 SetCancelConn(pset
.db
);
1242 transaction_status
= PQtransactionStatus(pset
.db
);
1244 if (transaction_status
== PQTRANS_IDLE
&&
1246 !command_no_begin(query
))
1248 results
= PQexec(pset
.db
, "BEGIN");
1249 if (PQresultStatus(results
) != PGRES_COMMAND_OK
)
1251 pg_log_info("%s", PQerrorMessage(pset
.db
));
1252 ClearOrSaveResult(results
);
1254 goto sendquery_cleanup
;
1256 ClearOrSaveResult(results
);
1257 transaction_status
= PQtransactionStatus(pset
.db
);
1260 if (transaction_status
== PQTRANS_INTRANS
&&
1261 pset
.on_error_rollback
!= PSQL_ERROR_ROLLBACK_OFF
&&
1262 (pset
.cur_cmd_interactive
||
1263 pset
.on_error_rollback
== PSQL_ERROR_ROLLBACK_ON
))
1265 if (on_error_rollback_warning
== false && pset
.sversion
< 80000)
1269 pg_log_warning("The server (version %s) does not support savepoints for ON_ERROR_ROLLBACK.",
1270 formatPGVersionNumber(pset
.sversion
, false,
1271 sverbuf
, sizeof(sverbuf
)));
1272 on_error_rollback_warning
= true;
1276 results
= PQexec(pset
.db
, "SAVEPOINT pg_psql_temporary_savepoint");
1277 if (PQresultStatus(results
) != PGRES_COMMAND_OK
)
1279 pg_log_info("%s", PQerrorMessage(pset
.db
));
1280 ClearOrSaveResult(results
);
1282 goto sendquery_cleanup
;
1284 ClearOrSaveResult(results
);
1285 on_error_rollback_savepoint
= true;
1289 if (pset
.gdesc_flag
)
1291 /* Describe query's result columns, without executing it */
1292 OK
= DescribeQuery(query
, &elapsed_msec
);
1294 results
= NULL
; /* PQclear(NULL) does nothing */
1296 else if (pset
.fetch_count
<= 0 || pset
.gexec_flag
||
1297 pset
.crosstab_flag
|| !is_select_command(query
))
1299 /* Default fetch-it-all-and-print mode */
1304 INSTR_TIME_SET_CURRENT(before
);
1306 results
= PQexec(pset
.db
, query
);
1308 /* these operations are included in the timing result: */
1310 OK
= ProcessResult(&results
);
1314 INSTR_TIME_SET_CURRENT(after
);
1315 INSTR_TIME_SUBTRACT(after
, before
);
1316 elapsed_msec
= INSTR_TIME_GET_MILLISEC(after
);
1319 /* but printing results isn't: */
1321 OK
= PrintQueryResults(results
);
1325 /* Fetch-in-segments mode */
1326 OK
= ExecQueryUsingCursor(query
, &elapsed_msec
);
1328 results
= NULL
; /* PQclear(NULL) does nothing */
1331 if (!OK
&& pset
.echo
== PSQL_ECHO_ERRORS
)
1332 pg_log_info("STATEMENT: %s", query
);
1334 /* If we made a temporary savepoint, possibly release/rollback */
1335 if (on_error_rollback_savepoint
)
1337 const char *svptcmd
= NULL
;
1339 transaction_status
= PQtransactionStatus(pset
.db
);
1341 switch (transaction_status
)
1343 case PQTRANS_INERROR
:
1344 /* We always rollback on an error */
1345 svptcmd
= "ROLLBACK TO pg_psql_temporary_savepoint";
1349 /* If they are no longer in a transaction, then do nothing */
1352 case PQTRANS_INTRANS
:
1355 * Do nothing if they are messing with savepoints themselves:
1356 * If the user did COMMIT AND CHAIN, RELEASE or ROLLBACK, our
1357 * savepoint is gone. If they issued a SAVEPOINT, releasing
1358 * ours would remove theirs.
1361 (strcmp(PQcmdStatus(results
), "COMMIT") == 0 ||
1362 strcmp(PQcmdStatus(results
), "SAVEPOINT") == 0 ||
1363 strcmp(PQcmdStatus(results
), "RELEASE") == 0 ||
1364 strcmp(PQcmdStatus(results
), "ROLLBACK") == 0))
1367 svptcmd
= "RELEASE pg_psql_temporary_savepoint";
1370 case PQTRANS_ACTIVE
:
1371 case PQTRANS_UNKNOWN
:
1374 /* PQTRANS_UNKNOWN is expected given a broken connection. */
1375 if (transaction_status
!= PQTRANS_UNKNOWN
|| ConnectionUp())
1376 pg_log_error("unexpected transaction status (%d)",
1377 transaction_status
);
1385 svptres
= PQexec(pset
.db
, svptcmd
);
1386 if (PQresultStatus(svptres
) != PGRES_COMMAND_OK
)
1388 pg_log_info("%s", PQerrorMessage(pset
.db
));
1389 ClearOrSaveResult(svptres
);
1394 goto sendquery_cleanup
;
1400 ClearOrSaveResult(results
);
1402 /* Possible microtiming output */
1404 PrintTiming(elapsed_msec
);
1406 /* check for events that may occur during query execution */
1408 if (pset
.encoding
!= PQclientEncoding(pset
.db
) &&
1409 PQclientEncoding(pset
.db
) >= 0)
1411 /* track effects of SET CLIENT_ENCODING */
1412 pset
.encoding
= PQclientEncoding(pset
.db
);
1413 pset
.popt
.topt
.encoding
= pset
.encoding
;
1414 SetVariable(pset
.vars
, "ENCODING",
1415 pg_encoding_to_char(pset
.encoding
));
1418 PrintNotifications();
1420 /* perform cleanup that should occur after any attempted query */
1424 /* reset \g's output-to-filename trigger */
1431 /* restore print settings if \g changed them */
1434 restorePsetInfo(&pset
.popt
, pset
.gsavepopt
);
1435 pset
.gsavepopt
= NULL
;
1438 /* reset \gset trigger */
1439 if (pset
.gset_prefix
)
1441 free(pset
.gset_prefix
);
1442 pset
.gset_prefix
= NULL
;
1445 /* reset \gdesc trigger */
1446 pset
.gdesc_flag
= false;
1448 /* reset \gexec trigger */
1449 pset
.gexec_flag
= false;
1451 /* reset \crosstabview trigger */
1452 pset
.crosstab_flag
= false;
1453 for (i
= 0; i
< lengthof(pset
.ctv_args
); i
++)
1455 pg_free(pset
.ctv_args
[i
]);
1456 pset
.ctv_args
[i
] = NULL
;
1464 * DescribeQuery: describe the result columns of a query, without executing it
1466 * Returns true if the operation executed successfully, false otherwise.
1468 * If pset.timing is on, total query time (exclusive of result-printing) is
1469 * stored into *elapsed_msec.
1472 DescribeQuery(const char *query
, double *elapsed_msec
)
1482 INSTR_TIME_SET_CURRENT(before
);
1485 * To parse the query but not execute it, we prepare it, using the unnamed
1486 * prepared statement. This is invisible to psql users, since there's no
1487 * way to access the unnamed prepared statement from psql user space. The
1488 * next Parse or Query protocol message would overwrite the statement
1489 * anyway. (So there's no great need to clear it when done, which is a
1490 * good thing because libpq provides no easy way to do that.)
1492 results
= PQprepare(pset
.db
, "", query
, 0, NULL
);
1493 if (PQresultStatus(results
) != PGRES_COMMAND_OK
)
1495 pg_log_info("%s", PQerrorMessage(pset
.db
));
1496 SetResultVariables(results
, false);
1497 ClearOrSaveResult(results
);
1502 results
= PQdescribePrepared(pset
.db
, "");
1503 OK
= AcceptResult(results
) &&
1504 (PQresultStatus(results
) == PGRES_COMMAND_OK
);
1507 if (PQnfields(results
) > 0)
1509 PQExpBufferData buf
;
1512 initPQExpBuffer(&buf
);
1514 printfPQExpBuffer(&buf
,
1515 "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1517 gettext_noop("Column"),
1518 gettext_noop("Type"));
1520 for (i
= 0; i
< PQnfields(results
); i
++)
1526 appendPQExpBufferStr(&buf
, ",");
1528 name
= PQfname(results
, i
);
1529 escname
= PQescapeLiteral(pset
.db
, name
, strlen(name
));
1531 if (escname
== NULL
)
1533 pg_log_info("%s", PQerrorMessage(pset
.db
));
1535 termPQExpBuffer(&buf
);
1539 appendPQExpBuffer(&buf
, "(%s, '%u'::pg_catalog.oid, %d)",
1541 PQftype(results
, i
),
1542 PQfmod(results
, i
));
1547 appendPQExpBufferStr(&buf
, ") s(name, tp, tpm)");
1550 results
= PQexec(pset
.db
, buf
.data
);
1551 OK
= AcceptResult(results
);
1555 INSTR_TIME_SET_CURRENT(after
);
1556 INSTR_TIME_SUBTRACT(after
, before
);
1557 *elapsed_msec
+= INSTR_TIME_GET_MILLISEC(after
);
1561 OK
= PrintQueryResults(results
);
1563 termPQExpBuffer(&buf
);
1566 fprintf(pset
.queryFout
,
1567 _("The command has no result, or the result has no columns.\n"));
1570 SetResultVariables(results
, OK
);
1571 ClearOrSaveResult(results
);
1578 * ExecQueryUsingCursor: run a SELECT-like query using a cursor
1580 * This feature allows result sets larger than RAM to be dealt with.
1582 * Returns true if the query executed successfully, false otherwise.
1584 * If pset.timing is on, total query time (exclusive of result-printing) is
1585 * stored into *elapsed_msec.
1588 ExecQueryUsingCursor(const char *query
, double *elapsed_msec
)
1592 PQExpBufferData buf
;
1593 printQueryOpt my_popt
= pset
.popt
;
1596 bool is_pager
= false;
1597 bool started_txn
= false;
1598 int64 total_tuples
= 0;
1608 /* initialize print options for partial table output */
1609 my_popt
.topt
.start_table
= true;
1610 my_popt
.topt
.stop_table
= false;
1611 my_popt
.topt
.prior_records
= 0;
1614 INSTR_TIME_SET_CURRENT(before
);
1616 /* if we're not in a transaction, start one */
1617 if (PQtransactionStatus(pset
.db
) == PQTRANS_IDLE
)
1619 results
= PQexec(pset
.db
, "BEGIN");
1620 OK
= AcceptResult(results
) &&
1621 (PQresultStatus(results
) == PGRES_COMMAND_OK
);
1622 ClearOrSaveResult(results
);
1628 /* Send DECLARE CURSOR */
1629 initPQExpBuffer(&buf
);
1630 appendPQExpBuffer(&buf
, "DECLARE _psql_cursor NO SCROLL CURSOR FOR\n%s",
1633 results
= PQexec(pset
.db
, buf
.data
);
1634 OK
= AcceptResult(results
) &&
1635 (PQresultStatus(results
) == PGRES_COMMAND_OK
);
1637 SetResultVariables(results
, OK
);
1638 ClearOrSaveResult(results
);
1639 termPQExpBuffer(&buf
);
1645 INSTR_TIME_SET_CURRENT(after
);
1646 INSTR_TIME_SUBTRACT(after
, before
);
1647 *elapsed_msec
+= INSTR_TIME_GET_MILLISEC(after
);
1651 * In \gset mode, we force the fetch count to be 2, so that we will throw
1652 * the appropriate error if the query returns more than one row.
1654 if (pset
.gset_prefix
)
1657 fetch_count
= pset
.fetch_count
;
1659 snprintf(fetch_cmd
, sizeof(fetch_cmd
),
1660 "FETCH FORWARD %d FROM _psql_cursor",
1663 /* prepare to write output to \g argument, if any */
1666 if (!openQueryOutputFile(pset
.gfname
, &fout
, &is_pipe
))
1672 disable_sigpipe_trap();
1676 fout
= pset
.queryFout
;
1677 is_pipe
= false; /* doesn't matter */
1680 /* clear any pre-existing error indication on the output stream */
1686 INSTR_TIME_SET_CURRENT(before
);
1688 /* get fetch_count tuples at a time */
1689 results
= PQexec(pset
.db
, fetch_cmd
);
1693 INSTR_TIME_SET_CURRENT(after
);
1694 INSTR_TIME_SUBTRACT(after
, before
);
1695 *elapsed_msec
+= INSTR_TIME_GET_MILLISEC(after
);
1698 if (PQresultStatus(results
) != PGRES_TUPLES_OK
)
1700 /* shut down pager before printing error message */
1707 OK
= AcceptResult(results
);
1709 SetResultVariables(results
, OK
);
1710 ClearOrSaveResult(results
);
1714 if (pset
.gset_prefix
)
1716 /* StoreQueryTuple will complain if not exactly one row */
1717 OK
= StoreQueryTuple(results
);
1718 ClearOrSaveResult(results
);
1723 * Note we do not deal with \gdesc, \gexec or \crosstabview modes here
1726 ntuples
= PQntuples(results
);
1727 total_tuples
+= ntuples
;
1729 if (ntuples
< fetch_count
)
1731 /* this is the last result set, so allow footer decoration */
1732 my_popt
.topt
.stop_table
= true;
1734 else if (fout
== stdout
&& !is_pager
)
1737 * If query requires multiple result sets, hack to ensure that
1738 * only one pager instance is used for the whole mess
1740 fout
= PageOutput(INT_MAX
, &(my_popt
.topt
));
1744 printQuery(results
, &my_popt
, fout
, is_pager
, pset
.logfile
);
1746 ClearOrSaveResult(results
);
1748 /* after the first result set, disallow header decoration */
1749 my_popt
.topt
.start_table
= false;
1750 my_popt
.topt
.prior_records
+= ntuples
;
1753 * Make sure to flush the output stream, so intermediate results are
1754 * visible to the client immediately. We check the results because if
1755 * the pager dies/exits/etc, there's no sense throwing more data at
1758 flush_error
= fflush(fout
);
1761 * Check if we are at the end, if a cancel was pressed, or if there
1762 * were any errors either trying to flush out the results, or more
1763 * generally on the output stream at all. If we hit any errors
1764 * writing things to the stream, we presume $PAGER has disappeared and
1765 * stop bothering to pull down more data.
1767 if (ntuples
< fetch_count
|| cancel_pressed
|| flush_error
||
1774 /* close \g argument file/pipe */
1778 restore_sigpipe_trap();
1785 /* close transient pager */
1792 * We don't have a PGresult here, and even if we did it wouldn't have
1793 * the right row count, so fake SetResultVariables(). In error cases,
1794 * we already set the result variables above.
1798 SetVariable(pset
.vars
, "ERROR", "false");
1799 SetVariable(pset
.vars
, "SQLSTATE", "00000");
1800 snprintf(buf
, sizeof(buf
), INT64_FORMAT
, total_tuples
);
1801 SetVariable(pset
.vars
, "ROW_COUNT", buf
);
1806 INSTR_TIME_SET_CURRENT(before
);
1809 * We try to close the cursor on either success or failure, but on failure
1810 * ignore the result (it's probably just a bleat about being in an aborted
1813 results
= PQexec(pset
.db
, "CLOSE _psql_cursor");
1816 OK
= AcceptResult(results
) &&
1817 (PQresultStatus(results
) == PGRES_COMMAND_OK
);
1818 ClearOrSaveResult(results
);
1825 results
= PQexec(pset
.db
, OK
? "COMMIT" : "ROLLBACK");
1826 OK
&= AcceptResult(results
) &&
1827 (PQresultStatus(results
) == PGRES_COMMAND_OK
);
1828 ClearOrSaveResult(results
);
1833 INSTR_TIME_SET_CURRENT(after
);
1834 INSTR_TIME_SUBTRACT(after
, before
);
1835 *elapsed_msec
+= INSTR_TIME_GET_MILLISEC(after
);
1843 * Advance the given char pointer over white space and SQL comments.
1846 skip_white_space(const char *query
)
1848 int cnestlevel
= 0; /* slash-star comment nest level */
1852 int mblen
= PQmblenBounded(query
, pset
.encoding
);
1855 * Note: we assume the encoding is a superset of ASCII, so that for
1856 * example "query[0] == '/'" is meaningful. However, we do NOT assume
1857 * that the second and subsequent bytes of a multibyte character
1858 * couldn't look like ASCII characters; so it is critical to advance
1859 * by mblen, not 1, whenever we haven't exactly identified the
1860 * character we are skipping over.
1862 if (isspace((unsigned char) *query
))
1864 else if (query
[0] == '/' && query
[1] == '*')
1869 else if (cnestlevel
> 0 && query
[0] == '*' && query
[1] == '/')
1874 else if (cnestlevel
== 0 && query
[0] == '-' && query
[1] == '-')
1879 * We have to skip to end of line since any slash-star inside the
1880 * -- comment does NOT start a slash-star comment.
1889 query
+= PQmblenBounded(query
, pset
.encoding
);
1892 else if (cnestlevel
> 0)
1895 break; /* found first token */
1903 * Check whether a command is one of those for which we should NOT start
1904 * a new transaction block (ie, send a preceding BEGIN).
1906 * These include the transaction control statements themselves, plus
1907 * certain statements that the backend disallows inside transaction blocks.
1910 command_no_begin(const char *query
)
1915 * First we must advance over any whitespace and comments.
1917 query
= skip_white_space(query
);
1920 * Check word length (since "beginx" is not "begin").
1923 while (isalpha((unsigned char) query
[wordlen
]))
1924 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
1927 * Transaction control commands. These should include every keyword that
1928 * gives rise to a TransactionStmt in the backend grammar, except for the
1929 * savepoint-related commands.
1931 * (We assume that START must be START TRANSACTION, since there is
1932 * presently no other "START foo" command.)
1934 if (wordlen
== 5 && pg_strncasecmp(query
, "abort", 5) == 0)
1936 if (wordlen
== 5 && pg_strncasecmp(query
, "begin", 5) == 0)
1938 if (wordlen
== 5 && pg_strncasecmp(query
, "start", 5) == 0)
1940 if (wordlen
== 6 && pg_strncasecmp(query
, "commit", 6) == 0)
1942 if (wordlen
== 3 && pg_strncasecmp(query
, "end", 3) == 0)
1944 if (wordlen
== 8 && pg_strncasecmp(query
, "rollback", 8) == 0)
1946 if (wordlen
== 7 && pg_strncasecmp(query
, "prepare", 7) == 0)
1948 /* PREPARE TRANSACTION is a TC command, PREPARE foo is not */
1951 query
= skip_white_space(query
);
1954 while (isalpha((unsigned char) query
[wordlen
]))
1955 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
1957 if (wordlen
== 11 && pg_strncasecmp(query
, "transaction", 11) == 0)
1963 * Commands not allowed within transactions. The statements checked for
1964 * here should be exactly those that call PreventInTransactionBlock() in
1967 if (wordlen
== 6 && pg_strncasecmp(query
, "vacuum", 6) == 0)
1969 if (wordlen
== 7 && pg_strncasecmp(query
, "cluster", 7) == 0)
1971 /* CLUSTER with any arguments is allowed in transactions */
1974 query
= skip_white_space(query
);
1976 if (isalpha((unsigned char) query
[0]))
1977 return false; /* has additional words */
1978 return true; /* it's CLUSTER without arguments */
1981 if (wordlen
== 6 && pg_strncasecmp(query
, "create", 6) == 0)
1985 query
= skip_white_space(query
);
1988 while (isalpha((unsigned char) query
[wordlen
]))
1989 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
1991 if (wordlen
== 8 && pg_strncasecmp(query
, "database", 8) == 0)
1993 if (wordlen
== 10 && pg_strncasecmp(query
, "tablespace", 10) == 0)
1996 /* CREATE [UNIQUE] INDEX CONCURRENTLY isn't allowed in xacts */
1997 if (wordlen
== 6 && pg_strncasecmp(query
, "unique", 6) == 0)
2001 query
= skip_white_space(query
);
2004 while (isalpha((unsigned char) query
[wordlen
]))
2005 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
2008 if (wordlen
== 5 && pg_strncasecmp(query
, "index", 5) == 0)
2012 query
= skip_white_space(query
);
2015 while (isalpha((unsigned char) query
[wordlen
]))
2016 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
2018 if (wordlen
== 12 && pg_strncasecmp(query
, "concurrently", 12) == 0)
2025 if (wordlen
== 5 && pg_strncasecmp(query
, "alter", 5) == 0)
2029 query
= skip_white_space(query
);
2032 while (isalpha((unsigned char) query
[wordlen
]))
2033 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
2035 /* ALTER SYSTEM isn't allowed in xacts */
2036 if (wordlen
== 6 && pg_strncasecmp(query
, "system", 6) == 0)
2043 * Note: these tests will match DROP SYSTEM and REINDEX TABLESPACE, which
2044 * aren't really valid commands so we don't care much. The other four
2045 * possible matches are correct.
2047 if ((wordlen
== 4 && pg_strncasecmp(query
, "drop", 4) == 0) ||
2048 (wordlen
== 7 && pg_strncasecmp(query
, "reindex", 7) == 0))
2052 query
= skip_white_space(query
);
2055 while (isalpha((unsigned char) query
[wordlen
]))
2056 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
2058 if (wordlen
== 8 && pg_strncasecmp(query
, "database", 8) == 0)
2060 if (wordlen
== 6 && pg_strncasecmp(query
, "system", 6) == 0)
2062 if (wordlen
== 10 && pg_strncasecmp(query
, "tablespace", 10) == 0)
2064 if (wordlen
== 5 && (pg_strncasecmp(query
, "index", 5) == 0 ||
2065 pg_strncasecmp(query
, "table", 5) == 0))
2068 query
= skip_white_space(query
);
2070 while (isalpha((unsigned char) query
[wordlen
]))
2071 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
2074 * REINDEX [ TABLE | INDEX ] CONCURRENTLY are not allowed in
2077 if (wordlen
== 12 && pg_strncasecmp(query
, "concurrently", 12) == 0)
2081 /* DROP INDEX CONCURRENTLY isn't allowed in xacts */
2082 if (wordlen
== 5 && pg_strncasecmp(query
, "index", 5) == 0)
2086 query
= skip_white_space(query
);
2089 while (isalpha((unsigned char) query
[wordlen
]))
2090 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
2092 if (wordlen
== 12 && pg_strncasecmp(query
, "concurrently", 12) == 0)
2101 /* DISCARD ALL isn't allowed in xacts, but other variants are allowed. */
2102 if (wordlen
== 7 && pg_strncasecmp(query
, "discard", 7) == 0)
2106 query
= skip_white_space(query
);
2109 while (isalpha((unsigned char) query
[wordlen
]))
2110 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
2112 if (wordlen
== 3 && pg_strncasecmp(query
, "all", 3) == 0)
2122 * Check whether the specified command is a SELECT (or VALUES).
2125 is_select_command(const char *query
)
2130 * First advance over any whitespace, comments and left parentheses.
2134 query
= skip_white_space(query
);
2135 if (query
[0] == '(')
2142 * Check word length (since "selectx" is not "select").
2145 while (isalpha((unsigned char) query
[wordlen
]))
2146 wordlen
+= PQmblenBounded(&query
[wordlen
], pset
.encoding
);
2148 if (wordlen
== 6 && pg_strncasecmp(query
, "select", 6) == 0)
2151 if (wordlen
== 6 && pg_strncasecmp(query
, "values", 6) == 0)
2159 * Test if the current user is a database superuser.
2169 val
= PQparameterStatus(pset
.db
, "is_superuser");
2171 if (val
&& strcmp(val
, "on") == 0)
2179 * Test if the current session uses standard string literals.
2182 standard_strings(void)
2189 val
= PQparameterStatus(pset
.db
, "standard_conforming_strings");
2191 if (val
&& strcmp(val
, "on") == 0)
2199 * Return the session user of the current connection.
2202 session_username(void)
2209 val
= PQparameterStatus(pset
.db
, "session_authorization");
2213 return PQuser(pset
.db
);
2219 * substitute '~' with HOME or '~username' with username's home dir
2223 expand_tilde(char **filename
)
2225 if (!filename
|| !(*filename
))
2229 * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2230 * for short versions of long file names, though the tilde is usually
2231 * toward the end, not at the beginning.
2235 /* try tilde expansion */
2236 if (**filename
== '~')
2242 char home
[MAXPGPATH
];
2248 while (*p
!= '/' && *p
!= '\0')
2254 if (*(fn
+ 1) == '\0')
2255 get_home_path(home
); /* ~ or ~/ only */
2256 else if ((pw
= getpwnam(fn
+ 1)) != NULL
)
2257 strlcpy(home
, pw
->pw_dir
, sizeof(home
)); /* ~user */
2260 if (strlen(home
) != 0)
2264 newfn
= psprintf("%s%s", home
, p
);
2273 * Checks if connection string starts with either of the valid URI prefix
2276 * Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
2278 * XXX This is a duplicate of the eponymous libpq function.
2281 uri_prefix_length(const char *connstr
)
2283 /* The connection URI must start with either of the following designators: */
2284 static const char uri_designator
[] = "postgresql://";
2285 static const char short_uri_designator
[] = "postgres://";
2287 if (strncmp(connstr
, uri_designator
,
2288 sizeof(uri_designator
) - 1) == 0)
2289 return sizeof(uri_designator
) - 1;
2291 if (strncmp(connstr
, short_uri_designator
,
2292 sizeof(short_uri_designator
) - 1) == 0)
2293 return sizeof(short_uri_designator
) - 1;
2299 * Recognized connection string either starts with a valid URI prefix or
2300 * contains a "=" in it.
2302 * Must be consistent with parse_connection_string: anything for which this
2303 * returns true should at least look like it's parseable by that routine.
2305 * XXX This is a duplicate of the eponymous libpq function.
2308 recognized_connection_string(const char *connstr
)
2310 return uri_prefix_length(connstr
) != 0 || strchr(connstr
, '=') != NULL
;