PostgreSQL Source Code git master
common.c
Go to the documentation of this file.
1/*
2 * psql - the PostgreSQL interactive terminal
3 *
4 * Copyright (c) 2000-2025, PostgreSQL Global Development Group
5 *
6 * src/bin/psql/common.c
7 */
8#include "postgres_fe.h"
9
10#include <ctype.h>
11#include <limits.h>
12#include <math.h>
13#include <pwd.h>
14#include <signal.h>
15#ifndef WIN32
16#include <unistd.h> /* for write() */
17#else
18#include <io.h> /* for _write() */
19#include <win32.h>
20#endif
21
22#include "command.h"
23#include "common.h"
24#include "common/logging.h"
25#include "copy.h"
26#include "crosstabview.h"
27#include "fe_utils/cancel.h"
28#include "fe_utils/mbprint.h"
31#include "settings.h"
32
33static bool DescribeQuery(const char *query, double *elapsed_msec);
34static int ExecQueryAndProcessResults(const char *query,
35 double *elapsed_msec,
36 bool *svpt_gone_p,
37 bool is_watch,
38 int min_rows,
39 const printQueryOpt *opt,
40 FILE *printQueryFout);
41static bool command_no_begin(const char *query);
42
43
44/*
45 * openQueryOutputFile --- attempt to open a query output file
46 *
47 * fname == NULL selects stdout, else an initial '|' selects a pipe,
48 * else plain file.
49 *
50 * Returns output file pointer into *fout, and is-a-pipe flag into *is_pipe.
51 * Caller is responsible for adjusting SIGPIPE state if it's a pipe.
52 *
53 * On error, reports suitable error message and returns false.
54 */
55bool
56openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
57{
58 if (!fname || fname[0] == '\0')
59 {
60 *fout = stdout;
61 *is_pipe = false;
62 }
63 else if (*fname == '|')
64 {
65 fflush(NULL);
66 *fout = popen(fname + 1, "w");
67 *is_pipe = true;
68 }
69 else
70 {
71 *fout = fopen(fname, "w");
72 *is_pipe = false;
73 }
74
75 if (*fout == NULL)
76 {
77 pg_log_error("%s: %m", fname);
78 return false;
79 }
80
81 return true;
82}
83
84/*
85 * Check if an output stream for \g needs to be opened, and if yes,
86 * open it and update the caller's gfile_fout and is_pipe state variables.
87 * Return true if OK, false if an error occurred.
88 */
89static bool
90SetupGOutput(FILE **gfile_fout, bool *is_pipe)
91{
92 /* If there is a \g file or program, and it's not already open, open it */
93 if (pset.gfname != NULL && *gfile_fout == NULL)
94 {
95 if (openQueryOutputFile(pset.gfname, gfile_fout, is_pipe))
96 {
97 if (*is_pipe)
99 }
100 else
101 return false;
102 }
103 return true;
104}
105
106/*
107 * Close the output stream for \g, if we opened it.
108 */
109static void
110CloseGOutput(FILE *gfile_fout, bool is_pipe)
111{
112 if (gfile_fout)
113 {
114 if (is_pipe)
115 {
116 SetShellResultVariables(pclose(gfile_fout));
118 }
119 else
120 fclose(gfile_fout);
121 }
122}
123
124/*
125 * setQFout
126 * -- handler for -o command line option and \o command
127 *
128 * On success, updates pset with the new output file and returns true.
129 * On failure, returns false without changing pset state.
130 */
131bool
132setQFout(const char *fname)
133{
134 FILE *fout;
135 bool is_pipe;
136
137 /* First make sure we can open the new output file/pipe */
138 if (!openQueryOutputFile(fname, &fout, &is_pipe))
139 return false;
140
141 /* Close old file/pipe */
142 if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
143 {
146 else
147 fclose(pset.queryFout);
148 }
149
150 pset.queryFout = fout;
151 pset.queryFoutPipe = is_pipe;
152
153 /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
154 set_sigpipe_trap_state(is_pipe);
156
157 return true;
158}
159
160
161/*
162 * Variable-fetching callback for flex lexer
163 *
164 * If the specified variable exists, return its value as a string (malloc'd
165 * and expected to be freed by the caller); else return NULL.
166 *
167 * If "quote" isn't PQUOTE_PLAIN, then return the value suitably quoted and
168 * escaped for the specified quoting requirement. (Failure in escaping
169 * should lead to printing an error and returning NULL.)
170 *
171 * "passthrough" is the pointer previously given to psql_scan_set_passthrough.
172 * In psql, passthrough points to a ConditionalStack, which we check to
173 * determine whether variable expansion is allowed.
174 */
175char *
176psql_get_variable(const char *varname, PsqlScanQuoteType quote,
177 void *passthrough)
178{
179 char *result = NULL;
180 const char *value;
181
182 /* In an inactive \if branch, suppress all variable substitutions */
183 if (passthrough && !conditional_active((ConditionalStack) passthrough))
184 return NULL;
185
186 value = GetVariable(pset.vars, varname);
187 if (!value)
188 return NULL;
189
190 switch (quote)
191 {
192 case PQUOTE_PLAIN:
193 result = pg_strdup(value);
194 break;
196 case PQUOTE_SQL_IDENT:
197 {
198 /*
199 * For these cases, we use libpq's quoting functions, which
200 * assume the string is in the connection's client encoding.
201 */
202 char *escaped_value;
203
204 if (!pset.db)
205 {
206 pg_log_error("cannot escape without active connection");
207 return NULL;
208 }
209
210 if (quote == PQUOTE_SQL_LITERAL)
211 escaped_value =
212 PQescapeLiteral(pset.db, value, strlen(value));
213 else
214 escaped_value =
216
217 if (escaped_value == NULL)
218 {
219 const char *error = PQerrorMessage(pset.db);
220
221 pg_log_info("%s", error);
222 return NULL;
223 }
224
225 /*
226 * Rather than complicate the lexer's API with a notion of
227 * which free() routine to use, just pay the price of an extra
228 * strdup().
229 */
230 result = pg_strdup(escaped_value);
231 PQfreemem(escaped_value);
232 break;
233 }
234 case PQUOTE_SHELL_ARG:
235 {
236 /*
237 * For this we use appendShellStringNoError, which is
238 * encoding-agnostic, which is fine since the shell probably
239 * is too. In any case, the only special character is "'",
240 * which is not known to appear in valid multibyte characters.
241 */
243
246 {
247 pg_log_error("shell command argument contains a newline or carriage return: \"%s\"",
248 value);
249 free(buf.data);
250 return NULL;
251 }
252 result = buf.data;
253 break;
254 }
255
256 /* No default: we want a compiler warning for missing cases */
257 }
258
259 return result;
260}
261
262
263/*
264 * for backend Notice messages (INFO, WARNING, etc)
265 */
266void
267NoticeProcessor(void *arg, const char *message)
268{
269 (void) arg; /* not used */
270 pg_log_info("%s", message);
271}
272
273
274
275/*
276 * Code to support query cancellation
277 *
278 * Before we start a query, we enable the SIGINT signal catcher to send a
279 * cancel request to the backend.
280 *
281 * SIGINT is supposed to abort all long-running psql operations, not only
282 * database queries. In most places, this is accomplished by checking
283 * cancel_pressed during long-running loops. However, that won't work when
284 * blocked on user input (in readline() or fgets()). In those places, we
285 * set sigint_interrupt_enabled true while blocked, instructing the signal
286 * catcher to longjmp through sigint_interrupt_jmp. We assume readline and
287 * fgets are coded to handle possible interruption.
288 *
289 * On Windows, currently this does not work, so control-C is less useful
290 * there.
291 */
292volatile sig_atomic_t sigint_interrupt_enabled = false;
293
295
296static void
298{
299#ifndef WIN32
300 /* if we are waiting for input, longjmp out of it */
302 {
304 siglongjmp(sigint_interrupt_jmp, 1);
305 }
306#endif
307
308 /* else, set cancel flag to stop any long-running loops */
309 cancel_pressed = true;
310}
311
312void
314{
316}
317
318
319/* ConnectionUp
320 *
321 * Returns whether our backend connection is still there.
322 */
323static bool
325{
326 return PQstatus(pset.db) != CONNECTION_BAD;
327}
328
329
330
331/* CheckConnection
332 *
333 * Verify that we still have a good connection to the backend, and if not,
334 * see if it can be restored.
335 *
336 * Returns true if either the connection was still there, or it could be
337 * restored successfully; false otherwise. If, however, there was no
338 * connection and the session is non-interactive, this will exit the program
339 * with a code of EXIT_BADCONN.
340 */
341static bool
343{
344 bool OK;
345
346 OK = ConnectionUp();
347 if (!OK)
348 {
350 {
351 pg_log_error("connection to server was lost");
353 }
354
355 fprintf(stderr, _("The connection to the server was lost. Attempting reset: "));
356 PQreset(pset.db);
357 OK = ConnectionUp();
358 if (!OK)
359 {
360 fprintf(stderr, _("Failed.\n"));
361
362 /*
363 * Transition to having no connection; but stash away the failed
364 * connection so that we can still refer to its parameters in a
365 * later \connect attempt. Keep the state cleanup here in sync
366 * with do_connect().
367 */
368 if (pset.dead_conn)
371 pset.db = NULL;
374 }
375 else
376 {
377 fprintf(stderr, _("Succeeded.\n"));
378
379 /*
380 * Re-sync, just in case anything changed. Keep this in sync with
381 * do_connect().
382 */
384 connection_warnings(false); /* Must be after SyncVariables */
385 }
386 }
387
388 return OK;
389}
390
391
392
393
394/*
395 * AcceptResult
396 *
397 * Checks whether a result is valid, giving an error message if necessary;
398 * and ensures that the connection to the backend is still up.
399 *
400 * Returns true for valid result, false for error state.
401 */
402static bool
403AcceptResult(const PGresult *result, bool show_error)
404{
405 bool OK;
406
407 if (!result)
408 OK = false;
409 else
410 switch (PQresultStatus(result))
411 {
412 case PGRES_COMMAND_OK:
413 case PGRES_TUPLES_OK:
416 case PGRES_COPY_IN:
417 case PGRES_COPY_OUT:
418 /* Fine, do nothing */
419 OK = true;
420 break;
421
425 OK = false;
426 break;
427
428 default:
429 OK = false;
430 pg_log_error("unexpected PQresultStatus: %d",
431 PQresultStatus(result));
432 break;
433 }
434
435 if (!OK && show_error)
436 {
437 const char *error = PQerrorMessage(pset.db);
438
439 if (strlen(error))
440 pg_log_info("%s", error);
441
443 }
444
445 return OK;
446}
447
448
449/*
450 * Set special variables from a query result
451 * - ERROR: true/false, whether an error occurred on this query
452 * - SQLSTATE: code of error, or "00000" if no error, or "" if unknown
453 * - ROW_COUNT: how many rows were returned or affected, or "0"
454 * - LAST_ERROR_SQLSTATE: same for last error
455 * - LAST_ERROR_MESSAGE: message of last error
456 *
457 * Note: current policy is to apply this only to the results of queries
458 * entered by the user, not queries generated by slash commands.
459 */
460static void
462{
463 if (success)
464 {
465 const char *ntuples = PQcmdTuples(result);
466
467 SetVariable(pset.vars, "ERROR", "false");
468 SetVariable(pset.vars, "SQLSTATE", "00000");
469 SetVariable(pset.vars, "ROW_COUNT", *ntuples ? ntuples : "0");
470 }
471 else
472 {
473 const char *code = PQresultErrorField(result, PG_DIAG_SQLSTATE);
474 const char *mesg = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
475
476 SetVariable(pset.vars, "ERROR", "true");
477
478 /*
479 * If there is no SQLSTATE code, use an empty string. This can happen
480 * for libpq-detected errors (e.g., lost connection, ENOMEM).
481 */
482 if (code == NULL)
483 code = "";
484 SetVariable(pset.vars, "SQLSTATE", code);
485 SetVariable(pset.vars, "ROW_COUNT", "0");
486 SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", code);
487 SetVariable(pset.vars, "LAST_ERROR_MESSAGE", mesg ? mesg : "");
488 }
489}
490
491
492/*
493 * Set special variables from a shell command result
494 * - SHELL_ERROR: true/false, whether command returned exit code 0
495 * - SHELL_EXIT_CODE: exit code according to shell conventions
496 *
497 * The argument is a wait status as returned by wait(2) or waitpid(2),
498 * which also applies to pclose(3) and system(3).
499 */
500void
502{
503 char buf[32];
504
505 SetVariable(pset.vars, "SHELL_ERROR",
506 (wait_result == 0) ? "false" : "true");
507 snprintf(buf, sizeof(buf), "%d", wait_result_to_exit_code(wait_result));
508 SetVariable(pset.vars, "SHELL_EXIT_CODE", buf);
509}
510
511
512/*
513 * ClearOrSaveResult
514 *
515 * If the result represents an error, remember it for possible display by
516 * \errverbose. Otherwise, just PQclear() it.
517 *
518 * Note: current policy is to apply this to the results of all queries,
519 * including "back door" queries, for debugging's sake. It's OK to use
520 * PQclear() directly on results known to not be error results, however.
521 */
522static void
524{
525 if (result)
526 {
527 switch (PQresultStatus(result))
528 {
532 pset.last_error_result = result;
533 break;
534
535 default:
536 PQclear(result);
537 break;
538 }
539 }
540}
541
542
543/*
544 * Consume all results
545 */
546static void
548{
549 PGresult *result;
550
551 while ((result = PQgetResult(pset.db)) != NULL)
552 ClearOrSaveResult(result);
553}
554
555
556/*
557 * Print microtiming output. Always print raw milliseconds; if the interval
558 * is >= 1 second, also break it down into days/hours/minutes/seconds.
559 */
560static void
561PrintTiming(double elapsed_msec)
562{
563 double seconds;
564 double minutes;
565 double hours;
566 double days;
567
568 if (elapsed_msec < 1000.0)
569 {
570 /* This is the traditional (pre-v10) output format */
571 printf(_("Time: %.3f ms\n"), elapsed_msec);
572 return;
573 }
574
575 /*
576 * Note: we could print just seconds, in a format like %06.3f, when the
577 * total is less than 1min. But that's hard to interpret unless we tack
578 * on "s" or otherwise annotate it. Forcing the display to include
579 * minutes seems like a better solution.
580 */
581 seconds = elapsed_msec / 1000.0;
582 minutes = floor(seconds / 60.0);
583 seconds -= 60.0 * minutes;
584 if (minutes < 60.0)
585 {
586 printf(_("Time: %.3f ms (%02d:%06.3f)\n"),
587 elapsed_msec, (int) minutes, seconds);
588 return;
589 }
590
591 hours = floor(minutes / 60.0);
592 minutes -= 60.0 * hours;
593 if (hours < 24.0)
594 {
595 printf(_("Time: %.3f ms (%02d:%02d:%06.3f)\n"),
596 elapsed_msec, (int) hours, (int) minutes, seconds);
597 return;
598 }
599
600 days = floor(hours / 24.0);
601 hours -= 24.0 * days;
602 printf(_("Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"),
603 elapsed_msec, days, (int) hours, (int) minutes, seconds);
604}
605
606
607/*
608 * PSQLexec
609 *
610 * This is the way to send "backdoor" queries (those not directly entered
611 * by the user). It is subject to -E but not -e.
612 *
613 * Caller is responsible for handling the ensuing processing if a COPY
614 * command is sent.
615 *
616 * Note: we don't bother to check PQclientEncoding; it is assumed that no
617 * caller uses this path to issue "SET CLIENT_ENCODING".
618 */
619PGresult *
620PSQLexec(const char *query)
621{
622 PGresult *res;
623
624 if (!pset.db)
625 {
626 pg_log_error("You are currently not connected to a database.");
627 return NULL;
628 }
629
631 {
632 printf(_("/******** QUERY *********/\n"
633 "%s\n"
634 "/************************/\n\n"), query);
635 fflush(stdout);
636 if (pset.logfile)
637 {
639 _("/******** QUERY *********/\n"
640 "%s\n"
641 "/************************/\n\n"), query);
643 }
644
646 return NULL;
647 }
648
650
651 res = PQexec(pset.db, query);
652
654
655 if (!AcceptResult(res, true))
656 {
658 res = NULL;
659 }
660
661 return res;
662}
663
664
665/*
666 * PSQLexecWatch
667 *
668 * This function is used for \watch command to send the query to
669 * the server and print out the result.
670 *
671 * Returns 1 if the query executed successfully, 0 if it cannot be repeated,
672 * e.g., because of the interrupt, -1 on error.
673 */
674int
675PSQLexecWatch(const char *query, const printQueryOpt *opt, FILE *printQueryFout, int min_rows)
676{
677 bool timing = pset.timing;
678 double elapsed_msec = 0;
679 int res;
680
681 if (!pset.db)
682 {
683 pg_log_error("You are currently not connected to a database.");
684 return 0;
685 }
686
688
689 res = ExecQueryAndProcessResults(query, &elapsed_msec, NULL, true, min_rows, opt, printQueryFout);
690
692
693 /* Possible microtiming output */
694 if (timing)
695 PrintTiming(elapsed_msec);
696
697 return res;
698}
699
700
701/*
702 * PrintNotifications: check for asynchronous notifications, and print them out
703 */
704static void
706{
707 PGnotify *notify;
708
710 while ((notify = PQnotifies(pset.db)) != NULL)
711 {
712 /* for backward compatibility, only show payload if nonempty */
713 if (notify->extra[0])
714 fprintf(pset.queryFout, _("Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
715 notify->relname, notify->extra, notify->be_pid);
716 else
717 fprintf(pset.queryFout, _("Asynchronous notification \"%s\" received from server process with PID %d.\n"),
718 notify->relname, notify->be_pid);
720 PQfreemem(notify);
722 }
723}
724
725
726/*
727 * PrintQueryTuples: assuming query result is OK, print its tuples
728 *
729 * We use the options given by opt unless that's NULL, in which case
730 * we use pset.popt.
731 *
732 * Output is to printQueryFout unless that's NULL, in which case
733 * we use pset.queryFout.
734 *
735 * Returns true if successful, false otherwise.
736 */
737static bool
738PrintQueryTuples(const PGresult *result, const printQueryOpt *opt,
739 FILE *printQueryFout)
740{
741 bool ok = true;
742 FILE *fout = printQueryFout ? printQueryFout : pset.queryFout;
743
744 printQuery(result, opt ? opt : &pset.popt, fout, false, pset.logfile);
745 fflush(fout);
746 if (ferror(fout))
747 {
748 pg_log_error("could not print result table: %m");
749 ok = false;
750 }
751
752 return ok;
753}
754
755
756/*
757 * StoreQueryTuple: assuming query result is OK, save data into variables
758 *
759 * Returns true if successful, false otherwise.
760 */
761static bool
763{
764 bool success = true;
765
766 if (PQntuples(result) < 1)
767 {
768 pg_log_error("no rows returned for \\gset");
769 success = false;
770 }
771 else if (PQntuples(result) > 1)
772 {
773 pg_log_error("more than one row returned for \\gset");
774 success = false;
775 }
776 else
777 {
778 int i;
779
780 for (i = 0; i < PQnfields(result); i++)
781 {
782 char *colname = PQfname(result, i);
783 char *varname;
784 char *value;
785
786 /* concatenate prefix and column name */
787 varname = psprintf("%s%s", pset.gset_prefix, colname);
788
789 if (VariableHasHook(pset.vars, varname))
790 {
791 pg_log_warning("attempt to \\gset into specially treated variable \"%s\" ignored",
792 varname);
793 continue;
794 }
795
796 if (!PQgetisnull(result, 0, i))
797 value = PQgetvalue(result, 0, i);
798 else
799 {
800 /* for NULL value, unset rather than set the variable */
801 value = NULL;
802 }
803
804 if (!SetVariable(pset.vars, varname, value))
805 {
806 free(varname);
807 success = false;
808 break;
809 }
810
811 free(varname);
812 }
813 }
814
815 return success;
816}
817
818
819/*
820 * ExecQueryTuples: assuming query result is OK, execute each query
821 * result field as a SQL statement
822 *
823 * Returns true if successful, false otherwise.
824 */
825static bool
827{
828 bool success = true;
829 int nrows = PQntuples(result);
830 int ncolumns = PQnfields(result);
831 int r,
832 c;
833
834 /*
835 * We must turn off gexec_flag to avoid infinite recursion.
836 */
837 pset.gexec_flag = false;
838
839 for (r = 0; r < nrows; r++)
840 {
841 for (c = 0; c < ncolumns; c++)
842 {
843 if (!PQgetisnull(result, r, c))
844 {
845 const char *query = PQgetvalue(result, r, c);
846
847 /* Abandon execution if cancel_pressed */
848 if (cancel_pressed)
849 goto loop_exit;
850
851 /*
852 * ECHO_ALL mode should echo these queries, but SendQuery
853 * assumes that MainLoop did that, so we have to do it here.
854 */
856 {
857 puts(query);
858 fflush(stdout);
859 }
860
861 if (!SendQuery(query))
862 {
863 /* Error - abandon execution if ON_ERROR_STOP */
864 success = false;
866 goto loop_exit;
867 }
868 }
869 }
870 }
871
872loop_exit:
873
874 /*
875 * Restore state. We know gexec_flag was on, else we'd not be here. (We
876 * also know it'll get turned off at end of command, but that's not ours
877 * to do here.)
878 */
879 pset.gexec_flag = true;
880
881 /* Return true if all queries were successful */
882 return success;
883}
884
885
886/*
887 * Marshal the COPY data. Either path will get the
888 * connection out of its COPY state, then call PQresultStatus()
889 * once and report any error. Return whether all was ok.
890 *
891 * For COPY OUT, direct the output to copystream, or discard if that's NULL.
892 * For COPY IN, use pset.copyStream as data source if it's set,
893 * otherwise cur_cmd_source.
894 *
895 * Update *resultp if further processing is necessary; set to NULL otherwise.
896 * Return a result when queryFout can safely output a result status: on COPY
897 * IN, or on COPY OUT if written to something other than pset.queryFout.
898 * Returning NULL prevents the command status from being printed, which we
899 * want if the status line doesn't get taken as part of the COPY data.
900 */
901static bool
902HandleCopyResult(PGresult **resultp, FILE *copystream)
903{
904 bool success;
905 PGresult *copy_result;
906 ExecStatusType result_status = PQresultStatus(*resultp);
907
908 Assert(result_status == PGRES_COPY_OUT ||
909 result_status == PGRES_COPY_IN);
910
912
913 if (result_status == PGRES_COPY_OUT)
914 {
916 copystream,
917 &copy_result)
918 && (copystream != NULL);
919
920 /*
921 * Suppress status printing if the report would go to the same place
922 * as the COPY data just went. Note this doesn't prevent error
923 * reporting, since handleCopyOut did that.
924 */
925 if (copystream == pset.queryFout)
926 {
927 PQclear(copy_result);
928 copy_result = NULL;
929 }
930 }
931 else
932 {
933 /* COPY IN */
934 /* Ignore the copystream argument passed to the function */
937 copystream,
938 PQbinaryTuples(*resultp),
939 &copy_result);
940 }
942
943 /*
944 * Replace the PGRES_COPY_OUT/IN result with COPY command's exit status,
945 * or with NULL if we want to suppress printing anything.
946 */
947 PQclear(*resultp);
948 *resultp = copy_result;
949
950 return success;
951}
952
953/*
954 * PrintQueryStatus: report command status as required
955 */
956static void
957PrintQueryStatus(PGresult *result, FILE *printQueryFout)
958{
959 char buf[16];
960 const char *cmdstatus = PQcmdStatus(result);
961 FILE *fout = printQueryFout ? printQueryFout : pset.queryFout;
962
963 /* Do nothing if it's a TUPLES_OK result that isn't from RETURNING */
964 if (PQresultStatus(result) == PGRES_TUPLES_OK)
965 {
966 if (!(strncmp(cmdstatus, "INSERT", 6) == 0 ||
967 strncmp(cmdstatus, "UPDATE", 6) == 0 ||
968 strncmp(cmdstatus, "DELETE", 6) == 0 ||
969 strncmp(cmdstatus, "MERGE", 5) == 0))
970 return;
971 }
972
973 if (!pset.quiet)
974 {
976 {
977 fputs("<p>", fout);
978 html_escaped_print(cmdstatus, fout);
979 fputs("</p>\n", fout);
980 }
981 else
982 fprintf(fout, "%s\n", cmdstatus);
983 fflush(fout);
984 }
985
986 if (pset.logfile)
987 fprintf(pset.logfile, "%s\n", cmdstatus);
988
989 snprintf(buf, sizeof(buf), "%u", (unsigned int) PQoidValue(result));
990 SetVariable(pset.vars, "LASTOID", buf);
991}
992
993
994/*
995 * PrintQueryResult: print out (or store or execute) query result as required
996 *
997 * last is true if this is the last result of a command string.
998 * opt and printQueryFout are defined as for PrintQueryTuples.
999 * printStatusFout is where to send command status; NULL means pset.queryFout.
1000 *
1001 * Returns true if the query executed successfully, false otherwise.
1002 */
1003static bool
1004PrintQueryResult(PGresult *result, bool last,
1005 const printQueryOpt *opt, FILE *printQueryFout,
1006 FILE *printStatusFout)
1007{
1008 bool success;
1009
1010 if (!result)
1011 return false;
1012
1013 switch (PQresultStatus(result))
1014 {
1015 case PGRES_TUPLES_OK:
1016 /* store or execute or print the data ... */
1017 if (last && pset.gset_prefix)
1018 success = StoreQueryTuple(result);
1019 else if (last && pset.gexec_flag)
1020 success = ExecQueryTuples(result);
1021 else if (last && pset.crosstab_flag)
1023 else if (last || pset.show_all_results)
1024 success = PrintQueryTuples(result, opt, printQueryFout);
1025 else
1026 success = true;
1027
1028 /*
1029 * If it's INSERT/UPDATE/DELETE/MERGE RETURNING, also print
1030 * status.
1031 */
1032 if (last || pset.show_all_results)
1033 PrintQueryStatus(result, printStatusFout);
1034
1035 break;
1036
1037 case PGRES_COMMAND_OK:
1038 if (last || pset.show_all_results)
1039 PrintQueryStatus(result, printStatusFout);
1040 success = true;
1041 break;
1042
1043 case PGRES_EMPTY_QUERY:
1044 success = true;
1045 break;
1046
1047 case PGRES_COPY_OUT:
1048 case PGRES_COPY_IN:
1049 /* nothing to do here: already processed */
1050 success = true;
1051 break;
1052
1053 case PGRES_BAD_RESPONSE:
1055 case PGRES_FATAL_ERROR:
1056 success = false;
1057 break;
1058
1059 default:
1060 success = false;
1061 pg_log_error("unexpected PQresultStatus: %d",
1062 PQresultStatus(result));
1063 break;
1064 }
1065
1066 return success;
1067}
1068
1069/*
1070 * SendQuery: send the query string to the backend
1071 * (and print out result)
1072 *
1073 * Note: This is the "front door" way to send a query. That is, use it to
1074 * send queries actually entered by the user. These queries will be subject to
1075 * single step mode.
1076 * To send "back door" queries (generated by slash commands, etc.) in a
1077 * controlled way, use PSQLexec().
1078 *
1079 * Returns true if the query executed successfully, false otherwise.
1080 */
1081bool
1082SendQuery(const char *query)
1083{
1084 bool timing = pset.timing;
1085 PGTransactionStatusType transaction_status;
1086 double elapsed_msec = 0;
1087 bool OK = false;
1088 int i;
1089 bool on_error_rollback_savepoint = false;
1090 bool svpt_gone = false;
1091
1092 if (!pset.db)
1093 {
1094 pg_log_error("You are currently not connected to a database.");
1095 goto sendquery_cleanup;
1096 }
1097
1098 if (pset.singlestep)
1099 {
1100 char buf[3];
1101
1102 fflush(stderr);
1103 printf(_("/**(Single step mode: verify command)******************************************/\n"
1104 "%s\n"
1105 "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1106 query);
1107 fflush(stdout);
1108 if (fgets(buf, sizeof(buf), stdin) != NULL)
1109 if (buf[0] == 'x')
1110 goto sendquery_cleanup;
1111 if (cancel_pressed)
1112 goto sendquery_cleanup;
1113 }
1114 else if (pset.echo == PSQL_ECHO_QUERIES)
1115 {
1116 puts(query);
1117 fflush(stdout);
1118 }
1119
1120 if (pset.logfile)
1121 {
1123 _("/******** QUERY *********/\n"
1124 "%s\n"
1125 "/************************/\n\n"), query);
1127 }
1128
1130
1131 transaction_status = PQtransactionStatus(pset.db);
1132
1133 if (transaction_status == PQTRANS_IDLE &&
1134 !pset.autocommit &&
1135 !command_no_begin(query))
1136 {
1137 PGresult *result;
1138
1139 result = PQexec(pset.db, "BEGIN");
1140 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1141 {
1143 ClearOrSaveResult(result);
1144 goto sendquery_cleanup;
1145 }
1146 ClearOrSaveResult(result);
1147 transaction_status = PQtransactionStatus(pset.db);
1148 }
1149
1150 if (transaction_status == PQTRANS_INTRANS &&
1154 {
1155 PGresult *result;
1156
1157 result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1158 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1159 {
1161 ClearOrSaveResult(result);
1162 goto sendquery_cleanup;
1163 }
1164 ClearOrSaveResult(result);
1165 on_error_rollback_savepoint = true;
1166 }
1167
1168 if (pset.gdesc_flag)
1169 {
1170 /* Describe query's result columns, without executing it */
1171 OK = DescribeQuery(query, &elapsed_msec);
1172 }
1173 else
1174 {
1175 /* Default fetch-and-print mode */
1176 OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, 0, NULL, NULL) > 0);
1177 }
1178
1179 if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1180 pg_log_info("STATEMENT: %s", query);
1181
1182 /* If we made a temporary savepoint, possibly release/rollback */
1183 if (on_error_rollback_savepoint)
1184 {
1185 const char *svptcmd = NULL;
1186
1187 transaction_status = PQtransactionStatus(pset.db);
1188
1189 switch (transaction_status)
1190 {
1191 case PQTRANS_INERROR:
1192 /* We always rollback on an error */
1193 svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1194 break;
1195
1196 case PQTRANS_IDLE:
1197 /* If they are no longer in a transaction, then do nothing */
1198 break;
1199
1200 case PQTRANS_INTRANS:
1201
1202 /*
1203 * Release our savepoint, but do nothing if they are messing
1204 * with savepoints themselves
1205 */
1206 if (!svpt_gone)
1207 svptcmd = "RELEASE pg_psql_temporary_savepoint";
1208 break;
1209
1210 case PQTRANS_ACTIVE:
1211 case PQTRANS_UNKNOWN:
1212 default:
1213 OK = false;
1214 /* PQTRANS_UNKNOWN is expected given a broken connection. */
1215 if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1216 pg_log_error("unexpected transaction status (%d)",
1217 transaction_status);
1218 break;
1219 }
1220
1221 if (svptcmd)
1222 {
1223 PGresult *svptres;
1224
1225 svptres = PQexec(pset.db, svptcmd);
1226 if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1227 {
1229 ClearOrSaveResult(svptres);
1230 OK = false;
1231
1232 goto sendquery_cleanup;
1233 }
1234 PQclear(svptres);
1235 }
1236 }
1237
1238 /* Possible microtiming output */
1239 if (timing)
1240 PrintTiming(elapsed_msec);
1241
1242 /* check for events that may occur during query execution */
1243
1245 PQclientEncoding(pset.db) >= 0)
1246 {
1247 /* track effects of SET CLIENT_ENCODING */
1250 SetVariable(pset.vars, "ENCODING",
1252 }
1253
1255
1256 /* perform cleanup that should occur after any attempted query */
1257
1258sendquery_cleanup:
1259
1260 /* global cancellation reset */
1262
1263 /* reset \g's output-to-filename trigger */
1264 if (pset.gfname)
1265 {
1266 free(pset.gfname);
1267 pset.gfname = NULL;
1268 }
1269
1270 /* restore print settings if \g changed them */
1271 if (pset.gsavepopt)
1272 {
1274 pset.gsavepopt = NULL;
1275 }
1276
1277 /* clean up after extended protocol queries */
1279
1280 /* reset \gset trigger */
1281 if (pset.gset_prefix)
1282 {
1284 pset.gset_prefix = NULL;
1285 }
1286
1287 /* reset \gdesc trigger */
1288 pset.gdesc_flag = false;
1289
1290 /* reset \gexec trigger */
1291 pset.gexec_flag = false;
1292
1293 /* reset \crosstabview trigger */
1294 pset.crosstab_flag = false;
1295 for (i = 0; i < lengthof(pset.ctv_args); i++)
1296 {
1298 pset.ctv_args[i] = NULL;
1299 }
1300
1301 return OK;
1302}
1303
1304
1305/*
1306 * DescribeQuery: describe the result columns of a query, without executing it
1307 *
1308 * Returns true if the operation executed successfully, false otherwise.
1309 *
1310 * If pset.timing is on, total query time (exclusive of result-printing) is
1311 * stored into *elapsed_msec.
1312 */
1313static bool
1314DescribeQuery(const char *query, double *elapsed_msec)
1315{
1316 bool timing = pset.timing;
1317 PGresult *result;
1318 bool OK;
1320 after;
1321
1322 *elapsed_msec = 0;
1323
1324 if (timing)
1326 else
1328
1329 /*
1330 * To parse the query but not execute it, we prepare it, using the unnamed
1331 * prepared statement. This is invisible to psql users, since there's no
1332 * way to access the unnamed prepared statement from psql user space. The
1333 * next Parse or Query protocol message would overwrite the statement
1334 * anyway. (So there's no great need to clear it when done, which is a
1335 * good thing because libpq provides no easy way to do that.)
1336 */
1337 result = PQprepare(pset.db, "", query, 0, NULL);
1338 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1339 {
1341 SetResultVariables(result, false);
1342 ClearOrSaveResult(result);
1343 return false;
1344 }
1345 PQclear(result);
1346
1347 result = PQdescribePrepared(pset.db, "");
1348 OK = AcceptResult(result, true) &&
1349 (PQresultStatus(result) == PGRES_COMMAND_OK);
1350 if (OK && result)
1351 {
1352 if (PQnfields(result) > 0)
1353 {
1355 int i;
1356
1358
1360 "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1361 "FROM (VALUES ",
1362 gettext_noop("Column"),
1363 gettext_noop("Type"));
1364
1365 for (i = 0; i < PQnfields(result); i++)
1366 {
1367 const char *name;
1368 char *escname;
1369
1370 if (i > 0)
1372
1373 name = PQfname(result, i);
1374 escname = PQescapeLiteral(pset.db, name, strlen(name));
1375
1376 if (escname == NULL)
1377 {
1379 PQclear(result);
1381 return false;
1382 }
1383
1384 appendPQExpBuffer(&buf, "(%s, '%u'::pg_catalog.oid, %d)",
1385 escname,
1386 PQftype(result, i),
1387 PQfmod(result, i));
1388
1389 PQfreemem(escname);
1390 }
1391
1392 appendPQExpBufferStr(&buf, ") s(name, tp, tpm)");
1393 PQclear(result);
1394
1395 result = PQexec(pset.db, buf.data);
1396 OK = AcceptResult(result, true);
1397
1398 if (timing)
1399 {
1402 *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1403 }
1404
1405 if (OK && result)
1406 OK = PrintQueryResult(result, true, NULL, NULL, NULL);
1407
1409 }
1410 else
1412 _("The command has no result, or the result has no columns.\n"));
1413 }
1414
1415 SetResultVariables(result, OK);
1416 ClearOrSaveResult(result);
1417
1418 return OK;
1419}
1420
1421
1422/*
1423 * ExecQueryAndProcessResults: utility function for use by SendQuery()
1424 * and PSQLexecWatch().
1425 *
1426 * Sends query and cycles through PGresult objects.
1427 *
1428 * If our command string contained a COPY FROM STDIN or COPY TO STDOUT, the
1429 * PGresult associated with these commands must be processed by providing an
1430 * input or output stream. In that event, we'll marshal data for the COPY.
1431 *
1432 * For other commands, the results are processed normally, depending on their
1433 * status.
1434 *
1435 * When invoked from \watch, is_watch is true and min_rows is the value
1436 * of that option, or 0 if it wasn't set.
1437 *
1438 * Returns 1 on complete success, 0 on interrupt and -1 or errors. Possible
1439 * failure modes include purely client-side problems; check the transaction
1440 * status for the server-side opinion.
1441 *
1442 * Note that on a combined query, failure does not mean that nothing was
1443 * committed.
1444 */
1445static int
1447 double *elapsed_msec, bool *svpt_gone_p,
1448 bool is_watch, int min_rows,
1449 const printQueryOpt *opt, FILE *printQueryFout)
1450{
1451 bool timing = pset.timing;
1452 bool success = false;
1453 bool return_early = false;
1455 after;
1456 PGresult *result;
1457 FILE *gfile_fout = NULL;
1458 bool gfile_is_pipe = false;
1459
1460 if (timing)
1462 else
1464
1465 switch (pset.send_mode)
1466 {
1469 break;
1471 success = PQsendPrepare(pset.db, pset.stmtName, query, 0, NULL);
1472 break;
1474 Assert(pset.stmtName == NULL);
1476 pset.bind_nparams, NULL,
1477 (const char *const *) pset.bind_params,
1478 NULL, NULL, 0);
1479 break;
1481 Assert(pset.stmtName != NULL);
1484 (const char *const *) pset.bind_params,
1485 NULL, NULL, 0);
1486 break;
1487 case PSQL_SEND_QUERY:
1488 success = PQsendQuery(pset.db, query);
1489 break;
1490 }
1491
1492 if (!success)
1493 {
1494 const char *error = PQerrorMessage(pset.db);
1495
1496 if (strlen(error))
1497 pg_log_info("%s", error);
1498
1500
1501 return -1;
1502 }
1503
1504 /*
1505 * Fetch the result in chunks if FETCH_COUNT is set, except when:
1506 *
1507 * * SHOW_ALL_RESULTS is false, since that requires us to complete the
1508 * query before we can tell if its results should be displayed.
1509 *
1510 * * We're doing \crosstab, which likewise needs to see all the rows at
1511 * once.
1512 *
1513 * * We're doing \gexec: we must complete the data fetch to make the
1514 * connection free for issuing the resulting commands.
1515 *
1516 * * We're doing \gset: only one result row is allowed anyway.
1517 *
1518 * * We're doing \watch: users probably don't want us to force use of the
1519 * pager for that, plus chunking could break the min_rows check.
1520 */
1523 !pset.gset_prefix && !is_watch)
1524 {
1526 pg_log_warning("fetching results in chunked mode failed");
1527 }
1528
1529 /*
1530 * If SIGINT is sent while the query is processing, the interrupt will be
1531 * consumed. The user's intention, though, is to cancel the entire watch
1532 * process, so detect a sent cancellation request and exit in this case.
1533 */
1534 if (is_watch && cancel_pressed)
1535 {
1537 return 0;
1538 }
1539
1540 /* first result */
1541 result = PQgetResult(pset.db);
1542 if (min_rows > 0 && PQntuples(result) < min_rows)
1543 {
1544 return_early = true;
1545 }
1546
1547 while (result != NULL)
1548 {
1549 ExecStatusType result_status;
1550 bool is_chunked_result = false;
1551 PGresult *next_result;
1552 bool last;
1553
1554 if (!AcceptResult(result, false))
1555 {
1556 /*
1557 * Some error occurred, either a server-side failure or a failure
1558 * to submit the command string. Record that.
1559 */
1560 const char *error = PQresultErrorMessage(result);
1561
1562 if (strlen(error))
1563 pg_log_info("%s", error);
1564
1566 if (!is_watch)
1567 SetResultVariables(result, false);
1568
1569 /* keep the result status before clearing it */
1570 result_status = PQresultStatus(result);
1571 ClearOrSaveResult(result);
1572 success = false;
1573
1574 /*
1575 * switch to next result
1576 */
1577 if (result_status == PGRES_COPY_BOTH ||
1578 result_status == PGRES_COPY_OUT ||
1579 result_status == PGRES_COPY_IN)
1580
1581 /*
1582 * For some obscure reason PQgetResult does *not* return a
1583 * NULL in copy cases despite the result having been cleared,
1584 * but keeps returning an "empty" result that we have to
1585 * ignore manually.
1586 */
1587 result = NULL;
1588 else
1589 result = PQgetResult(pset.db);
1590
1591 /*
1592 * Get current timing measure in case an error occurs
1593 */
1594 if (timing)
1595 {
1598 *elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1599 }
1600
1601 continue;
1602 }
1603 else if (svpt_gone_p && !*svpt_gone_p)
1604 {
1605 /*
1606 * Check if the user ran any command that would destroy our
1607 * internal savepoint: If the user did COMMIT AND CHAIN, RELEASE
1608 * or ROLLBACK, our savepoint is gone. If they issued a SAVEPOINT,
1609 * releasing ours would remove theirs.
1610 */
1611 const char *cmd = PQcmdStatus(result);
1612
1613 *svpt_gone_p = (strcmp(cmd, "COMMIT") == 0 ||
1614 strcmp(cmd, "SAVEPOINT") == 0 ||
1615 strcmp(cmd, "RELEASE") == 0 ||
1616 strcmp(cmd, "ROLLBACK") == 0);
1617 }
1618
1619 result_status = PQresultStatus(result);
1620
1621 /* must handle COPY before changing the current result */
1622 Assert(result_status != PGRES_COPY_BOTH);
1623 if (result_status == PGRES_COPY_IN ||
1624 result_status == PGRES_COPY_OUT)
1625 {
1626 FILE *copy_stream = NULL;
1627
1628 /*
1629 * For COPY OUT, direct the output to the default place (probably
1630 * a pager pipe) for \watch, or to pset.copyStream for \copy,
1631 * otherwise to pset.gfname if that's set, otherwise to
1632 * pset.queryFout.
1633 */
1634 if (result_status == PGRES_COPY_OUT)
1635 {
1636 if (is_watch)
1637 {
1638 /* invoked by \watch */
1639 copy_stream = printQueryFout ? printQueryFout : pset.queryFout;
1640 }
1641 else if (pset.copyStream)
1642 {
1643 /* invoked by \copy */
1644 copy_stream = pset.copyStream;
1645 }
1646 else if (pset.gfname)
1647 {
1648 /* COPY followed by \g filename or \g |program */
1649 success &= SetupGOutput(&gfile_fout, &gfile_is_pipe);
1650 if (gfile_fout)
1651 copy_stream = gfile_fout;
1652 }
1653 else
1654 {
1655 /* fall back to the generic query output stream */
1656 copy_stream = pset.queryFout;
1657 }
1658 }
1659
1660 /*
1661 * Even if the output stream could not be opened, we call
1662 * HandleCopyResult() with a NULL output stream to collect and
1663 * discard the COPY data.
1664 */
1665 success &= HandleCopyResult(&result, copy_stream);
1666 }
1667
1668 /* If we have a chunked result, collect and print all chunks */
1669 if (result_status == PGRES_TUPLES_CHUNK)
1670 {
1671 FILE *tuples_fout = printQueryFout ? printQueryFout : pset.queryFout;
1672 printQueryOpt my_popt = opt ? *opt : pset.popt;
1673 int64 total_tuples = 0;
1674 bool is_pager = false;
1675 int flush_error = 0;
1676
1677 /* initialize print options for partial table output */
1678 my_popt.topt.start_table = true;
1679 my_popt.topt.stop_table = false;
1680 my_popt.topt.prior_records = 0;
1681
1682 /* open \g file if needed */
1683 success &= SetupGOutput(&gfile_fout, &gfile_is_pipe);
1684 if (gfile_fout)
1685 tuples_fout = gfile_fout;
1686
1687 /* force use of pager for any chunked resultset going to stdout */
1688 if (success && tuples_fout == stdout)
1689 {
1690 tuples_fout = PageOutput(INT_MAX, &(my_popt.topt));
1691 is_pager = true;
1692 }
1693
1694 do
1695 {
1696 /*
1697 * Display the current chunk of results, unless the output
1698 * stream stopped working or we got canceled. We skip use of
1699 * PrintQueryResult and go directly to printQuery, so that we
1700 * can pass the correct is_pager value and because we don't
1701 * want PrintQueryStatus to happen yet. Above, we rejected
1702 * use of chunking for all cases in which PrintQueryResult
1703 * would send the result to someplace other than printQuery.
1704 */
1705 if (success && !flush_error && !cancel_pressed)
1706 {
1707 printQuery(result, &my_popt, tuples_fout, is_pager, pset.logfile);
1708 flush_error = fflush(tuples_fout);
1709 }
1710
1711 /* after the first result set, disallow header decoration */
1712 my_popt.topt.start_table = false;
1713
1714 /* count tuples before dropping the result */
1715 my_popt.topt.prior_records += PQntuples(result);
1716 total_tuples += PQntuples(result);
1717
1718 ClearOrSaveResult(result);
1719
1720 /* get the next result, loop if it's PGRES_TUPLES_CHUNK */
1721 result = PQgetResult(pset.db);
1722 } while (PQresultStatus(result) == PGRES_TUPLES_CHUNK);
1723
1724 /* We expect an empty PGRES_TUPLES_OK, else there's a problem */
1725 if (PQresultStatus(result) == PGRES_TUPLES_OK)
1726 {
1727 char buf[32];
1728
1729 Assert(PQntuples(result) == 0);
1730
1731 /* Display the footer using the empty result */
1732 if (success && !flush_error && !cancel_pressed)
1733 {
1734 my_popt.topt.stop_table = true;
1735 printQuery(result, &my_popt, tuples_fout, is_pager, pset.logfile);
1736 fflush(tuples_fout);
1737 }
1738
1739 if (is_pager)
1740 ClosePager(tuples_fout);
1741
1742 /*
1743 * It's possible the data is from a RETURNING clause, in which
1744 * case we need to print query status.
1745 */
1746 PrintQueryStatus(result, printQueryFout);
1747
1748 /*
1749 * We must do a fake SetResultVariables(), since we don't have
1750 * a PGresult corresponding to the whole query.
1751 */
1752 SetVariable(pset.vars, "ERROR", "false");
1753 SetVariable(pset.vars, "SQLSTATE", "00000");
1754 snprintf(buf, sizeof(buf), INT64_FORMAT, total_tuples);
1755 SetVariable(pset.vars, "ROW_COUNT", buf);
1756 /* Prevent SetResultVariables call below */
1757 is_chunked_result = true;
1758
1759 /* Clear the empty result so it isn't printed below */
1760 ClearOrSaveResult(result);
1761 result = NULL;
1762 }
1763 else
1764 {
1765 /* Probably an error report, so close the pager and print it */
1766 if (is_pager)
1767 ClosePager(tuples_fout);
1768
1769 success &= AcceptResult(result, true);
1770 /* SetResultVariables and ClearOrSaveResult happen below */
1771 }
1772 }
1773
1774 /*
1775 * Check PQgetResult() again. In the typical case of a single-command
1776 * string, it will return NULL. Otherwise, we'll have other results
1777 * to process. We need to do that to check whether this is the last.
1778 */
1779 next_result = PQgetResult(pset.db);
1780 last = (next_result == NULL);
1781
1782 /*
1783 * Update current timing measure.
1784 *
1785 * It will include the display of previous results, if any. This
1786 * cannot be helped because the server goes on processing further
1787 * queries anyway while the previous ones are being displayed. The
1788 * parallel execution of the client display hides the server time when
1789 * it is shorter.
1790 *
1791 * With combined queries, timing must be understood as an upper bound
1792 * of the time spent processing them.
1793 */
1794 if (timing)
1795 {
1798 *elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1799 }
1800
1801 /* this may or may not print something depending on settings */
1802 if (result != NULL)
1803 {
1804 /*
1805 * If results need to be printed into the file specified by \g,
1806 * open it, unless we already did. Note that when pset.gfname is
1807 * set, the passed-in value of printQueryFout is not used for
1808 * tuple output, but it's still used for status output.
1809 */
1810 FILE *tuples_fout = printQueryFout;
1811
1812 if (PQresultStatus(result) == PGRES_TUPLES_OK)
1813 success &= SetupGOutput(&gfile_fout, &gfile_is_pipe);
1814 if (gfile_fout)
1815 tuples_fout = gfile_fout;
1816 if (success)
1817 success &= PrintQueryResult(result, last, opt,
1818 tuples_fout, printQueryFout);
1819 }
1820
1821 /* set variables from last result, unless dealt with elsewhere */
1822 if (last && !is_watch && !is_chunked_result)
1823 SetResultVariables(result, success);
1824
1825 ClearOrSaveResult(result);
1826 result = next_result;
1827
1828 if (cancel_pressed)
1829 {
1830 /* drop this next result, as well as any others not yet read */
1831 ClearOrSaveResult(result);
1833 break;
1834 }
1835 }
1836
1837 /* close \g file if we opened it */
1838 CloseGOutput(gfile_fout, gfile_is_pipe);
1839
1840 /* may need this to recover from conn loss during COPY */
1841 if (!CheckConnection())
1842 return -1;
1843
1844 if (cancel_pressed || return_early)
1845 return 0;
1846
1847 return success ? 1 : -1;
1848}
1849
1850
1851/*
1852 * Advance the given char pointer over white space and SQL comments.
1853 */
1854static const char *
1855skip_white_space(const char *query)
1856{
1857 int cnestlevel = 0; /* slash-star comment nest level */
1858
1859 while (*query)
1860 {
1861 int mblen = PQmblenBounded(query, pset.encoding);
1862
1863 /*
1864 * Note: we assume the encoding is a superset of ASCII, so that for
1865 * example "query[0] == '/'" is meaningful. However, we do NOT assume
1866 * that the second and subsequent bytes of a multibyte character
1867 * couldn't look like ASCII characters; so it is critical to advance
1868 * by mblen, not 1, whenever we haven't exactly identified the
1869 * character we are skipping over.
1870 */
1871 if (isspace((unsigned char) *query))
1872 query += mblen;
1873 else if (query[0] == '/' && query[1] == '*')
1874 {
1875 cnestlevel++;
1876 query += 2;
1877 }
1878 else if (cnestlevel > 0 && query[0] == '*' && query[1] == '/')
1879 {
1880 cnestlevel--;
1881 query += 2;
1882 }
1883 else if (cnestlevel == 0 && query[0] == '-' && query[1] == '-')
1884 {
1885 query += 2;
1886
1887 /*
1888 * We have to skip to end of line since any slash-star inside the
1889 * -- comment does NOT start a slash-star comment.
1890 */
1891 while (*query)
1892 {
1893 if (*query == '\n')
1894 {
1895 query++;
1896 break;
1897 }
1898 query += PQmblenBounded(query, pset.encoding);
1899 }
1900 }
1901 else if (cnestlevel > 0)
1902 query += mblen;
1903 else
1904 break; /* found first token */
1905 }
1906
1907 return query;
1908}
1909
1910
1911/*
1912 * Check whether a command is one of those for which we should NOT start
1913 * a new transaction block (ie, send a preceding BEGIN).
1914 *
1915 * These include the transaction control statements themselves, plus
1916 * certain statements that the backend disallows inside transaction blocks.
1917 */
1918static bool
1919command_no_begin(const char *query)
1920{
1921 int wordlen;
1922
1923 /*
1924 * First we must advance over any whitespace and comments.
1925 */
1926 query = skip_white_space(query);
1927
1928 /*
1929 * Check word length (since "beginx" is not "begin").
1930 */
1931 wordlen = 0;
1932 while (isalpha((unsigned char) query[wordlen]))
1933 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
1934
1935 /*
1936 * Transaction control commands. These should include every keyword that
1937 * gives rise to a TransactionStmt in the backend grammar, except for the
1938 * savepoint-related commands.
1939 *
1940 * (We assume that START must be START TRANSACTION, since there is
1941 * presently no other "START foo" command.)
1942 */
1943 if (wordlen == 5 && pg_strncasecmp(query, "abort", 5) == 0)
1944 return true;
1945 if (wordlen == 5 && pg_strncasecmp(query, "begin", 5) == 0)
1946 return true;
1947 if (wordlen == 5 && pg_strncasecmp(query, "start", 5) == 0)
1948 return true;
1949 if (wordlen == 6 && pg_strncasecmp(query, "commit", 6) == 0)
1950 return true;
1951 if (wordlen == 3 && pg_strncasecmp(query, "end", 3) == 0)
1952 return true;
1953 if (wordlen == 8 && pg_strncasecmp(query, "rollback", 8) == 0)
1954 return true;
1955 if (wordlen == 7 && pg_strncasecmp(query, "prepare", 7) == 0)
1956 {
1957 /* PREPARE TRANSACTION is a TC command, PREPARE foo is not */
1958 query += wordlen;
1959
1960 query = skip_white_space(query);
1961
1962 wordlen = 0;
1963 while (isalpha((unsigned char) query[wordlen]))
1964 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
1965
1966 if (wordlen == 11 && pg_strncasecmp(query, "transaction", 11) == 0)
1967 return true;
1968 return false;
1969 }
1970
1971 /*
1972 * Commands not allowed within transactions. The statements checked for
1973 * here should be exactly those that call PreventInTransactionBlock() in
1974 * the backend.
1975 */
1976 if (wordlen == 6 && pg_strncasecmp(query, "vacuum", 6) == 0)
1977 return true;
1978 if (wordlen == 7 && pg_strncasecmp(query, "cluster", 7) == 0)
1979 {
1980 /* CLUSTER with any arguments is allowed in transactions */
1981 query += wordlen;
1982
1983 query = skip_white_space(query);
1984
1985 if (isalpha((unsigned char) query[0]))
1986 return false; /* has additional words */
1987 return true; /* it's CLUSTER without arguments */
1988 }
1989
1990 if (wordlen == 6 && pg_strncasecmp(query, "create", 6) == 0)
1991 {
1992 query += wordlen;
1993
1994 query = skip_white_space(query);
1995
1996 wordlen = 0;
1997 while (isalpha((unsigned char) query[wordlen]))
1998 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
1999
2000 if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
2001 return true;
2002 if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
2003 return true;
2004
2005 /* CREATE [UNIQUE] INDEX CONCURRENTLY isn't allowed in xacts */
2006 if (wordlen == 6 && pg_strncasecmp(query, "unique", 6) == 0)
2007 {
2008 query += wordlen;
2009
2010 query = skip_white_space(query);
2011
2012 wordlen = 0;
2013 while (isalpha((unsigned char) query[wordlen]))
2014 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2015 }
2016
2017 if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
2018 {
2019 query += wordlen;
2020
2021 query = skip_white_space(query);
2022
2023 wordlen = 0;
2024 while (isalpha((unsigned char) query[wordlen]))
2025 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2026
2027 if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2028 return true;
2029 }
2030
2031 return false;
2032 }
2033
2034 if (wordlen == 5 && pg_strncasecmp(query, "alter", 5) == 0)
2035 {
2036 query += wordlen;
2037
2038 query = skip_white_space(query);
2039
2040 wordlen = 0;
2041 while (isalpha((unsigned char) query[wordlen]))
2042 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2043
2044 /* ALTER SYSTEM isn't allowed in xacts */
2045 if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
2046 return true;
2047
2048 return false;
2049 }
2050
2051 /*
2052 * Note: these tests will match DROP SYSTEM and REINDEX TABLESPACE, which
2053 * aren't really valid commands so we don't care much. The other four
2054 * possible matches are correct.
2055 */
2056 if ((wordlen == 4 && pg_strncasecmp(query, "drop", 4) == 0) ||
2057 (wordlen == 7 && pg_strncasecmp(query, "reindex", 7) == 0))
2058 {
2059 query += wordlen;
2060
2061 query = skip_white_space(query);
2062
2063 wordlen = 0;
2064 while (isalpha((unsigned char) query[wordlen]))
2065 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2066
2067 if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
2068 return true;
2069 if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
2070 return true;
2071 if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
2072 return true;
2073 if (wordlen == 5 && (pg_strncasecmp(query, "index", 5) == 0 ||
2074 pg_strncasecmp(query, "table", 5) == 0))
2075 {
2076 query += wordlen;
2077 query = skip_white_space(query);
2078 wordlen = 0;
2079 while (isalpha((unsigned char) query[wordlen]))
2080 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2081
2082 /*
2083 * REINDEX [ TABLE | INDEX ] CONCURRENTLY are not allowed in
2084 * xacts.
2085 */
2086 if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2087 return true;
2088 }
2089
2090 /* DROP INDEX CONCURRENTLY isn't allowed in xacts */
2091 if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
2092 {
2093 query += wordlen;
2094
2095 query = skip_white_space(query);
2096
2097 wordlen = 0;
2098 while (isalpha((unsigned char) query[wordlen]))
2099 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2100
2101 if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2102 return true;
2103
2104 return false;
2105 }
2106
2107 return false;
2108 }
2109
2110 /* DISCARD ALL isn't allowed in xacts, but other variants are allowed. */
2111 if (wordlen == 7 && pg_strncasecmp(query, "discard", 7) == 0)
2112 {
2113 query += wordlen;
2114
2115 query = skip_white_space(query);
2116
2117 wordlen = 0;
2118 while (isalpha((unsigned char) query[wordlen]))
2119 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2120
2121 if (wordlen == 3 && pg_strncasecmp(query, "all", 3) == 0)
2122 return true;
2123 return false;
2124 }
2125
2126 return false;
2127}
2128
2129
2130/*
2131 * Test if the current user is a database superuser.
2132 */
2133bool
2135{
2136 const char *val;
2137
2138 if (!pset.db)
2139 return false;
2140
2141 val = PQparameterStatus(pset.db, "is_superuser");
2142
2143 if (val && strcmp(val, "on") == 0)
2144 return true;
2145
2146 return false;
2147}
2148
2149
2150/*
2151 * Test if the current session uses standard string literals.
2152 */
2153bool
2155{
2156 const char *val;
2157
2158 if (!pset.db)
2159 return false;
2160
2161 val = PQparameterStatus(pset.db, "standard_conforming_strings");
2162
2163 if (val && strcmp(val, "on") == 0)
2164 return true;
2165
2166 return false;
2167}
2168
2169
2170/*
2171 * Return the session user of the current connection.
2172 */
2173const char *
2175{
2176 const char *val;
2177
2178 if (!pset.db)
2179 return NULL;
2180
2181 val = PQparameterStatus(pset.db, "session_authorization");
2182 if (val)
2183 return val;
2184 else
2185 return PQuser(pset.db);
2186}
2187
2188
2189/* expand_tilde
2190 *
2191 * substitute '~' with HOME or '~username' with username's home dir
2192 *
2193 */
2194void
2196{
2197 if (!filename || !(*filename))
2198 return;
2199
2200 /*
2201 * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2202 * for short versions of long file names, though the tilde is usually
2203 * toward the end, not at the beginning.
2204 */
2205#ifndef WIN32
2206
2207 /* try tilde expansion */
2208 if (**filename == '~')
2209 {
2210 char *fn;
2211 char oldp,
2212 *p;
2213 struct passwd *pw;
2214 char home[MAXPGPATH];
2215
2216 fn = *filename;
2217 *home = '\0';
2218
2219 p = fn + 1;
2220 while (*p != '/' && *p != '\0')
2221 p++;
2222
2223 oldp = *p;
2224 *p = '\0';
2225
2226 if (*(fn + 1) == '\0')
2227 get_home_path(home); /* ~ or ~/ only */
2228 else if ((pw = getpwnam(fn + 1)) != NULL)
2229 strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2230
2231 *p = oldp;
2232 if (strlen(home) != 0)
2233 {
2234 char *newfn;
2235
2236 newfn = psprintf("%s%s", home, p);
2237 free(fn);
2238 *filename = newfn;
2239 }
2240 }
2241#endif
2242}
2243
2244/*
2245 * Checks if connection string starts with either of the valid URI prefix
2246 * designators.
2247 *
2248 * Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
2249 *
2250 * XXX This is a duplicate of the eponymous libpq function.
2251 */
2252static int
2254{
2255 /* The connection URI must start with either of the following designators: */
2256 static const char uri_designator[] = "postgresql://";
2257 static const char short_uri_designator[] = "postgres://";
2258
2259 if (strncmp(connstr, uri_designator,
2260 sizeof(uri_designator) - 1) == 0)
2261 return sizeof(uri_designator) - 1;
2262
2263 if (strncmp(connstr, short_uri_designator,
2264 sizeof(short_uri_designator) - 1) == 0)
2265 return sizeof(short_uri_designator) - 1;
2266
2267 return 0;
2268}
2269
2270/*
2271 * Reset state related to extended query protocol
2272 *
2273 * Clean up any state related to bind parameters, statement name and
2274 * PSQL_SEND_MODE. This needs to be called after processing a query or when
2275 * running a new meta-command that uses the extended query protocol, like
2276 * \parse, \bind, etc.
2277 */
2278void
2280{
2281 int i;
2282
2283 switch (pset.send_mode)
2284 {
2285 case PSQL_SEND_EXTENDED_CLOSE: /* \close */
2287 break;
2288 case PSQL_SEND_EXTENDED_PARSE: /* \parse */
2290 break;
2291 case PSQL_SEND_EXTENDED_QUERY_PARAMS: /* \bind */
2292 case PSQL_SEND_EXTENDED_QUERY_PREPARED: /* \bind_named */
2293 for (i = 0; i < pset.bind_nparams; i++)
2297 pset.bind_params = NULL;
2298 break;
2299 case PSQL_SEND_QUERY:
2300 break;
2301 }
2302
2303 pset.stmtName = NULL;
2305}
2306
2307/*
2308 * Recognized connection string either starts with a valid URI prefix or
2309 * contains a "=" in it.
2310 *
2311 * Must be consistent with parse_connection_string: anything for which this
2312 * returns true should at least look like it's parseable by that routine.
2313 *
2314 * XXX This is a duplicate of the eponymous libpq function.
2315 */
2316bool
2318{
2319 return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2320}
const char *const days[]
Definition: datetime.c:84
void expand_tilde(char **filename)
Definition: common.c:2195
PGresult * PSQLexec(const char *query)
Definition: common.c:620
void psql_setup_cancel_handler(void)
Definition: common.c:313
volatile sig_atomic_t sigint_interrupt_enabled
Definition: common.c:292
static bool PrintQueryResult(PGresult *result, bool last, const printQueryOpt *opt, FILE *printQueryFout, FILE *printStatusFout)
Definition: common.c:1004
static bool CheckConnection(void)
Definition: common.c:342
const char * session_username(void)
Definition: common.c:2174
static void PrintQueryStatus(PGresult *result, FILE *printQueryFout)
Definition: common.c:957
static int uri_prefix_length(const char *connstr)
Definition: common.c:2253
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1314
static void ClearOrSaveAllResults(void)
Definition: common.c:547
static bool ExecQueryTuples(const PGresult *result)
Definition: common.c:826
static void PrintNotifications(void)
Definition: common.c:705
static void SetResultVariables(PGresult *result, bool success)
Definition: common.c:461
char * psql_get_variable(const char *varname, PsqlScanQuoteType quote, void *passthrough)
Definition: common.c:176
sigjmp_buf sigint_interrupt_jmp
Definition: common.c:294
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:523
static bool SetupGOutput(FILE **gfile_fout, bool *is_pipe)
Definition: common.c:90
static void psql_cancel_callback(void)
Definition: common.c:297
static bool AcceptResult(const PGresult *result, bool show_error)
Definition: common.c:403
static bool HandleCopyResult(PGresult **resultp, FILE *copystream)
Definition: common.c:902
int PSQLexecWatch(const char *query, const printQueryOpt *opt, FILE *printQueryFout, int min_rows)
Definition: common.c:675
void SetShellResultVariables(int wait_result)
Definition: common.c:501
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:267
void clean_extended_state(void)
Definition: common.c:2279
static bool StoreQueryTuple(const PGresult *result)
Definition: common.c:762
static void CloseGOutput(FILE *gfile_fout, bool is_pipe)
Definition: common.c:110
static const char * skip_white_space(const char *query)
Definition: common.c:1855
bool standard_strings(void)
Definition: common.c:2154
static bool PrintQueryTuples(const PGresult *result, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:738
static bool ConnectionUp(void)
Definition: common.c:324
bool is_superuser(void)
Definition: common.c:2134
bool setQFout(const char *fname)
Definition: common.c:132
static int ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, int min_rows, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:1446
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:56
bool recognized_connection_string(const char *connstr)
Definition: common.c:2317
static void PrintTiming(double elapsed_msec)
Definition: common.c:561
bool SendQuery(const char *query)
Definition: common.c:1082
static bool command_no_begin(const char *query)
Definition: common.c:1919
bool handleCopyOut(PGconn *conn, FILE *copystream, PGresult **res)
Definition: copy.c:434
bool handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary, PGresult **res)
Definition: copy.c:511
#define gettext_noop(x)
Definition: c.h:1150
#define INT64_FORMAT
Definition: c.h:503
#define Assert(condition)
Definition: c.h:812
int64_t int64
Definition: c.h:482
#define lengthof(array)
Definition: c.h:742
void ResetCancelConn(void)
Definition: cancel.c:107
void SetCancelConn(PGconn *conn)
Definition: cancel.c:77
void setup_cancel_handler(void(*query_cancel_callback)(void))
Definition: cancel.c:183
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:5210
void UnsyncVariables(void)
Definition: command.c:4170
void SyncVariables(void)
Definition: command.c:4128
void connection_warnings(bool in_startup)
Definition: command.c:4000
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
bool PrintResultInCrosstab(const PGresult *res)
Definition: crosstabview.c:104
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
#define _(x)
Definition: elog.c:90
void PQreset(PGconn *conn)
Definition: fe-connect.c:4941
static const char short_uri_designator[]
Definition: fe-connect.c:401
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7201
static const char uri_designator[]
Definition: fe-connect.c:400
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7211
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7193
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7344
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4927
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7100
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7256
int PQsendQueryParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:1492
int PQbinaryTuples(const PGresult *res)
Definition: fe-exec.c:3497
void PQfreemem(void *ptr)
Definition: fe-exec.c:4032
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3719
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2455
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2062
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
int PQsendClosePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2556
char * PQcmdTuples(PGresult *res)
Definition: fe-exec.c:3822
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
PGresult * PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Definition: fe-exec.c:2306
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:3427
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:3567
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1984
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4304
int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Definition: fe-exec.c:1536
int PQfmod(const PGresult *res, int field_num)
Definition: fe-exec.c:3741
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
int PQsetChunkedRowsMode(PGconn *conn, int chunkSize)
Definition: fe-exec.c:1965
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3466
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1416
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3752
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
int PQsendQueryPrepared(PGconn *conn, const char *stmtName, int nParams, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:1633
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4310
Oid PQoidValue(const PGresult *res)
Definition: fe-exec.c:3793
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3489
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2667
int PQmblenBounded(const char *s, int encoding)
Definition: fe-misc.c:1234
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void restore_sigpipe_trap(void)
Definition: print.c:3062
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3549
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:3089
void ClosePager(FILE *pagerpipe)
Definition: print.c:3141
void disable_sigpipe_trap(void)
Definition: print.c:3039
void html_escaped_print(const char *in, FILE *fout)
Definition: print.c:1952
void set_sigpipe_trap_state(bool ignore)
Definition: print.c:3075
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
@ PRINT_HTML
Definition: print.h:34
#define free(a)
Definition: header.h:65
static struct @161 value
long val
Definition: informix.c:689
static bool success
Definition: initdb.c:186
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:181
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:191
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:172
int i
Definition: isn.c:72
@ CONNECTION_BAD
Definition: libpq-fe.h:82
ExecStatusType
Definition: libpq-fe.h:118
@ PGRES_COPY_IN
Definition: libpq-fe.h:127
@ PGRES_COPY_BOTH
Definition: libpq-fe.h:132
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:120
@ PGRES_TUPLES_CHUNK
Definition: libpq-fe.h:137
@ PGRES_FATAL_ERROR
Definition: libpq-fe.h:131
@ PGRES_COPY_OUT
Definition: libpq-fe.h:126
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:119
@ PGRES_BAD_RESPONSE
Definition: libpq-fe.h:128
@ PGRES_NONFATAL_ERROR
Definition: libpq-fe.h:130
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:123
PGTransactionStatusType
Definition: libpq-fe.h:141
@ PQTRANS_INTRANS
Definition: libpq-fe.h:144
@ PQTRANS_IDLE
Definition: libpq-fe.h:142
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:143
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:146
@ PQTRANS_INERROR
Definition: libpq-fe.h:145
static void const char fflush(stdout)
exit(1)
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_info(...)
Definition: logging.h:124
void * arg
#define MAXPGPATH
static char * connstr
Definition: pg_dumpall.c:88
static char * filename
Definition: pg_dumpall.c:119
static char * buf
Definition: pg_test_fsync.c:72
#define pg_encoding_to_char
Definition: pg_wchar.h:630
#define pg_log_warning(...)
Definition: pgfnames.c:24
bool get_home_path(char *ret_path)
Definition: path.c:928
#define snprintf
Definition: port.h:238
#define printf(...)
Definition: port.h:244
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:56
#define PG_DIAG_MESSAGE_PRIMARY
Definition: postgres_ext.h:57
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
char * c
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
PsqlScanQuoteType
Definition: psqlscan.h:53
@ PQUOTE_SQL_LITERAL
Definition: psqlscan.h:55
@ PQUOTE_PLAIN
Definition: psqlscan.h:54
@ PQUOTE_SHELL_ARG
Definition: psqlscan.h:57
@ PQUOTE_SQL_IDENT
Definition: psqlscan.h:56
static int before(chr x, chr y)
Definition: regc_locale.c:488
@ PSQL_ERROR_ROLLBACK_ON
Definition: settings.h:54
@ PSQL_ERROR_ROLLBACK_OFF
Definition: settings.h:52
@ PSQL_ECHO_ALL
Definition: settings.h:40
@ PSQL_ECHO_ERRORS
Definition: settings.h:39
@ PSQL_ECHO_QUERIES
Definition: settings.h:38
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition: settings.h:47
@ PSQL_ECHO_HIDDEN_OFF
Definition: settings.h:45
PsqlSettings pset
Definition: startup.c:32
#define EXIT_BADCONN
Definition: settings.h:181
@ PSQL_SEND_QUERY
Definition: settings.h:67
@ PSQL_SEND_EXTENDED_QUERY_PARAMS
Definition: settings.h:70
@ PSQL_SEND_EXTENDED_PARSE
Definition: settings.h:69
@ PSQL_SEND_EXTENDED_CLOSE
Definition: settings.h:68
@ PSQL_SEND_EXTENDED_QUERY_PREPARED
Definition: settings.h:71
static void error(void)
Definition: sql-dyntest.c:147
bool appendShellStringNoError(PQExpBuffer buf, const char *str)
Definition: string_utils.c:441
printQueryOpt popt
Definition: settings.h:100
char * gset_prefix
Definition: settings.h:105
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:159
VariableSpace vars
Definition: settings.h:133
int encoding
Definition: settings.h:92
FILE * logfile
Definition: settings.h:131
char * stmtName
Definition: settings.h:112
bool on_error_stop
Definition: settings.h:148
PGconn * dead_conn
Definition: settings.h:140
bool autocommit
Definition: settings.h:147
char * ctv_args[4]
Definition: settings.h:115
PGresult * last_error_result
Definition: settings.h:98
bool show_all_results
Definition: settings.h:166
FILE * copyStream
Definition: settings.h:96
char ** bind_params
Definition: settings.h:111
PGconn * db
Definition: settings.h:91
FILE * queryFout
Definition: settings.h:93
PSQL_SEND_MODE send_mode
Definition: settings.h:108
bool queryFoutPipe
Definition: settings.h:94
PSQL_ECHO echo
Definition: settings.h:157
bool singlestep
Definition: settings.h:151
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:158
printQueryOpt * gsavepopt
Definition: settings.h:103
char * gfname
Definition: settings.h:102
int fetch_count
Definition: settings.h:154
bool cur_cmd_interactive
Definition: settings.h:122
bool gexec_flag
Definition: settings.h:107
bool crosstab_flag
Definition: settings.h:114
FILE * cur_cmd_source
Definition: settings.h:120
bool gdesc_flag
Definition: settings.h:106
int bind_nparams
Definition: settings.h:110
int be_pid
Definition: libpq-fe.h:219
char * relname
Definition: libpq-fe.h:218
char * extra
Definition: libpq-fe.h:220
printTableOpt topt
Definition: print.h:185
bool start_table
Definition: print.h:127
unsigned long prior_records
Definition: print.h:130
enum printFormat format
Definition: print.h:113
int encoding
Definition: print.h:138
bool stop_table
Definition: print.h:128
static void * fn(void *arg)
Definition: thread-alloc.c:119
bool VariableHasHook(VariableSpace space, const char *name)
Definition: variables.c:367
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
const char * GetVariable(VariableSpace space, const char *name)
Definition: variables.c:71
int wait_result_to_exit_code(int exit_status)
Definition: wait_error.c:138
const char * name