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, 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)
 
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 364 of file common.c.

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

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

508 {
509  PGresult *result;
510 
511  while ((result = PQgetResult(pset.db)) != NULL)
512  ClearOrSaveResult(result);
513 }
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:483
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 483 of file common.c.

484 {
485  if (result)
486  {
487  switch (PQresultStatus(result))
488  {
490  case PGRES_FATAL_ERROR:
492  pset.last_error_result = result;
493  break;
494 
495  default:
496  PQclear(result);
497  break;
498  }
499  }
500 }
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 2026 of file common.c.

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

286 {
287  return PQstatus(pset.db) != CONNECTION_BAD;
288 }
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7192
@ 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 1287 of file common.c.

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

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,
const printQueryOpt opt,
FILE *  printQueryFout 
)
static

Definition at line 1416 of file common.c.

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

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

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

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

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

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

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

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

Referenced by SendQuery().

◆ is_superuser()

bool is_superuser ( void  )

Definition at line 2278 of file common.c.

2279 {
2280  const char *val;
2281 
2282  if (!pset.db)
2283  return false;
2284 
2285  val = PQparameterStatus(pset.db, "is_superuser");
2286 
2287  if (val && strcmp(val, "on") == 0)
2288  return true;
2289 
2290  return false;
2291 }
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7210
long val
Definition: informix.c:664

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

◆ NoticeProcessor()

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

Definition at line 228 of file common.c.

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

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

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

666 {
667  PGnotify *notify;
668 
670  while ((notify = PQnotifies(pset.db)) != NULL)
671  {
672  /* for backward compatibility, only show payload if nonempty */
673  if (notify->extra[0])
674  fprintf(pset.queryFout, _("Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
675  notify->relname, notify->extra, notify->be_pid);
676  else
677  fprintf(pset.queryFout, _("Asynchronous notification \"%s\" received from server process with PID %d.\n"),
678  notify->relname, notify->be_pid);
680  PQfreemem(notify);
682  }
683 }
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2551
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 960 of file common.c.

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

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

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

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

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

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

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

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

275 {
277 }
static void setup_cancel_handler(void)
Definition: parallel.c:608
static void psql_cancel_callback(void)
Definition: common.c:258

References psql_cancel_callback(), and setup_cancel_handler().

Referenced by main().

◆ PSQLexec()

PGresult* PSQLexec ( const char *  query)

Definition at line 580 of file common.c.

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

Definition at line 635 of file common.c.

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

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

2425 {
2426  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2427 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2397
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 1042 of file common.c.

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

2319 {
2320  const char *val;
2321 
2322  if (!pset.db)
2323  return NULL;
2324 
2325  val = PQparameterStatus(pset.db, "session_authorization");
2326  if (val)
2327  return val;
2328  else
2329  return PQuser(pset.db);
2330 }
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7099

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

Referenced by get_prompt().

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 93 of file common.c.

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

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

462 {
463  char buf[32];
464 
465  SetVariable(pset.vars, "SHELL_ERROR",
466  (wait_result == 0) ? "false" : "true");
467  snprintf(buf, sizeof(buf), "%d", wait_result_to_exit_code(wait_result));
468  SetVariable(pset.vars, "SHELL_EXIT_CODE", buf);
469 }
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 1962 of file common.c.

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

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

Referenced by command_no_begin(), and is_select_command().

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2298 of file common.c.

2299 {
2300  const char *val;
2301 
2302  if (!pset.db)
2303  return false;
2304 
2305  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2306 
2307  if (val && strcmp(val, "on") == 0)
2308  return true;
2309 
2310  return false;
2311 }

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

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

2398 {
2399  /* The connection URI must start with either of the following designators: */
2400  static const char uri_designator[] = "postgresql://";
2401  static const char short_uri_designator[] = "postgres://";
2402 
2403  if (strncmp(connstr, uri_designator,
2404  sizeof(uri_designator) - 1) == 0)
2405  return sizeof(uri_designator) - 1;
2406 
2407  if (strncmp(connstr, short_uri_designator,
2408  sizeof(short_uri_designator) - 1) == 0)
2409  return sizeof(short_uri_designator) - 1;
2410 
2411  return 0;
2412 }
static const char short_uri_designator[]
Definition: fe-connect.c:389
static const char uri_designator[]
Definition: fe-connect.c:388

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