PostgreSQL Source Code  git master
common.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <pwd.h>
#include <signal.h>
#include <unistd.h>
#include "command.h"
#include "common.h"
#include "common/logging.h"
#include "copy.h"
#include "crosstabview.h"
#include "fe_utils/cancel.h"
#include "fe_utils/mbprint.h"
#include "fe_utils/string_utils.h"
#include "portability/instr_time.h"
#include "settings.h"
Include dependency graph for common.c:

Go to the source code of this file.

Functions

static bool DescribeQuery (const char *query, double *elapsed_msec)
 
static int ExecQueryAndProcessResults (const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, int min_rows, const printQueryOpt *opt, FILE *printQueryFout)
 
static bool command_no_begin (const char *query)
 
bool openQueryOutputFile (const char *fname, FILE **fout, bool *is_pipe)
 
static bool SetupGOutput (FILE **gfile_fout, bool *is_pipe)
 
static void CloseGOutput (FILE *gfile_fout, bool is_pipe)
 
bool setQFout (const char *fname)
 
char * psql_get_variable (const char *varname, PsqlScanQuoteType quote, void *passthrough)
 
void NoticeProcessor (void *arg, const char *message)
 
static void psql_cancel_callback (void)
 
void psql_setup_cancel_handler (void)
 
static bool ConnectionUp (void)
 
static bool CheckConnection (void)
 
static bool AcceptResult (const PGresult *result, bool show_error)
 
static void SetResultVariables (PGresult *result, bool success)
 
void SetShellResultVariables (int wait_result)
 
static void ClearOrSaveResult (PGresult *result)
 
static void ClearOrSaveAllResults (void)
 
static void PrintTiming (double elapsed_msec)
 
PGresultPSQLexec (const char *query)
 
int PSQLexecWatch (const char *query, const printQueryOpt *opt, FILE *printQueryFout, int min_rows)
 
static void PrintNotifications (void)
 
static bool PrintQueryTuples (const PGresult *result, const printQueryOpt *opt, FILE *printQueryFout)
 
static bool StoreQueryTuple (const PGresult *result)
 
static bool ExecQueryTuples (const PGresult *result)
 
static bool HandleCopyResult (PGresult **resultp, FILE *copystream)
 
static void PrintQueryStatus (PGresult *result, FILE *printQueryFout)
 
static bool PrintQueryResult (PGresult *result, bool last, const printQueryOpt *opt, FILE *printQueryFout, FILE *printStatusFout)
 
bool SendQuery (const char *query)
 
static const char * skip_white_space (const char *query)
 
bool is_superuser (void)
 
bool standard_strings (void)
 
const char * session_username (void)
 
void expand_tilde (char **filename)
 
static int uri_prefix_length (const char *connstr)
 
void clean_extended_state (void)
 
bool recognized_connection_string (const char *connstr)
 

Variables

volatile sig_atomic_t sigint_interrupt_enabled = false
 
sigjmp_buf sigint_interrupt_jmp
 

Function Documentation

◆ AcceptResult()

static bool AcceptResult ( const PGresult result,
bool  show_error 
)
static

Definition at line 403 of file common.c.

404 {
405  bool OK;
406 
407  if (!result)
408  OK = false;
409  else
410  switch (PQresultStatus(result))
411  {
412  case PGRES_COMMAND_OK:
413  case PGRES_TUPLES_OK:
414  case PGRES_TUPLES_CHUNK:
415  case PGRES_EMPTY_QUERY:
416  case PGRES_COPY_IN:
417  case PGRES_COPY_OUT:
418  /* Fine, do nothing */
419  OK = true;
420  break;
421 
422  case PGRES_BAD_RESPONSE:
424  case PGRES_FATAL_ERROR:
425  OK = false;
426  break;
427 
428  default:
429  OK = false;
430  pg_log_error("unexpected PQresultStatus: %d",
431  PQresultStatus(result));
432  break;
433  }
434 
435  if (!OK && show_error)
436  {
437  const char *error = PQerrorMessage(pset.db);
438 
439  if (strlen(error))
440  pg_log_info("%s", error);
441 
442  CheckConnection();
443  }
444 
445  return OK;
446 }
static bool CheckConnection(void)
Definition: common.c:342
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7200
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
@ PGRES_COPY_IN
Definition: libpq-fe.h:127
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:120
@ PGRES_TUPLES_CHUNK
Definition: libpq-fe.h:137
@ PGRES_FATAL_ERROR
Definition: libpq-fe.h:131
@ PGRES_COPY_OUT
Definition: libpq-fe.h:126
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:119
@ PGRES_BAD_RESPONSE
Definition: libpq-fe.h:128
@ PGRES_NONFATAL_ERROR
Definition: libpq-fe.h:130
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:123
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_info(...)
Definition: logging.h:124
PsqlSettings pset
Definition: startup.c:32
static void error(void)
Definition: sql-dyntest.c:147
PGconn * db
Definition: settings.h:91

References CheckConnection(), _psqlSettings::db, error(), pg_log_error, pg_log_info, PGRES_BAD_RESPONSE, PGRES_COMMAND_OK, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_EMPTY_QUERY, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PGRES_TUPLES_CHUNK, PGRES_TUPLES_OK, PQerrorMessage(), PQresultStatus(), and pset.

Referenced by DescribeQuery(), ExecQueryAndProcessResults(), and PSQLexec().

◆ CheckConnection()

static bool CheckConnection ( void  )
static

Definition at line 342 of file common.c.

343 {
344  bool OK;
345 
346  OK = ConnectionUp();
347  if (!OK)
348  {
350  {
351  pg_log_error("connection to server was lost");
353  }
354 
355  fprintf(stderr, _("The connection to the server was lost. Attempting reset: "));
356  PQreset(pset.db);
357  OK = ConnectionUp();
358  if (!OK)
359  {
360  fprintf(stderr, _("Failed.\n"));
361 
362  /*
363  * Transition to having no connection; but stash away the failed
364  * connection so that we can still refer to its parameters in a
365  * later \connect attempt. Keep the state cleanup here in sync
366  * with do_connect().
367  */
368  if (pset.dead_conn)
370  pset.dead_conn = pset.db;
371  pset.db = NULL;
372  ResetCancelConn();
373  UnsyncVariables();
374  }
375  else
376  {
377  fprintf(stderr, _("Succeeded.\n"));
378 
379  /*
380  * Re-sync, just in case anything changed. Keep this in sync with
381  * do_connect().
382  */
383  SyncVariables();
384  connection_warnings(false); /* Must be after SyncVariables */
385  }
386  }
387 
388  return OK;
389 }
static bool ConnectionUp(void)
Definition: common.c:324
void ResetCancelConn(void)
Definition: cancel.c:107
void UnsyncVariables(void)
Definition: command.c:4115
void SyncVariables(void)
Definition: command.c:4074
void connection_warnings(bool in_startup)
Definition: command.c:3946
#define _(x)
Definition: elog.c:90
void PQreset(PGconn *conn)
Definition: fe-connect.c:4893
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4879
exit(1)
#define fprintf
Definition: port.h:242
#define EXIT_BADCONN
Definition: settings.h:181
PGconn * dead_conn
Definition: settings.h:140
bool cur_cmd_interactive
Definition: settings.h:122

References _, connection_warnings(), ConnectionUp(), _psqlSettings::cur_cmd_interactive, _psqlSettings::db, _psqlSettings::dead_conn, exit(), EXIT_BADCONN, fprintf, pg_log_error, PQfinish(), PQreset(), pset, ResetCancelConn(), SyncVariables(), and UnsyncVariables().

Referenced by AcceptResult(), and ExecQueryAndProcessResults().

◆ clean_extended_state()

void clean_extended_state ( void  )

Definition at line 2279 of file common.c.

2280 {
2281  int i;
2282 
2283  switch (pset.send_mode)
2284  {
2285  case PSQL_SEND_EXTENDED_CLOSE: /* \close */
2286  free(pset.stmtName);
2287  break;
2288  case PSQL_SEND_EXTENDED_PARSE: /* \parse */
2289  free(pset.stmtName);
2290  break;
2291  case PSQL_SEND_EXTENDED_QUERY_PARAMS: /* \bind */
2292  case PSQL_SEND_EXTENDED_QUERY_PREPARED: /* \bind_named */
2293  for (i = 0; i < pset.bind_nparams; i++)
2294  free(pset.bind_params[i]);
2296  free(pset.stmtName);
2297  pset.bind_params = NULL;
2298  break;
2299  case PSQL_SEND_QUERY:
2300  break;
2301  }
2302 
2303  pset.stmtName = NULL;
2305 }
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:72
@ PSQL_SEND_QUERY
Definition: settings.h:67
@ PSQL_SEND_EXTENDED_QUERY_PARAMS
Definition: settings.h:70
@ PSQL_SEND_EXTENDED_PARSE
Definition: settings.h:69
@ PSQL_SEND_EXTENDED_CLOSE
Definition: settings.h:68
@ PSQL_SEND_EXTENDED_QUERY_PREPARED
Definition: settings.h:71
char * stmtName
Definition: settings.h:112
char ** bind_params
Definition: settings.h:111
PSQL_SEND_MODE send_mode
Definition: settings.h:108
int bind_nparams
Definition: settings.h:110

References _psqlSettings::bind_nparams, _psqlSettings::bind_params, free, i, pset, PSQL_SEND_EXTENDED_CLOSE, PSQL_SEND_EXTENDED_PARSE, PSQL_SEND_EXTENDED_QUERY_PARAMS, PSQL_SEND_EXTENDED_QUERY_PREPARED, PSQL_SEND_QUERY, _psqlSettings::send_mode, and _psqlSettings::stmtName.

Referenced by exec_command_bind(), exec_command_bind_named(), exec_command_close(), exec_command_parse(), and SendQuery().

◆ ClearOrSaveAllResults()

static void ClearOrSaveAllResults ( void  )
static

Definition at line 547 of file common.c.

548 {
549  PGresult *result;
550 
551  while ((result = PQgetResult(pset.db)) != NULL)
552  ClearOrSaveResult(result);
553 }
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:523
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2062

References ClearOrSaveResult(), _psqlSettings::db, PQgetResult(), and pset.

Referenced by ExecQueryAndProcessResults().

◆ ClearOrSaveResult()

static void ClearOrSaveResult ( PGresult result)
static

Definition at line 523 of file common.c.

524 {
525  if (result)
526  {
527  switch (PQresultStatus(result))
528  {
530  case PGRES_FATAL_ERROR:
532  pset.last_error_result = result;
533  break;
534 
535  default:
536  PQclear(result);
537  break;
538  }
539  }
540 }
PGresult * last_error_result
Definition: settings.h:98

References _psqlSettings::last_error_result, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PQclear(), PQresultStatus(), and pset.

Referenced by ClearOrSaveAllResults(), DescribeQuery(), ExecQueryAndProcessResults(), PSQLexec(), and SendQuery().

◆ CloseGOutput()

static void CloseGOutput ( FILE *  gfile_fout,
bool  is_pipe 
)
static

Definition at line 110 of file common.c.

111 {
112  if (gfile_fout)
113  {
114  if (is_pipe)
115  {
116  SetShellResultVariables(pclose(gfile_fout));
118  }
119  else
120  fclose(gfile_fout);
121  }
122 }
void SetShellResultVariables(int wait_result)
Definition: common.c:501
void restore_sigpipe_trap(void)
Definition: print.c:3062

References restore_sigpipe_trap(), and SetShellResultVariables().

Referenced by ExecQueryAndProcessResults().

◆ command_no_begin()

static bool command_no_begin ( const char *  query)
static

Definition at line 1919 of file common.c.

1920 {
1921  int wordlen;
1922 
1923  /*
1924  * First we must advance over any whitespace and comments.
1925  */
1926  query = skip_white_space(query);
1927 
1928  /*
1929  * Check word length (since "beginx" is not "begin").
1930  */
1931  wordlen = 0;
1932  while (isalpha((unsigned char) query[wordlen]))
1933  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
1934 
1935  /*
1936  * Transaction control commands. These should include every keyword that
1937  * gives rise to a TransactionStmt in the backend grammar, except for the
1938  * savepoint-related commands.
1939  *
1940  * (We assume that START must be START TRANSACTION, since there is
1941  * presently no other "START foo" command.)
1942  */
1943  if (wordlen == 5 && pg_strncasecmp(query, "abort", 5) == 0)
1944  return true;
1945  if (wordlen == 5 && pg_strncasecmp(query, "begin", 5) == 0)
1946  return true;
1947  if (wordlen == 5 && pg_strncasecmp(query, "start", 5) == 0)
1948  return true;
1949  if (wordlen == 6 && pg_strncasecmp(query, "commit", 6) == 0)
1950  return true;
1951  if (wordlen == 3 && pg_strncasecmp(query, "end", 3) == 0)
1952  return true;
1953  if (wordlen == 8 && pg_strncasecmp(query, "rollback", 8) == 0)
1954  return true;
1955  if (wordlen == 7 && pg_strncasecmp(query, "prepare", 7) == 0)
1956  {
1957  /* PREPARE TRANSACTION is a TC command, PREPARE foo is not */
1958  query += wordlen;
1959 
1960  query = skip_white_space(query);
1961 
1962  wordlen = 0;
1963  while (isalpha((unsigned char) query[wordlen]))
1964  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
1965 
1966  if (wordlen == 11 && pg_strncasecmp(query, "transaction", 11) == 0)
1967  return true;
1968  return false;
1969  }
1970 
1971  /*
1972  * Commands not allowed within transactions. The statements checked for
1973  * here should be exactly those that call PreventInTransactionBlock() in
1974  * the backend.
1975  */
1976  if (wordlen == 6 && pg_strncasecmp(query, "vacuum", 6) == 0)
1977  return true;
1978  if (wordlen == 7 && pg_strncasecmp(query, "cluster", 7) == 0)
1979  {
1980  /* CLUSTER with any arguments is allowed in transactions */
1981  query += wordlen;
1982 
1983  query = skip_white_space(query);
1984 
1985  if (isalpha((unsigned char) query[0]))
1986  return false; /* has additional words */
1987  return true; /* it's CLUSTER without arguments */
1988  }
1989 
1990  if (wordlen == 6 && pg_strncasecmp(query, "create", 6) == 0)
1991  {
1992  query += wordlen;
1993 
1994  query = skip_white_space(query);
1995 
1996  wordlen = 0;
1997  while (isalpha((unsigned char) query[wordlen]))
1998  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
1999 
2000  if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
2001  return true;
2002  if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
2003  return true;
2004 
2005  /* CREATE [UNIQUE] INDEX CONCURRENTLY isn't allowed in xacts */
2006  if (wordlen == 6 && pg_strncasecmp(query, "unique", 6) == 0)
2007  {
2008  query += wordlen;
2009 
2010  query = skip_white_space(query);
2011 
2012  wordlen = 0;
2013  while (isalpha((unsigned char) query[wordlen]))
2014  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2015  }
2016 
2017  if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
2018  {
2019  query += wordlen;
2020 
2021  query = skip_white_space(query);
2022 
2023  wordlen = 0;
2024  while (isalpha((unsigned char) query[wordlen]))
2025  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2026 
2027  if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2028  return true;
2029  }
2030 
2031  return false;
2032  }
2033 
2034  if (wordlen == 5 && pg_strncasecmp(query, "alter", 5) == 0)
2035  {
2036  query += wordlen;
2037 
2038  query = skip_white_space(query);
2039 
2040  wordlen = 0;
2041  while (isalpha((unsigned char) query[wordlen]))
2042  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2043 
2044  /* ALTER SYSTEM isn't allowed in xacts */
2045  if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
2046  return true;
2047 
2048  return false;
2049  }
2050 
2051  /*
2052  * Note: these tests will match DROP SYSTEM and REINDEX TABLESPACE, which
2053  * aren't really valid commands so we don't care much. The other four
2054  * possible matches are correct.
2055  */
2056  if ((wordlen == 4 && pg_strncasecmp(query, "drop", 4) == 0) ||
2057  (wordlen == 7 && pg_strncasecmp(query, "reindex", 7) == 0))
2058  {
2059  query += wordlen;
2060 
2061  query = skip_white_space(query);
2062 
2063  wordlen = 0;
2064  while (isalpha((unsigned char) query[wordlen]))
2065  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2066 
2067  if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
2068  return true;
2069  if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
2070  return true;
2071  if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
2072  return true;
2073  if (wordlen == 5 && (pg_strncasecmp(query, "index", 5) == 0 ||
2074  pg_strncasecmp(query, "table", 5) == 0))
2075  {
2076  query += wordlen;
2077  query = skip_white_space(query);
2078  wordlen = 0;
2079  while (isalpha((unsigned char) query[wordlen]))
2080  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2081 
2082  /*
2083  * REINDEX [ TABLE | INDEX ] CONCURRENTLY are not allowed in
2084  * xacts.
2085  */
2086  if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2087  return true;
2088  }
2089 
2090  /* DROP INDEX CONCURRENTLY isn't allowed in xacts */
2091  if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
2092  {
2093  query += wordlen;
2094 
2095  query = skip_white_space(query);
2096 
2097  wordlen = 0;
2098  while (isalpha((unsigned char) query[wordlen]))
2099  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2100 
2101  if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2102  return true;
2103 
2104  return false;
2105  }
2106 
2107  return false;
2108  }
2109 
2110  /* DISCARD ALL isn't allowed in xacts, but other variants are allowed. */
2111  if (wordlen == 7 && pg_strncasecmp(query, "discard", 7) == 0)
2112  {
2113  query += wordlen;
2114 
2115  query = skip_white_space(query);
2116 
2117  wordlen = 0;
2118  while (isalpha((unsigned char) query[wordlen]))
2119  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2120 
2121  if (wordlen == 3 && pg_strncasecmp(query, "all", 3) == 0)
2122  return true;
2123  return false;
2124  }
2125 
2126  return false;
2127 }
static const char * skip_white_space(const char *query)
Definition: common.c:1855
int PQmblenBounded(const char *s, int encoding)
Definition: fe-misc.c:1234
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
int encoding
Definition: settings.h:92

References _psqlSettings::encoding, pg_strncasecmp(), PQmblenBounded(), pset, and skip_white_space().

Referenced by SendQuery().

◆ ConnectionUp()

static bool ConnectionUp ( void  )
static

Definition at line 324 of file common.c.

325 {
326  return PQstatus(pset.db) != CONNECTION_BAD;
327 }
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7137
@ CONNECTION_BAD
Definition: libpq-fe.h:82

References CONNECTION_BAD, _psqlSettings::db, PQstatus(), and pset.

Referenced by CheckConnection(), and SendQuery().

◆ DescribeQuery()

static bool DescribeQuery ( const char *  query,
double *  elapsed_msec 
)
static

Definition at line 1314 of file common.c.

1315 {
1316  bool timing = pset.timing;
1317  PGresult *result;
1318  bool OK;
1320  after;
1321 
1322  *elapsed_msec = 0;
1323 
1324  if (timing)
1326  else
1328 
1329  /*
1330  * To parse the query but not execute it, we prepare it, using the unnamed
1331  * prepared statement. This is invisible to psql users, since there's no
1332  * way to access the unnamed prepared statement from psql user space. The
1333  * next Parse or Query protocol message would overwrite the statement
1334  * anyway. (So there's no great need to clear it when done, which is a
1335  * good thing because libpq provides no easy way to do that.)
1336  */
1337  result = PQprepare(pset.db, "", query, 0, NULL);
1338  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1339  {
1341  SetResultVariables(result, false);
1342  ClearOrSaveResult(result);
1343  return false;
1344  }
1345  PQclear(result);
1346 
1347  result = PQdescribePrepared(pset.db, "");
1348  OK = AcceptResult(result, true) &&
1349  (PQresultStatus(result) == PGRES_COMMAND_OK);
1350  if (OK && result)
1351  {
1352  if (PQnfields(result) > 0)
1353  {
1355  int i;
1356 
1357  initPQExpBuffer(&buf);
1358 
1360  "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1361  "FROM (VALUES ",
1362  gettext_noop("Column"),
1363  gettext_noop("Type"));
1364 
1365  for (i = 0; i < PQnfields(result); i++)
1366  {
1367  const char *name;
1368  char *escname;
1369 
1370  if (i > 0)
1371  appendPQExpBufferStr(&buf, ",");
1372 
1373  name = PQfname(result, i);
1374  escname = PQescapeLiteral(pset.db, name, strlen(name));
1375 
1376  if (escname == NULL)
1377  {
1379  PQclear(result);
1380  termPQExpBuffer(&buf);
1381  return false;
1382  }
1383 
1384  appendPQExpBuffer(&buf, "(%s, '%u'::pg_catalog.oid, %d)",
1385  escname,
1386  PQftype(result, i),
1387  PQfmod(result, i));
1388 
1389  PQfreemem(escname);
1390  }
1391 
1392  appendPQExpBufferStr(&buf, ") s(name, tp, tpm)");
1393  PQclear(result);
1394 
1395  result = PQexec(pset.db, buf.data);
1396  OK = AcceptResult(result, true);
1397 
1398  if (timing)
1399  {
1400  INSTR_TIME_SET_CURRENT(after);
1401  INSTR_TIME_SUBTRACT(after, before);
1402  *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1403  }
1404 
1405  if (OK && result)
1406  OK = PrintQueryResult(result, true, NULL, NULL, NULL);
1407 
1408  termPQExpBuffer(&buf);
1409  }
1410  else
1412  _("The command has no result, or the result has no columns.\n"));
1413  }
1414 
1415  SetResultVariables(result, OK);
1416  ClearOrSaveResult(result);
1417 
1418  return OK;
1419 }
static bool PrintQueryResult(PGresult *result, bool last, const printQueryOpt *opt, FILE *printQueryFout, FILE *printStatusFout)
Definition: common.c:1004
static void SetResultVariables(PGresult *result, bool success)
Definition: common.c:461
static bool AcceptResult(const PGresult *result, bool show_error)
Definition: common.c:403
#define gettext_noop(x)
Definition: c.h:1175
void PQfreemem(void *ptr)
Definition: fe-exec.c:4032
PGresult * PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Definition: fe-exec.c:2306
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3719
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2455
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:3567
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4304
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
int PQfmod(const PGresult *res, int field_num)
Definition: fe-exec.c:3741
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3489
#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
static char * buf
Definition: pg_test_fsync.c:72
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
static int before(chr x, chr y)
Definition: regc_locale.c:488
FILE * queryFout
Definition: settings.h:93
const char * name

References _, AcceptResult(), appendPQExpBuffer(), appendPQExpBufferStr(), before(), buf, ClearOrSaveResult(), _psqlSettings::db, fprintf, gettext_noop, i, initPQExpBuffer(), INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, INSTR_TIME_SUBTRACT, name, pg_log_info, PGRES_COMMAND_OK, PQclear(), PQdescribePrepared(), PQerrorMessage(), PQescapeLiteral(), PQexec(), PQfmod(), PQfname(), PQfreemem(), PQftype(), PQnfields(), PQprepare(), PQresultStatus(), printfPQExpBuffer(), PrintQueryResult(), pset, _psqlSettings::queryFout, SetResultVariables(), termPQExpBuffer(), and _psqlSettings::timing.

Referenced by SendQuery().

◆ ExecQueryAndProcessResults()

static int ExecQueryAndProcessResults ( const char *  query,
double *  elapsed_msec,
bool *  svpt_gone_p,
bool  is_watch,
int  min_rows,
const printQueryOpt opt,
FILE *  printQueryFout 
)
static

Definition at line 1446 of file common.c.

1450 {
1451  bool timing = pset.timing;
1452  bool success = false;
1453  bool return_early = false;
1455  after;
1456  PGresult *result;
1457  FILE *gfile_fout = NULL;
1458  bool gfile_is_pipe = false;
1459 
1460  if (timing)
1462  else
1464 
1465  switch (pset.send_mode)
1466  {
1469  break;
1471  success = PQsendPrepare(pset.db, pset.stmtName, query, 0, NULL);
1472  break;
1474  Assert(pset.stmtName == NULL);
1475  success = PQsendQueryParams(pset.db, query,
1476  pset.bind_nparams, NULL,
1477  (const char *const *) pset.bind_params,
1478  NULL, NULL, 0);
1479  break;
1481  Assert(pset.stmtName != NULL);
1484  (const char *const *) pset.bind_params,
1485  NULL, NULL, 0);
1486  break;
1487  case PSQL_SEND_QUERY:
1488  success = PQsendQuery(pset.db, query);
1489  break;
1490  }
1491 
1492  if (!success)
1493  {
1494  const char *error = PQerrorMessage(pset.db);
1495 
1496  if (strlen(error))
1497  pg_log_info("%s", error);
1498 
1499  CheckConnection();
1500 
1501  return -1;
1502  }
1503 
1504  /*
1505  * Fetch the result in chunks if FETCH_COUNT is set, except when:
1506  *
1507  * * SHOW_ALL_RESULTS is false, since that requires us to complete the
1508  * query before we can tell if its results should be displayed.
1509  *
1510  * * We're doing \crosstab, which likewise needs to see all the rows at
1511  * once.
1512  *
1513  * * We're doing \gexec: we must complete the data fetch to make the
1514  * connection free for issuing the resulting commands.
1515  *
1516  * * We're doing \gset: only one result row is allowed anyway.
1517  *
1518  * * We're doing \watch: users probably don't want us to force use of the
1519  * pager for that, plus chunking could break the min_rows check.
1520  */
1521  if (pset.fetch_count > 0 && pset.show_all_results &&
1523  !pset.gset_prefix && !is_watch)
1524  {
1526  pg_log_warning("fetching results in chunked mode failed");
1527  }
1528 
1529  /*
1530  * If SIGINT is sent while the query is processing, the interrupt will be
1531  * consumed. The user's intention, though, is to cancel the entire watch
1532  * process, so detect a sent cancellation request and exit in this case.
1533  */
1534  if (is_watch && cancel_pressed)
1535  {
1537  return 0;
1538  }
1539 
1540  /* first result */
1541  result = PQgetResult(pset.db);
1542  if (min_rows > 0 && PQntuples(result) < min_rows)
1543  {
1544  return_early = true;
1545  }
1546 
1547  while (result != NULL)
1548  {
1549  ExecStatusType result_status;
1550  bool is_chunked_result = false;
1551  PGresult *next_result;
1552  bool last;
1553 
1554  if (!AcceptResult(result, false))
1555  {
1556  /*
1557  * Some error occurred, either a server-side failure or a failure
1558  * to submit the command string. Record that.
1559  */
1560  const char *error = PQresultErrorMessage(result);
1561 
1562  if (strlen(error))
1563  pg_log_info("%s", error);
1564 
1565  CheckConnection();
1566  if (!is_watch)
1567  SetResultVariables(result, false);
1568 
1569  /* keep the result status before clearing it */
1570  result_status = PQresultStatus(result);
1571  ClearOrSaveResult(result);
1572  success = false;
1573 
1574  /*
1575  * switch to next result
1576  */
1577  if (result_status == PGRES_COPY_BOTH ||
1578  result_status == PGRES_COPY_OUT ||
1579  result_status == PGRES_COPY_IN)
1580 
1581  /*
1582  * For some obscure reason PQgetResult does *not* return a
1583  * NULL in copy cases despite the result having been cleared,
1584  * but keeps returning an "empty" result that we have to
1585  * ignore manually.
1586  */
1587  result = NULL;
1588  else
1589  result = PQgetResult(pset.db);
1590 
1591  /*
1592  * Get current timing measure in case an error occurs
1593  */
1594  if (timing)
1595  {
1596  INSTR_TIME_SET_CURRENT(after);
1597  INSTR_TIME_SUBTRACT(after, before);
1598  *elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1599  }
1600 
1601  continue;
1602  }
1603  else if (svpt_gone_p && !*svpt_gone_p)
1604  {
1605  /*
1606  * Check if the user ran any command that would destroy our
1607  * internal savepoint: If the user did COMMIT AND CHAIN, RELEASE
1608  * or ROLLBACK, our savepoint is gone. If they issued a SAVEPOINT,
1609  * releasing ours would remove theirs.
1610  */
1611  const char *cmd = PQcmdStatus(result);
1612 
1613  *svpt_gone_p = (strcmp(cmd, "COMMIT") == 0 ||
1614  strcmp(cmd, "SAVEPOINT") == 0 ||
1615  strcmp(cmd, "RELEASE") == 0 ||
1616  strcmp(cmd, "ROLLBACK") == 0);
1617  }
1618 
1619  result_status = PQresultStatus(result);
1620 
1621  /* must handle COPY before changing the current result */
1622  Assert(result_status != PGRES_COPY_BOTH);
1623  if (result_status == PGRES_COPY_IN ||
1624  result_status == PGRES_COPY_OUT)
1625  {
1626  FILE *copy_stream = NULL;
1627 
1628  /*
1629  * For COPY OUT, direct the output to the default place (probably
1630  * a pager pipe) for \watch, or to pset.copyStream for \copy,
1631  * otherwise to pset.gfname if that's set, otherwise to
1632  * pset.queryFout.
1633  */
1634  if (result_status == PGRES_COPY_OUT)
1635  {
1636  if (is_watch)
1637  {
1638  /* invoked by \watch */
1639  copy_stream = printQueryFout ? printQueryFout : pset.queryFout;
1640  }
1641  else if (pset.copyStream)
1642  {
1643  /* invoked by \copy */
1644  copy_stream = pset.copyStream;
1645  }
1646  else if (pset.gfname)
1647  {
1648  /* COPY followed by \g filename or \g |program */
1649  success &= SetupGOutput(&gfile_fout, &gfile_is_pipe);
1650  if (gfile_fout)
1651  copy_stream = gfile_fout;
1652  }
1653  else
1654  {
1655  /* fall back to the generic query output stream */
1656  copy_stream = pset.queryFout;
1657  }
1658  }
1659 
1660  /*
1661  * Even if the output stream could not be opened, we call
1662  * HandleCopyResult() with a NULL output stream to collect and
1663  * discard the COPY data.
1664  */
1665  success &= HandleCopyResult(&result, copy_stream);
1666  }
1667 
1668  /* If we have a chunked result, collect and print all chunks */
1669  if (result_status == PGRES_TUPLES_CHUNK)
1670  {
1671  FILE *tuples_fout = printQueryFout ? printQueryFout : pset.queryFout;
1672  printQueryOpt my_popt = opt ? *opt : pset.popt;
1673  int64 total_tuples = 0;
1674  bool is_pager = false;
1675  int flush_error = 0;
1676 
1677  /* initialize print options for partial table output */
1678  my_popt.topt.start_table = true;
1679  my_popt.topt.stop_table = false;
1680  my_popt.topt.prior_records = 0;
1681 
1682  /* open \g file if needed */
1683  success &= SetupGOutput(&gfile_fout, &gfile_is_pipe);
1684  if (gfile_fout)
1685  tuples_fout = gfile_fout;
1686 
1687  /* force use of pager for any chunked resultset going to stdout */
1688  if (success && tuples_fout == stdout)
1689  {
1690  tuples_fout = PageOutput(INT_MAX, &(my_popt.topt));
1691  is_pager = true;
1692  }
1693 
1694  do
1695  {
1696  /*
1697  * Display the current chunk of results, unless the output
1698  * stream stopped working or we got canceled. We skip use of
1699  * PrintQueryResult and go directly to printQuery, so that we
1700  * can pass the correct is_pager value and because we don't
1701  * want PrintQueryStatus to happen yet. Above, we rejected
1702  * use of chunking for all cases in which PrintQueryResult
1703  * would send the result to someplace other than printQuery.
1704  */
1705  if (success && !flush_error && !cancel_pressed)
1706  {
1707  printQuery(result, &my_popt, tuples_fout, is_pager, pset.logfile);
1708  flush_error = fflush(tuples_fout);
1709  }
1710 
1711  /* after the first result set, disallow header decoration */
1712  my_popt.topt.start_table = false;
1713 
1714  /* count tuples before dropping the result */
1715  my_popt.topt.prior_records += PQntuples(result);
1716  total_tuples += PQntuples(result);
1717 
1718  ClearOrSaveResult(result);
1719 
1720  /* get the next result, loop if it's PGRES_TUPLES_CHUNK */
1721  result = PQgetResult(pset.db);
1722  } while (PQresultStatus(result) == PGRES_TUPLES_CHUNK);
1723 
1724  /* We expect an empty PGRES_TUPLES_OK, else there's a problem */
1725  if (PQresultStatus(result) == PGRES_TUPLES_OK)
1726  {
1727  char buf[32];
1728 
1729  Assert(PQntuples(result) == 0);
1730 
1731  /* Display the footer using the empty result */
1732  if (success && !flush_error && !cancel_pressed)
1733  {
1734  my_popt.topt.stop_table = true;
1735  printQuery(result, &my_popt, tuples_fout, is_pager, pset.logfile);
1736  fflush(tuples_fout);
1737  }
1738 
1739  if (is_pager)
1740  ClosePager(tuples_fout);
1741 
1742  /*
1743  * It's possible the data is from a RETURNING clause, in which
1744  * case we need to print query status.
1745  */
1746  PrintQueryStatus(result, printQueryFout);
1747 
1748  /*
1749  * We must do a fake SetResultVariables(), since we don't have
1750  * a PGresult corresponding to the whole query.
1751  */
1752  SetVariable(pset.vars, "ERROR", "false");
1753  SetVariable(pset.vars, "SQLSTATE", "00000");
1754  snprintf(buf, sizeof(buf), INT64_FORMAT, total_tuples);
1755  SetVariable(pset.vars, "ROW_COUNT", buf);
1756  /* Prevent SetResultVariables call below */
1757  is_chunked_result = true;
1758 
1759  /* Clear the empty result so it isn't printed below */
1760  ClearOrSaveResult(result);
1761  result = NULL;
1762  }
1763  else
1764  {
1765  /* Probably an error report, so close the pager and print it */
1766  if (is_pager)
1767  ClosePager(tuples_fout);
1768 
1769  success &= AcceptResult(result, true);
1770  /* SetResultVariables and ClearOrSaveResult happen below */
1771  }
1772  }
1773 
1774  /*
1775  * Check PQgetResult() again. In the typical case of a single-command
1776  * string, it will return NULL. Otherwise, we'll have other results
1777  * to process. We need to do that to check whether this is the last.
1778  */
1779  next_result = PQgetResult(pset.db);
1780  last = (next_result == NULL);
1781 
1782  /*
1783  * Update current timing measure.
1784  *
1785  * It will include the display of previous results, if any. This
1786  * cannot be helped because the server goes on processing further
1787  * queries anyway while the previous ones are being displayed. The
1788  * parallel execution of the client display hides the server time when
1789  * it is shorter.
1790  *
1791  * With combined queries, timing must be understood as an upper bound
1792  * of the time spent processing them.
1793  */
1794  if (timing)
1795  {
1796  INSTR_TIME_SET_CURRENT(after);
1797  INSTR_TIME_SUBTRACT(after, before);
1798  *elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1799  }
1800 
1801  /* this may or may not print something depending on settings */
1802  if (result != NULL)
1803  {
1804  /*
1805  * If results need to be printed into the file specified by \g,
1806  * open it, unless we already did. Note that when pset.gfname is
1807  * set, the passed-in value of printQueryFout is not used for
1808  * tuple output, but it's still used for status output.
1809  */
1810  FILE *tuples_fout = printQueryFout;
1811 
1812  if (PQresultStatus(result) == PGRES_TUPLES_OK)
1813  success &= SetupGOutput(&gfile_fout, &gfile_is_pipe);
1814  if (gfile_fout)
1815  tuples_fout = gfile_fout;
1816  if (success)
1817  success &= PrintQueryResult(result, last, opt,
1818  tuples_fout, printQueryFout);
1819  }
1820 
1821  /* set variables from last result, unless dealt with elsewhere */
1822  if (last && !is_watch && !is_chunked_result)
1823  SetResultVariables(result, success);
1824 
1825  ClearOrSaveResult(result);
1826  result = next_result;
1827 
1828  if (cancel_pressed)
1829  {
1830  /* drop this next result, as well as any others not yet read */
1831  ClearOrSaveResult(result);
1833  break;
1834  }
1835  }
1836 
1837  /* close \g file if we opened it */
1838  CloseGOutput(gfile_fout, gfile_is_pipe);
1839 
1840  /* may need this to recover from conn loss during COPY */
1841  if (!CheckConnection())
1842  return -1;
1843 
1844  if (cancel_pressed || return_early)
1845  return 0;
1846 
1847  return success ? 1 : -1;
1848 }
static void PrintQueryStatus(PGresult *result, FILE *printQueryFout)
Definition: common.c:957
static void ClearOrSaveAllResults(void)
Definition: common.c:547
static bool SetupGOutput(FILE **gfile_fout, bool *is_pipe)
Definition: common.c:90
static bool HandleCopyResult(PGresult **resultp, FILE *copystream)
Definition: common.c:902
static void CloseGOutput(FILE *gfile_fout, bool is_pipe)
Definition: common.c:110
#define INT64_FORMAT
Definition: c.h:525
#define Assert(condition)
Definition: c.h:837
int PQsendQueryParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:1492
int PQsendClosePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2556
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:3427
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3752
int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Definition: fe-exec.c:1536
int PQsetChunkedRowsMode(PGconn *conn, int chunkSize)
Definition: fe-exec.c:1965
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1416
int PQsendQueryPrepared(PGconn *conn, const char *stmtName, int nParams, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:1633
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
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:3089
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
static bool success
Definition: initdb.c:186
ExecStatusType
Definition: libpq-fe.h:118
@ PGRES_COPY_BOTH
Definition: libpq-fe.h:132
static void const char fflush(stdout)
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define snprintf
Definition: port.h:238
printQueryOpt popt
Definition: settings.h:100
char * gset_prefix
Definition: settings.h:105
VariableSpace vars
Definition: settings.h:133
FILE * logfile
Definition: settings.h:131
bool show_all_results
Definition: settings.h:166
FILE * copyStream
Definition: settings.h:96
char * gfname
Definition: settings.h:102
int fetch_count
Definition: settings.h:154
bool gexec_flag
Definition: settings.h:107
bool crosstab_flag
Definition: settings.h:114
printTableOpt topt
Definition: print.h:185
bool start_table
Definition: print.h:127
unsigned long prior_records
Definition: print.h:130
bool stop_table
Definition: print.h:128
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211

References AcceptResult(), Assert, before(), _psqlSettings::bind_nparams, _psqlSettings::bind_params, buf, cancel_pressed, CheckConnection(), ClearOrSaveAllResults(), ClearOrSaveResult(), CloseGOutput(), ClosePager(), _psqlSettings::copyStream, _psqlSettings::crosstab_flag, _psqlSettings::db, error(), _psqlSettings::fetch_count, fflush(), _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gset_prefix, HandleCopyResult(), INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, INSTR_TIME_SUBTRACT, INT64_FORMAT, _psqlSettings::logfile, PageOutput(), pg_log_info, pg_log_warning, PGRES_COPY_BOTH, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_TUPLES_CHUNK, PGRES_TUPLES_OK, _psqlSettings::popt, PQcmdStatus(), PQerrorMessage(), PQgetResult(), PQntuples(), PQresultErrorMessage(), PQresultStatus(), PQsendClosePrepared(), PQsendPrepare(), PQsendQuery(), PQsendQueryParams(), PQsendQueryPrepared(), PQsetChunkedRowsMode(), printQuery(), PrintQueryResult(), PrintQueryStatus(), printTableOpt::prior_records, pset, PSQL_SEND_EXTENDED_CLOSE, PSQL_SEND_EXTENDED_PARSE, PSQL_SEND_EXTENDED_QUERY_PARAMS, PSQL_SEND_EXTENDED_QUERY_PREPARED, PSQL_SEND_QUERY, _psqlSettings::queryFout, _psqlSettings::send_mode, SetResultVariables(), SetupGOutput(), SetVariable(), _psqlSettings::show_all_results, snprintf, printTableOpt::start_table, generate_unaccent_rules::stdout, _psqlSettings::stmtName, printTableOpt::stop_table, success, _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by PSQLexecWatch(), and SendQuery().

◆ ExecQueryTuples()

static bool ExecQueryTuples ( const PGresult result)
static

Definition at line 826 of file common.c.

827 {
828  bool success = true;
829  int nrows = PQntuples(result);
830  int ncolumns = PQnfields(result);
831  int r,
832  c;
833 
834  /*
835  * We must turn off gexec_flag to avoid infinite recursion.
836  */
837  pset.gexec_flag = false;
838 
839  for (r = 0; r < nrows; r++)
840  {
841  for (c = 0; c < ncolumns; c++)
842  {
843  if (!PQgetisnull(result, r, c))
844  {
845  const char *query = PQgetvalue(result, r, c);
846 
847  /* Abandon execution if cancel_pressed */
848  if (cancel_pressed)
849  goto loop_exit;
850 
851  /*
852  * ECHO_ALL mode should echo these queries, but SendQuery
853  * assumes that MainLoop did that, so we have to do it here.
854  */
856  {
857  puts(query);
858  fflush(stdout);
859  }
860 
861  if (!SendQuery(query))
862  {
863  /* Error - abandon execution if ON_ERROR_STOP */
864  success = false;
865  if (pset.on_error_stop)
866  goto loop_exit;
867  }
868  }
869  }
870  }
871 
872 loop_exit:
873 
874  /*
875  * Restore state. We know gexec_flag was on, else we'd not be here. (We
876  * also know it'll get turned off at end of command, but that's not ours
877  * to do here.)
878  */
879  pset.gexec_flag = true;
880 
881  /* Return true if all queries were successful */
882  return success;
883 }
bool SendQuery(const char *query)
Definition: common.c:1082
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
char * c
@ PSQL_ECHO_ALL
Definition: settings.h:40
bool on_error_stop
Definition: settings.h:148
PSQL_ECHO echo
Definition: settings.h:157
bool singlestep
Definition: settings.h:151

References cancel_pressed, _psqlSettings::echo, fflush(), _psqlSettings::gexec_flag, _psqlSettings::on_error_stop, PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), pset, PSQL_ECHO_ALL, SendQuery(), _psqlSettings::singlestep, generate_unaccent_rules::stdout, and success.

Referenced by PrintQueryResult().

◆ expand_tilde()

void expand_tilde ( char **  filename)

Definition at line 2195 of file common.c.

2196 {
2197  if (!filename || !(*filename))
2198  return;
2199 
2200  /*
2201  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2202  * for short versions of long file names, though the tilde is usually
2203  * toward the end, not at the beginning.
2204  */
2205 #ifndef WIN32
2206 
2207  /* try tilde expansion */
2208  if (**filename == '~')
2209  {
2210  char *fn;
2211  char oldp,
2212  *p;
2213  struct passwd *pw;
2214  char home[MAXPGPATH];
2215 
2216  fn = *filename;
2217  *home = '\0';
2218 
2219  p = fn + 1;
2220  while (*p != '/' && *p != '\0')
2221  p++;
2222 
2223  oldp = *p;
2224  *p = '\0';
2225 
2226  if (*(fn + 1) == '\0')
2227  get_home_path(home); /* ~ or ~/ only */
2228  else if ((pw = getpwnam(fn + 1)) != NULL)
2229  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2230 
2231  *p = oldp;
2232  if (strlen(home) != 0)
2233  {
2234  char *newfn;
2235 
2236  newfn = psprintf("%s%s", home, p);
2237  free(fn);
2238  *filename = newfn;
2239  }
2240  }
2241 #endif
2242 }
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:119
bool get_home_path(char *ret_path)
Definition: path.c:928
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
static void * fn(void *arg)
Definition: thread-alloc.c:119

References filename, fn(), free, get_home_path(), MAXPGPATH, psprintf(), and strlcpy().

Referenced by exec_command_edit(), exec_command_g(), exec_command_include(), exec_command_lo(), exec_command_out(), exec_command_s(), exec_command_write(), initializeInput(), parse_slash_copy(), and process_psqlrc().

◆ HandleCopyResult()

static bool HandleCopyResult ( PGresult **  resultp,
FILE *  copystream 
)
static

Definition at line 902 of file common.c.

903 {
904  bool success;
905  PGresult *copy_result;
906  ExecStatusType result_status = PQresultStatus(*resultp);
907 
908  Assert(result_status == PGRES_COPY_OUT ||
909  result_status == PGRES_COPY_IN);
910 
912 
913  if (result_status == PGRES_COPY_OUT)
914  {
916  copystream,
917  &copy_result)
918  && (copystream != NULL);
919 
920  /*
921  * Suppress status printing if the report would go to the same place
922  * as the COPY data just went. Note this doesn't prevent error
923  * reporting, since handleCopyOut did that.
924  */
925  if (copystream == pset.queryFout)
926  {
927  PQclear(copy_result);
928  copy_result = NULL;
929  }
930  }
931  else
932  {
933  /* COPY IN */
934  /* Ignore the copystream argument passed to the function */
937  copystream,
938  PQbinaryTuples(*resultp),
939  &copy_result);
940  }
941  ResetCancelConn();
942 
943  /*
944  * Replace the PGRES_COPY_OUT/IN result with COPY command's exit status,
945  * or with NULL if we want to suppress printing anything.
946  */
947  PQclear(*resultp);
948  *resultp = copy_result;
949 
950  return success;
951 }
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
void SetCancelConn(PGconn *conn)
Definition: cancel.c:77
int PQbinaryTuples(const PGresult *res)
Definition: fe-exec.c:3497
FILE * cur_cmd_source
Definition: settings.h:120

References Assert, _psqlSettings::copyStream, _psqlSettings::cur_cmd_source, _psqlSettings::db, handleCopyIn(), handleCopyOut(), PGRES_COPY_IN, PGRES_COPY_OUT, PQbinaryTuples(), PQclear(), PQresultStatus(), pset, _psqlSettings::queryFout, ResetCancelConn(), SetCancelConn(), and success.

Referenced by ExecQueryAndProcessResults().

◆ is_superuser()

bool is_superuser ( void  )

Definition at line 2134 of file common.c.

2135 {
2136  const char *val;
2137 
2138  if (!pset.db)
2139  return false;
2140 
2141  val = PQparameterStatus(pset.db, "is_superuser");
2142 
2143  if (val && strcmp(val, "on") == 0)
2144  return true;
2145 
2146  return false;
2147 }
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7155
long val
Definition: informix.c:689

References _psqlSettings::db, PQparameterStatus(), pset, and val.

◆ NoticeProcessor()

void NoticeProcessor ( void *  arg,
const char *  message 
)

Definition at line 267 of file common.c.

268 {
269  (void) arg; /* not used */
270  pg_log_info("%s", message);
271 }
void * arg

References arg, and pg_log_info.

Referenced by do_connect(), and main().

◆ openQueryOutputFile()

bool openQueryOutputFile ( const char *  fname,
FILE **  fout,
bool *  is_pipe 
)

Definition at line 56 of file common.c.

57 {
58  if (!fname || fname[0] == '\0')
59  {
60  *fout = stdout;
61  *is_pipe = false;
62  }
63  else if (*fname == '|')
64  {
65  fflush(NULL);
66  *fout = popen(fname + 1, "w");
67  *is_pipe = true;
68  }
69  else
70  {
71  *fout = fopen(fname, "w");
72  *is_pipe = false;
73  }
74 
75  if (*fout == NULL)
76  {
77  pg_log_error("%s: %m", fname);
78  return false;
79  }
80 
81  return true;
82 }

References fflush(), pg_log_error, and generate_unaccent_rules::stdout.

Referenced by setQFout(), and SetupGOutput().

◆ PrintNotifications()

static void PrintNotifications ( void  )
static

Definition at line 705 of file common.c.

706 {
707  PGnotify *notify;
708 
710  while ((notify = PQnotifies(pset.db)) != NULL)
711  {
712  /* for backward compatibility, only show payload if nonempty */
713  if (notify->extra[0])
714  fprintf(pset.queryFout, _("Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
715  notify->relname, notify->extra, notify->be_pid);
716  else
717  fprintf(pset.queryFout, _("Asynchronous notification \"%s\" received from server process with PID %d.\n"),
718  notify->relname, notify->be_pid);
720  PQfreemem(notify);
722  }
723 }
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2667
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1984
int be_pid
Definition: libpq-fe.h:219
char * relname
Definition: libpq-fe.h:218
char * extra
Definition: libpq-fe.h:220

References _, pgNotify::be_pid, _psqlSettings::db, pgNotify::extra, fflush(), fprintf, PQconsumeInput(), PQfreemem(), PQnotifies(), pset, _psqlSettings::queryFout, and pgNotify::relname.

Referenced by SendQuery().

◆ PrintQueryResult()

static bool PrintQueryResult ( PGresult result,
bool  last,
const printQueryOpt opt,
FILE *  printQueryFout,
FILE *  printStatusFout 
)
static

Definition at line 1004 of file common.c.

1007 {
1008  bool success;
1009 
1010  if (!result)
1011  return false;
1012 
1013  switch (PQresultStatus(result))
1014  {
1015  case PGRES_TUPLES_OK:
1016  /* store or execute or print the data ... */
1017  if (last && pset.gset_prefix)
1018  success = StoreQueryTuple(result);
1019  else if (last && pset.gexec_flag)
1020  success = ExecQueryTuples(result);
1021  else if (last && pset.crosstab_flag)
1022  success = PrintResultInCrosstab(result);
1023  else if (last || pset.show_all_results)
1024  success = PrintQueryTuples(result, opt, printQueryFout);
1025  else
1026  success = true;
1027 
1028  /*
1029  * If it's INSERT/UPDATE/DELETE/MERGE RETURNING, also print
1030  * status.
1031  */
1032  if (last || pset.show_all_results)
1033  PrintQueryStatus(result, printStatusFout);
1034 
1035  break;
1036 
1037  case PGRES_COMMAND_OK:
1038  if (last || pset.show_all_results)
1039  PrintQueryStatus(result, printStatusFout);
1040  success = true;
1041  break;
1042 
1043  case PGRES_EMPTY_QUERY:
1044  success = true;
1045  break;
1046 
1047  case PGRES_COPY_OUT:
1048  case PGRES_COPY_IN:
1049  /* nothing to do here: already processed */
1050  success = true;
1051  break;
1052 
1053  case PGRES_BAD_RESPONSE:
1054  case PGRES_NONFATAL_ERROR:
1055  case PGRES_FATAL_ERROR:
1056  success = false;
1057  break;
1058 
1059  default:
1060  success = false;
1061  pg_log_error("unexpected PQresultStatus: %d",
1062  PQresultStatus(result));
1063  break;
1064  }
1065 
1066  return success;
1067 }
static bool ExecQueryTuples(const PGresult *result)
Definition: common.c:826
static bool StoreQueryTuple(const PGresult *result)
Definition: common.c:762
static bool PrintQueryTuples(const PGresult *result, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:738
bool PrintResultInCrosstab(const PGresult *res)
Definition: crosstabview.c:104

References _psqlSettings::crosstab_flag, ExecQueryTuples(), _psqlSettings::gexec_flag, _psqlSettings::gset_prefix, pg_log_error, PGRES_BAD_RESPONSE, PGRES_COMMAND_OK, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_EMPTY_QUERY, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PGRES_TUPLES_OK, PQresultStatus(), PrintQueryStatus(), PrintQueryTuples(), PrintResultInCrosstab(), pset, _psqlSettings::show_all_results, StoreQueryTuple(), and success.

Referenced by DescribeQuery(), and ExecQueryAndProcessResults().

◆ PrintQueryStatus()

static void PrintQueryStatus ( PGresult result,
FILE *  printQueryFout 
)
static

Definition at line 957 of file common.c.

958 {
959  char buf[16];
960  const char *cmdstatus = PQcmdStatus(result);
961  FILE *fout = printQueryFout ? printQueryFout : pset.queryFout;
962 
963  /* Do nothing if it's a TUPLES_OK result that isn't from RETURNING */
964  if (PQresultStatus(result) == PGRES_TUPLES_OK)
965  {
966  if (!(strncmp(cmdstatus, "INSERT", 6) == 0 ||
967  strncmp(cmdstatus, "UPDATE", 6) == 0 ||
968  strncmp(cmdstatus, "DELETE", 6) == 0 ||
969  strncmp(cmdstatus, "MERGE", 5) == 0))
970  return;
971  }
972 
973  if (!pset.quiet)
974  {
975  if (pset.popt.topt.format == PRINT_HTML)
976  {
977  fputs("<p>", fout);
978  html_escaped_print(cmdstatus, fout);
979  fputs("</p>\n", fout);
980  }
981  else
982  fprintf(fout, "%s\n", cmdstatus);
983  fflush(fout);
984  }
985 
986  if (pset.logfile)
987  fprintf(pset.logfile, "%s\n", cmdstatus);
988 
989  snprintf(buf, sizeof(buf), "%u", (unsigned int) PQoidValue(result));
990  SetVariable(pset.vars, "LASTOID", buf);
991 }
Oid PQoidValue(const PGresult *res)
Definition: fe-exec.c:3793
void html_escaped_print(const char *in, FILE *fout)
Definition: print.c:1952
@ PRINT_HTML
Definition: print.h:34
enum printFormat format
Definition: print.h:113

References buf, fflush(), printTableOpt::format, fprintf, html_escaped_print(), _psqlSettings::logfile, PGRES_TUPLES_OK, _psqlSettings::popt, PQcmdStatus(), PQoidValue(), PQresultStatus(), PRINT_HTML, pset, _psqlSettings::queryFout, _psqlSettings::quiet, SetVariable(), snprintf, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by ExecQueryAndProcessResults(), and PrintQueryResult().

◆ PrintQueryTuples()

static bool PrintQueryTuples ( const PGresult result,
const printQueryOpt opt,
FILE *  printQueryFout 
)
static

Definition at line 738 of file common.c.

740 {
741  bool ok = true;
742  FILE *fout = printQueryFout ? printQueryFout : pset.queryFout;
743 
744  printQuery(result, opt ? opt : &pset.popt, fout, false, pset.logfile);
745  fflush(fout);
746  if (ferror(fout))
747  {
748  pg_log_error("could not print result table: %m");
749  ok = false;
750  }
751 
752  return ok;
753 }

References fflush(), _psqlSettings::logfile, pg_log_error, _psqlSettings::popt, printQuery(), pset, and _psqlSettings::queryFout.

Referenced by PrintQueryResult().

◆ PrintTiming()

static void PrintTiming ( double  elapsed_msec)
static

Definition at line 561 of file common.c.

562 {
563  double seconds;
564  double minutes;
565  double hours;
566  double days;
567 
568  if (elapsed_msec < 1000.0)
569  {
570  /* This is the traditional (pre-v10) output format */
571  printf(_("Time: %.3f ms\n"), elapsed_msec);
572  return;
573  }
574 
575  /*
576  * Note: we could print just seconds, in a format like %06.3f, when the
577  * total is less than 1min. But that's hard to interpret unless we tack
578  * on "s" or otherwise annotate it. Forcing the display to include
579  * minutes seems like a better solution.
580  */
581  seconds = elapsed_msec / 1000.0;
582  minutes = floor(seconds / 60.0);
583  seconds -= 60.0 * minutes;
584  if (minutes < 60.0)
585  {
586  printf(_("Time: %.3f ms (%02d:%06.3f)\n"),
587  elapsed_msec, (int) minutes, seconds);
588  return;
589  }
590 
591  hours = floor(minutes / 60.0);
592  minutes -= 60.0 * hours;
593  if (hours < 24.0)
594  {
595  printf(_("Time: %.3f ms (%02d:%02d:%06.3f)\n"),
596  elapsed_msec, (int) hours, (int) minutes, seconds);
597  return;
598  }
599 
600  days = floor(hours / 24.0);
601  hours -= 24.0 * days;
602  printf(_("Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"),
603  elapsed_msec, days, (int) hours, (int) minutes, seconds);
604 }
const char *const days[]
Definition: datetime.c:84
#define printf(...)
Definition: port.h:244

References _, days, and printf.

Referenced by PSQLexecWatch(), and SendQuery().

◆ psql_cancel_callback()

static void psql_cancel_callback ( void  )
static

Definition at line 297 of file common.c.

298 {
299 #ifndef WIN32
300  /* if we are waiting for input, longjmp out of it */
302  {
303  sigint_interrupt_enabled = false;
304  siglongjmp(sigint_interrupt_jmp, 1);
305  }
306 #endif
307 
308  /* else, set cancel flag to stop any long-running loops */
309  cancel_pressed = true;
310 }
volatile sig_atomic_t sigint_interrupt_enabled
Definition: common.c:292
sigjmp_buf sigint_interrupt_jmp
Definition: common.c:294

References cancel_pressed, sigint_interrupt_enabled, and sigint_interrupt_jmp.

Referenced by psql_setup_cancel_handler().

◆ psql_get_variable()

char* psql_get_variable ( const char *  varname,
PsqlScanQuoteType  quote,
void *  passthrough 
)

Definition at line 176 of file common.c.

178 {
179  char *result = NULL;
180  const char *value;
181 
182  /* In an inactive \if branch, suppress all variable substitutions */
183  if (passthrough && !conditional_active((ConditionalStack) passthrough))
184  return NULL;
185 
186  value = GetVariable(pset.vars, varname);
187  if (!value)
188  return NULL;
189 
190  switch (quote)
191  {
192  case PQUOTE_PLAIN:
193  result = pg_strdup(value);
194  break;
195  case PQUOTE_SQL_LITERAL:
196  case PQUOTE_SQL_IDENT:
197  {
198  /*
199  * For these cases, we use libpq's quoting functions, which
200  * assume the string is in the connection's client encoding.
201  */
202  char *escaped_value;
203 
204  if (!pset.db)
205  {
206  pg_log_error("cannot escape without active connection");
207  return NULL;
208  }
209 
210  if (quote == PQUOTE_SQL_LITERAL)
211  escaped_value =
212  PQescapeLiteral(pset.db, value, strlen(value));
213  else
214  escaped_value =
215  PQescapeIdentifier(pset.db, value, strlen(value));
216 
217  if (escaped_value == NULL)
218  {
219  const char *error = PQerrorMessage(pset.db);
220 
221  pg_log_info("%s", error);
222  return NULL;
223  }
224 
225  /*
226  * Rather than complicate the lexer's API with a notion of
227  * which free() routine to use, just pay the price of an extra
228  * strdup().
229  */
230  result = pg_strdup(escaped_value);
231  PQfreemem(escaped_value);
232  break;
233  }
234  case PQUOTE_SHELL_ARG:
235  {
236  /*
237  * For this we use appendShellStringNoError, which is
238  * encoding-agnostic, which is fine since the shell probably
239  * is too. In any case, the only special character is "'",
240  * which is not known to appear in valid multibyte characters.
241  */
243 
246  {
247  pg_log_error("shell command argument contains a newline or carriage return: \"%s\"",
248  value);
249  free(buf.data);
250  return NULL;
251  }
252  result = buf.data;
253  break;
254  }
255 
256  /* No default: we want a compiler warning for missing cases */
257  }
258 
259  return result;
260 }
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4310
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static struct @160 value
@ 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
bool appendShellStringNoError(PQExpBuffer buf, const char *str)
Definition: string_utils.c:441
const char * GetVariable(VariableSpace space, const char *name)
Definition: variables.c:71

References appendShellStringNoError(), buf, conditional_active(), _psqlSettings::db, error(), free, GetVariable(), initPQExpBuffer(), pg_log_error, pg_log_info, pg_strdup(), PQerrorMessage(), PQescapeIdentifier(), PQescapeLiteral(), PQfreemem(), PQUOTE_PLAIN, PQUOTE_SHELL_ARG, PQUOTE_SQL_IDENT, PQUOTE_SQL_LITERAL, pset, value, and _psqlSettings::vars.

◆ psql_setup_cancel_handler()

void psql_setup_cancel_handler ( void  )

Definition at line 313 of file common.c.

314 {
316 }
static void psql_cancel_callback(void)
Definition: common.c:297
void setup_cancel_handler(void(*query_cancel_callback)(void))
Definition: cancel.c:183

References psql_cancel_callback(), and setup_cancel_handler().

Referenced by main().

◆ PSQLexec()

PGresult* PSQLexec ( const char *  query)

Definition at line 620 of file common.c.

621 {
622  PGresult *res;
623 
624  if (!pset.db)
625  {
626  pg_log_error("You are currently not connected to a database.");
627  return NULL;
628  }
629 
631  {
632  printf(_("/******** QUERY *********/\n"
633  "%s\n"
634  "/************************/\n\n"), query);
635  fflush(stdout);
636  if (pset.logfile)
637  {
639  _("/******** QUERY *********/\n"
640  "%s\n"
641  "/************************/\n\n"), query);
643  }
644 
646  return NULL;
647  }
648 
650 
651  res = PQexec(pset.db, query);
652 
653  ResetCancelConn();
654 
655  if (!AcceptResult(res, true))
656  {
658  res = NULL;
659  }
660 
661  return res;
662 }
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition: settings.h:47
@ PSQL_ECHO_HIDDEN_OFF
Definition: settings.h:45
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:158

References _, AcceptResult(), ClearOrSaveResult(), _psqlSettings::db, _psqlSettings::echo_hidden, fflush(), fprintf, _psqlSettings::logfile, pg_log_error, PQexec(), printf, pset, PSQL_ECHO_HIDDEN_NOEXEC, PSQL_ECHO_HIDDEN_OFF, res, ResetCancelConn(), SetCancelConn(), and generate_unaccent_rules::stdout.

Referenced by add_tablespace_footer(), addFooterToPublicationDesc(), describeAccessMethods(), describeAggregates(), describeConfigurationParameters(), describeFunctions(), describeOneTableDetails(), describeOneTSConfig(), describeOneTSParser(), describeOperators(), describePublications(), describeRoleGrants(), describeRoles(), describeSubscriptions(), describeTableDetails(), describeTablespaces(), describeTypes(), do_lo_import(), exec_command_password(), fail_lo_xact(), finish_lo_xact(), listAllDbs(), listCasts(), listCollations(), listConversions(), listDbRoleSettings(), listDefaultACLs(), listDomains(), listEventTriggers(), listExtendedStats(), listExtensionContents(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), listLargeObjects(), listOneExtensionContents(), listOperatorClasses(), listOperatorFamilies(), listOpFamilyFunctions(), listOpFamilyOperators(), listPartitionedTables(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSConfigsVerbose(), listTSDictionaries(), listTSParsers(), listTSParsersVerbose(), listTSTemplates(), listUserMappings(), main(), objectDescription(), permissionsList(), and start_lo_xact().

◆ PSQLexecWatch()

int PSQLexecWatch ( const char *  query,
const printQueryOpt opt,
FILE *  printQueryFout,
int  min_rows 
)

Definition at line 675 of file common.c.

676 {
677  bool timing = pset.timing;
678  double elapsed_msec = 0;
679  int res;
680 
681  if (!pset.db)
682  {
683  pg_log_error("You are currently not connected to a database.");
684  return 0;
685  }
686 
688 
689  res = ExecQueryAndProcessResults(query, &elapsed_msec, NULL, true, min_rows, opt, printQueryFout);
690 
691  ResetCancelConn();
692 
693  /* Possible microtiming output */
694  if (timing)
695  PrintTiming(elapsed_msec);
696 
697  return res;
698 }
static int ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, int min_rows, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:1446
static void PrintTiming(double elapsed_msec)
Definition: common.c:561

References _psqlSettings::db, ExecQueryAndProcessResults(), pg_log_error, PrintTiming(), pset, res, ResetCancelConn(), SetCancelConn(), and _psqlSettings::timing.

Referenced by do_watch().

◆ recognized_connection_string()

static bool recognized_connection_string ( const char *  connstr)

Definition at line 2317 of file common.c.

2318 {
2319  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2320 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2253
static char * connstr
Definition: pg_dumpall.c:88

References connstr, and uri_prefix_length().

Referenced by do_connect().

◆ SendQuery()

bool SendQuery ( const char *  query)

Definition at line 1082 of file common.c.

1083 {
1084  bool timing = pset.timing;
1085  PGTransactionStatusType transaction_status;
1086  double elapsed_msec = 0;
1087  bool OK = false;
1088  int i;
1089  bool on_error_rollback_savepoint = false;
1090  bool svpt_gone = false;
1091 
1092  if (!pset.db)
1093  {
1094  pg_log_error("You are currently not connected to a database.");
1095  goto sendquery_cleanup;
1096  }
1097 
1098  if (pset.singlestep)
1099  {
1100  char buf[3];
1101 
1102  fflush(stderr);
1103  printf(_("/**(Single step mode: verify command)******************************************/\n"
1104  "%s\n"
1105  "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1106  query);
1107  fflush(stdout);
1108  if (fgets(buf, sizeof(buf), stdin) != NULL)
1109  if (buf[0] == 'x')
1110  goto sendquery_cleanup;
1111  if (cancel_pressed)
1112  goto sendquery_cleanup;
1113  }
1114  else if (pset.echo == PSQL_ECHO_QUERIES)
1115  {
1116  puts(query);
1117  fflush(stdout);
1118  }
1119 
1120  if (pset.logfile)
1121  {
1123  _("/******** QUERY *********/\n"
1124  "%s\n"
1125  "/************************/\n\n"), query);
1126  fflush(pset.logfile);
1127  }
1128 
1130 
1131  transaction_status = PQtransactionStatus(pset.db);
1132 
1133  if (transaction_status == PQTRANS_IDLE &&
1134  !pset.autocommit &&
1135  !command_no_begin(query))
1136  {
1137  PGresult *result;
1138 
1139  result = PQexec(pset.db, "BEGIN");
1140  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1141  {
1143  ClearOrSaveResult(result);
1144  goto sendquery_cleanup;
1145  }
1146  ClearOrSaveResult(result);
1147  transaction_status = PQtransactionStatus(pset.db);
1148  }
1149 
1150  if (transaction_status == PQTRANS_INTRANS &&
1154  {
1155  PGresult *result;
1156 
1157  result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1158  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1159  {
1161  ClearOrSaveResult(result);
1162  goto sendquery_cleanup;
1163  }
1164  ClearOrSaveResult(result);
1165  on_error_rollback_savepoint = true;
1166  }
1167 
1168  if (pset.gdesc_flag)
1169  {
1170  /* Describe query's result columns, without executing it */
1171  OK = DescribeQuery(query, &elapsed_msec);
1172  }
1173  else
1174  {
1175  /* Default fetch-and-print mode */
1176  OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, 0, NULL, NULL) > 0);
1177  }
1178 
1179  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1180  pg_log_info("STATEMENT: %s", query);
1181 
1182  /* If we made a temporary savepoint, possibly release/rollback */
1183  if (on_error_rollback_savepoint)
1184  {
1185  const char *svptcmd = NULL;
1186 
1187  transaction_status = PQtransactionStatus(pset.db);
1188 
1189  switch (transaction_status)
1190  {
1191  case PQTRANS_INERROR:
1192  /* We always rollback on an error */
1193  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1194  break;
1195 
1196  case PQTRANS_IDLE:
1197  /* If they are no longer in a transaction, then do nothing */
1198  break;
1199 
1200  case PQTRANS_INTRANS:
1201 
1202  /*
1203  * Release our savepoint, but do nothing if they are messing
1204  * with savepoints themselves
1205  */
1206  if (!svpt_gone)
1207  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1208  break;
1209 
1210  case PQTRANS_ACTIVE:
1211  case PQTRANS_UNKNOWN:
1212  default:
1213  OK = false;
1214  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1215  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1216  pg_log_error("unexpected transaction status (%d)",
1217  transaction_status);
1218  break;
1219  }
1220 
1221  if (svptcmd)
1222  {
1223  PGresult *svptres;
1224 
1225  svptres = PQexec(pset.db, svptcmd);
1226  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1227  {
1229  ClearOrSaveResult(svptres);
1230  OK = false;
1231 
1232  goto sendquery_cleanup;
1233  }
1234  PQclear(svptres);
1235  }
1236  }
1237 
1238  /* Possible microtiming output */
1239  if (timing)
1240  PrintTiming(elapsed_msec);
1241 
1242  /* check for events that may occur during query execution */
1243 
1244  if (pset.encoding != PQclientEncoding(pset.db) &&
1245  PQclientEncoding(pset.db) >= 0)
1246  {
1247  /* track effects of SET CLIENT_ENCODING */
1250  SetVariable(pset.vars, "ENCODING",
1252  }
1253 
1255 
1256  /* perform cleanup that should occur after any attempted query */
1257 
1258 sendquery_cleanup:
1259 
1260  /* global cancellation reset */
1261  ResetCancelConn();
1262 
1263  /* reset \g's output-to-filename trigger */
1264  if (pset.gfname)
1265  {
1266  free(pset.gfname);
1267  pset.gfname = NULL;
1268  }
1269 
1270  /* restore print settings if \g changed them */
1271  if (pset.gsavepopt)
1272  {
1274  pset.gsavepopt = NULL;
1275  }
1276 
1277  /* clean up after extended protocol queries */
1279 
1280  /* reset \gset trigger */
1281  if (pset.gset_prefix)
1282  {
1284  pset.gset_prefix = NULL;
1285  }
1286 
1287  /* reset \gdesc trigger */
1288  pset.gdesc_flag = false;
1289 
1290  /* reset \gexec trigger */
1291  pset.gexec_flag = false;
1292 
1293  /* reset \crosstabview trigger */
1294  pset.crosstab_flag = false;
1295  for (i = 0; i < lengthof(pset.ctv_args); i++)
1296  {
1297  pg_free(pset.ctv_args[i]);
1298  pset.ctv_args[i] = NULL;
1299  }
1300 
1301  return OK;
1302 }
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1314
static void PrintNotifications(void)
Definition: common.c:705
void clean_extended_state(void)
Definition: common.c:2279
static bool command_no_begin(const char *query)
Definition: common.c:1919
#define lengthof(array)
Definition: c.h:767
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:5154
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7145
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7288
void pg_free(void *ptr)
Definition: fe_memutils.c:105
PGTransactionStatusType
Definition: libpq-fe.h:141
@ PQTRANS_INTRANS
Definition: libpq-fe.h:144
@ PQTRANS_IDLE
Definition: libpq-fe.h:142
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:143
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:146
@ PQTRANS_INERROR
Definition: libpq-fe.h:145
#define pg_encoding_to_char
Definition: pg_wchar.h:630
@ PSQL_ERROR_ROLLBACK_ON
Definition: settings.h:54
@ PSQL_ERROR_ROLLBACK_OFF
Definition: settings.h:52
@ PSQL_ECHO_ERRORS
Definition: settings.h:39
@ PSQL_ECHO_QUERIES
Definition: settings.h:38
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:159
bool autocommit
Definition: settings.h:147
char * ctv_args[4]
Definition: settings.h:115
printQueryOpt * gsavepopt
Definition: settings.h:103
bool gdesc_flag
Definition: settings.h:106
int encoding
Definition: print.h:138

References _, _psqlSettings::autocommit, buf, cancel_pressed, clean_extended_state(), ClearOrSaveResult(), command_no_begin(), ConnectionUp(), _psqlSettings::crosstab_flag, _psqlSettings::ctv_args, _psqlSettings::cur_cmd_interactive, _psqlSettings::db, DescribeQuery(), _psqlSettings::echo, _psqlSettings::encoding, printTableOpt::encoding, ExecQueryAndProcessResults(), fflush(), fprintf, free, _psqlSettings::gdesc_flag, _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gsavepopt, _psqlSettings::gset_prefix, i, lengthof, _psqlSettings::logfile, _psqlSettings::on_error_rollback, pg_encoding_to_char, pg_free(), pg_log_error, pg_log_info, PGRES_COMMAND_OK, _psqlSettings::popt, PQclear(), PQclientEncoding(), PQerrorMessage(), PQexec(), PQresultStatus(), PQTRANS_ACTIVE, PQTRANS_IDLE, PQTRANS_INERROR, PQTRANS_INTRANS, PQTRANS_UNKNOWN, PQtransactionStatus(), printf, PrintNotifications(), PrintTiming(), pset, PSQL_ECHO_ERRORS, PSQL_ECHO_QUERIES, PSQL_ERROR_ROLLBACK_OFF, PSQL_ERROR_ROLLBACK_ON, ResetCancelConn(), restorePsetInfo(), SetCancelConn(), SetVariable(), _psqlSettings::singlestep, generate_unaccent_rules::stdout, _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by do_copy(), ExecQueryTuples(), main(), and MainLoop().

◆ session_username()

const char* session_username ( void  )

Definition at line 2174 of file common.c.

2175 {
2176  const char *val;
2177 
2178  if (!pset.db)
2179  return NULL;
2180 
2181  val = PQparameterStatus(pset.db, "session_authorization");
2182  if (val)
2183  return val;
2184  else
2185  return PQuser(pset.db);
2186 }
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7044

References _psqlSettings::db, PQparameterStatus(), PQuser(), pset, and val.

Referenced by get_prompt().

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 132 of file common.c.

133 {
134  FILE *fout;
135  bool is_pipe;
136 
137  /* First make sure we can open the new output file/pipe */
138  if (!openQueryOutputFile(fname, &fout, &is_pipe))
139  return false;
140 
141  /* Close old file/pipe */
142  if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
143  {
144  if (pset.queryFoutPipe)
146  else
147  fclose(pset.queryFout);
148  }
149 
150  pset.queryFout = fout;
151  pset.queryFoutPipe = is_pipe;
152 
153  /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
154  set_sigpipe_trap_state(is_pipe);
156 
157  return true;
158 }
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:56
void set_sigpipe_trap_state(bool ignore)
Definition: print.c:3075
bool queryFoutPipe
Definition: settings.h:94

References openQueryOutputFile(), pset, _psqlSettings::queryFout, _psqlSettings::queryFoutPipe, restore_sigpipe_trap(), set_sigpipe_trap_state(), SetShellResultVariables(), and generate_unaccent_rules::stdout.

Referenced by exec_command_out(), main(), and parse_psql_options().

◆ SetResultVariables()

static void SetResultVariables ( PGresult result,
bool  success 
)
static

Definition at line 461 of file common.c.

462 {
463  if (success)
464  {
465  const char *ntuples = PQcmdTuples(result);
466 
467  SetVariable(pset.vars, "ERROR", "false");
468  SetVariable(pset.vars, "SQLSTATE", "00000");
469  SetVariable(pset.vars, "ROW_COUNT", *ntuples ? ntuples : "0");
470  }
471  else
472  {
473  const char *code = PQresultErrorField(result, PG_DIAG_SQLSTATE);
474  const char *mesg = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
475 
476  SetVariable(pset.vars, "ERROR", "true");
477 
478  /*
479  * If there is no SQLSTATE code, use an empty string. This can happen
480  * for libpq-detected errors (e.g., lost connection, ENOMEM).
481  */
482  if (code == NULL)
483  code = "";
484  SetVariable(pset.vars, "SQLSTATE", code);
485  SetVariable(pset.vars, "ROW_COUNT", "0");
486  SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", code);
487  SetVariable(pset.vars, "LAST_ERROR_MESSAGE", mesg ? mesg : "");
488  }
489 }
char * PQcmdTuples(PGresult *res)
Definition: fe-exec.c:3822
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3466
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:56
#define PG_DIAG_MESSAGE_PRIMARY
Definition: postgres_ext.h:57

References PG_DIAG_MESSAGE_PRIMARY, PG_DIAG_SQLSTATE, PQcmdTuples(), PQresultErrorField(), pset, SetVariable(), success, and _psqlSettings::vars.

Referenced by DescribeQuery(), and ExecQueryAndProcessResults().

◆ SetShellResultVariables()

void SetShellResultVariables ( int  wait_result)

Definition at line 501 of file common.c.

502 {
503  char buf[32];
504 
505  SetVariable(pset.vars, "SHELL_ERROR",
506  (wait_result == 0) ? "false" : "true");
507  snprintf(buf, sizeof(buf), "%d", wait_result_to_exit_code(wait_result));
508  SetVariable(pset.vars, "SHELL_EXIT_CODE", buf);
509 }
int wait_result_to_exit_code(int exit_status)
Definition: wait_error.c:138

References buf, pset, SetVariable(), snprintf, _psqlSettings::vars, and wait_result_to_exit_code().

Referenced by CloseGOutput(), do_copy(), do_shell(), exec_command_write(), and setQFout().

◆ SetupGOutput()

static bool SetupGOutput ( FILE **  gfile_fout,
bool *  is_pipe 
)
static

Definition at line 90 of file common.c.

91 {
92  /* If there is a \g file or program, and it's not already open, open it */
93  if (pset.gfname != NULL && *gfile_fout == NULL)
94  {
95  if (openQueryOutputFile(pset.gfname, gfile_fout, is_pipe))
96  {
97  if (*is_pipe)
99  }
100  else
101  return false;
102  }
103  return true;
104 }
void disable_sigpipe_trap(void)
Definition: print.c:3039

References disable_sigpipe_trap(), _psqlSettings::gfname, openQueryOutputFile(), and pset.

Referenced by ExecQueryAndProcessResults().

◆ skip_white_space()

static const char* skip_white_space ( const char *  query)
static

Definition at line 1855 of file common.c.

1856 {
1857  int cnestlevel = 0; /* slash-star comment nest level */
1858 
1859  while (*query)
1860  {
1861  int mblen = PQmblenBounded(query, pset.encoding);
1862 
1863  /*
1864  * Note: we assume the encoding is a superset of ASCII, so that for
1865  * example "query[0] == '/'" is meaningful. However, we do NOT assume
1866  * that the second and subsequent bytes of a multibyte character
1867  * couldn't look like ASCII characters; so it is critical to advance
1868  * by mblen, not 1, whenever we haven't exactly identified the
1869  * character we are skipping over.
1870  */
1871  if (isspace((unsigned char) *query))
1872  query += mblen;
1873  else if (query[0] == '/' && query[1] == '*')
1874  {
1875  cnestlevel++;
1876  query += 2;
1877  }
1878  else if (cnestlevel > 0 && query[0] == '*' && query[1] == '/')
1879  {
1880  cnestlevel--;
1881  query += 2;
1882  }
1883  else if (cnestlevel == 0 && query[0] == '-' && query[1] == '-')
1884  {
1885  query += 2;
1886 
1887  /*
1888  * We have to skip to end of line since any slash-star inside the
1889  * -- comment does NOT start a slash-star comment.
1890  */
1891  while (*query)
1892  {
1893  if (*query == '\n')
1894  {
1895  query++;
1896  break;
1897  }
1898  query += PQmblenBounded(query, pset.encoding);
1899  }
1900  }
1901  else if (cnestlevel > 0)
1902  query += mblen;
1903  else
1904  break; /* found first token */
1905  }
1906 
1907  return query;
1908 }

References _psqlSettings::encoding, PQmblenBounded(), and pset.

Referenced by command_no_begin().

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2154 of file common.c.

2155 {
2156  const char *val;
2157 
2158  if (!pset.db)
2159  return false;
2160 
2161  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2162 
2163  if (val && strcmp(val, "on") == 0)
2164  return true;
2165 
2166  return false;
2167 }

References _psqlSettings::db, PQparameterStatus(), pset, and val.

Referenced by get_create_object_cmd(), main(), MainLoop(), and parse_slash_copy().

◆ StoreQueryTuple()

static bool StoreQueryTuple ( const PGresult result)
static

Definition at line 762 of file common.c.

763 {
764  bool success = true;
765 
766  if (PQntuples(result) < 1)
767  {
768  pg_log_error("no rows returned for \\gset");
769  success = false;
770  }
771  else if (PQntuples(result) > 1)
772  {
773  pg_log_error("more than one row returned for \\gset");
774  success = false;
775  }
776  else
777  {
778  int i;
779 
780  for (i = 0; i < PQnfields(result); i++)
781  {
782  char *colname = PQfname(result, i);
783  char *varname;
784  char *value;
785 
786  /* concatenate prefix and column name */
787  varname = psprintf("%s%s", pset.gset_prefix, colname);
788 
789  if (VariableHasHook(pset.vars, varname))
790  {
791  pg_log_warning("attempt to \\gset into specially treated variable \"%s\" ignored",
792  varname);
793  continue;
794  }
795 
796  if (!PQgetisnull(result, 0, i))
797  value = PQgetvalue(result, 0, i);
798  else
799  {
800  /* for NULL value, unset rather than set the variable */
801  value = NULL;
802  }
803 
804  if (!SetVariable(pset.vars, varname, value))
805  {
806  free(varname);
807  success = false;
808  break;
809  }
810 
811  free(varname);
812  }
813  }
814 
815  return success;
816 }
bool VariableHasHook(VariableSpace space, const char *name)
Definition: variables.c:367

References free, _psqlSettings::gset_prefix, i, pg_log_error, pg_log_warning, PQfname(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), pset, psprintf(), SetVariable(), success, value, VariableHasHook(), and _psqlSettings::vars.

Referenced by PrintQueryResult().

◆ uri_prefix_length()

static int uri_prefix_length ( const char *  connstr)
static

Definition at line 2253 of file common.c.

2254 {
2255  /* The connection URI must start with either of the following designators: */
2256  static const char uri_designator[] = "postgresql://";
2257  static const char short_uri_designator[] = "postgres://";
2258 
2259  if (strncmp(connstr, uri_designator,
2260  sizeof(uri_designator) - 1) == 0)
2261  return sizeof(uri_designator) - 1;
2262 
2263  if (strncmp(connstr, short_uri_designator,
2264  sizeof(short_uri_designator) - 1) == 0)
2265  return sizeof(short_uri_designator) - 1;
2266 
2267  return 0;
2268 }
static const char short_uri_designator[]
Definition: fe-connect.c:393
static const char uri_designator[]
Definition: fe-connect.c:392

References connstr, short_uri_designator, and uri_designator.

Referenced by recognized_connection_string().

Variable Documentation

◆ sigint_interrupt_enabled

volatile sig_atomic_t sigint_interrupt_enabled = false

◆ sigint_interrupt_jmp

sigjmp_buf sigint_interrupt_jmp