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)
 
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:7147
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
@ PGRES_COPY_IN
Definition: libpq-fe.h:107
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:100
@ PGRES_TUPLES_CHUNK
Definition: libpq-fe.h:117
@ 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
#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:82

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:3990
void SyncVariables(void)
Definition: command.c:3949
void connection_warnings(bool in_startup)
Definition: command.c:3821
#define _(x)
Definition: elog.c:90
void PQreset(PGconn *conn)
Definition: fe-connect.c:4882
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4868
exit(1)
#define fprintf
Definition: port.h:242
#define EXIT_BADCONN
Definition: settings.h:170
PGconn * dead_conn
Definition: settings.h:129
bool cur_cmd_interactive
Definition: settings.h:111

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().

◆ 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:89

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 1904 of file common.c.

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

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:7094
@ CONNECTION_BAD
Definition: libpq-fe.h:62

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 1321 of file common.c.

1322 {
1323  bool timing = pset.timing;
1324  PGresult *result;
1325  bool OK;
1327  after;
1328 
1329  *elapsed_msec = 0;
1330 
1331  if (timing)
1333  else
1335 
1336  /*
1337  * To parse the query but not execute it, we prepare it, using the unnamed
1338  * prepared statement. This is invisible to psql users, since there's no
1339  * way to access the unnamed prepared statement from psql user space. The
1340  * next Parse or Query protocol message would overwrite the statement
1341  * anyway. (So there's no great need to clear it when done, which is a
1342  * good thing because libpq provides no easy way to do that.)
1343  */
1344  result = PQprepare(pset.db, "", query, 0, NULL);
1345  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1346  {
1348  SetResultVariables(result, false);
1349  ClearOrSaveResult(result);
1350  return false;
1351  }
1352  PQclear(result);
1353 
1354  result = PQdescribePrepared(pset.db, "");
1355  OK = AcceptResult(result, true) &&
1356  (PQresultStatus(result) == PGRES_COMMAND_OK);
1357  if (OK && result)
1358  {
1359  if (PQnfields(result) > 0)
1360  {
1362  int i;
1363 
1364  initPQExpBuffer(&buf);
1365 
1367  "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1368  "FROM (VALUES ",
1369  gettext_noop("Column"),
1370  gettext_noop("Type"));
1371 
1372  for (i = 0; i < PQnfields(result); i++)
1373  {
1374  const char *name;
1375  char *escname;
1376 
1377  if (i > 0)
1378  appendPQExpBufferStr(&buf, ",");
1379 
1380  name = PQfname(result, i);
1381  escname = PQescapeLiteral(pset.db, name, strlen(name));
1382 
1383  if (escname == NULL)
1384  {
1386  PQclear(result);
1387  termPQExpBuffer(&buf);
1388  return false;
1389  }
1390 
1391  appendPQExpBuffer(&buf, "(%s, '%u'::pg_catalog.oid, %d)",
1392  escname,
1393  PQftype(result, i),
1394  PQfmod(result, i));
1395 
1396  PQfreemem(escname);
1397  }
1398 
1399  appendPQExpBufferStr(&buf, ") s(name, tp, tpm)");
1400  PQclear(result);
1401 
1402  result = PQexec(pset.db, buf.data);
1403  OK = AcceptResult(result, true);
1404 
1405  if (timing)
1406  {
1407  INSTR_TIME_SET_CURRENT(after);
1408  INSTR_TIME_SUBTRACT(after, before);
1409  *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1410  }
1411 
1412  if (OK && result)
1413  OK = PrintQueryResult(result, true, NULL, NULL, NULL);
1414 
1415  termPQExpBuffer(&buf);
1416  }
1417  else
1419  _("The command has no result, or the result has no columns.\n"));
1420  }
1421 
1422  SetResultVariables(result, OK);
1423  ClearOrSaveResult(result);
1424 
1425  return OK;
1426 }
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:1196
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
int i
Definition: isn.c:73
static char * buf
Definition: pg_test_fsync.c:73
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:84
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 1453 of file common.c.

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

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 2180 of file common.c.

2181 {
2182  if (!filename || !(*filename))
2183  return;
2184 
2185  /*
2186  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2187  * for short versions of long file names, though the tilde is usually
2188  * toward the end, not at the beginning.
2189  */
2190 #ifndef WIN32
2191 
2192  /* try tilde expansion */
2193  if (**filename == '~')
2194  {
2195  char *fn;
2196  char oldp,
2197  *p;
2198  struct passwd *pw;
2199  char home[MAXPGPATH];
2200 
2201  fn = *filename;
2202  *home = '\0';
2203 
2204  p = fn + 1;
2205  while (*p != '/' && *p != '\0')
2206  p++;
2207 
2208  oldp = *p;
2209  *p = '\0';
2210 
2211  if (*(fn + 1) == '\0')
2212  get_home_path(home); /* ~ or ~/ only */
2213  else if ((pw = getpwnam(fn + 1)) != NULL)
2214  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2215 
2216  *p = oldp;
2217  if (strlen(home) != 0)
2218  {
2219  char *newfn;
2220 
2221  newfn = psprintf("%s%s", home, p);
2222  free(fn);
2223  *filename = newfn;
2224  }
2225  }
2226 #endif
2227 }
#define free(a)
Definition: header.h:65
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:119
bool get_home_path(char *ret_path)
Definition: path.c:927
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
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:109

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 2119 of file common.c.

2120 {
2121  const char *val;
2122 
2123  if (!pset.db)
2124  return false;
2125 
2126  val = PQparameterStatus(pset.db, "is_superuser");
2127 
2128  if (val && strcmp(val, "on") == 0)
2129  return true;
2130 
2131  return false;
2132 }
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7112
long val
Definition: informix.c:670

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:199
char * relname
Definition: libpq-fe.h:198
char * extra
Definition: libpq-fe.h:200

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 @155 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:147

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:1453
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 2265 of file common.c.

2266 {
2267  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2268 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2238
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 \bind */
1278  if (pset.bind_flag)
1279  {
1280  for (i = 0; i < pset.bind_nparams; i++)
1281  free(pset.bind_params[i]);
1283  pset.bind_params = NULL;
1284  pset.bind_flag = false;
1285  }
1286 
1287  /* reset \gset trigger */
1288  if (pset.gset_prefix)
1289  {
1291  pset.gset_prefix = NULL;
1292  }
1293 
1294  /* reset \gdesc trigger */
1295  pset.gdesc_flag = false;
1296 
1297  /* reset \gexec trigger */
1298  pset.gexec_flag = false;
1299 
1300  /* reset \crosstabview trigger */
1301  pset.crosstab_flag = false;
1302  for (i = 0; i < lengthof(pset.ctv_args); i++)
1303  {
1304  pg_free(pset.ctv_args[i]);
1305  pset.ctv_args[i] = NULL;
1306  }
1307 
1308  return OK;
1309 }
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1321
static void PrintNotifications(void)
Definition: common.c:705
static bool command_no_begin(const char *query)
Definition: common.c:1904
#define lengthof(array)
Definition: c.h:788
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:5029
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7102
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7235
void pg_free(void *ptr)
Definition: fe_memutils.c:105
PGTransactionStatusType
Definition: libpq-fe.h:121
@ PQTRANS_INTRANS
Definition: libpq-fe.h:124
@ PQTRANS_IDLE
Definition: libpq-fe.h:122
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:123
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:126
@ PQTRANS_INERROR
Definition: libpq-fe.h:125
#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:148
bool autocommit
Definition: settings.h:136
char * ctv_args[4]
Definition: settings.h:104
printQueryOpt * gsavepopt
Definition: settings.h:94
bool gdesc_flag
Definition: settings.h:97
int encoding
Definition: print.h:138

References _, _psqlSettings::autocommit, _psqlSettings::bind_flag, _psqlSettings::bind_nparams, _psqlSettings::bind_params, buf, cancel_pressed, 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 2159 of file common.c.

2160 {
2161  const char *val;
2162 
2163  if (!pset.db)
2164  return NULL;
2165 
2166  val = PQparameterStatus(pset.db, "session_authorization");
2167  if (val)
2168  return val;
2169  else
2170  return PQuser(pset.db);
2171 }
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7001

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:85

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 1840 of file common.c.

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

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

Referenced by command_no_begin().

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2139 of file common.c.

2140 {
2141  const char *val;
2142 
2143  if (!pset.db)
2144  return false;
2145 
2146  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2147 
2148  if (val && strcmp(val, "on") == 0)
2149  return true;
2150 
2151  return false;
2152 }

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 2238 of file common.c.

2239 {
2240  /* The connection URI must start with either of the following designators: */
2241  static const char uri_designator[] = "postgresql://";
2242  static const char short_uri_designator[] = "postgres://";
2243 
2244  if (strncmp(connstr, uri_designator,
2245  sizeof(uri_designator) - 1) == 0)
2246  return sizeof(uri_designator) - 1;
2247 
2248  if (strncmp(connstr, short_uri_designator,
2249  sizeof(short_uri_designator) - 1) == 0)
2250  return sizeof(short_uri_designator) - 1;
2251 
2252  return 0;
2253 }
static const char short_uri_designator[]
Definition: fe-connect.c:392
static const char uri_designator[]
Definition: fe-connect.c:391

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