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-2024, 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  /*
987  * If it's INSERT/UPDATE/DELETE/MERGE RETURNING, also print
988  * status.
989  */
990  if (last || pset.show_all_results)
991  {
992  cmdstatus = PQcmdStatus(result);
993  if (strncmp(cmdstatus, "INSERT", 6) == 0 ||
994  strncmp(cmdstatus, "UPDATE", 6) == 0 ||
995  strncmp(cmdstatus, "DELETE", 6) == 0 ||
996  strncmp(cmdstatus, "MERGE", 5) == 0)
997  PrintQueryStatus(result, printStatusFout);
998  }
999 
1000  break;
1001 
1002  case PGRES_COMMAND_OK:
1003  if (last || pset.show_all_results)
1004  PrintQueryStatus(result, printStatusFout);
1005  success = true;
1006  break;
1007 
1008  case PGRES_EMPTY_QUERY:
1009  success = true;
1010  break;
1011 
1012  case PGRES_COPY_OUT:
1013  case PGRES_COPY_IN:
1014  /* nothing to do here: already processed */
1015  success = true;
1016  break;
1017 
1018  case PGRES_BAD_RESPONSE:
1019  case PGRES_NONFATAL_ERROR:
1020  case PGRES_FATAL_ERROR:
1021  success = false;
1022  break;
1023 
1024  default:
1025  success = false;
1026  pg_log_error("unexpected PQresultStatus: %d",
1027  PQresultStatus(result));
1028  break;
1029  }
1030 
1031  return success;
1032 }
1033 
1034 /*
1035  * SendQuery: send the query string to the backend
1036  * (and print out result)
1037  *
1038  * Note: This is the "front door" way to send a query. That is, use it to
1039  * send queries actually entered by the user. These queries will be subject to
1040  * single step mode.
1041  * To send "back door" queries (generated by slash commands, etc.) in a
1042  * controlled way, use PSQLexec().
1043  *
1044  * Returns true if the query executed successfully, false otherwise.
1045  */
1046 bool
1047 SendQuery(const char *query)
1048 {
1049  bool timing = pset.timing;
1050  PGTransactionStatusType transaction_status;
1051  double elapsed_msec = 0;
1052  bool OK = false;
1053  int i;
1054  bool on_error_rollback_savepoint = false;
1055  bool svpt_gone = false;
1056 
1057  if (!pset.db)
1058  {
1059  pg_log_error("You are currently not connected to a database.");
1060  goto sendquery_cleanup;
1061  }
1062 
1063  if (pset.singlestep)
1064  {
1065  char buf[3];
1066 
1067  fflush(stderr);
1068  printf(_("/**(Single step mode: verify command)******************************************/\n"
1069  "%s\n"
1070  "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1071  query);
1072  fflush(stdout);
1073  if (fgets(buf, sizeof(buf), stdin) != NULL)
1074  if (buf[0] == 'x')
1075  goto sendquery_cleanup;
1076  if (cancel_pressed)
1077  goto sendquery_cleanup;
1078  }
1079  else if (pset.echo == PSQL_ECHO_QUERIES)
1080  {
1081  puts(query);
1082  fflush(stdout);
1083  }
1084 
1085  if (pset.logfile)
1086  {
1088  _("/******** QUERY *********/\n"
1089  "%s\n"
1090  "/************************/\n\n"), query);
1091  fflush(pset.logfile);
1092  }
1093 
1095 
1096  transaction_status = PQtransactionStatus(pset.db);
1097 
1098  if (transaction_status == PQTRANS_IDLE &&
1099  !pset.autocommit &&
1100  !command_no_begin(query))
1101  {
1102  PGresult *result;
1103 
1104  result = PQexec(pset.db, "BEGIN");
1105  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1106  {
1108  ClearOrSaveResult(result);
1109  goto sendquery_cleanup;
1110  }
1111  ClearOrSaveResult(result);
1112  transaction_status = PQtransactionStatus(pset.db);
1113  }
1114 
1115  if (transaction_status == PQTRANS_INTRANS &&
1119  {
1120  PGresult *result;
1121 
1122  result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1123  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1124  {
1126  ClearOrSaveResult(result);
1127  goto sendquery_cleanup;
1128  }
1129  ClearOrSaveResult(result);
1130  on_error_rollback_savepoint = true;
1131  }
1132 
1133  if (pset.gdesc_flag)
1134  {
1135  /* Describe query's result columns, without executing it */
1136  OK = DescribeQuery(query, &elapsed_msec);
1137  }
1138  else if (pset.fetch_count <= 0 || pset.gexec_flag ||
1140  {
1141  /* Default fetch-it-all-and-print mode */
1142  OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, 0, NULL, NULL) > 0);
1143  }
1144  else
1145  {
1146  /* Fetch-in-segments mode */
1147  OK = ExecQueryUsingCursor(query, &elapsed_msec);
1148  }
1149 
1150  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1151  pg_log_info("STATEMENT: %s", query);
1152 
1153  /* If we made a temporary savepoint, possibly release/rollback */
1154  if (on_error_rollback_savepoint)
1155  {
1156  const char *svptcmd = NULL;
1157 
1158  transaction_status = PQtransactionStatus(pset.db);
1159 
1160  switch (transaction_status)
1161  {
1162  case PQTRANS_INERROR:
1163  /* We always rollback on an error */
1164  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1165  break;
1166 
1167  case PQTRANS_IDLE:
1168  /* If they are no longer in a transaction, then do nothing */
1169  break;
1170 
1171  case PQTRANS_INTRANS:
1172 
1173  /*
1174  * Release our savepoint, but do nothing if they are messing
1175  * with savepoints themselves
1176  */
1177  if (!svpt_gone)
1178  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1179  break;
1180 
1181  case PQTRANS_ACTIVE:
1182  case PQTRANS_UNKNOWN:
1183  default:
1184  OK = false;
1185  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1186  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1187  pg_log_error("unexpected transaction status (%d)",
1188  transaction_status);
1189  break;
1190  }
1191 
1192  if (svptcmd)
1193  {
1194  PGresult *svptres;
1195 
1196  svptres = PQexec(pset.db, svptcmd);
1197  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1198  {
1200  ClearOrSaveResult(svptres);
1201  OK = false;
1202 
1203  goto sendquery_cleanup;
1204  }
1205  PQclear(svptres);
1206  }
1207  }
1208 
1209  /* Possible microtiming output */
1210  if (timing)
1211  PrintTiming(elapsed_msec);
1212 
1213  /* check for events that may occur during query execution */
1214 
1215  if (pset.encoding != PQclientEncoding(pset.db) &&
1216  PQclientEncoding(pset.db) >= 0)
1217  {
1218  /* track effects of SET CLIENT_ENCODING */
1221  SetVariable(pset.vars, "ENCODING",
1223  }
1224 
1226 
1227  /* perform cleanup that should occur after any attempted query */
1228 
1229 sendquery_cleanup:
1230 
1231  /* global cancellation reset */
1232  ResetCancelConn();
1233 
1234  /* reset \g's output-to-filename trigger */
1235  if (pset.gfname)
1236  {
1237  free(pset.gfname);
1238  pset.gfname = NULL;
1239  }
1240 
1241  /* restore print settings if \g changed them */
1242  if (pset.gsavepopt)
1243  {
1245  pset.gsavepopt = NULL;
1246  }
1247 
1248  /* clean up after \bind */
1249  if (pset.bind_flag)
1250  {
1251  for (i = 0; i < pset.bind_nparams; i++)
1252  free(pset.bind_params[i]);
1254  pset.bind_params = NULL;
1255  pset.bind_flag = false;
1256  }
1257 
1258  /* reset \gset trigger */
1259  if (pset.gset_prefix)
1260  {
1262  pset.gset_prefix = NULL;
1263  }
1264 
1265  /* reset \gdesc trigger */
1266  pset.gdesc_flag = false;
1267 
1268  /* reset \gexec trigger */
1269  pset.gexec_flag = false;
1270 
1271  /* reset \crosstabview trigger */
1272  pset.crosstab_flag = false;
1273  for (i = 0; i < lengthof(pset.ctv_args); i++)
1274  {
1275  pg_free(pset.ctv_args[i]);
1276  pset.ctv_args[i] = NULL;
1277  }
1278 
1279  return OK;
1280 }
1281 
1282 
1283 /*
1284  * DescribeQuery: describe the result columns of a query, without executing it
1285  *
1286  * Returns true if the operation executed successfully, false otherwise.
1287  *
1288  * If pset.timing is on, total query time (exclusive of result-printing) is
1289  * stored into *elapsed_msec.
1290  */
1291 static bool
1292 DescribeQuery(const char *query, double *elapsed_msec)
1293 {
1294  bool timing = pset.timing;
1295  PGresult *result;
1296  bool OK;
1298  after;
1299 
1300  *elapsed_msec = 0;
1301 
1302  if (timing)
1304  else
1306 
1307  /*
1308  * To parse the query but not execute it, we prepare it, using the unnamed
1309  * prepared statement. This is invisible to psql users, since there's no
1310  * way to access the unnamed prepared statement from psql user space. The
1311  * next Parse or Query protocol message would overwrite the statement
1312  * anyway. (So there's no great need to clear it when done, which is a
1313  * good thing because libpq provides no easy way to do that.)
1314  */
1315  result = PQprepare(pset.db, "", query, 0, NULL);
1316  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1317  {
1319  SetResultVariables(result, false);
1320  ClearOrSaveResult(result);
1321  return false;
1322  }
1323  PQclear(result);
1324 
1325  result = PQdescribePrepared(pset.db, "");
1326  OK = AcceptResult(result, true) &&
1327  (PQresultStatus(result) == PGRES_COMMAND_OK);
1328  if (OK && result)
1329  {
1330  if (PQnfields(result) > 0)
1331  {
1333  int i;
1334 
1335  initPQExpBuffer(&buf);
1336 
1338  "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1339  "FROM (VALUES ",
1340  gettext_noop("Column"),
1341  gettext_noop("Type"));
1342 
1343  for (i = 0; i < PQnfields(result); i++)
1344  {
1345  const char *name;
1346  char *escname;
1347 
1348  if (i > 0)
1349  appendPQExpBufferStr(&buf, ",");
1350 
1351  name = PQfname(result, i);
1352  escname = PQescapeLiteral(pset.db, name, strlen(name));
1353 
1354  if (escname == NULL)
1355  {
1357  PQclear(result);
1358  termPQExpBuffer(&buf);
1359  return false;
1360  }
1361 
1362  appendPQExpBuffer(&buf, "(%s, '%u'::pg_catalog.oid, %d)",
1363  escname,
1364  PQftype(result, i),
1365  PQfmod(result, i));
1366 
1367  PQfreemem(escname);
1368  }
1369 
1370  appendPQExpBufferStr(&buf, ") s(name, tp, tpm)");
1371  PQclear(result);
1372 
1373  result = PQexec(pset.db, buf.data);
1374  OK = AcceptResult(result, true);
1375 
1376  if (timing)
1377  {
1378  INSTR_TIME_SET_CURRENT(after);
1379  INSTR_TIME_SUBTRACT(after, before);
1380  *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1381  }
1382 
1383  if (OK && result)
1384  OK = PrintQueryResult(result, true, NULL, NULL, NULL);
1385 
1386  termPQExpBuffer(&buf);
1387  }
1388  else
1390  _("The command has no result, or the result has no columns.\n"));
1391  }
1392 
1393  SetResultVariables(result, OK);
1394  ClearOrSaveResult(result);
1395 
1396  return OK;
1397 }
1398 
1399 
1400 /*
1401  * ExecQueryAndProcessResults: utility function for use by SendQuery()
1402  * and PSQLexecWatch().
1403  *
1404  * Sends query and cycles through PGresult objects.
1405  *
1406  * If our command string contained a COPY FROM STDIN or COPY TO STDOUT, the
1407  * PGresult associated with these commands must be processed by providing an
1408  * input or output stream. In that event, we'll marshal data for the COPY.
1409  *
1410  * For other commands, the results are processed normally, depending on their
1411  * status.
1412  *
1413  * Returns 1 on complete success, 0 on interrupt and -1 or errors. Possible
1414  * failure modes include purely client-side problems; check the transaction
1415  * status for the server-side opinion.
1416  *
1417  * Note that on a combined query, failure does not mean that nothing was
1418  * committed.
1419  */
1420 static int
1421 ExecQueryAndProcessResults(const char *query,
1422  double *elapsed_msec, bool *svpt_gone_p,
1423  bool is_watch, int min_rows,
1424  const printQueryOpt *opt, FILE *printQueryFout)
1425 {
1426  bool timing = pset.timing;
1427  bool success;
1428  bool return_early = false;
1430  after;
1431  PGresult *result;
1432  FILE *gfile_fout = NULL;
1433  bool gfile_is_pipe = false;
1434 
1435  if (timing)
1437  else
1439 
1440  if (pset.bind_flag)
1441  success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char *const *) pset.bind_params, NULL, NULL, 0);
1442  else
1443  success = PQsendQuery(pset.db, query);
1444 
1445  if (!success)
1446  {
1447  const char *error = PQerrorMessage(pset.db);
1448 
1449  if (strlen(error))
1450  pg_log_info("%s", error);
1451 
1452  CheckConnection();
1453 
1454  return -1;
1455  }
1456 
1457  /*
1458  * If SIGINT is sent while the query is processing, the interrupt will be
1459  * consumed. The user's intention, though, is to cancel the entire watch
1460  * process, so detect a sent cancellation request and exit in this case.
1461  */
1462  if (is_watch && cancel_pressed)
1463  {
1465  return 0;
1466  }
1467 
1468  /* first result */
1469  result = PQgetResult(pset.db);
1470  if (min_rows > 0 && PQntuples(result) < min_rows)
1471  {
1472  return_early = true;
1473  }
1474 
1475  while (result != NULL)
1476  {
1477  ExecStatusType result_status;
1478  PGresult *next_result;
1479  bool last;
1480 
1481  if (!AcceptResult(result, false))
1482  {
1483  /*
1484  * Some error occurred, either a server-side failure or a failure
1485  * to submit the command string. Record that.
1486  */
1487  const char *error = PQresultErrorMessage(result);
1488 
1489  if (strlen(error))
1490  pg_log_info("%s", error);
1491 
1492  CheckConnection();
1493  if (!is_watch)
1494  SetResultVariables(result, false);
1495 
1496  /* keep the result status before clearing it */
1497  result_status = PQresultStatus(result);
1498  ClearOrSaveResult(result);
1499  success = false;
1500 
1501  /*
1502  * switch to next result
1503  */
1504  if (result_status == PGRES_COPY_BOTH ||
1505  result_status == PGRES_COPY_OUT ||
1506  result_status == PGRES_COPY_IN)
1507 
1508  /*
1509  * For some obscure reason PQgetResult does *not* return a
1510  * NULL in copy cases despite the result having been cleared,
1511  * but keeps returning an "empty" result that we have to
1512  * ignore manually.
1513  */
1514  result = NULL;
1515  else
1516  result = PQgetResult(pset.db);
1517 
1518  /*
1519  * Get current timing measure in case an error occurs
1520  */
1521  if (timing)
1522  {
1523  INSTR_TIME_SET_CURRENT(after);
1524  INSTR_TIME_SUBTRACT(after, before);
1525  *elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1526  }
1527 
1528  continue;
1529  }
1530  else if (svpt_gone_p && !*svpt_gone_p)
1531  {
1532  /*
1533  * Check if the user ran any command that would destroy our
1534  * internal savepoint: If the user did COMMIT AND CHAIN, RELEASE
1535  * or ROLLBACK, our savepoint is gone. If they issued a SAVEPOINT,
1536  * releasing ours would remove theirs.
1537  */
1538  const char *cmd = PQcmdStatus(result);
1539 
1540  *svpt_gone_p = (strcmp(cmd, "COMMIT") == 0 ||
1541  strcmp(cmd, "SAVEPOINT") == 0 ||
1542  strcmp(cmd, "RELEASE") == 0 ||
1543  strcmp(cmd, "ROLLBACK") == 0);
1544  }
1545 
1546  result_status = PQresultStatus(result);
1547 
1548  /* must handle COPY before changing the current result */
1549  Assert(result_status != PGRES_COPY_BOTH);
1550  if (result_status == PGRES_COPY_IN ||
1551  result_status == PGRES_COPY_OUT)
1552  {
1553  FILE *copy_stream = NULL;
1554 
1555  /*
1556  * For COPY OUT, direct the output to the default place (probably
1557  * a pager pipe) for \watch, or to pset.copyStream for \copy,
1558  * otherwise to pset.gfname if that's set, otherwise to
1559  * pset.queryFout.
1560  */
1561  if (result_status == PGRES_COPY_OUT)
1562  {
1563  if (is_watch)
1564  {
1565  /* invoked by \watch */
1566  copy_stream = printQueryFout ? printQueryFout : pset.queryFout;
1567  }
1568  else if (pset.copyStream)
1569  {
1570  /* invoked by \copy */
1571  copy_stream = pset.copyStream;
1572  }
1573  else if (pset.gfname)
1574  {
1575  /* send to \g file, which we may have opened already */
1576  if (gfile_fout == NULL)
1577  {
1579  &gfile_fout, &gfile_is_pipe))
1580  {
1581  if (gfile_is_pipe)
1583  copy_stream = gfile_fout;
1584  }
1585  else
1586  success = false;
1587  }
1588  else
1589  copy_stream = gfile_fout;
1590  }
1591  else
1592  {
1593  /* fall back to the generic query output stream */
1594  copy_stream = pset.queryFout;
1595  }
1596  }
1597 
1598  /*
1599  * Even if the output stream could not be opened, we call
1600  * HandleCopyResult() with a NULL output stream to collect and
1601  * discard the COPY data.
1602  */
1603  success &= HandleCopyResult(&result, copy_stream);
1604  }
1605 
1606  /*
1607  * Check PQgetResult() again. In the typical case of a single-command
1608  * string, it will return NULL. Otherwise, we'll have other results
1609  * to process. We need to do that to check whether this is the last.
1610  */
1611  next_result = PQgetResult(pset.db);
1612  last = (next_result == NULL);
1613 
1614  /*
1615  * Update current timing measure.
1616  *
1617  * It will include the display of previous results, if any. This
1618  * cannot be helped because the server goes on processing further
1619  * queries anyway while the previous ones are being displayed. The
1620  * parallel execution of the client display hides the server time when
1621  * it is shorter.
1622  *
1623  * With combined queries, timing must be understood as an upper bound
1624  * of the time spent processing them.
1625  */
1626  if (timing)
1627  {
1628  INSTR_TIME_SET_CURRENT(after);
1629  INSTR_TIME_SUBTRACT(after, before);
1630  *elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1631  }
1632 
1633  /* this may or may not print something depending on settings */
1634  if (result != NULL)
1635  {
1636  /*
1637  * If results need to be printed into the file specified by \g,
1638  * open it, unless we already did. Note that when pset.gfname is
1639  * set, the passed-in value of printQueryFout is not used for
1640  * tuple output, but it's still used for status output.
1641  */
1642  FILE *tuples_fout = printQueryFout;
1643  bool do_print = true;
1644 
1645  if (PQresultStatus(result) == PGRES_TUPLES_OK &&
1646  pset.gfname)
1647  {
1648  if (gfile_fout == NULL)
1649  {
1651  &gfile_fout, &gfile_is_pipe))
1652  {
1653  if (gfile_is_pipe)
1655  }
1656  else
1657  success = do_print = false;
1658  }
1659  tuples_fout = gfile_fout;
1660  }
1661  if (do_print)
1662  success &= PrintQueryResult(result, last, opt,
1663  tuples_fout, printQueryFout);
1664  }
1665 
1666  /* set variables from last result */
1667  if (!is_watch && last)
1668  SetResultVariables(result, success);
1669 
1670  ClearOrSaveResult(result);
1671  result = next_result;
1672 
1673  if (cancel_pressed)
1674  {
1676  break;
1677  }
1678  }
1679 
1680  /* close \g file if we opened it */
1681  if (gfile_fout)
1682  {
1683  if (gfile_is_pipe)
1684  {
1685  SetShellResultVariables(pclose(gfile_fout));
1687  }
1688  else
1689  fclose(gfile_fout);
1690  }
1691 
1692  /* may need this to recover from conn loss during COPY */
1693  if (!CheckConnection())
1694  return -1;
1695 
1696  if (cancel_pressed || return_early)
1697  return 0;
1698 
1699  return success ? 1 : -1;
1700 }
1701 
1702 
1703 /*
1704  * ExecQueryUsingCursor: run a SELECT-like query using a cursor
1705  *
1706  * This feature allows result sets larger than RAM to be dealt with.
1707  *
1708  * Returns true if the query executed successfully, false otherwise.
1709  *
1710  * If pset.timing is on, total query time (exclusive of result-printing) is
1711  * stored into *elapsed_msec.
1712  */
1713 static bool
1714 ExecQueryUsingCursor(const char *query, double *elapsed_msec)
1715 {
1716  bool OK = true;
1717  PGresult *result;
1719  printQueryOpt my_popt = pset.popt;
1720  bool timing = pset.timing;
1721  FILE *fout;
1722  bool is_pipe;
1723  bool is_pager = false;
1724  bool started_txn = false;
1725  int64 total_tuples = 0;
1726  int ntuples;
1727  int fetch_count;
1728  char fetch_cmd[64];
1730  after;
1731  int flush_error;
1732 
1733  *elapsed_msec = 0;
1734 
1735  /* initialize print options for partial table output */
1736  my_popt.topt.start_table = true;
1737  my_popt.topt.stop_table = false;
1738  my_popt.topt.prior_records = 0;
1739 
1740  if (timing)
1742  else
1744 
1745  /* if we're not in a transaction, start one */
1747  {
1748  result = PQexec(pset.db, "BEGIN");
1749  OK = AcceptResult(result, true) &&
1750  (PQresultStatus(result) == PGRES_COMMAND_OK);
1751  ClearOrSaveResult(result);
1752  if (!OK)
1753  return false;
1754  started_txn = true;
1755  }
1756 
1757  /* Send DECLARE CURSOR */
1758  initPQExpBuffer(&buf);
1759  appendPQExpBuffer(&buf, "DECLARE _psql_cursor NO SCROLL CURSOR FOR\n%s",
1760  query);
1761 
1762  result = PQexec(pset.db, buf.data);
1763  OK = AcceptResult(result, true) &&
1764  (PQresultStatus(result) == PGRES_COMMAND_OK);
1765  if (!OK)
1766  SetResultVariables(result, OK);
1767  ClearOrSaveResult(result);
1768  termPQExpBuffer(&buf);
1769  if (!OK)
1770  goto cleanup;
1771 
1772  if (timing)
1773  {
1774  INSTR_TIME_SET_CURRENT(after);
1775  INSTR_TIME_SUBTRACT(after, before);
1776  *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1777  }
1778 
1779  /*
1780  * In \gset mode, we force the fetch count to be 2, so that we will throw
1781  * the appropriate error if the query returns more than one row.
1782  */
1783  if (pset.gset_prefix)
1784  fetch_count = 2;
1785  else
1786  fetch_count = pset.fetch_count;
1787 
1788  snprintf(fetch_cmd, sizeof(fetch_cmd),
1789  "FETCH FORWARD %d FROM _psql_cursor",
1790  fetch_count);
1791 
1792  /* prepare to write output to \g argument, if any */
1793  if (pset.gfname)
1794  {
1795  if (!openQueryOutputFile(pset.gfname, &fout, &is_pipe))
1796  {
1797  OK = false;
1798  goto cleanup;
1799  }
1800  if (is_pipe)
1802  }
1803  else
1804  {
1805  fout = pset.queryFout;
1806  is_pipe = false; /* doesn't matter */
1807  }
1808 
1809  /* clear any pre-existing error indication on the output stream */
1810  clearerr(fout);
1811 
1812  for (;;)
1813  {
1814  if (timing)
1816 
1817  /* get fetch_count tuples at a time */
1818  result = PQexec(pset.db, fetch_cmd);
1819 
1820  if (timing)
1821  {
1822  INSTR_TIME_SET_CURRENT(after);
1823  INSTR_TIME_SUBTRACT(after, before);
1824  *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1825  }
1826 
1827  if (PQresultStatus(result) != PGRES_TUPLES_OK)
1828  {
1829  /* shut down pager before printing error message */
1830  if (is_pager)
1831  {
1832  ClosePager(fout);
1833  is_pager = false;
1834  }
1835 
1836  OK = AcceptResult(result, true);
1837  Assert(!OK);
1838  SetResultVariables(result, OK);
1839  ClearOrSaveResult(result);
1840  break;
1841  }
1842 
1843  if (pset.gset_prefix)
1844  {
1845  /* StoreQueryTuple will complain if not exactly one row */
1846  OK = StoreQueryTuple(result);
1847  ClearOrSaveResult(result);
1848  break;
1849  }
1850 
1851  /*
1852  * Note we do not deal with \gdesc, \gexec or \crosstabview modes here
1853  */
1854 
1855  ntuples = PQntuples(result);
1856  total_tuples += ntuples;
1857 
1858  if (ntuples < fetch_count)
1859  {
1860  /* this is the last result set, so allow footer decoration */
1861  my_popt.topt.stop_table = true;
1862  }
1863  else if (fout == stdout && !is_pager)
1864  {
1865  /*
1866  * If query requires multiple result sets, hack to ensure that
1867  * only one pager instance is used for the whole mess
1868  */
1869  fout = PageOutput(INT_MAX, &(my_popt.topt));
1870  is_pager = true;
1871  }
1872 
1873  printQuery(result, &my_popt, fout, is_pager, pset.logfile);
1874 
1875  ClearOrSaveResult(result);
1876 
1877  /* after the first result set, disallow header decoration */
1878  my_popt.topt.start_table = false;
1879  my_popt.topt.prior_records += ntuples;
1880 
1881  /*
1882  * Make sure to flush the output stream, so intermediate results are
1883  * visible to the client immediately. We check the results because if
1884  * the pager dies/exits/etc, there's no sense throwing more data at
1885  * it.
1886  */
1887  flush_error = fflush(fout);
1888 
1889  /*
1890  * Check if we are at the end, if a cancel was pressed, or if there
1891  * were any errors either trying to flush out the results, or more
1892  * generally on the output stream at all. If we hit any errors
1893  * writing things to the stream, we presume $PAGER has disappeared and
1894  * stop bothering to pull down more data.
1895  */
1896  if (ntuples < fetch_count || cancel_pressed || flush_error ||
1897  ferror(fout))
1898  break;
1899  }
1900 
1901  if (pset.gfname)
1902  {
1903  /* close \g argument file/pipe */
1904  if (is_pipe)
1905  {
1906  SetShellResultVariables(pclose(fout));
1908  }
1909  else
1910  fclose(fout);
1911  }
1912  else if (is_pager)
1913  {
1914  /* close transient pager */
1915  ClosePager(fout);
1916  }
1917 
1918  if (OK)
1919  {
1920  /*
1921  * We don't have a PGresult here, and even if we did it wouldn't have
1922  * the right row count, so fake SetResultVariables(). In error cases,
1923  * we already set the result variables above.
1924  */
1925  char buf[32];
1926 
1927  SetVariable(pset.vars, "ERROR", "false");
1928  SetVariable(pset.vars, "SQLSTATE", "00000");
1929  snprintf(buf, sizeof(buf), INT64_FORMAT, total_tuples);
1930  SetVariable(pset.vars, "ROW_COUNT", buf);
1931  }
1932 
1933 cleanup:
1934  if (timing)
1936 
1937  /*
1938  * We try to close the cursor on either success or failure, but on failure
1939  * ignore the result (it's probably just a bleat about being in an aborted
1940  * transaction)
1941  */
1942  result = PQexec(pset.db, "CLOSE _psql_cursor");
1943  if (OK)
1944  {
1945  OK = AcceptResult(result, true) &&
1946  (PQresultStatus(result) == PGRES_COMMAND_OK);
1947  ClearOrSaveResult(result);
1948  }
1949  else
1950  PQclear(result);
1951 
1952  if (started_txn)
1953  {
1954  result = PQexec(pset.db, OK ? "COMMIT" : "ROLLBACK");
1955  OK &= AcceptResult(result, true) &&
1956  (PQresultStatus(result) == PGRES_COMMAND_OK);
1957  ClearOrSaveResult(result);
1958  }
1959 
1960  if (timing)
1961  {
1962  INSTR_TIME_SET_CURRENT(after);
1963  INSTR_TIME_SUBTRACT(after, before);
1964  *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1965  }
1966 
1967  return OK;
1968 }
1969 
1970 
1971 /*
1972  * Advance the given char pointer over white space and SQL comments.
1973  */
1974 static const char *
1975 skip_white_space(const char *query)
1976 {
1977  int cnestlevel = 0; /* slash-star comment nest level */
1978 
1979  while (*query)
1980  {
1981  int mblen = PQmblenBounded(query, pset.encoding);
1982 
1983  /*
1984  * Note: we assume the encoding is a superset of ASCII, so that for
1985  * example "query[0] == '/'" is meaningful. However, we do NOT assume
1986  * that the second and subsequent bytes of a multibyte character
1987  * couldn't look like ASCII characters; so it is critical to advance
1988  * by mblen, not 1, whenever we haven't exactly identified the
1989  * character we are skipping over.
1990  */
1991  if (isspace((unsigned char) *query))
1992  query += mblen;
1993  else if (query[0] == '/' && query[1] == '*')
1994  {
1995  cnestlevel++;
1996  query += 2;
1997  }
1998  else if (cnestlevel > 0 && query[0] == '*' && query[1] == '/')
1999  {
2000  cnestlevel--;
2001  query += 2;
2002  }
2003  else if (cnestlevel == 0 && query[0] == '-' && query[1] == '-')
2004  {
2005  query += 2;
2006 
2007  /*
2008  * We have to skip to end of line since any slash-star inside the
2009  * -- comment does NOT start a slash-star comment.
2010  */
2011  while (*query)
2012  {
2013  if (*query == '\n')
2014  {
2015  query++;
2016  break;
2017  }
2018  query += PQmblenBounded(query, pset.encoding);
2019  }
2020  }
2021  else if (cnestlevel > 0)
2022  query += mblen;
2023  else
2024  break; /* found first token */
2025  }
2026 
2027  return query;
2028 }
2029 
2030 
2031 /*
2032  * Check whether a command is one of those for which we should NOT start
2033  * a new transaction block (ie, send a preceding BEGIN).
2034  *
2035  * These include the transaction control statements themselves, plus
2036  * certain statements that the backend disallows inside transaction blocks.
2037  */
2038 static bool
2039 command_no_begin(const char *query)
2040 {
2041  int wordlen;
2042 
2043  /*
2044  * First we must advance over any whitespace and comments.
2045  */
2046  query = skip_white_space(query);
2047 
2048  /*
2049  * Check word length (since "beginx" is not "begin").
2050  */
2051  wordlen = 0;
2052  while (isalpha((unsigned char) query[wordlen]))
2053  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2054 
2055  /*
2056  * Transaction control commands. These should include every keyword that
2057  * gives rise to a TransactionStmt in the backend grammar, except for the
2058  * savepoint-related commands.
2059  *
2060  * (We assume that START must be START TRANSACTION, since there is
2061  * presently no other "START foo" command.)
2062  */
2063  if (wordlen == 5 && pg_strncasecmp(query, "abort", 5) == 0)
2064  return true;
2065  if (wordlen == 5 && pg_strncasecmp(query, "begin", 5) == 0)
2066  return true;
2067  if (wordlen == 5 && pg_strncasecmp(query, "start", 5) == 0)
2068  return true;
2069  if (wordlen == 6 && pg_strncasecmp(query, "commit", 6) == 0)
2070  return true;
2071  if (wordlen == 3 && pg_strncasecmp(query, "end", 3) == 0)
2072  return true;
2073  if (wordlen == 8 && pg_strncasecmp(query, "rollback", 8) == 0)
2074  return true;
2075  if (wordlen == 7 && pg_strncasecmp(query, "prepare", 7) == 0)
2076  {
2077  /* PREPARE TRANSACTION is a TC command, PREPARE foo is not */
2078  query += wordlen;
2079 
2080  query = skip_white_space(query);
2081 
2082  wordlen = 0;
2083  while (isalpha((unsigned char) query[wordlen]))
2084  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2085 
2086  if (wordlen == 11 && pg_strncasecmp(query, "transaction", 11) == 0)
2087  return true;
2088  return false;
2089  }
2090 
2091  /*
2092  * Commands not allowed within transactions. The statements checked for
2093  * here should be exactly those that call PreventInTransactionBlock() in
2094  * the backend.
2095  */
2096  if (wordlen == 6 && pg_strncasecmp(query, "vacuum", 6) == 0)
2097  return true;
2098  if (wordlen == 7 && pg_strncasecmp(query, "cluster", 7) == 0)
2099  {
2100  /* CLUSTER with any arguments is allowed in transactions */
2101  query += wordlen;
2102 
2103  query = skip_white_space(query);
2104 
2105  if (isalpha((unsigned char) query[0]))
2106  return false; /* has additional words */
2107  return true; /* it's CLUSTER without arguments */
2108  }
2109 
2110  if (wordlen == 6 && pg_strncasecmp(query, "create", 6) == 0)
2111  {
2112  query += wordlen;
2113 
2114  query = skip_white_space(query);
2115 
2116  wordlen = 0;
2117  while (isalpha((unsigned char) query[wordlen]))
2118  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2119 
2120  if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
2121  return true;
2122  if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
2123  return true;
2124 
2125  /* CREATE [UNIQUE] INDEX CONCURRENTLY isn't allowed in xacts */
2126  if (wordlen == 6 && pg_strncasecmp(query, "unique", 6) == 0)
2127  {
2128  query += wordlen;
2129 
2130  query = skip_white_space(query);
2131 
2132  wordlen = 0;
2133  while (isalpha((unsigned char) query[wordlen]))
2134  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2135  }
2136 
2137  if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
2138  {
2139  query += wordlen;
2140 
2141  query = skip_white_space(query);
2142 
2143  wordlen = 0;
2144  while (isalpha((unsigned char) query[wordlen]))
2145  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2146 
2147  if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2148  return true;
2149  }
2150 
2151  return false;
2152  }
2153 
2154  if (wordlen == 5 && pg_strncasecmp(query, "alter", 5) == 0)
2155  {
2156  query += wordlen;
2157 
2158  query = skip_white_space(query);
2159 
2160  wordlen = 0;
2161  while (isalpha((unsigned char) query[wordlen]))
2162  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2163 
2164  /* ALTER SYSTEM isn't allowed in xacts */
2165  if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
2166  return true;
2167 
2168  return false;
2169  }
2170 
2171  /*
2172  * Note: these tests will match DROP SYSTEM and REINDEX TABLESPACE, which
2173  * aren't really valid commands so we don't care much. The other four
2174  * possible matches are correct.
2175  */
2176  if ((wordlen == 4 && pg_strncasecmp(query, "drop", 4) == 0) ||
2177  (wordlen == 7 && pg_strncasecmp(query, "reindex", 7) == 0))
2178  {
2179  query += wordlen;
2180 
2181  query = skip_white_space(query);
2182 
2183  wordlen = 0;
2184  while (isalpha((unsigned char) query[wordlen]))
2185  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2186 
2187  if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
2188  return true;
2189  if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
2190  return true;
2191  if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
2192  return true;
2193  if (wordlen == 5 && (pg_strncasecmp(query, "index", 5) == 0 ||
2194  pg_strncasecmp(query, "table", 5) == 0))
2195  {
2196  query += wordlen;
2197  query = skip_white_space(query);
2198  wordlen = 0;
2199  while (isalpha((unsigned char) query[wordlen]))
2200  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2201 
2202  /*
2203  * REINDEX [ TABLE | INDEX ] CONCURRENTLY are not allowed in
2204  * xacts.
2205  */
2206  if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2207  return true;
2208  }
2209 
2210  /* DROP INDEX CONCURRENTLY isn't allowed in xacts */
2211  if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
2212  {
2213  query += wordlen;
2214 
2215  query = skip_white_space(query);
2216 
2217  wordlen = 0;
2218  while (isalpha((unsigned char) query[wordlen]))
2219  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2220 
2221  if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2222  return true;
2223 
2224  return false;
2225  }
2226 
2227  return false;
2228  }
2229 
2230  /* DISCARD ALL isn't allowed in xacts, but other variants are allowed. */
2231  if (wordlen == 7 && pg_strncasecmp(query, "discard", 7) == 0)
2232  {
2233  query += wordlen;
2234 
2235  query = skip_white_space(query);
2236 
2237  wordlen = 0;
2238  while (isalpha((unsigned char) query[wordlen]))
2239  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2240 
2241  if (wordlen == 3 && pg_strncasecmp(query, "all", 3) == 0)
2242  return true;
2243  return false;
2244  }
2245 
2246  return false;
2247 }
2248 
2249 
2250 /*
2251  * Check whether the specified command is a SELECT (or VALUES).
2252  */
2253 static bool
2254 is_select_command(const char *query)
2255 {
2256  int wordlen;
2257 
2258  /*
2259  * First advance over any whitespace, comments and left parentheses.
2260  */
2261  for (;;)
2262  {
2263  query = skip_white_space(query);
2264  if (query[0] == '(')
2265  query++;
2266  else
2267  break;
2268  }
2269 
2270  /*
2271  * Check word length (since "selectx" is not "select").
2272  */
2273  wordlen = 0;
2274  while (isalpha((unsigned char) query[wordlen]))
2275  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2276 
2277  if (wordlen == 6 && pg_strncasecmp(query, "select", 6) == 0)
2278  return true;
2279 
2280  if (wordlen == 6 && pg_strncasecmp(query, "values", 6) == 0)
2281  return true;
2282 
2283  return false;
2284 }
2285 
2286 
2287 /*
2288  * Test if the current user is a database superuser.
2289  */
2290 bool
2292 {
2293  const char *val;
2294 
2295  if (!pset.db)
2296  return false;
2297 
2298  val = PQparameterStatus(pset.db, "is_superuser");
2299 
2300  if (val && strcmp(val, "on") == 0)
2301  return true;
2302 
2303  return false;
2304 }
2305 
2306 
2307 /*
2308  * Test if the current session uses standard string literals.
2309  */
2310 bool
2312 {
2313  const char *val;
2314 
2315  if (!pset.db)
2316  return false;
2317 
2318  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2319 
2320  if (val && strcmp(val, "on") == 0)
2321  return true;
2322 
2323  return false;
2324 }
2325 
2326 
2327 /*
2328  * Return the session user of the current connection.
2329  */
2330 const char *
2332 {
2333  const char *val;
2334 
2335  if (!pset.db)
2336  return NULL;
2337 
2338  val = PQparameterStatus(pset.db, "session_authorization");
2339  if (val)
2340  return val;
2341  else
2342  return PQuser(pset.db);
2343 }
2344 
2345 
2346 /* expand_tilde
2347  *
2348  * substitute '~' with HOME or '~username' with username's home dir
2349  *
2350  */
2351 void
2353 {
2354  if (!filename || !(*filename))
2355  return;
2356 
2357  /*
2358  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2359  * for short versions of long file names, though the tilde is usually
2360  * toward the end, not at the beginning.
2361  */
2362 #ifndef WIN32
2363 
2364  /* try tilde expansion */
2365  if (**filename == '~')
2366  {
2367  char *fn;
2368  char oldp,
2369  *p;
2370  struct passwd *pw;
2371  char home[MAXPGPATH];
2372 
2373  fn = *filename;
2374  *home = '\0';
2375 
2376  p = fn + 1;
2377  while (*p != '/' && *p != '\0')
2378  p++;
2379 
2380  oldp = *p;
2381  *p = '\0';
2382 
2383  if (*(fn + 1) == '\0')
2384  get_home_path(home); /* ~ or ~/ only */
2385  else if ((pw = getpwnam(fn + 1)) != NULL)
2386  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2387 
2388  *p = oldp;
2389  if (strlen(home) != 0)
2390  {
2391  char *newfn;
2392 
2393  newfn = psprintf("%s%s", home, p);
2394  free(fn);
2395  *filename = newfn;
2396  }
2397  }
2398 #endif
2399 }
2400 
2401 /*
2402  * Checks if connection string starts with either of the valid URI prefix
2403  * designators.
2404  *
2405  * Returns the URI prefix length, 0 if the string doesn't contain a URI prefix.
2406  *
2407  * XXX This is a duplicate of the eponymous libpq function.
2408  */
2409 static int
2411 {
2412  /* The connection URI must start with either of the following designators: */
2413  static const char uri_designator[] = "postgresql://";
2414  static const char short_uri_designator[] = "postgres://";
2415 
2416  if (strncmp(connstr, uri_designator,
2417  sizeof(uri_designator) - 1) == 0)
2418  return sizeof(uri_designator) - 1;
2419 
2420  if (strncmp(connstr, short_uri_designator,
2421  sizeof(short_uri_designator) - 1) == 0)
2422  return sizeof(short_uri_designator) - 1;
2423 
2424  return 0;
2425 }
2426 
2427 /*
2428  * Recognized connection string either starts with a valid URI prefix or
2429  * contains a "=" in it.
2430  *
2431  * Must be consistent with parse_connection_string: anything for which this
2432  * returns true should at least look like it's parseable by that routine.
2433  *
2434  * XXX This is a duplicate of the eponymous libpq function.
2435  */
2436 bool
2438 {
2439  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2440 }
const char *const days[]
Definition: datetime.c:84
void expand_tilde(char **filename)
Definition: common.c:2352
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:2410
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1292
static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec)
Definition: common.c:1714
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:2254
char * psql_get_variable(const char *varname, PsqlScanQuoteType quote, void *passthrough)
Definition: common.c:138
const char * session_username(void)
Definition: common.c:2331
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:2311
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:1975
static bool ConnectionUp(void)
Definition: common.c:286
bool is_superuser(void)
Definition: common.c:2291
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:1421
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:58
bool recognized_connection_string(const char *connstr)
Definition: common.c:2437
static void PrintTiming(double elapsed_msec)
Definition: common.c:522
bool SendQuery(const char *query)
Definition: common.c:1047
static bool command_no_begin(const char *query)
Definition: common.c:2039
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:682
#define gettext_noop(x)
Definition: c.h:1183
#define INT64_FORMAT
Definition: c.h:535
#define lengthof(array)
Definition: c.h:775
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:4958
void UnsyncVariables(void)
Definition: command.c:3919
void SyncVariables(void)
Definition: command.c:3878
void connection_warnings(bool in_startup)
Definition: command.c:3753
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
bool PrintResultInCrosstab(const PGresult *res)
Definition: crosstabview.c:104
#define _(x)
Definition: elog.c:90
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6913
void PQreset(PGconn *conn)
Definition: fe-connect.c:4683
static const char short_uri_designator[]
Definition: fe-connect.c:387
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6903
static const char uri_designator[]
Definition: fe-connect.c:386
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6948
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6895
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7036
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4669
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:6802
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:1501
int PQbinaryTuples(const PGresult *res)
Definition: fe-exec.c:3457
void PQfreemem(void *ptr)
Definition: fe-exec.c:3992
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2629
PGresult * PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Definition: fe-exec.c:2268
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3679
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4270
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3371
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2417
char * PQcmdTuples(PGresult *res)
Definition: fe-exec.c:3782
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:3387
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3441
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:3527
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4264
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2224
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1960
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3836
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3712
int PQfmod(const PGresult *res, int field_num)
Definition: fe-exec.c:3701
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3861
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1425
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3426
Oid PQoidValue(const PGresult *res)
Definition: fe-exec.c:3753
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3449
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2038
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:3549
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 @150 value
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:73
@ CONNECTION_BAD
Definition: libpq-fe.h:61
ExecStatusType
Definition: libpq-fe.h:98
@ PGRES_COPY_IN
Definition: libpq-fe.h:107
@ PGRES_COPY_BOTH
Definition: libpq-fe.h:112
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:100
@ PGRES_FATAL_ERROR
Definition: libpq-fe.h:111
@ PGRES_COPY_OUT
Definition: libpq-fe.h:106
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:99
@ PGRES_BAD_RESPONSE
Definition: libpq-fe.h:108
@ PGRES_NONFATAL_ERROR
Definition: libpq-fe.h:110
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:103
PGTransactionStatusType
Definition: libpq-fe.h:120
@ PQTRANS_INTRANS
Definition: libpq-fe.h:123
@ PQTRANS_IDLE
Definition: libpq-fe.h:121
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:122
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:125
@ PQTRANS_INERROR
Definition: libpq-fe.h:124
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:90
static char * filename
Definition: pg_dumpall.c:121
static char * buf
Definition: pg_test_fsync.c:73
#define pg_encoding_to_char
Definition: pg_wchar.h:569
#define pg_log_warning(...)
Definition: pgfnames.c:24
bool get_home_path(char *ret_path)
Definition: path.c:927
#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:198
char * relname
Definition: libpq-fe.h:197
char * extra
Definition: libpq-fe.h:199
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