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