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 bool ExecQueryUsingCursor (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)
 
static bool is_select_command (const char *query)
 
bool openQueryOutputFile (const char *fname, FILE **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 365 of file common.c.

366 {
367  bool OK;
368 
369  if (!result)
370  OK = false;
371  else
372  switch (PQresultStatus(result))
373  {
374  case PGRES_COMMAND_OK:
375  case PGRES_TUPLES_OK:
376  case PGRES_EMPTY_QUERY:
377  case PGRES_COPY_IN:
378  case PGRES_COPY_OUT:
379  /* Fine, do nothing */
380  OK = true;
381  break;
382 
383  case PGRES_BAD_RESPONSE:
385  case PGRES_FATAL_ERROR:
386  OK = false;
387  break;
388 
389  default:
390  OK = false;
391  pg_log_error("unexpected PQresultStatus: %d",
392  PQresultStatus(result));
393  break;
394  }
395 
396  if (!OK && show_error)
397  {
398  const char *error = PQerrorMessage(pset.db);
399 
400  if (strlen(error))
401  pg_log_info("%s", error);
402 
403  CheckConnection();
404  }
405 
406  return OK;
407 }
static bool CheckConnection(void)
Definition: common.c:304
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7248
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3325
@ PGRES_COPY_IN
Definition: libpq-fe.h:104
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
@ PGRES_FATAL_ERROR
Definition: libpq-fe.h:108
@ PGRES_COPY_OUT
Definition: libpq-fe.h:103
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:96
@ PGRES_BAD_RESPONSE
Definition: libpq-fe.h:105
@ PGRES_NONFATAL_ERROR
Definition: libpq-fe.h:107
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:100
#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_OK, PQerrorMessage(), PQresultStatus(), and pset.

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

◆ CheckConnection()

static bool CheckConnection ( void  )
static

Definition at line 304 of file common.c.

305 {
306  bool OK;
307 
308  OK = ConnectionUp();
309  if (!OK)
310  {
312  {
313  pg_log_error("connection to server was lost");
315  }
316 
317  fprintf(stderr, _("The connection to the server was lost. Attempting reset: "));
318  PQreset(pset.db);
319  OK = ConnectionUp();
320  if (!OK)
321  {
322  fprintf(stderr, _("Failed.\n"));
323 
324  /*
325  * Transition to having no connection; but stash away the failed
326  * connection so that we can still refer to its parameters in a
327  * later \connect attempt. Keep the state cleanup here in sync
328  * with do_connect().
329  */
330  if (pset.dead_conn)
332  pset.dead_conn = pset.db;
333  pset.db = NULL;
334  ResetCancelConn();
335  UnsyncVariables();
336  }
337  else
338  {
339  fprintf(stderr, _("Succeeded.\n"));
340 
341  /*
342  * Re-sync, just in case anything changed. Keep this in sync with
343  * do_connect().
344  */
345  SyncVariables();
346  connection_warnings(false); /* Must be after SyncVariables */
347  }
348  }
349 
350  return OK;
351 }
static bool ConnectionUp(void)
Definition: common.c:286
void ResetCancelConn(void)
Definition: cancel.c:107
void UnsyncVariables(void)
Definition: command.c:3938
void SyncVariables(void)
Definition: command.c:3897
void connection_warnings(bool in_startup)
Definition: command.c:3772
#define _(x)
Definition: elog.c:91
void PQreset(PGconn *conn)
Definition: fe-connect.c:4616
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4602
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 508 of file common.c.

509 {
510  PGresult *result;
511 
512  while ((result = PQgetResult(pset.db)) != NULL)
513  ClearOrSaveResult(result);
514 }
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:484
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2035

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

Referenced by ExecQueryAndProcessResults().

◆ ClearOrSaveResult()

static void ClearOrSaveResult ( PGresult result)
static

Definition at line 484 of file common.c.

485 {
486  if (result)
487  {
488  switch (PQresultStatus(result))
489  {
491  case PGRES_FATAL_ERROR:
493  pset.last_error_result = result;
494  break;
495 
496  default:
497  PQclear(result);
498  break;
499  }
500  }
501 }
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(), ExecQueryUsingCursor(), PSQLexec(), and SendQuery().

◆ command_no_begin()

static bool command_no_begin ( const char *  query)
static

Definition at line 2035 of file common.c.

2036 {
2037  int wordlen;
2038 
2039  /*
2040  * First we must advance over any whitespace and comments.
2041  */
2042  query = skip_white_space(query);
2043 
2044  /*
2045  * Check word length (since "beginx" is not "begin").
2046  */
2047  wordlen = 0;
2048  while (isalpha((unsigned char) query[wordlen]))
2049  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2050 
2051  /*
2052  * Transaction control commands. These should include every keyword that
2053  * gives rise to a TransactionStmt in the backend grammar, except for the
2054  * savepoint-related commands.
2055  *
2056  * (We assume that START must be START TRANSACTION, since there is
2057  * presently no other "START foo" command.)
2058  */
2059  if (wordlen == 5 && pg_strncasecmp(query, "abort", 5) == 0)
2060  return true;
2061  if (wordlen == 5 && pg_strncasecmp(query, "begin", 5) == 0)
2062  return true;
2063  if (wordlen == 5 && pg_strncasecmp(query, "start", 5) == 0)
2064  return true;
2065  if (wordlen == 6 && pg_strncasecmp(query, "commit", 6) == 0)
2066  return true;
2067  if (wordlen == 3 && pg_strncasecmp(query, "end", 3) == 0)
2068  return true;
2069  if (wordlen == 8 && pg_strncasecmp(query, "rollback", 8) == 0)
2070  return true;
2071  if (wordlen == 7 && pg_strncasecmp(query, "prepare", 7) == 0)
2072  {
2073  /* PREPARE TRANSACTION is a TC command, PREPARE foo is not */
2074  query += wordlen;
2075 
2076  query = skip_white_space(query);
2077 
2078  wordlen = 0;
2079  while (isalpha((unsigned char) query[wordlen]))
2080  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2081 
2082  if (wordlen == 11 && pg_strncasecmp(query, "transaction", 11) == 0)
2083  return true;
2084  return false;
2085  }
2086 
2087  /*
2088  * Commands not allowed within transactions. The statements checked for
2089  * here should be exactly those that call PreventInTransactionBlock() in
2090  * the backend.
2091  */
2092  if (wordlen == 6 && pg_strncasecmp(query, "vacuum", 6) == 0)
2093  return true;
2094  if (wordlen == 7 && pg_strncasecmp(query, "cluster", 7) == 0)
2095  {
2096  /* CLUSTER with any arguments is allowed in transactions */
2097  query += wordlen;
2098 
2099  query = skip_white_space(query);
2100 
2101  if (isalpha((unsigned char) query[0]))
2102  return false; /* has additional words */
2103  return true; /* it's CLUSTER without arguments */
2104  }
2105 
2106  if (wordlen == 6 && pg_strncasecmp(query, "create", 6) == 0)
2107  {
2108  query += wordlen;
2109 
2110  query = skip_white_space(query);
2111 
2112  wordlen = 0;
2113  while (isalpha((unsigned char) query[wordlen]))
2114  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2115 
2116  if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
2117  return true;
2118  if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
2119  return true;
2120 
2121  /* CREATE [UNIQUE] INDEX CONCURRENTLY isn't allowed in xacts */
2122  if (wordlen == 6 && pg_strncasecmp(query, "unique", 6) == 0)
2123  {
2124  query += wordlen;
2125 
2126  query = skip_white_space(query);
2127 
2128  wordlen = 0;
2129  while (isalpha((unsigned char) query[wordlen]))
2130  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2131  }
2132 
2133  if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
2134  {
2135  query += wordlen;
2136 
2137  query = skip_white_space(query);
2138 
2139  wordlen = 0;
2140  while (isalpha((unsigned char) query[wordlen]))
2141  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2142 
2143  if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2144  return true;
2145  }
2146 
2147  return false;
2148  }
2149 
2150  if (wordlen == 5 && pg_strncasecmp(query, "alter", 5) == 0)
2151  {
2152  query += wordlen;
2153 
2154  query = skip_white_space(query);
2155 
2156  wordlen = 0;
2157  while (isalpha((unsigned char) query[wordlen]))
2158  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2159 
2160  /* ALTER SYSTEM isn't allowed in xacts */
2161  if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
2162  return true;
2163 
2164  return false;
2165  }
2166 
2167  /*
2168  * Note: these tests will match DROP SYSTEM and REINDEX TABLESPACE, which
2169  * aren't really valid commands so we don't care much. The other four
2170  * possible matches are correct.
2171  */
2172  if ((wordlen == 4 && pg_strncasecmp(query, "drop", 4) == 0) ||
2173  (wordlen == 7 && pg_strncasecmp(query, "reindex", 7) == 0))
2174  {
2175  query += wordlen;
2176 
2177  query = skip_white_space(query);
2178 
2179  wordlen = 0;
2180  while (isalpha((unsigned char) query[wordlen]))
2181  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2182 
2183  if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
2184  return true;
2185  if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
2186  return true;
2187  if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
2188  return true;
2189  if (wordlen == 5 && (pg_strncasecmp(query, "index", 5) == 0 ||
2190  pg_strncasecmp(query, "table", 5) == 0))
2191  {
2192  query += wordlen;
2193  query = skip_white_space(query);
2194  wordlen = 0;
2195  while (isalpha((unsigned char) query[wordlen]))
2196  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2197 
2198  /*
2199  * REINDEX [ TABLE | INDEX ] CONCURRENTLY are not allowed in
2200  * xacts.
2201  */
2202  if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2203  return true;
2204  }
2205 
2206  /* DROP INDEX CONCURRENTLY isn't allowed in xacts */
2207  if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
2208  {
2209  query += wordlen;
2210 
2211  query = skip_white_space(query);
2212 
2213  wordlen = 0;
2214  while (isalpha((unsigned char) query[wordlen]))
2215  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2216 
2217  if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2218  return true;
2219 
2220  return false;
2221  }
2222 
2223  return false;
2224  }
2225 
2226  /* DISCARD ALL isn't allowed in xacts, but other variants are allowed. */
2227  if (wordlen == 7 && pg_strncasecmp(query, "discard", 7) == 0)
2228  {
2229  query += wordlen;
2230 
2231  query = skip_white_space(query);
2232 
2233  wordlen = 0;
2234  while (isalpha((unsigned char) query[wordlen]))
2235  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2236 
2237  if (wordlen == 3 && pg_strncasecmp(query, "all", 3) == 0)
2238  return true;
2239  return false;
2240  }
2241 
2242  return false;
2243 }
static const char * skip_white_space(const char *query)
Definition: common.c:1971
int PQmblenBounded(const char *s, int encoding)
Definition: fe-misc.c:1187
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 286 of file common.c.

287 {
288  return PQstatus(pset.db) != CONNECTION_BAD;
289 }
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7195
@ CONNECTION_BAD
Definition: libpq-fe.h:61

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

1289 {
1290  bool timing = pset.timing;
1291  PGresult *result;
1292  bool OK;
1294  after;
1295 
1296  *elapsed_msec = 0;
1297 
1298  if (timing)
1300  else
1302 
1303  /*
1304  * To parse the query but not execute it, we prepare it, using the unnamed
1305  * prepared statement. This is invisible to psql users, since there's no
1306  * way to access the unnamed prepared statement from psql user space. The
1307  * next Parse or Query protocol message would overwrite the statement
1308  * anyway. (So there's no great need to clear it when done, which is a
1309  * good thing because libpq provides no easy way to do that.)
1310  */
1311  result = PQprepare(pset.db, "", query, 0, NULL);
1312  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1313  {
1315  SetResultVariables(result, false);
1316  ClearOrSaveResult(result);
1317  return false;
1318  }
1319  PQclear(result);
1320 
1321  result = PQdescribePrepared(pset.db, "");
1322  OK = AcceptResult(result, true) &&
1323  (PQresultStatus(result) == PGRES_COMMAND_OK);
1324  if (OK && result)
1325  {
1326  if (PQnfields(result) > 0)
1327  {
1329  int i;
1330 
1331  initPQExpBuffer(&buf);
1332 
1334  "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1335  "FROM (VALUES ",
1336  gettext_noop("Column"),
1337  gettext_noop("Type"));
1338 
1339  for (i = 0; i < PQnfields(result); i++)
1340  {
1341  const char *name;
1342  char *escname;
1343 
1344  if (i > 0)
1345  appendPQExpBufferStr(&buf, ",");
1346 
1347  name = PQfname(result, i);
1348  escname = PQescapeLiteral(pset.db, name, strlen(name));
1349 
1350  if (escname == NULL)
1351  {
1353  PQclear(result);
1354  termPQExpBuffer(&buf);
1355  return false;
1356  }
1357 
1358  appendPQExpBuffer(&buf, "(%s, '%u'::pg_catalog.oid, %d)",
1359  escname,
1360  PQftype(result, i),
1361  PQfmod(result, i));
1362 
1363  PQfreemem(escname);
1364  }
1365 
1366  appendPQExpBufferStr(&buf, ") s(name, tp, tpm)");
1367  PQclear(result);
1368 
1369  result = PQexec(pset.db, buf.data);
1370  OK = AcceptResult(result, true);
1371 
1372  if (timing)
1373  {
1374  INSTR_TIME_SET_CURRENT(after);
1375  INSTR_TIME_SUBTRACT(after, before);
1376  *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1377  }
1378 
1379  if (OK && result)
1380  OK = PrintQueryResult(result, true, NULL, NULL, NULL);
1381 
1382  termPQExpBuffer(&buf);
1383  }
1384  else
1386  _("The command has no result, or the result has no columns.\n"));
1387  }
1388 
1389  SetResultVariables(result, OK);
1390  ClearOrSaveResult(result);
1391 
1392  return OK;
1393 }
static bool PrintQueryResult(PGresult *result, bool last, const printQueryOpt *opt, FILE *printQueryFout, FILE *printStatusFout)
Definition: common.c:961
static void SetResultVariables(PGresult *result, bool success)
Definition: common.c:422
static bool AcceptResult(const PGresult *result, bool show_error)
Definition: common.c:365
#define gettext_noop(x)
Definition: c.h:1209
void PQfreemem(void *ptr)
Definition: fe-exec.c:3946
PGresult * PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Definition: fe-exec.c:2272
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3633
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2421
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:3481
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4218
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2228
int PQfmod(const PGresult *res, int field_num)
Definition: fe-exec.c:3655
int PQnfields(const PGresult *res)
Definition: fe-exec.c:3403
#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:67
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 1417 of file common.c.

1421 {
1422  bool timing = pset.timing;
1423  bool success;
1424  bool return_early = false;
1426  after;
1427  PGresult *result;
1428  FILE *gfile_fout = NULL;
1429  bool gfile_is_pipe = false;
1430 
1431  if (timing)
1433  else
1435 
1436  if (pset.bind_flag)
1437  success = PQsendQueryParams(pset.db, query, pset.bind_nparams, NULL, (const char *const *) pset.bind_params, NULL, NULL, 0);
1438  else
1439  success = PQsendQuery(pset.db, query);
1440 
1441  if (!success)
1442  {
1443  const char *error = PQerrorMessage(pset.db);
1444 
1445  if (strlen(error))
1446  pg_log_info("%s", error);
1447 
1448  CheckConnection();
1449 
1450  return -1;
1451  }
1452 
1453  /*
1454  * If SIGINT is sent while the query is processing, the interrupt will be
1455  * consumed. The user's intention, though, is to cancel the entire watch
1456  * process, so detect a sent cancellation request and exit in this case.
1457  */
1458  if (is_watch && cancel_pressed)
1459  {
1461  return 0;
1462  }
1463 
1464  /* first result */
1465  result = PQgetResult(pset.db);
1466  if (min_rows > 0 && PQntuples(result) < min_rows)
1467  {
1468  return_early = true;
1469  }
1470 
1471  while (result != NULL)
1472  {
1473  ExecStatusType result_status;
1474  PGresult *next_result;
1475  bool last;
1476 
1477  if (!AcceptResult(result, false))
1478  {
1479  /*
1480  * Some error occurred, either a server-side failure or a failure
1481  * to submit the command string. Record that.
1482  */
1483  const char *error = PQresultErrorMessage(result);
1484 
1485  if (strlen(error))
1486  pg_log_info("%s", error);
1487 
1488  CheckConnection();
1489  if (!is_watch)
1490  SetResultVariables(result, false);
1491 
1492  /* keep the result status before clearing it */
1493  result_status = PQresultStatus(result);
1494  ClearOrSaveResult(result);
1495  success = false;
1496 
1497  /*
1498  * switch to next result
1499  */
1500  if (result_status == PGRES_COPY_BOTH ||
1501  result_status == PGRES_COPY_OUT ||
1502  result_status == PGRES_COPY_IN)
1503 
1504  /*
1505  * For some obscure reason PQgetResult does *not* return a
1506  * NULL in copy cases despite the result having been cleared,
1507  * but keeps returning an "empty" result that we have to
1508  * ignore manually.
1509  */
1510  result = NULL;
1511  else
1512  result = PQgetResult(pset.db);
1513 
1514  /*
1515  * Get current timing measure in case an error occurs
1516  */
1517  if (timing)
1518  {
1519  INSTR_TIME_SET_CURRENT(after);
1520  INSTR_TIME_SUBTRACT(after, before);
1521  *elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1522  }
1523 
1524  continue;
1525  }
1526  else if (svpt_gone_p && !*svpt_gone_p)
1527  {
1528  /*
1529  * Check if the user ran any command that would destroy our
1530  * internal savepoint: If the user did COMMIT AND CHAIN, RELEASE
1531  * or ROLLBACK, our savepoint is gone. If they issued a SAVEPOINT,
1532  * releasing ours would remove theirs.
1533  */
1534  const char *cmd = PQcmdStatus(result);
1535 
1536  *svpt_gone_p = (strcmp(cmd, "COMMIT") == 0 ||
1537  strcmp(cmd, "SAVEPOINT") == 0 ||
1538  strcmp(cmd, "RELEASE") == 0 ||
1539  strcmp(cmd, "ROLLBACK") == 0);
1540  }
1541 
1542  result_status = PQresultStatus(result);
1543 
1544  /* must handle COPY before changing the current result */
1545  Assert(result_status != PGRES_COPY_BOTH);
1546  if (result_status == PGRES_COPY_IN ||
1547  result_status == PGRES_COPY_OUT)
1548  {
1549  FILE *copy_stream = NULL;
1550 
1551  /*
1552  * For COPY OUT, direct the output to the default place (probably
1553  * a pager pipe) for \watch, or to pset.copyStream for \copy,
1554  * otherwise to pset.gfname if that's set, otherwise to
1555  * pset.queryFout.
1556  */
1557  if (result_status == PGRES_COPY_OUT)
1558  {
1559  if (is_watch)
1560  {
1561  /* invoked by \watch */
1562  copy_stream = printQueryFout ? printQueryFout : pset.queryFout;
1563  }
1564  else if (pset.copyStream)
1565  {
1566  /* invoked by \copy */
1567  copy_stream = pset.copyStream;
1568  }
1569  else if (pset.gfname)
1570  {
1571  /* send to \g file, which we may have opened already */
1572  if (gfile_fout == NULL)
1573  {
1575  &gfile_fout, &gfile_is_pipe))
1576  {
1577  if (gfile_is_pipe)
1579  copy_stream = gfile_fout;
1580  }
1581  else
1582  success = false;
1583  }
1584  else
1585  copy_stream = gfile_fout;
1586  }
1587  else
1588  {
1589  /* fall back to the generic query output stream */
1590  copy_stream = pset.queryFout;
1591  }
1592  }
1593 
1594  /*
1595  * Even if the output stream could not be opened, we call
1596  * HandleCopyResult() with a NULL output stream to collect and
1597  * discard the COPY data.
1598  */
1599  success &= HandleCopyResult(&result, copy_stream);
1600  }
1601 
1602  /*
1603  * Check PQgetResult() again. In the typical case of a single-command
1604  * string, it will return NULL. Otherwise, we'll have other results
1605  * to process. We need to do that to check whether this is the last.
1606  */
1607  next_result = PQgetResult(pset.db);
1608  last = (next_result == NULL);
1609 
1610  /*
1611  * Update current timing measure.
1612  *
1613  * It will include the display of previous results, if any. This
1614  * cannot be helped because the server goes on processing further
1615  * queries anyway while the previous ones are being displayed. The
1616  * parallel execution of the client display hides the server time when
1617  * it is shorter.
1618  *
1619  * With combined queries, timing must be understood as an upper bound
1620  * of the time spent processing them.
1621  */
1622  if (timing)
1623  {
1624  INSTR_TIME_SET_CURRENT(after);
1625  INSTR_TIME_SUBTRACT(after, before);
1626  *elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1627  }
1628 
1629  /* this may or may not print something depending on settings */
1630  if (result != NULL)
1631  {
1632  /*
1633  * If results need to be printed into the file specified by \g,
1634  * open it, unless we already did. Note that when pset.gfname is
1635  * set, the passed-in value of printQueryFout is not used for
1636  * tuple output, but it's still used for status output.
1637  */
1638  FILE *tuples_fout = printQueryFout;
1639  bool do_print = true;
1640 
1641  if (PQresultStatus(result) == PGRES_TUPLES_OK &&
1642  pset.gfname)
1643  {
1644  if (gfile_fout == NULL)
1645  {
1647  &gfile_fout, &gfile_is_pipe))
1648  {
1649  if (gfile_is_pipe)
1651  }
1652  else
1653  success = do_print = false;
1654  }
1655  tuples_fout = gfile_fout;
1656  }
1657  if (do_print)
1658  success &= PrintQueryResult(result, last, opt,
1659  tuples_fout, printQueryFout);
1660  }
1661 
1662  /* set variables on last result if all went well */
1663  if (!is_watch && last && success)
1664  SetResultVariables(result, true);
1665 
1666  ClearOrSaveResult(result);
1667  result = next_result;
1668 
1669  if (cancel_pressed)
1670  {
1672  break;
1673  }
1674  }
1675 
1676  /* close \g file if we opened it */
1677  if (gfile_fout)
1678  {
1679  if (gfile_is_pipe)
1680  {
1681  SetShellResultVariables(pclose(gfile_fout));
1683  }
1684  else
1685  fclose(gfile_fout);
1686  }
1687 
1688  /* may need this to recover from conn loss during COPY */
1689  if (!CheckConnection())
1690  return -1;
1691 
1692  if (cancel_pressed || return_early)
1693  return 0;
1694 
1695  return success ? 1 : -1;
1696 }
static void ClearOrSaveAllResults(void)
Definition: common.c:508
static bool HandleCopyResult(PGresult **resultp, FILE *copystream)
Definition: common.c:866
void SetShellResultVariables(int wait_result)
Definition: common.c:462
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:58
int PQsendQueryParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:1498
char * PQresultErrorMessage(const PGresult *res)
Definition: fe-exec.c:3341
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3395
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3666
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1422
void restore_sigpipe_trap(void)
Definition: print.c:3062
void disable_sigpipe_trap(void)
Definition: print.c:3039
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
static bool success
Definition: initdb.c:184
ExecStatusType
Definition: libpq-fe.h:95
@ PGRES_COPY_BOTH
Definition: libpq-fe.h:109
Assert(fmt[strlen(fmt) - 1] !='\n')
bool bind_flag
Definition: settings.h:99
FILE * copyStream
Definition: settings.h:87
char ** bind_params
Definition: settings.h:102
char * gfname
Definition: settings.h:93
int bind_nparams
Definition: settings.h:101

References AcceptResult(), Assert(), before(), _psqlSettings::bind_flag, _psqlSettings::bind_nparams, _psqlSettings::bind_params, cancel_pressed, CheckConnection(), ClearOrSaveAllResults(), ClearOrSaveResult(), _psqlSettings::copyStream, _psqlSettings::db, disable_sigpipe_trap(), error(), _psqlSettings::gfname, HandleCopyResult(), INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, INSTR_TIME_SUBTRACT, openQueryOutputFile(), pg_log_info, PGRES_COPY_BOTH, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_TUPLES_OK, PQcmdStatus(), PQerrorMessage(), PQgetResult(), PQntuples(), PQresultErrorMessage(), PQresultStatus(), PQsendQuery(), PQsendQueryParams(), PrintQueryResult(), pset, _psqlSettings::queryFout, restore_sigpipe_trap(), SetResultVariables(), SetShellResultVariables(), success, and _psqlSettings::timing.

Referenced by PSQLexecWatch(), and SendQuery().

◆ ExecQueryTuples()

static bool ExecQueryTuples ( const PGresult result)
static

Definition at line 787 of file common.c.

788 {
789  bool success = true;
790  int nrows = PQntuples(result);
791  int ncolumns = PQnfields(result);
792  int r,
793  c;
794 
795  /*
796  * We must turn off gexec_flag to avoid infinite recursion. Note that
797  * this allows ExecQueryUsingCursor to be applied to the individual query
798  * results. SendQuery prevents it from being applied when fetching the
799  * queries-to-execute, because it can't handle recursion either.
800  */
801  pset.gexec_flag = false;
802 
803  for (r = 0; r < nrows; r++)
804  {
805  for (c = 0; c < ncolumns; c++)
806  {
807  if (!PQgetisnull(result, r, c))
808  {
809  const char *query = PQgetvalue(result, r, c);
810 
811  /* Abandon execution if cancel_pressed */
812  if (cancel_pressed)
813  goto loop_exit;
814 
815  /*
816  * ECHO_ALL mode should echo these queries, but SendQuery
817  * assumes that MainLoop did that, so we have to do it here.
818  */
820  {
821  puts(query);
822  fflush(stdout);
823  }
824 
825  if (!SendQuery(query))
826  {
827  /* Error - abandon execution if ON_ERROR_STOP */
828  success = false;
829  if (pset.on_error_stop)
830  goto loop_exit;
831  }
832  }
833  }
834  }
835 
836 loop_exit:
837 
838  /*
839  * Restore state. We know gexec_flag was on, else we'd not be here. (We
840  * also know it'll get turned off at end of command, but that's not ours
841  * to do here.)
842  */
843  pset.gexec_flag = true;
844 
845  /* Return true if all queries were successful */
846  return success;
847 }
bool SendQuery(const char *query)
Definition: common.c:1043
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3790
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3815
static void const char fflush(stdout)
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
bool gexec_flag
Definition: settings.h:98

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

◆ ExecQueryUsingCursor()

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

Definition at line 1710 of file common.c.

1711 {
1712  bool OK = true;
1713  PGresult *result;
1715  printQueryOpt my_popt = pset.popt;
1716  bool timing = pset.timing;
1717  FILE *fout;
1718  bool is_pipe;
1719  bool is_pager = false;
1720  bool started_txn = false;
1721  int64 total_tuples = 0;
1722  int ntuples;
1723  int fetch_count;
1724  char fetch_cmd[64];
1726  after;
1727  int flush_error;
1728 
1729  *elapsed_msec = 0;
1730 
1731  /* initialize print options for partial table output */
1732  my_popt.topt.start_table = true;
1733  my_popt.topt.stop_table = false;
1734  my_popt.topt.prior_records = 0;
1735 
1736  if (timing)
1738  else
1740 
1741  /* if we're not in a transaction, start one */
1743  {
1744  result = PQexec(pset.db, "BEGIN");
1745  OK = AcceptResult(result, true) &&
1746  (PQresultStatus(result) == PGRES_COMMAND_OK);
1747  ClearOrSaveResult(result);
1748  if (!OK)
1749  return false;
1750  started_txn = true;
1751  }
1752 
1753  /* Send DECLARE CURSOR */
1754  initPQExpBuffer(&buf);
1755  appendPQExpBuffer(&buf, "DECLARE _psql_cursor NO SCROLL CURSOR FOR\n%s",
1756  query);
1757 
1758  result = PQexec(pset.db, buf.data);
1759  OK = AcceptResult(result, true) &&
1760  (PQresultStatus(result) == PGRES_COMMAND_OK);
1761  if (!OK)
1762  SetResultVariables(result, OK);
1763  ClearOrSaveResult(result);
1764  termPQExpBuffer(&buf);
1765  if (!OK)
1766  goto cleanup;
1767 
1768  if (timing)
1769  {
1770  INSTR_TIME_SET_CURRENT(after);
1771  INSTR_TIME_SUBTRACT(after, before);
1772  *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1773  }
1774 
1775  /*
1776  * In \gset mode, we force the fetch count to be 2, so that we will throw
1777  * the appropriate error if the query returns more than one row.
1778  */
1779  if (pset.gset_prefix)
1780  fetch_count = 2;
1781  else
1782  fetch_count = pset.fetch_count;
1783 
1784  snprintf(fetch_cmd, sizeof(fetch_cmd),
1785  "FETCH FORWARD %d FROM _psql_cursor",
1786  fetch_count);
1787 
1788  /* prepare to write output to \g argument, if any */
1789  if (pset.gfname)
1790  {
1791  if (!openQueryOutputFile(pset.gfname, &fout, &is_pipe))
1792  {
1793  OK = false;
1794  goto cleanup;
1795  }
1796  if (is_pipe)
1798  }
1799  else
1800  {
1801  fout = pset.queryFout;
1802  is_pipe = false; /* doesn't matter */
1803  }
1804 
1805  /* clear any pre-existing error indication on the output stream */
1806  clearerr(fout);
1807 
1808  for (;;)
1809  {
1810  if (timing)
1812 
1813  /* get fetch_count tuples at a time */
1814  result = PQexec(pset.db, fetch_cmd);
1815 
1816  if (timing)
1817  {
1818  INSTR_TIME_SET_CURRENT(after);
1819  INSTR_TIME_SUBTRACT(after, before);
1820  *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1821  }
1822 
1823  if (PQresultStatus(result) != PGRES_TUPLES_OK)
1824  {
1825  /* shut down pager before printing error message */
1826  if (is_pager)
1827  {
1828  ClosePager(fout);
1829  is_pager = false;
1830  }
1831 
1832  OK = AcceptResult(result, true);
1833  Assert(!OK);
1834  SetResultVariables(result, OK);
1835  ClearOrSaveResult(result);
1836  break;
1837  }
1838 
1839  if (pset.gset_prefix)
1840  {
1841  /* StoreQueryTuple will complain if not exactly one row */
1842  OK = StoreQueryTuple(result);
1843  ClearOrSaveResult(result);
1844  break;
1845  }
1846 
1847  /*
1848  * Note we do not deal with \gdesc, \gexec or \crosstabview modes here
1849  */
1850 
1851  ntuples = PQntuples(result);
1852  total_tuples += ntuples;
1853 
1854  if (ntuples < fetch_count)
1855  {
1856  /* this is the last result set, so allow footer decoration */
1857  my_popt.topt.stop_table = true;
1858  }
1859  else if (fout == stdout && !is_pager)
1860  {
1861  /*
1862  * If query requires multiple result sets, hack to ensure that
1863  * only one pager instance is used for the whole mess
1864  */
1865  fout = PageOutput(INT_MAX, &(my_popt.topt));
1866  is_pager = true;
1867  }
1868 
1869  printQuery(result, &my_popt, fout, is_pager, pset.logfile);
1870 
1871  ClearOrSaveResult(result);
1872 
1873  /* after the first result set, disallow header decoration */
1874  my_popt.topt.start_table = false;
1875  my_popt.topt.prior_records += ntuples;
1876 
1877  /*
1878  * Make sure to flush the output stream, so intermediate results are
1879  * visible to the client immediately. We check the results because if
1880  * the pager dies/exits/etc, there's no sense throwing more data at
1881  * it.
1882  */
1883  flush_error = fflush(fout);
1884 
1885  /*
1886  * Check if we are at the end, if a cancel was pressed, or if there
1887  * were any errors either trying to flush out the results, or more
1888  * generally on the output stream at all. If we hit any errors
1889  * writing things to the stream, we presume $PAGER has disappeared and
1890  * stop bothering to pull down more data.
1891  */
1892  if (ntuples < fetch_count || cancel_pressed || flush_error ||
1893  ferror(fout))
1894  break;
1895  }
1896 
1897  if (pset.gfname)
1898  {
1899  /* close \g argument file/pipe */
1900  if (is_pipe)
1901  {
1902  SetShellResultVariables(pclose(fout));
1904  }
1905  else
1906  fclose(fout);
1907  }
1908  else if (is_pager)
1909  {
1910  /* close transient pager */
1911  ClosePager(fout);
1912  }
1913 
1914  if (OK)
1915  {
1916  /*
1917  * We don't have a PGresult here, and even if we did it wouldn't have
1918  * the right row count, so fake SetResultVariables(). In error cases,
1919  * we already set the result variables above.
1920  */
1921  char buf[32];
1922 
1923  SetVariable(pset.vars, "ERROR", "false");
1924  SetVariable(pset.vars, "SQLSTATE", "00000");
1925  snprintf(buf, sizeof(buf), INT64_FORMAT, total_tuples);
1926  SetVariable(pset.vars, "ROW_COUNT", buf);
1927  }
1928 
1929 cleanup:
1930  if (timing)
1932 
1933  /*
1934  * We try to close the cursor on either success or failure, but on failure
1935  * ignore the result (it's probably just a bleat about being in an aborted
1936  * transaction)
1937  */
1938  result = PQexec(pset.db, "CLOSE _psql_cursor");
1939  if (OK)
1940  {
1941  OK = AcceptResult(result, true) &&
1942  (PQresultStatus(result) == PGRES_COMMAND_OK);
1943  ClearOrSaveResult(result);
1944  }
1945  else
1946  PQclear(result);
1947 
1948  if (started_txn)
1949  {
1950  result = PQexec(pset.db, OK ? "COMMIT" : "ROLLBACK");
1951  OK &= AcceptResult(result, true) &&
1952  (PQresultStatus(result) == PGRES_COMMAND_OK);
1953  ClearOrSaveResult(result);
1954  }
1955 
1956  if (timing)
1957  {
1958  INSTR_TIME_SET_CURRENT(after);
1959  INSTR_TIME_SUBTRACT(after, before);
1960  *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1961  }
1962 
1963  return OK;
1964 }
static bool StoreQueryTuple(const PGresult *result)
Definition: common.c:723
static void cleanup(void)
Definition: bootstrap.c:687
#define INT64_FORMAT
Definition: c.h:537
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7203
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3535
void ClosePager(FILE *pagerpipe)
Definition: print.c:3141
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:3089
@ PQTRANS_IDLE
Definition: libpq-fe.h:118
#define snprintf
Definition: port.h:238
printQueryOpt popt
Definition: settings.h:91
char * gset_prefix
Definition: settings.h:96
VariableSpace vars
Definition: settings.h:122
FILE * logfile
Definition: settings.h:120
int fetch_count
Definition: settings.h:143
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(), appendPQExpBuffer(), Assert(), before(), buf, cancel_pressed, cleanup(), ClearOrSaveResult(), ClosePager(), _psqlSettings::db, disable_sigpipe_trap(), _psqlSettings::fetch_count, fflush(), _psqlSettings::gfname, _psqlSettings::gset_prefix, initPQExpBuffer(), INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, INSTR_TIME_SUBTRACT, INT64_FORMAT, _psqlSettings::logfile, openQueryOutputFile(), PageOutput(), PGRES_COMMAND_OK, PGRES_TUPLES_OK, _psqlSettings::popt, PQclear(), PQexec(), PQntuples(), PQresultStatus(), PQTRANS_IDLE, PQtransactionStatus(), printQuery(), printTableOpt::prior_records, pset, _psqlSettings::queryFout, restore_sigpipe_trap(), SetResultVariables(), SetShellResultVariables(), SetVariable(), snprintf, printTableOpt::start_table, generate_unaccent_rules::stdout, printTableOpt::stop_table, StoreQueryTuple(), termPQExpBuffer(), _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by SendQuery().

◆ expand_tilde()

void expand_tilde ( char **  filename)

Definition at line 2348 of file common.c.

2349 {
2350  if (!filename || !(*filename))
2351  return;
2352 
2353  /*
2354  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2355  * for short versions of long file names, though the tilde is usually
2356  * toward the end, not at the beginning.
2357  */
2358 #ifndef WIN32
2359 
2360  /* try tilde expansion */
2361  if (**filename == '~')
2362  {
2363  char *fn;
2364  char oldp,
2365  *p;
2366  struct passwd *pw;
2367  char home[MAXPGPATH];
2368 
2369  fn = *filename;
2370  *home = '\0';
2371 
2372  p = fn + 1;
2373  while (*p != '/' && *p != '\0')
2374  p++;
2375 
2376  oldp = *p;
2377  *p = '\0';
2378 
2379  if (*(fn + 1) == '\0')
2380  get_home_path(home); /* ~ or ~/ only */
2381  else if ((pw = getpwnam(fn + 1)) != NULL)
2382  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2383 
2384  *p = oldp;
2385  if (strlen(home) != 0)
2386  {
2387  char *newfn;
2388 
2389  newfn = psprintf("%s%s", home, p);
2390  free(fn);
2391  *filename = newfn;
2392  }
2393  }
2394 #endif
2395 }
#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:928
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 866 of file common.c.

867 {
868  bool success;
869  PGresult *copy_result;
870  ExecStatusType result_status = PQresultStatus(*resultp);
871 
872  Assert(result_status == PGRES_COPY_OUT ||
873  result_status == PGRES_COPY_IN);
874 
876 
877  if (result_status == PGRES_COPY_OUT)
878  {
880  copystream,
881  &copy_result)
882  && (copystream != NULL);
883 
884  /*
885  * Suppress status printing if the report would go to the same place
886  * as the COPY data just went. Note this doesn't prevent error
887  * reporting, since handleCopyOut did that.
888  */
889  if (copystream == pset.queryFout)
890  {
891  PQclear(copy_result);
892  copy_result = NULL;
893  }
894  }
895  else
896  {
897  /* COPY IN */
898  /* Ignore the copystream argument passed to the function */
901  copystream,
902  PQbinaryTuples(*resultp),
903  &copy_result);
904  }
905  ResetCancelConn();
906 
907  /*
908  * Replace the PGRES_COPY_OUT/IN result with COPY command's exit status,
909  * or with NULL if we want to suppress printing anything.
910  */
911  PQclear(*resultp);
912  *resultp = copy_result;
913 
914  return success;
915 }
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:3411
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_select_command()

static bool is_select_command ( const char *  query)
static

Definition at line 2250 of file common.c.

2251 {
2252  int wordlen;
2253 
2254  /*
2255  * First advance over any whitespace, comments and left parentheses.
2256  */
2257  for (;;)
2258  {
2259  query = skip_white_space(query);
2260  if (query[0] == '(')
2261  query++;
2262  else
2263  break;
2264  }
2265 
2266  /*
2267  * Check word length (since "selectx" is not "select").
2268  */
2269  wordlen = 0;
2270  while (isalpha((unsigned char) query[wordlen]))
2271  wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2272 
2273  if (wordlen == 6 && pg_strncasecmp(query, "select", 6) == 0)
2274  return true;
2275 
2276  if (wordlen == 6 && pg_strncasecmp(query, "values", 6) == 0)
2277  return true;
2278 
2279  return false;
2280 }

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

Referenced by SendQuery().

◆ is_superuser()

bool is_superuser ( void  )

Definition at line 2287 of file common.c.

2288 {
2289  const char *val;
2290 
2291  if (!pset.db)
2292  return false;
2293 
2294  val = PQparameterStatus(pset.db, "is_superuser");
2295 
2296  if (val && strcmp(val, "on") == 0)
2297  return true;
2298 
2299  return false;
2300 }
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7213
long val
Definition: informix.c:664

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

◆ NoticeProcessor()

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

Definition at line 229 of file common.c.

230 {
231  (void) arg; /* not used */
232  pg_log_info("%s", message);
233 }
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 58 of file common.c.

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

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

Referenced by ExecQueryAndProcessResults(), ExecQueryUsingCursor(), and setQFout().

◆ PrintNotifications()

static void PrintNotifications ( void  )
static

Definition at line 666 of file common.c.

667 {
668  PGnotify *notify;
669 
671  while ((notify = PQnotifies(pset.db)) != NULL)
672  {
673  /* for backward compatibility, only show payload if nonempty */
674  if (notify->extra[0])
675  fprintf(pset.queryFout, _("Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
676  notify->relname, notify->extra, notify->be_pid);
677  else
678  fprintf(pset.queryFout, _("Asynchronous notification \"%s\" received from server process with PID %d.\n"),
679  notify->relname, notify->be_pid);
681  PQfreemem(notify);
683  }
684 }
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2633
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1957
int be_pid
Definition: libpq-fe.h:190
char * relname
Definition: libpq-fe.h:189
char * extra
Definition: libpq-fe.h:191

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

964 {
965  bool success;
966  const char *cmdstatus;
967 
968  if (!result)
969  return false;
970 
971  switch (PQresultStatus(result))
972  {
973  case PGRES_TUPLES_OK:
974  /* store or execute or print the data ... */
975  if (last && pset.gset_prefix)
976  success = StoreQueryTuple(result);
977  else if (last && pset.gexec_flag)
978  success = ExecQueryTuples(result);
979  else if (last && pset.crosstab_flag)
980  success = PrintResultInCrosstab(result);
981  else if (last || pset.show_all_results)
982  success = PrintQueryTuples(result, opt, printQueryFout);
983  else
984  success = true;
985 
986  /* if it's INSERT/UPDATE/DELETE RETURNING, also print status */
987  if (last || pset.show_all_results)
988  {
989  cmdstatus = PQcmdStatus(result);
990  if (strncmp(cmdstatus, "INSERT", 6) == 0 ||
991  strncmp(cmdstatus, "UPDATE", 6) == 0 ||
992  strncmp(cmdstatus, "DELETE", 6) == 0)
993  PrintQueryStatus(result, printStatusFout);
994  }
995 
996  break;
997 
998  case PGRES_COMMAND_OK:
999  if (last || pset.show_all_results)
1000  PrintQueryStatus(result, printStatusFout);
1001  success = true;
1002  break;
1003 
1004  case PGRES_EMPTY_QUERY:
1005  success = true;
1006  break;
1007 
1008  case PGRES_COPY_OUT:
1009  case PGRES_COPY_IN:
1010  /* nothing to do here: already processed */
1011  success = true;
1012  break;
1013 
1014  case PGRES_BAD_RESPONSE:
1015  case PGRES_NONFATAL_ERROR:
1016  case PGRES_FATAL_ERROR:
1017  success = false;
1018  break;
1019 
1020  default:
1021  success = false;
1022  pg_log_error("unexpected PQresultStatus: %d",
1023  PQresultStatus(result));
1024  break;
1025  }
1026 
1027  return success;
1028 }
static void PrintQueryStatus(PGresult *result, FILE *printQueryFout)
Definition: common.c:923
static bool ExecQueryTuples(const PGresult *result)
Definition: common.c:787
static bool PrintQueryTuples(const PGresult *result, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:699
bool PrintResultInCrosstab(const PGresult *res)
Definition: crosstabview.c:103
bool show_all_results
Definition: settings.h:155
bool crosstab_flag
Definition: settings.h:103

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, PQcmdStatus(), 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 923 of file common.c.

924 {
925  char buf[16];
926  FILE *fout = printQueryFout ? printQueryFout : pset.queryFout;
927 
928  if (!pset.quiet)
929  {
930  if (pset.popt.topt.format == PRINT_HTML)
931  {
932  fputs("<p>", fout);
933  html_escaped_print(PQcmdStatus(result), fout);
934  fputs("</p>\n", fout);
935  }
936  else
937  fprintf(fout, "%s\n", PQcmdStatus(result));
938  fflush(fout);
939  }
940 
941  if (pset.logfile)
942  fprintf(pset.logfile, "%s\n", PQcmdStatus(result));
943 
944  snprintf(buf, sizeof(buf), "%u", (unsigned int) PQoidValue(result));
945  SetVariable(pset.vars, "LASTOID", buf);
946 }
Oid PQoidValue(const PGresult *res)
Definition: fe-exec.c:3707
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, _psqlSettings::popt, PQcmdStatus(), PQoidValue(), PRINT_HTML, pset, _psqlSettings::queryFout, _psqlSettings::quiet, SetVariable(), snprintf, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by PrintQueryResult().

◆ PrintQueryTuples()

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

Definition at line 699 of file common.c.

701 {
702  bool ok = true;
703  FILE *fout = printQueryFout ? printQueryFout : pset.queryFout;
704 
705  printQuery(result, opt ? opt : &pset.popt, fout, false, pset.logfile);
706  fflush(fout);
707  if (ferror(fout))
708  {
709  pg_log_error("could not print result table: %m");
710  ok = false;
711  }
712 
713  return ok;
714 }

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

523 {
524  double seconds;
525  double minutes;
526  double hours;
527  double days;
528 
529  if (elapsed_msec < 1000.0)
530  {
531  /* This is the traditional (pre-v10) output format */
532  printf(_("Time: %.3f ms\n"), elapsed_msec);
533  return;
534  }
535 
536  /*
537  * Note: we could print just seconds, in a format like %06.3f, when the
538  * total is less than 1min. But that's hard to interpret unless we tack
539  * on "s" or otherwise annotate it. Forcing the display to include
540  * minutes seems like a better solution.
541  */
542  seconds = elapsed_msec / 1000.0;
543  minutes = floor(seconds / 60.0);
544  seconds -= 60.0 * minutes;
545  if (minutes < 60.0)
546  {
547  printf(_("Time: %.3f ms (%02d:%06.3f)\n"),
548  elapsed_msec, (int) minutes, seconds);
549  return;
550  }
551 
552  hours = floor(minutes / 60.0);
553  minutes -= 60.0 * hours;
554  if (hours < 24.0)
555  {
556  printf(_("Time: %.3f ms (%02d:%02d:%06.3f)\n"),
557  elapsed_msec, (int) hours, (int) minutes, seconds);
558  return;
559  }
560 
561  days = floor(hours / 24.0);
562  hours -= 24.0 * days;
563  printf(_("Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"),
564  elapsed_msec, days, (int) hours, (int) minutes, seconds);
565 }
const char *const days[]
Definition: datetime.c:86
#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 259 of file common.c.

260 {
261 #ifndef WIN32
262  /* if we are waiting for input, longjmp out of it */
264  {
265  sigint_interrupt_enabled = false;
266  siglongjmp(sigint_interrupt_jmp, 1);
267  }
268 #endif
269 
270  /* else, set cancel flag to stop any long-running loops */
271  cancel_pressed = true;
272 }
volatile sig_atomic_t sigint_interrupt_enabled
Definition: common.c:254
sigjmp_buf sigint_interrupt_jmp
Definition: common.c:256

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

140 {
141  char *result = NULL;
142  const char *value;
143 
144  /* In an inactive \if branch, suppress all variable substitutions */
145  if (passthrough && !conditional_active((ConditionalStack) passthrough))
146  return NULL;
147 
148  value = GetVariable(pset.vars, varname);
149  if (!value)
150  return NULL;
151 
152  switch (quote)
153  {
154  case PQUOTE_PLAIN:
155  result = pg_strdup(value);
156  break;
157  case PQUOTE_SQL_LITERAL:
158  case PQUOTE_SQL_IDENT:
159  {
160  /*
161  * For these cases, we use libpq's quoting functions, which
162  * assume the string is in the connection's client encoding.
163  */
164  char *escaped_value;
165 
166  if (!pset.db)
167  {
168  pg_log_error("cannot escape without active connection");
169  return NULL;
170  }
171 
172  if (quote == PQUOTE_SQL_LITERAL)
173  escaped_value =
174  PQescapeLiteral(pset.db, value, strlen(value));
175  else
176  escaped_value =
177  PQescapeIdentifier(pset.db, value, strlen(value));
178 
179  if (escaped_value == NULL)
180  {
181  const char *error = PQerrorMessage(pset.db);
182 
183  pg_log_info("%s", error);
184  return NULL;
185  }
186 
187  /*
188  * Rather than complicate the lexer's API with a notion of
189  * which free() routine to use, just pay the price of an extra
190  * strdup().
191  */
192  result = pg_strdup(escaped_value);
193  PQfreemem(escaped_value);
194  break;
195  }
196  case PQUOTE_SHELL_ARG:
197  {
198  /*
199  * For this we use appendShellStringNoError, which is
200  * encoding-agnostic, which is fine since the shell probably
201  * is too. In any case, the only special character is "'",
202  * which is not known to appear in valid multibyte characters.
203  */
205 
208  {
209  pg_log_error("shell command argument contains a newline or carriage return: \"%s\"",
210  value);
211  free(buf.data);
212  return NULL;
213  }
214  result = buf.data;
215  break;
216  }
217 
218  /* No default: we want a compiler warning for missing cases */
219  }
220 
221  return result;
222 }
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4224
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static struct @148 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 275 of file common.c.

276 {
278 }
static void setup_cancel_handler(void)
Definition: parallel.c:608
static void psql_cancel_callback(void)
Definition: common.c:259

References psql_cancel_callback(), and setup_cancel_handler().

Referenced by main().

◆ PSQLexec()

PGresult* PSQLexec ( const char *  query)

Definition at line 581 of file common.c.

582 {
583  PGresult *res;
584 
585  if (!pset.db)
586  {
587  pg_log_error("You are currently not connected to a database.");
588  return NULL;
589  }
590 
592  {
593  printf(_("/******** QUERY *********/\n"
594  "%s\n"
595  "/************************/\n\n"), query);
596  fflush(stdout);
597  if (pset.logfile)
598  {
600  _("/******** QUERY *********/\n"
601  "%s\n"
602  "/************************/\n\n"), query);
604  }
605 
607  return NULL;
608  }
609 
611 
612  res = PQexec(pset.db, query);
613 
614  ResetCancelConn();
615 
616  if (!AcceptResult(res, true))
617  {
619  res = NULL;
620  }
621 
622  return res;
623 }
@ 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 636 of file common.c.

637 {
638  bool timing = pset.timing;
639  double elapsed_msec = 0;
640  int res;
641 
642  if (!pset.db)
643  {
644  pg_log_error("You are currently not connected to a database.");
645  return 0;
646  }
647 
649 
650  res = ExecQueryAndProcessResults(query, &elapsed_msec, NULL, true, min_rows, opt, printQueryFout);
651 
652  ResetCancelConn();
653 
654  /* Possible microtiming output */
655  if (timing)
656  PrintTiming(elapsed_msec);
657 
658  return res;
659 }
static int ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, int min_rows, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:1417
static void PrintTiming(double elapsed_msec)
Definition: common.c:522

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

2434 {
2435  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2436 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2406
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 1043 of file common.c.

1044 {
1045  bool timing = pset.timing;
1046  PGTransactionStatusType transaction_status;
1047  double elapsed_msec = 0;
1048  bool OK = false;
1049  int i;
1050  bool on_error_rollback_savepoint = false;
1051  bool svpt_gone = false;
1052 
1053  if (!pset.db)
1054  {
1055  pg_log_error("You are currently not connected to a database.");
1056  goto sendquery_cleanup;
1057  }
1058 
1059  if (pset.singlestep)
1060  {
1061  char buf[3];
1062 
1063  fflush(stderr);
1064  printf(_("/**(Single step mode: verify command)******************************************/\n"
1065  "%s\n"
1066  "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1067  query);
1068  fflush(stdout);
1069  if (fgets(buf, sizeof(buf), stdin) != NULL)
1070  if (buf[0] == 'x')
1071  goto sendquery_cleanup;
1072  if (cancel_pressed)
1073  goto sendquery_cleanup;
1074  }
1075  else if (pset.echo == PSQL_ECHO_QUERIES)
1076  {
1077  puts(query);
1078  fflush(stdout);
1079  }
1080 
1081  if (pset.logfile)
1082  {
1084  _("/******** QUERY *********/\n"
1085  "%s\n"
1086  "/************************/\n\n"), query);
1087  fflush(pset.logfile);
1088  }
1089 
1091 
1092  transaction_status = PQtransactionStatus(pset.db);
1093 
1094  if (transaction_status == PQTRANS_IDLE &&
1095  !pset.autocommit &&
1096  !command_no_begin(query))
1097  {
1098  PGresult *result;
1099 
1100  result = PQexec(pset.db, "BEGIN");
1101  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1102  {
1104  ClearOrSaveResult(result);
1105  goto sendquery_cleanup;
1106  }
1107  ClearOrSaveResult(result);
1108  transaction_status = PQtransactionStatus(pset.db);
1109  }
1110 
1111  if (transaction_status == PQTRANS_INTRANS &&
1115  {
1116  PGresult *result;
1117 
1118  result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1119  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1120  {
1122  ClearOrSaveResult(result);
1123  goto sendquery_cleanup;
1124  }
1125  ClearOrSaveResult(result);
1126  on_error_rollback_savepoint = true;
1127  }
1128 
1129  if (pset.gdesc_flag)
1130  {
1131  /* Describe query's result columns, without executing it */
1132  OK = DescribeQuery(query, &elapsed_msec);
1133  }
1134  else if (pset.fetch_count <= 0 || pset.gexec_flag ||
1136  {
1137  /* Default fetch-it-all-and-print mode */
1138  OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, 0, NULL, NULL) > 0);
1139  }
1140  else
1141  {
1142  /* Fetch-in-segments mode */
1143  OK = ExecQueryUsingCursor(query, &elapsed_msec);
1144  }
1145 
1146  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1147  pg_log_info("STATEMENT: %s", query);
1148 
1149  /* If we made a temporary savepoint, possibly release/rollback */
1150  if (on_error_rollback_savepoint)
1151  {
1152  const char *svptcmd = NULL;
1153 
1154  transaction_status = PQtransactionStatus(pset.db);
1155 
1156  switch (transaction_status)
1157  {
1158  case PQTRANS_INERROR:
1159  /* We always rollback on an error */
1160  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1161  break;
1162 
1163  case PQTRANS_IDLE:
1164  /* If they are no longer in a transaction, then do nothing */
1165  break;
1166 
1167  case PQTRANS_INTRANS:
1168 
1169  /*
1170  * Release our savepoint, but do nothing if they are messing
1171  * with savepoints themselves
1172  */
1173  if (!svpt_gone)
1174  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1175  break;
1176 
1177  case PQTRANS_ACTIVE:
1178  case PQTRANS_UNKNOWN:
1179  default:
1180  OK = false;
1181  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1182  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1183  pg_log_error("unexpected transaction status (%d)",
1184  transaction_status);
1185  break;
1186  }
1187 
1188  if (svptcmd)
1189  {
1190  PGresult *svptres;
1191 
1192  svptres = PQexec(pset.db, svptcmd);
1193  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1194  {
1196  ClearOrSaveResult(svptres);
1197  OK = false;
1198 
1199  goto sendquery_cleanup;
1200  }
1201  PQclear(svptres);
1202  }
1203  }
1204 
1205  /* Possible microtiming output */
1206  if (timing)
1207  PrintTiming(elapsed_msec);
1208 
1209  /* check for events that may occur during query execution */
1210 
1211  if (pset.encoding != PQclientEncoding(pset.db) &&
1212  PQclientEncoding(pset.db) >= 0)
1213  {
1214  /* track effects of SET CLIENT_ENCODING */
1217  SetVariable(pset.vars, "ENCODING",
1219  }
1220 
1222 
1223  /* perform cleanup that should occur after any attempted query */
1224 
1225 sendquery_cleanup:
1226 
1227  /* global cancellation reset */
1228  ResetCancelConn();
1229 
1230  /* reset \g's output-to-filename trigger */
1231  if (pset.gfname)
1232  {
1233  free(pset.gfname);
1234  pset.gfname = NULL;
1235  }
1236 
1237  /* restore print settings if \g changed them */
1238  if (pset.gsavepopt)
1239  {
1241  pset.gsavepopt = NULL;
1242  }
1243 
1244  /* clean up after \bind */
1245  if (pset.bind_flag)
1246  {
1247  for (i = 0; i < pset.bind_nparams; i++)
1248  free(pset.bind_params[i]);
1250  pset.bind_params = NULL;
1251  pset.bind_flag = false;
1252  }
1253 
1254  /* reset \gset trigger */
1255  if (pset.gset_prefix)
1256  {
1258  pset.gset_prefix = NULL;
1259  }
1260 
1261  /* reset \gdesc trigger */
1262  pset.gdesc_flag = false;
1263 
1264  /* reset \gexec trigger */
1265  pset.gexec_flag = false;
1266 
1267  /* reset \crosstabview trigger */
1268  pset.crosstab_flag = false;
1269  for (i = 0; i < lengthof(pset.ctv_args); i++)
1270  {
1271  pg_free(pset.ctv_args[i]);
1272  pset.ctv_args[i] = NULL;
1273  }
1274 
1275  return OK;
1276 }
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1288
static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec)
Definition: common.c:1710
static void PrintNotifications(void)
Definition: common.c:666
static bool is_select_command(const char *query)
Definition: common.c:2250
static bool command_no_begin(const char *query)
Definition: common.c:2035
#define lengthof(array)
Definition: c.h:777
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:4977
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7336
void pg_free(void *ptr)
Definition: fe_memutils.c:105
PGTransactionStatusType
Definition: libpq-fe.h:117
@ PQTRANS_INTRANS
Definition: libpq-fe.h:120
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:119
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:122
@ PQTRANS_INERROR
Definition: libpq-fe.h:121
@ 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(), ExecQueryUsingCursor(), _psqlSettings::fetch_count, fflush(), fprintf, free, _psqlSettings::gdesc_flag, _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gsavepopt, _psqlSettings::gset_prefix, i, is_select_command(), 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 2327 of file common.c.

2328 {
2329  const char *val;
2330 
2331  if (!pset.db)
2332  return NULL;
2333 
2334  val = PQparameterStatus(pset.db, "session_authorization");
2335  if (val)
2336  return val;
2337  else
2338  return PQuser(pset.db);
2339 }
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7102

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

Referenced by get_prompt().

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 94 of file common.c.

95 {
96  FILE *fout;
97  bool is_pipe;
98 
99  /* First make sure we can open the new output file/pipe */
100  if (!openQueryOutputFile(fname, &fout, &is_pipe))
101  return false;
102 
103  /* Close old file/pipe */
104  if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
105  {
106  if (pset.queryFoutPipe)
108  else
109  fclose(pset.queryFout);
110  }
111 
112  pset.queryFout = fout;
113  pset.queryFoutPipe = is_pipe;
114 
115  /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
116  set_sigpipe_trap_state(is_pipe);
118 
119  return true;
120 }
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 422 of file common.c.

423 {
424  if (success)
425  {
426  const char *ntuples = PQcmdTuples(result);
427 
428  SetVariable(pset.vars, "ERROR", "false");
429  SetVariable(pset.vars, "SQLSTATE", "00000");
430  SetVariable(pset.vars, "ROW_COUNT", *ntuples ? ntuples : "0");
431  }
432  else
433  {
434  const char *code = PQresultErrorField(result, PG_DIAG_SQLSTATE);
435  const char *mesg = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
436 
437  SetVariable(pset.vars, "ERROR", "true");
438 
439  /*
440  * If there is no SQLSTATE code, use an empty string. This can happen
441  * for libpq-detected errors (e.g., lost connection, ENOMEM).
442  */
443  if (code == NULL)
444  code = "";
445  SetVariable(pset.vars, "SQLSTATE", code);
446  SetVariable(pset.vars, "ROW_COUNT", "0");
447  SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", code);
448  SetVariable(pset.vars, "LAST_ERROR_MESSAGE", mesg ? mesg : "");
449  }
450 }
char * PQcmdTuples(PGresult *res)
Definition: fe-exec.c:3736
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3380
#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(), ExecQueryAndProcessResults(), and ExecQueryUsingCursor().

◆ SetShellResultVariables()

void SetShellResultVariables ( int  wait_result)

Definition at line 462 of file common.c.

463 {
464  char buf[32];
465 
466  SetVariable(pset.vars, "SHELL_ERROR",
467  (wait_result == 0) ? "false" : "true");
468  snprintf(buf, sizeof(buf), "%d", wait_result_to_exit_code(wait_result));
469  SetVariable(pset.vars, "SHELL_EXIT_CODE", buf);
470 }
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 do_copy(), do_shell(), exec_command_write(), ExecQueryAndProcessResults(), ExecQueryUsingCursor(), and setQFout().

◆ skip_white_space()

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

Definition at line 1971 of file common.c.

1972 {
1973  int cnestlevel = 0; /* slash-star comment nest level */
1974 
1975  while (*query)
1976  {
1977  int mblen = PQmblenBounded(query, pset.encoding);
1978 
1979  /*
1980  * Note: we assume the encoding is a superset of ASCII, so that for
1981  * example "query[0] == '/'" is meaningful. However, we do NOT assume
1982  * that the second and subsequent bytes of a multibyte character
1983  * couldn't look like ASCII characters; so it is critical to advance
1984  * by mblen, not 1, whenever we haven't exactly identified the
1985  * character we are skipping over.
1986  */
1987  if (isspace((unsigned char) *query))
1988  query += mblen;
1989  else if (query[0] == '/' && query[1] == '*')
1990  {
1991  cnestlevel++;
1992  query += 2;
1993  }
1994  else if (cnestlevel > 0 && query[0] == '*' && query[1] == '/')
1995  {
1996  cnestlevel--;
1997  query += 2;
1998  }
1999  else if (cnestlevel == 0 && query[0] == '-' && query[1] == '-')
2000  {
2001  query += 2;
2002 
2003  /*
2004  * We have to skip to end of line since any slash-star inside the
2005  * -- comment does NOT start a slash-star comment.
2006  */
2007  while (*query)
2008  {
2009  if (*query == '\n')
2010  {
2011  query++;
2012  break;
2013  }
2014  query += PQmblenBounded(query, pset.encoding);
2015  }
2016  }
2017  else if (cnestlevel > 0)
2018  query += mblen;
2019  else
2020  break; /* found first token */
2021  }
2022 
2023  return query;
2024 }

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

Referenced by command_no_begin(), and is_select_command().

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2307 of file common.c.

2308 {
2309  const char *val;
2310 
2311  if (!pset.db)
2312  return false;
2313 
2314  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2315 
2316  if (val && strcmp(val, "on") == 0)
2317  return true;
2318 
2319  return false;
2320 }

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

724 {
725  bool success = true;
726 
727  if (PQntuples(result) < 1)
728  {
729  pg_log_error("no rows returned for \\gset");
730  success = false;
731  }
732  else if (PQntuples(result) > 1)
733  {
734  pg_log_error("more than one row returned for \\gset");
735  success = false;
736  }
737  else
738  {
739  int i;
740 
741  for (i = 0; i < PQnfields(result); i++)
742  {
743  char *colname = PQfname(result, i);
744  char *varname;
745  char *value;
746 
747  /* concatenate prefix and column name */
748  varname = psprintf("%s%s", pset.gset_prefix, colname);
749 
750  if (VariableHasHook(pset.vars, varname))
751  {
752  pg_log_warning("attempt to \\gset into specially treated variable \"%s\" ignored",
753  varname);
754  continue;
755  }
756 
757  if (!PQgetisnull(result, 0, i))
758  value = PQgetvalue(result, 0, i);
759  else
760  {
761  /* for NULL value, unset rather than set the variable */
762  value = NULL;
763  }
764 
765  if (!SetVariable(pset.vars, varname, value))
766  {
767  free(varname);
768  success = false;
769  break;
770  }
771 
772  free(varname);
773  }
774  }
775 
776  return success;
777 }
#define pg_log_warning(...)
Definition: pgfnames.c:24
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 ExecQueryUsingCursor(), and PrintQueryResult().

◆ uri_prefix_length()

static int uri_prefix_length ( const char *  connstr)
static

Definition at line 2406 of file common.c.

2407 {
2408  /* The connection URI must start with either of the following designators: */
2409  static const char uri_designator[] = "postgresql://";
2410  static const char short_uri_designator[] = "postgres://";
2411 
2412  if (strncmp(connstr, uri_designator,
2413  sizeof(uri_designator) - 1) == 0)
2414  return sizeof(uri_designator) - 1;
2415 
2416  if (strncmp(connstr, short_uri_designator,
2417  sizeof(short_uri_designator) - 1) == 0)
2418  return sizeof(short_uri_designator) - 1;
2419 
2420  return 0;
2421 }
static const char short_uri_designator[]
Definition: fe-connect.c:387
static const char uri_designator[]
Definition: fe-connect.c:386

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