PostgreSQL Source Code  git master
common.h File Reference
#include <setjmp.h>
#include "libpq-fe.h"
#include "fe_utils/print.h"
#include "fe_utils/psqlscan.h"
Include dependency graph for common.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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)
 
void setup_cancel_handler (void)
 
void SetCancelConn (void)
 
void ResetCancelConn (void)
 
PGresultPSQLexec (const char *query)
 
int PSQLexecWatch (const char *query, const printQueryOpt *opt)
 
bool SendQuery (const char *query)
 
bool is_superuser (void)
 
bool standard_strings (void)
 
const char * session_username (void)
 
void expand_tilde (char **filename)
 
bool recognized_connection_string (const char *connstr)
 

Variables

volatile bool sigint_interrupt_enabled
 
sigjmp_buf sigint_interrupt_jmp
 

Function Documentation

◆ expand_tilde()

void expand_tilde ( char **  filename)

Definition at line 2343 of file common.c.

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

2344 {
2345  if (!filename || !(*filename))
2346  return;
2347 
2348  /*
2349  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2350  * for short versions of long file names, though the tilde is usually
2351  * toward the end, not at the beginning.
2352  */
2353 #ifndef WIN32
2354 
2355  /* try tilde expansion */
2356  if (**filename == '~')
2357  {
2358  char *fn;
2359  char oldp,
2360  *p;
2361  struct passwd *pw;
2362  char home[MAXPGPATH];
2363 
2364  fn = *filename;
2365  *home = '\0';
2366 
2367  p = fn + 1;
2368  while (*p != '/' && *p != '\0')
2369  p++;
2370 
2371  oldp = *p;
2372  *p = '\0';
2373 
2374  if (*(fn + 1) == '\0')
2375  get_home_path(home); /* ~ or ~/ only */
2376  else if ((pw = getpwnam(fn + 1)) != NULL)
2377  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2378 
2379  *p = oldp;
2380  if (strlen(home) != 0)
2381  {
2382  char *newfn;
2383 
2384  newfn = psprintf("%s%s", home, p);
2385  free(fn);
2386  *filename = newfn;
2387  }
2388  }
2389 #endif
2390 
2391  return;
2392 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define MAXPGPATH
static void * fn(void *arg)
#define free(a)
Definition: header.h:65
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
static char * filename
Definition: pg_dumpall.c:91
bool get_home_path(char *ret_path)
Definition: path.c:807

◆ is_superuser()

bool is_superuser ( void  )

Definition at line 2275 of file common.c.

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

2276 {
2277  const char *val;
2278 
2279  if (!pset.db)
2280  return false;
2281 
2282  val = PQparameterStatus(pset.db, "is_superuser");
2283 
2284  if (val && strcmp(val, "on") == 0)
2285  return true;
2286 
2287  return false;
2288 }
PGconn * db
Definition: settings.h:83
PsqlSettings pset
Definition: startup.c:35
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6543
long val
Definition: informix.c:684

◆ NoticeProcessor()

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

Definition at line 221 of file common.c.

References pg_log_info.

Referenced by do_connect(), and main().

222 {
223  (void) arg; /* not used */
224  pg_log_info("%s", message);
225 }
void * arg
#define pg_log_info(...)
Definition: logging.h:87

◆ openQueryOutputFile()

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

Definition at line 51 of file common.c.

References pg_log_error, and generate_unaccent_rules::stdout.

Referenced by ExecQueryUsingCursor(), PrintQueryTuples(), ProcessResult(), and setQFout().

52 {
53  if (!fname || fname[0] == '\0')
54  {
55  *fout = stdout;
56  *is_pipe = false;
57  }
58  else if (*fname == '|')
59  {
60  *fout = popen(fname + 1, "w");
61  *is_pipe = true;
62  }
63  else
64  {
65  *fout = fopen(fname, "w");
66  *is_pipe = false;
67  }
68 
69  if (*fout == NULL)
70  {
71  pg_log_error("%s: %m", fname);
72  return false;
73  }
74 
75  return true;
76 }
#define pg_log_error(...)
Definition: logging.h:79

◆ psql_get_variable()

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

Definition at line 130 of file common.c.

References appendShellStringNoError(), buf, conditional_active(), PQExpBufferData::data, _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.

132 {
133  char *result = NULL;
134  const char *value;
135 
136  /* In an inactive \if branch, suppress all variable substitutions */
137  if (passthrough && !conditional_active((ConditionalStack) passthrough))
138  return NULL;
139 
140  value = GetVariable(pset.vars, varname);
141  if (!value)
142  return NULL;
143 
144  switch (quote)
145  {
146  case PQUOTE_PLAIN:
147  result = pg_strdup(value);
148  break;
149  case PQUOTE_SQL_LITERAL:
150  case PQUOTE_SQL_IDENT:
151  {
152  /*
153  * For these cases, we use libpq's quoting functions, which
154  * assume the string is in the connection's client encoding.
155  */
156  char *escaped_value;
157 
158  if (!pset.db)
159  {
160  pg_log_error("cannot escape without active connection");
161  return NULL;
162  }
163 
164  if (quote == PQUOTE_SQL_LITERAL)
165  escaped_value =
166  PQescapeLiteral(pset.db, value, strlen(value));
167  else
168  escaped_value =
169  PQescapeIdentifier(pset.db, value, strlen(value));
170 
171  if (escaped_value == NULL)
172  {
173  const char *error = PQerrorMessage(pset.db);
174 
175  pg_log_info("%s", error);
176  return NULL;
177  }
178 
179  /*
180  * Rather than complicate the lexer's API with a notion of
181  * which free() routine to use, just pay the price of an extra
182  * strdup().
183  */
184  result = pg_strdup(escaped_value);
185  PQfreemem(escaped_value);
186  break;
187  }
188  case PQUOTE_SHELL_ARG:
189  {
190  /*
191  * For this we use appendShellStringNoError, which is
192  * encoding-agnostic, which is fine since the shell probably
193  * is too. In any case, the only special character is "'",
194  * which is not known to appear in valid multibyte characters.
195  */
197 
198  initPQExpBuffer(&buf);
199  if (!appendShellStringNoError(&buf, value))
200  {
201  pg_log_error("shell command argument contains a newline or carriage return: \"%s\"",
202  value);
203  free(buf.data);
204  return NULL;
205  }
206  result = buf.data;
207  break;
208  }
209 
210  /* No default: we want a compiler warning for missing cases */
211  }
212 
213  return result;
214 }
PGconn * db
Definition: settings.h:83
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6578
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:128
PsqlSettings pset
Definition: startup.c:35
static void error(void)
Definition: sql-dyntest.c:147
#define pg_log_error(...)
Definition: logging.h:79
bool appendShellStringNoError(PQExpBuffer buf, const char *str)
Definition: string_utils.c:443
static struct @144 value
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:3571
static char * buf
Definition: pg_test_fsync.c:68
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
const char * GetVariable(VariableSpace space, const char *name)
Definition: variables.c:73
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:3565
#define free(a)
Definition: header.h:65
void PQfreemem(void *ptr)
Definition: fe-exec.c:3297
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
#define pg_log_info(...)
Definition: logging.h:87
VariableSpace vars
Definition: settings.h:118

◆ PSQLexec()

PGresult* PSQLexec ( const char *  query)

Definition at line 669 of file common.c.

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

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

670 {
671  PGresult *res;
672 
673  if (!pset.db)
674  {
675  pg_log_error("You are currently not connected to a database.");
676  return NULL;
677  }
678 
680  {
681  printf(_("********* QUERY **********\n"
682  "%s\n"
683  "**************************\n\n"), query);
684  fflush(stdout);
685  if (pset.logfile)
686  {
688  _("********* QUERY **********\n"
689  "%s\n"
690  "**************************\n\n"), query);
691  fflush(pset.logfile);
692  }
693 
695  return NULL;
696  }
697 
698  SetCancelConn();
699 
700  res = PQexec(pset.db, query);
701 
702  ResetCancelConn();
703 
704  if (!AcceptResult(res))
705  {
706  ClearOrSaveResult(res);
707  res = NULL;
708  }
709 
710  return res;
711 }
PGconn * db
Definition: settings.h:83
PsqlSettings pset
Definition: startup.c:35
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:584
#define pg_log_error(...)
Definition: logging.h:79
#define printf(...)
Definition: port.h:198
#define fprintf
Definition: port.h:196
static bool AcceptResult(const PGresult *result)
Definition: common.c:485
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:135
void SetCancelConn(void)
Definition: common.c:425
FILE * logfile
Definition: settings.h:116
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940
void ResetCancelConn(void)
Definition: common.c:455
#define _(x)
Definition: elog.c:84

◆ PSQLexecWatch()

int PSQLexecWatch ( const char *  query,
const printQueryOpt opt 
)

Definition at line 724 of file common.c.

References AcceptResult(), before(), cancel_pressed, ClearOrSaveResult(), _psqlSettings::db, fprintf, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, _psqlSettings::logfile, pg_log_error, PGRES_COMMAND_OK, PGRES_COPY_BOTH, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_EMPTY_QUERY, PGRES_TUPLES_OK, PQclear(), PQcmdStatus(), PQexec(), PQresultStatus(), printQuery(), PrintTiming(), pset, _psqlSettings::queryFout, ResetCancelConn(), SetCancelConn(), _psqlSettings::timing, and printQueryOpt::title.

Referenced by do_watch().

725 {
726  PGresult *res;
727  double elapsed_msec = 0;
729  instr_time after;
730 
731  if (!pset.db)
732  {
733  pg_log_error("You are currently not connected to a database.");
734  return 0;
735  }
736 
737  SetCancelConn();
738 
739  if (pset.timing)
740  INSTR_TIME_SET_CURRENT(before);
741 
742  res = PQexec(pset.db, query);
743 
744  ResetCancelConn();
745 
746  if (!AcceptResult(res))
747  {
748  ClearOrSaveResult(res);
749  return 0;
750  }
751 
752  if (pset.timing)
753  {
754  INSTR_TIME_SET_CURRENT(after);
755  INSTR_TIME_SUBTRACT(after, before);
756  elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
757  }
758 
759  /*
760  * If SIGINT is sent while the query is processing, the interrupt will be
761  * consumed. The user's intention, though, is to cancel the entire watch
762  * process, so detect a sent cancellation request and exit in this case.
763  */
764  if (cancel_pressed)
765  {
766  PQclear(res);
767  return 0;
768  }
769 
770  switch (PQresultStatus(res))
771  {
772  case PGRES_TUPLES_OK:
773  printQuery(res, opt, pset.queryFout, false, pset.logfile);
774  break;
775 
776  case PGRES_COMMAND_OK:
777  fprintf(pset.queryFout, "%s\n%s\n\n", opt->title, PQcmdStatus(res));
778  break;
779 
780  case PGRES_EMPTY_QUERY:
781  pg_log_error("\\watch cannot be used with an empty query");
782  PQclear(res);
783  return -1;
784 
785  case PGRES_COPY_OUT:
786  case PGRES_COPY_IN:
787  case PGRES_COPY_BOTH:
788  pg_log_error("\\watch cannot be used with COPY");
789  PQclear(res);
790  return -1;
791 
792  default:
793  pg_log_error("unexpected result status for \\watch");
794  PQclear(res);
795  return -1;
796  }
797 
798  PQclear(res);
799 
800  fflush(pset.queryFout);
801 
802  /* Possible microtiming output */
803  if (pset.timing)
804  PrintTiming(elapsed_msec);
805 
806  return 1;
807 }
PGconn * db
Definition: settings.h:83
PsqlSettings pset
Definition: startup.c:35
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:584
#define pg_log_error(...)
Definition: logging.h:79
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
struct timeval instr_time
Definition: instr_time.h:150
static void PrintTiming(double elapsed_msec)
Definition: common.c:610
FILE * queryFout
Definition: settings.h:85
#define fprintf
Definition: port.h:196
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
static bool AcceptResult(const PGresult *result)
Definition: common.c:485
volatile bool cancel_pressed
Definition: print.c:46
static int before(chr x, chr y)
Definition: regc_locale.c:496
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
void SetCancelConn(void)
Definition: common.c:425
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3041
FILE * logfile
Definition: settings.h:116
void PQclear(PGresult *res)
Definition: fe-exec.c:695
char * title
Definition: print.h:169
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3436
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940
void ResetCancelConn(void)
Definition: common.c:455

◆ recognized_connection_string()

bool recognized_connection_string ( const char *  connstr)

Definition at line 2430 of file common.c.

References uri_prefix_length().

Referenced by do_connect().

2431 {
2432  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2433 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2403
static char * connstr
Definition: pg_dumpall.c:62

◆ ResetCancelConn()

void ResetCancelConn ( void  )

Definition at line 455 of file common.c.

456 {
457  PGcancel *oldCancelConn;
458 
459 #ifdef WIN32
460  EnterCriticalSection(&cancelConnLock);
461 #endif
462 
463  oldCancelConn = cancelConn;
464  /* be sure handle_sigint doesn't use pointer while freeing */
465  cancelConn = NULL;
466 
467  if (oldCancelConn != NULL)
468  PQfreeCancel(oldCancelConn);
469 
470 #ifdef WIN32
471  LeaveCriticalSection(&cancelConnLock);
472 #endif
473 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:4224
static PGcancel *volatile cancelConn
Definition: common.c:257

◆ SendQuery()

bool SendQuery ( const char *  query)

Definition at line 1304 of file common.c.

References _, _psqlSettings::autocommit, before(), 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, ExecQueryUsingCursor(), _psqlSettings::fetch_count, formatPGVersionNumber(), fprintf, free, _psqlSettings::g_expanded, _psqlSettings::gdesc_flag, _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gset_prefix, i, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, is_select_command(), lengthof, _psqlSettings::logfile, _psqlSettings::on_error_rollback, pg_encoding_to_char(), pg_free(), pg_log_error, pg_log_info, pg_log_warning, PGRES_COMMAND_OK, _psqlSettings::popt, PQclear(), PQclientEncoding(), PQcmdStatus(), PQerrorMessage(), PQexec(), PQresultStatus(), PQTRANS_ACTIVE, PQTRANS_IDLE, PQTRANS_INERROR, PQTRANS_INTRANS, PQTRANS_UNKNOWN, PQtransactionStatus(), printf, PrintNotifications(), PrintQueryResults(), PrintTiming(), ProcessResult(), pset, PSQL_ECHO_ERRORS, PSQL_ECHO_QUERIES, PSQL_ERROR_ROLLBACK_OFF, PSQL_ERROR_ROLLBACK_ON, ResetCancelConn(), SetCancelConn(), SetVariable(), _psqlSettings::singlestep, generate_unaccent_rules::stdout, _psqlSettings::sversion, _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.

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

1305 {
1306  PGresult *results;
1307  PGTransactionStatusType transaction_status;
1308  double elapsed_msec = 0;
1309  bool OK = false;
1310  int i;
1311  bool on_error_rollback_savepoint = false;
1312  static bool on_error_rollback_warning = false;
1313 
1314  if (!pset.db)
1315  {
1316  pg_log_error("You are currently not connected to a database.");
1317  goto sendquery_cleanup;
1318  }
1319 
1320  if (pset.singlestep)
1321  {
1322  char buf[3];
1323 
1324  fflush(stderr);
1325  printf(_("***(Single step mode: verify command)*******************************************\n"
1326  "%s\n"
1327  "***(press return to proceed or enter x and return to cancel)********************\n"),
1328  query);
1329  fflush(stdout);
1330  if (fgets(buf, sizeof(buf), stdin) != NULL)
1331  if (buf[0] == 'x')
1332  goto sendquery_cleanup;
1333  if (cancel_pressed)
1334  goto sendquery_cleanup;
1335  }
1336  else if (pset.echo == PSQL_ECHO_QUERIES)
1337  {
1338  puts(query);
1339  fflush(stdout);
1340  }
1341 
1342  if (pset.logfile)
1343  {
1345  _("********* QUERY **********\n"
1346  "%s\n"
1347  "**************************\n\n"), query);
1348  fflush(pset.logfile);
1349  }
1350 
1351  SetCancelConn();
1352 
1353  transaction_status = PQtransactionStatus(pset.db);
1354 
1355  if (transaction_status == PQTRANS_IDLE &&
1356  !pset.autocommit &&
1357  !command_no_begin(query))
1358  {
1359  results = PQexec(pset.db, "BEGIN");
1360  if (PQresultStatus(results) != PGRES_COMMAND_OK)
1361  {
1363  ClearOrSaveResult(results);
1364  ResetCancelConn();
1365  goto sendquery_cleanup;
1366  }
1367  ClearOrSaveResult(results);
1368  transaction_status = PQtransactionStatus(pset.db);
1369  }
1370 
1371  if (transaction_status == PQTRANS_INTRANS &&
1375  {
1376  if (on_error_rollback_warning == false && pset.sversion < 80000)
1377  {
1378  char sverbuf[32];
1379 
1380  pg_log_warning("The server (version %s) does not support savepoints for ON_ERROR_ROLLBACK.",
1382  sverbuf, sizeof(sverbuf)));
1383  on_error_rollback_warning = true;
1384  }
1385  else
1386  {
1387  results = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1388  if (PQresultStatus(results) != PGRES_COMMAND_OK)
1389  {
1391  ClearOrSaveResult(results);
1392  ResetCancelConn();
1393  goto sendquery_cleanup;
1394  }
1395  ClearOrSaveResult(results);
1396  on_error_rollback_savepoint = true;
1397  }
1398  }
1399 
1400  if (pset.gdesc_flag)
1401  {
1402  /* Describe query's result columns, without executing it */
1403  OK = DescribeQuery(query, &elapsed_msec);
1404  ResetCancelConn();
1405  results = NULL; /* PQclear(NULL) does nothing */
1406  }
1407  else if (pset.fetch_count <= 0 || pset.gexec_flag ||
1409  {
1410  /* Default fetch-it-all-and-print mode */
1412  after;
1413 
1414  if (pset.timing)
1415  INSTR_TIME_SET_CURRENT(before);
1416 
1417  results = PQexec(pset.db, query);
1418 
1419  /* these operations are included in the timing result: */
1420  ResetCancelConn();
1421  OK = ProcessResult(&results);
1422 
1423  if (pset.timing)
1424  {
1425  INSTR_TIME_SET_CURRENT(after);
1426  INSTR_TIME_SUBTRACT(after, before);
1427  elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1428  }
1429 
1430  /* but printing results isn't: */
1431  if (OK && results)
1432  OK = PrintQueryResults(results);
1433  }
1434  else
1435  {
1436  /* Fetch-in-segments mode */
1437  OK = ExecQueryUsingCursor(query, &elapsed_msec);
1438  ResetCancelConn();
1439  results = NULL; /* PQclear(NULL) does nothing */
1440  }
1441 
1442  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1443  pg_log_info("STATEMENT: %s", query);
1444 
1445  /* If we made a temporary savepoint, possibly release/rollback */
1446  if (on_error_rollback_savepoint)
1447  {
1448  const char *svptcmd = NULL;
1449 
1450  transaction_status = PQtransactionStatus(pset.db);
1451 
1452  switch (transaction_status)
1453  {
1454  case PQTRANS_INERROR:
1455  /* We always rollback on an error */
1456  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1457  break;
1458 
1459  case PQTRANS_IDLE:
1460  /* If they are no longer in a transaction, then do nothing */
1461  break;
1462 
1463  case PQTRANS_INTRANS:
1464 
1465  /*
1466  * Do nothing if they are messing with savepoints themselves:
1467  * If the user did RELEASE or ROLLBACK, our savepoint is gone.
1468  * If they issued a SAVEPOINT, releasing ours would remove
1469  * theirs.
1470  */
1471  if (results &&
1472  (strcmp(PQcmdStatus(results), "SAVEPOINT") == 0 ||
1473  strcmp(PQcmdStatus(results), "RELEASE") == 0 ||
1474  strcmp(PQcmdStatus(results), "ROLLBACK") == 0))
1475  svptcmd = NULL;
1476  else
1477  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1478  break;
1479 
1480  case PQTRANS_ACTIVE:
1481  case PQTRANS_UNKNOWN:
1482  default:
1483  OK = false;
1484  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1485  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1486  pg_log_error("unexpected transaction status (%d)",
1487  transaction_status);
1488  break;
1489  }
1490 
1491  if (svptcmd)
1492  {
1493  PGresult *svptres;
1494 
1495  svptres = PQexec(pset.db, svptcmd);
1496  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1497  {
1499  ClearOrSaveResult(svptres);
1500  OK = false;
1501 
1502  PQclear(results);
1503  ResetCancelConn();
1504  goto sendquery_cleanup;
1505  }
1506  PQclear(svptres);
1507  }
1508  }
1509 
1510  ClearOrSaveResult(results);
1511 
1512  /* Possible microtiming output */
1513  if (pset.timing)
1514  PrintTiming(elapsed_msec);
1515 
1516  /* check for events that may occur during query execution */
1517 
1518  if (pset.encoding != PQclientEncoding(pset.db) &&
1519  PQclientEncoding(pset.db) >= 0)
1520  {
1521  /* track effects of SET CLIENT_ENCODING */
1524  SetVariable(pset.vars, "ENCODING",
1526  }
1527 
1529 
1530  /* perform cleanup that should occur after any attempted query */
1531 
1532 sendquery_cleanup:
1533 
1534  /* reset \g's output-to-filename trigger */
1535  if (pset.gfname)
1536  {
1537  free(pset.gfname);
1538  pset.gfname = NULL;
1539  }
1540 
1541  /* reset \gx's expanded-mode flag */
1542  pset.g_expanded = false;
1543 
1544  /* reset \gset trigger */
1545  if (pset.gset_prefix)
1546  {
1548  pset.gset_prefix = NULL;
1549  }
1550 
1551  /* reset \gdesc trigger */
1552  pset.gdesc_flag = false;
1553 
1554  /* reset \gexec trigger */
1555  pset.gexec_flag = false;
1556 
1557  /* reset \crosstabview trigger */
1558  pset.crosstab_flag = false;
1559  for (i = 0; i < lengthof(pset.ctv_args); i++)
1560  {
1561  pg_free(pset.ctv_args[i]);
1562  pset.ctv_args[i] = NULL;
1563  }
1564 
1565  return OK;
1566 }
PSQL_ECHO echo
Definition: settings.h:134
bool singlestep
Definition: settings.h:129
char * gset_prefix
Definition: settings.h:96
static bool PrintQueryResults(PGresult *results)
Definition: common.c:1229
PGconn * db
Definition: settings.h:83
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6578
int encoding
Definition: print.h:120
PsqlSettings pset
Definition: startup.c:35
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:584
#define pg_log_error(...)
Definition: logging.h:79
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:202
struct timeval instr_time
Definition: instr_time.h:150
bool gexec_flag
Definition: settings.h:98
printTableOpt topt
Definition: print.h:167
static void PrintTiming(double elapsed_msec)
Definition: common.c:610
#define printf(...)
Definition: port.h:198
static bool ConnectionUp(void)
Definition: common.c:368
bool autocommit
Definition: settings.h:125
#define lengthof(array)
Definition: c.h:662
#define fprintf
Definition: port.h:196
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:136
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6638
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2693
volatile bool cancel_pressed
Definition: print.c:46
static void PrintNotifications(void)
Definition: common.c:814
char * ctv_args[4]
Definition: settings.h:100
static int before(chr x, chr y)
Definition: regc_locale.c:496
bool cur_cmd_interactive
Definition: settings.h:107
static bool is_select_command(const char *query)
Definition: common.c:2235
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
bool gdesc_flag
Definition: settings.h:97
static char * buf
Definition: pg_test_fsync.c:68
int fetch_count
Definition: settings.h:131
static bool ProcessResult(PGresult **results)
Definition: common.c:1024
bool g_expanded
Definition: settings.h:95
void SetCancelConn(void)
Definition: common.c:425
static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec)
Definition: common.c:1694
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6533
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1578
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3041
char * gfname
Definition: settings.h:94
FILE * logfile
Definition: settings.h:116
void PQclear(PGresult *res)
Definition: fe-exec.c:695
#define free(a)
Definition: header.h:65
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
PGTransactionStatusType
Definition: libpq-fe.h:102
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:213
printQueryOpt popt
Definition: settings.h:92
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
int i
bool crosstab_flag
Definition: settings.h:99
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1940
#define pg_log_warning(...)
Definition: pgfnames.c:24
int encoding
Definition: settings.h:84
static bool command_no_begin(const char *query)
Definition: common.c:2020
void ResetCancelConn(void)
Definition: common.c:455
#define _(x)
Definition: elog.c:84
#define pg_log_info(...)
Definition: logging.h:87
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:179
VariableSpace vars
Definition: settings.h:118

◆ session_username()

const char* session_username ( void  )

Definition at line 2322 of file common.c.

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

Referenced by get_prompt().

2323 {
2324  const char *val;
2325 
2326  if (!pset.db)
2327  return NULL;
2328 
2329  val = PQparameterStatus(pset.db, "session_authorization");
2330  if (val)
2331  return val;
2332  else
2333  return PQuser(pset.db);
2334 }
PGconn * db
Definition: settings.h:83
PsqlSettings pset
Definition: startup.c:35
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6543
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:6436
long val
Definition: informix.c:684

◆ SetCancelConn()

void SetCancelConn ( void  )

Definition at line 425 of file common.c.

References cancelConn, _psqlSettings::db, PQfreeCancel(), PQgetCancel(), and pset.

Referenced by consumeQueryResult(), do_lo_export(), do_lo_import(), do_lo_unlink(), executeMaintenanceCommand(), ParallelSlotsGetIdle(), ProcessResult(), PSQLexec(), PSQLexecWatch(), and SendQuery().

426 {
427  PGcancel *oldCancelConn;
428 
429 #ifdef WIN32
430  EnterCriticalSection(&cancelConnLock);
431 #endif
432 
433  /* Free the old one if we have one */
434  oldCancelConn = cancelConn;
435  /* be sure handle_sigint doesn't use pointer while freeing */
436  cancelConn = NULL;
437 
438  if (oldCancelConn != NULL)
439  PQfreeCancel(oldCancelConn);
440 
442 
443 #ifdef WIN32
444  LeaveCriticalSection(&cancelConnLock);
445 #endif
446 }
PGconn * db
Definition: settings.h:83
PsqlSettings pset
Definition: startup.c:35
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:4224
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:4201
static PGcancel *volatile cancelConn
Definition: common.c:257

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 86 of file common.c.

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

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

87 {
88  FILE *fout;
89  bool is_pipe;
90 
91  /* First make sure we can open the new output file/pipe */
92  if (!openQueryOutputFile(fname, &fout, &is_pipe))
93  return false;
94 
95  /* Close old file/pipe */
96  if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
97  {
98  if (pset.queryFoutPipe)
99  pclose(pset.queryFout);
100  else
101  fclose(pset.queryFout);
102  }
103 
104  pset.queryFout = fout;
105  pset.queryFoutPipe = is_pipe;
106 
107  /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
108  set_sigpipe_trap_state(is_pipe);
110 
111  return true;
112 }
PsqlSettings pset
Definition: startup.c:35
bool queryFoutPipe
Definition: settings.h:86
FILE * queryFout
Definition: settings.h:85
void set_sigpipe_trap_state(bool ignore)
Definition: print.c:2980
void restore_sigpipe_trap(void)
Definition: print.c:2967
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:51

◆ setup_cancel_handler()

void setup_cancel_handler ( void  )

Definition at line 311 of file common.c.

312 {
313  pqsignal(SIGINT, handle_sigint);
314 }
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static void handle_sigint(SIGNAL_ARGS)
Definition: common.c:280

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2298 of file common.c.

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

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

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 }
PGconn * db
Definition: settings.h:83
PsqlSettings pset
Definition: startup.c:35
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6543
long val
Definition: informix.c:684

Variable Documentation

◆ sigint_interrupt_enabled

volatile bool sigint_interrupt_enabled

◆ sigint_interrupt_jmp

sigjmp_buf sigint_interrupt_jmp

Definition at line 255 of file common.c.

Referenced by do_watch(), handle_sigint(), handleCopyIn(), and MainLoop().