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 psql_error (const char *fmt,...) pg_attribute_printf(1
 
void 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 2303 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().

2304 {
2305  if (!filename || !(*filename))
2306  return;
2307 
2308  /*
2309  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2310  * for short versions of long file names, though the tilde is usually
2311  * toward the end, not at the beginning.
2312  */
2313 #ifndef WIN32
2314 
2315  /* try tilde expansion */
2316  if (**filename == '~')
2317  {
2318  char *fn;
2319  char oldp,
2320  *p;
2321  struct passwd *pw;
2322  char home[MAXPGPATH];
2323 
2324  fn = *filename;
2325  *home = '\0';
2326 
2327  p = fn + 1;
2328  while (*p != '/' && *p != '\0')
2329  p++;
2330 
2331  oldp = *p;
2332  *p = '\0';
2333 
2334  if (*(fn + 1) == '\0')
2335  get_home_path(home); /* ~ or ~/ only */
2336  else if ((pw = getpwnam(fn + 1)) != NULL)
2337  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2338 
2339  *p = oldp;
2340  if (strlen(home) != 0)
2341  {
2342  char *newfn;
2343 
2344  newfn = psprintf("%s%s", home, p);
2345  free(fn);
2346  *filename = newfn;
2347  }
2348  }
2349 #endif
2350 
2351  return;
2352 }
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:90
bool get_home_path(char *ret_path)
Definition: path.c:807

◆ is_superuser()

bool is_superuser ( void  )

Definition at line 2235 of file common.c.

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

2236 {
2237  const char *val;
2238 
2239  if (!pset.db)
2240  return false;
2241 
2242  val = PQparameterStatus(pset.db, "is_superuser");
2243 
2244  if (val && strcmp(val, "on") == 0)
2245  return true;
2246 
2247  return false;
2248 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6071
long val
Definition: informix.c:689

◆ NoticeProcessor()

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

Definition at line 242 of file common.c.

References psql_error().

Referenced by do_connect(), and main().

243 {
244  (void) arg; /* not used */
245  psql_error("%s", message);
246 }
void psql_error(const char *fmt,...)
Definition: common.c:221
void * arg

◆ openQueryOutputFile()

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

Definition at line 50 of file common.c.

References psql_error(), and strerror().

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

51 {
52  if (!fname || fname[0] == '\0')
53  {
54  *fout = stdout;
55  *is_pipe = false;
56  }
57  else if (*fname == '|')
58  {
59  *fout = popen(fname + 1, "w");
60  *is_pipe = true;
61  }
62  else
63  {
64  *fout = fopen(fname, "w");
65  *is_pipe = false;
66  }
67 
68  if (*fout == NULL)
69  {
70  psql_error("%s: %s\n", fname, strerror(errno));
71  return false;
72  }
73 
74  return true;
75 }
void psql_error(const char *fmt,...)
Definition: common.c:221
const char * strerror(int errnum)
Definition: strerror.c:19

◆ psql_error()

void psql_error ( const char *  fmt,
  ... 
)

◆ psql_get_variable()

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

Definition at line 129 of file common.c.

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

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

◆ PSQLexec()

PGresult* PSQLexec ( const char *  query)

Definition at line 690 of file common.c.

References _, AcceptResult(), ClearOrSaveResult(), _psqlSettings::db, _psqlSettings::echo_hidden, _psqlSettings::logfile, PQexec(), pset, PSQL_ECHO_HIDDEN_NOEXEC, PSQL_ECHO_HIDDEN_OFF, psql_error(), ResetCancelConn(), and SetCancelConn().

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(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSConfigsVerbose(), listTSDictionaries(), listTSParsers(), listTSParsersVerbose(), listTSTemplates(), listUserMappings(), main(), objectDescription(), permissionsList(), and start_lo_xact().

691 {
692  PGresult *res;
693 
694  if (!pset.db)
695  {
696  psql_error("You are currently not connected to a database.\n");
697  return NULL;
698  }
699 
701  {
702  printf(_("********* QUERY **********\n"
703  "%s\n"
704  "**************************\n\n"), query);
705  fflush(stdout);
706  if (pset.logfile)
707  {
708  fprintf(pset.logfile,
709  _("********* QUERY **********\n"
710  "%s\n"
711  "**************************\n\n"), query);
712  fflush(pset.logfile);
713  }
714 
716  return NULL;
717  }
718 
719  SetCancelConn();
720 
721  res = PQexec(pset.db, query);
722 
723  ResetCancelConn();
724 
725  if (!AcceptResult(res))
726  {
727  ClearOrSaveResult(res);
728  res = NULL;
729  }
730 
731  return res;
732 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:605
static bool AcceptResult(const PGresult *result)
Definition: common.c:506
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:133
void SetCancelConn(void)
Definition: common.c:446
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
void ResetCancelConn(void)
Definition: common.c:476
#define _(x)
Definition: elog.c:84

◆ PSQLexecWatch()

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

Definition at line 745 of file common.c.

References _, AcceptResult(), before(), cancel_pressed, ClearOrSaveResult(), _psqlSettings::db, INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SUBTRACT, _psqlSettings::logfile, 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, psql_error(), _psqlSettings::queryFout, ResetCancelConn(), SetCancelConn(), _psqlSettings::timing, and printQueryOpt::title.

Referenced by do_watch().

746 {
747  PGresult *res;
748  double elapsed_msec = 0;
750  instr_time after;
751 
752  if (!pset.db)
753  {
754  psql_error("You are currently not connected to a database.\n");
755  return 0;
756  }
757 
758  SetCancelConn();
759 
760  if (pset.timing)
761  INSTR_TIME_SET_CURRENT(before);
762 
763  res = PQexec(pset.db, query);
764 
765  ResetCancelConn();
766 
767  if (!AcceptResult(res))
768  {
769  ClearOrSaveResult(res);
770  return 0;
771  }
772 
773  if (pset.timing)
774  {
775  INSTR_TIME_SET_CURRENT(after);
776  INSTR_TIME_SUBTRACT(after, before);
777  elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
778  }
779 
780  /*
781  * If SIGINT is sent while the query is processing, the interrupt will be
782  * consumed. The user's intention, though, is to cancel the entire watch
783  * process, so detect a sent cancellation request and exit in this case.
784  */
785  if (cancel_pressed)
786  {
787  PQclear(res);
788  return 0;
789  }
790 
791  switch (PQresultStatus(res))
792  {
793  case PGRES_TUPLES_OK:
794  printQuery(res, opt, pset.queryFout, false, pset.logfile);
795  break;
796 
797  case PGRES_COMMAND_OK:
798  fprintf(pset.queryFout, "%s\n%s\n\n", opt->title, PQcmdStatus(res));
799  break;
800 
801  case PGRES_EMPTY_QUERY:
802  psql_error(_("\\watch cannot be used with an empty query\n"));
803  PQclear(res);
804  return -1;
805 
806  case PGRES_COPY_OUT:
807  case PGRES_COPY_IN:
808  case PGRES_COPY_BOTH:
809  psql_error(_("\\watch cannot be used with COPY\n"));
810  PQclear(res);
811  return -1;
812 
813  default:
814  psql_error(_("unexpected result status for \\watch\n"));
815  PQclear(res);
816  return -1;
817  }
818 
819  PQclear(res);
820 
821  fflush(pset.queryFout);
822 
823  /* Possible microtiming output */
824  if (pset.timing)
825  PrintTiming(elapsed_msec);
826 
827  return 1;
828 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:605
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:199
struct timeval instr_time
Definition: instr_time.h:147
static void PrintTiming(double elapsed_msec)
Definition: common.c:631
FILE * queryFout
Definition: settings.h:84
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
static bool AcceptResult(const PGresult *result)
Definition: common.c:506
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:167
void SetCancelConn(void)
Definition: common.c:446
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:2995
void psql_error(const char *fmt,...)
Definition: common.c:221
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
char * title
Definition: print.h:167
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3287
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
void ResetCancelConn(void)
Definition: common.c:476
#define _(x)
Definition: elog.c:84

◆ recognized_connection_string()

bool recognized_connection_string ( const char *  connstr)

Definition at line 2390 of file common.c.

References uri_prefix_length().

Referenced by do_connect().

2391 {
2392  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2393 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2363
static char * connstr
Definition: pg_dumpall.c:64

◆ ResetCancelConn()

void ResetCancelConn ( void  )

Definition at line 476 of file common.c.

477 {
478  PGcancel *oldCancelConn;
479 
480 #ifdef WIN32
481  EnterCriticalSection(&cancelConnLock);
482 #endif
483 
484  oldCancelConn = cancelConn;
485  /* be sure handle_sigint doesn't use pointer while freeing */
486  cancelConn = NULL;
487 
488  if (oldCancelConn != NULL)
489  PQfreeCancel(oldCancelConn);
490 
491 #ifdef WIN32
492  LeaveCriticalSection(&cancelConnLock);
493 #endif
494 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3773
static PGcancel *volatile cancelConn
Definition: common.c:278

◆ SendQuery()

bool SendQuery ( const char *  query)

Definition at line 1280 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(), 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(), PGRES_COMMAND_OK, _psqlSettings::popt, PQclear(), PQclientEncoding(), PQcmdStatus(), PQerrorMessage(), PQexec(), PQresultStatus(), PQTRANS_ACTIVE, PQTRANS_IDLE, PQTRANS_INERROR, PQTRANS_INTRANS, PQTRANS_UNKNOWN, PQtransactionStatus(), PrintNotifications(), PrintQueryResults(), PrintTiming(), ProcessResult(), pset, PSQL_ECHO_ERRORS, PSQL_ECHO_QUERIES, psql_error(), PSQL_ERROR_ROLLBACK_OFF, PSQL_ERROR_ROLLBACK_ON, ResetCancelConn(), SetCancelConn(), SetVariable(), _psqlSettings::singlestep, _psqlSettings::sversion, _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.

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

1281 {
1282  PGresult *results;
1283  PGTransactionStatusType transaction_status;
1284  double elapsed_msec = 0;
1285  bool OK = false;
1286  int i;
1287  bool on_error_rollback_savepoint = false;
1288  static bool on_error_rollback_warning = false;
1289 
1290  if (!pset.db)
1291  {
1292  psql_error("You are currently not connected to a database.\n");
1293  goto sendquery_cleanup;
1294  }
1295 
1296  if (pset.singlestep)
1297  {
1298  char buf[3];
1299 
1300  fflush(stderr);
1301  printf(_("***(Single step mode: verify command)*******************************************\n"
1302  "%s\n"
1303  "***(press return to proceed or enter x and return to cancel)********************\n"),
1304  query);
1305  fflush(stdout);
1306  if (fgets(buf, sizeof(buf), stdin) != NULL)
1307  if (buf[0] == 'x')
1308  goto sendquery_cleanup;
1309  if (cancel_pressed)
1310  goto sendquery_cleanup;
1311  }
1312  else if (pset.echo == PSQL_ECHO_QUERIES)
1313  {
1314  puts(query);
1315  fflush(stdout);
1316  }
1317 
1318  if (pset.logfile)
1319  {
1320  fprintf(pset.logfile,
1321  _("********* QUERY **********\n"
1322  "%s\n"
1323  "**************************\n\n"), query);
1324  fflush(pset.logfile);
1325  }
1326 
1327  SetCancelConn();
1328 
1329  transaction_status = PQtransactionStatus(pset.db);
1330 
1331  if (transaction_status == PQTRANS_IDLE &&
1332  !pset.autocommit &&
1333  !command_no_begin(query))
1334  {
1335  results = PQexec(pset.db, "BEGIN");
1336  if (PQresultStatus(results) != PGRES_COMMAND_OK)
1337  {
1338  psql_error("%s", PQerrorMessage(pset.db));
1339  ClearOrSaveResult(results);
1340  ResetCancelConn();
1341  goto sendquery_cleanup;
1342  }
1343  ClearOrSaveResult(results);
1344  transaction_status = PQtransactionStatus(pset.db);
1345  }
1346 
1347  if (transaction_status == PQTRANS_INTRANS &&
1351  {
1352  if (on_error_rollback_warning == false && pset.sversion < 80000)
1353  {
1354  char sverbuf[32];
1355 
1356  psql_error("The server (version %s) does not support savepoints for ON_ERROR_ROLLBACK.\n",
1358  sverbuf, sizeof(sverbuf)));
1359  on_error_rollback_warning = true;
1360  }
1361  else
1362  {
1363  results = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1364  if (PQresultStatus(results) != PGRES_COMMAND_OK)
1365  {
1366  psql_error("%s", PQerrorMessage(pset.db));
1367  ClearOrSaveResult(results);
1368  ResetCancelConn();
1369  goto sendquery_cleanup;
1370  }
1371  ClearOrSaveResult(results);
1372  on_error_rollback_savepoint = true;
1373  }
1374  }
1375 
1376  if (pset.gdesc_flag)
1377  {
1378  /* Describe query's result columns, without executing it */
1379  OK = DescribeQuery(query, &elapsed_msec);
1380  ResetCancelConn();
1381  results = NULL; /* PQclear(NULL) does nothing */
1382  }
1383  else if (pset.fetch_count <= 0 || pset.gexec_flag ||
1385  {
1386  /* Default fetch-it-all-and-print mode */
1388  after;
1389 
1390  if (pset.timing)
1391  INSTR_TIME_SET_CURRENT(before);
1392 
1393  results = PQexec(pset.db, query);
1394 
1395  /* these operations are included in the timing result: */
1396  ResetCancelConn();
1397  OK = ProcessResult(&results);
1398 
1399  if (pset.timing)
1400  {
1401  INSTR_TIME_SET_CURRENT(after);
1402  INSTR_TIME_SUBTRACT(after, before);
1403  elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1404  }
1405 
1406  /* but printing results isn't: */
1407  if (OK && results)
1408  OK = PrintQueryResults(results);
1409  }
1410  else
1411  {
1412  /* Fetch-in-segments mode */
1413  OK = ExecQueryUsingCursor(query, &elapsed_msec);
1414  ResetCancelConn();
1415  results = NULL; /* PQclear(NULL) does nothing */
1416  }
1417 
1418  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1419  psql_error("STATEMENT: %s\n", query);
1420 
1421  /* If we made a temporary savepoint, possibly release/rollback */
1422  if (on_error_rollback_savepoint)
1423  {
1424  const char *svptcmd = NULL;
1425 
1426  transaction_status = PQtransactionStatus(pset.db);
1427 
1428  switch (transaction_status)
1429  {
1430  case PQTRANS_INERROR:
1431  /* We always rollback on an error */
1432  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1433  break;
1434 
1435  case PQTRANS_IDLE:
1436  /* If they are no longer in a transaction, then do nothing */
1437  break;
1438 
1439  case PQTRANS_INTRANS:
1440 
1441  /*
1442  * Do nothing if they are messing with savepoints themselves:
1443  * If the user did RELEASE or ROLLBACK, our savepoint is gone.
1444  * If they issued a SAVEPOINT, releasing ours would remove
1445  * theirs.
1446  */
1447  if (results &&
1448  (strcmp(PQcmdStatus(results), "SAVEPOINT") == 0 ||
1449  strcmp(PQcmdStatus(results), "RELEASE") == 0 ||
1450  strcmp(PQcmdStatus(results), "ROLLBACK") == 0))
1451  svptcmd = NULL;
1452  else
1453  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1454  break;
1455 
1456  case PQTRANS_ACTIVE:
1457  case PQTRANS_UNKNOWN:
1458  default:
1459  OK = false;
1460  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1461  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1462  psql_error("unexpected transaction status (%d)\n",
1463  transaction_status);
1464  break;
1465  }
1466 
1467  if (svptcmd)
1468  {
1469  PGresult *svptres;
1470 
1471  svptres = PQexec(pset.db, svptcmd);
1472  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1473  {
1474  psql_error("%s", PQerrorMessage(pset.db));
1475  ClearOrSaveResult(svptres);
1476  OK = false;
1477 
1478  PQclear(results);
1479  ResetCancelConn();
1480  goto sendquery_cleanup;
1481  }
1482  PQclear(svptres);
1483  }
1484  }
1485 
1486  ClearOrSaveResult(results);
1487 
1488  /* Possible microtiming output */
1489  if (pset.timing)
1490  PrintTiming(elapsed_msec);
1491 
1492  /* check for events that may occur during query execution */
1493 
1494  if (pset.encoding != PQclientEncoding(pset.db) &&
1495  PQclientEncoding(pset.db) >= 0)
1496  {
1497  /* track effects of SET CLIENT_ENCODING */
1500  SetVariable(pset.vars, "ENCODING",
1502  }
1503 
1505 
1506  /* perform cleanup that should occur after any attempted query */
1507 
1508 sendquery_cleanup:
1509 
1510  /* reset \g's output-to-filename trigger */
1511  if (pset.gfname)
1512  {
1513  free(pset.gfname);
1514  pset.gfname = NULL;
1515  }
1516 
1517  /* reset \gx's expanded-mode flag */
1518  pset.g_expanded = false;
1519 
1520  /* reset \gset trigger */
1521  if (pset.gset_prefix)
1522  {
1524  pset.gset_prefix = NULL;
1525  }
1526 
1527  /* reset \gdesc trigger */
1528  pset.gdesc_flag = false;
1529 
1530  /* reset \gexec trigger */
1531  pset.gexec_flag = false;
1532 
1533  /* reset \crosstabview trigger */
1534  pset.crosstab_flag = false;
1535  for (i = 0; i < lengthof(pset.ctv_args); i++)
1536  {
1537  pg_free(pset.ctv_args[i]);
1538  pset.ctv_args[i] = NULL;
1539  }
1540 
1541  return OK;
1542 }
PSQL_ECHO echo
Definition: settings.h:132
bool singlestep
Definition: settings.h:128
char * gset_prefix
Definition: settings.h:95
static bool PrintQueryResults(PGresult *results)
Definition: common.c:1205
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
int encoding
Definition: print.h:118
PsqlSettings pset
Definition: startup.c:33
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:605
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:199
struct timeval instr_time
Definition: instr_time.h:147
bool gexec_flag
Definition: settings.h:97
printTableOpt topt
Definition: print.h:165
static void PrintTiming(double elapsed_msec)
Definition: common.c:631
static bool ConnectionUp(void)
Definition: common.c:389
bool autocommit
Definition: settings.h:124
#define lengthof(array)
Definition: c.h:600
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:134
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6166
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
volatile bool cancel_pressed
Definition: print.c:46
static void PrintNotifications(void)
Definition: common.c:835
char * ctv_args[4]
Definition: settings.h:99
static int before(chr x, chr y)
Definition: regc_locale.c:496
bool cur_cmd_interactive
Definition: settings.h:106
static bool is_select_command(const char *query)
Definition: common.c:2195
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:167
bool gdesc_flag
Definition: settings.h:96
static char * buf
Definition: pg_test_fsync.c:67
int fetch_count
Definition: settings.h:129
static bool ProcessResult(PGresult **results)
Definition: common.c:1043
bool g_expanded
Definition: settings.h:94
void SetCancelConn(void)
Definition: common.c:446
static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec)
Definition: common.c:1670
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6061
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1554
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:2995
void psql_error(const char *fmt,...)
Definition: common.c:221
char * gfname
Definition: settings.h:93
FILE * logfile
Definition: settings.h:115
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define free(a)
Definition: header.h:65
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
PGTransactionStatusType
Definition: libpq-fe.h:101
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
printQueryOpt popt
Definition: settings.h:91
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
int i
bool crosstab_flag
Definition: settings.h:98
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
int encoding
Definition: settings.h:83
static bool command_no_begin(const char *query)
Definition: common.c:1996
void ResetCancelConn(void)
Definition: common.c:476
#define _(x)
Definition: elog.c:84
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
VariableSpace vars
Definition: settings.h:117

◆ session_username()

const char* session_username ( void  )

Definition at line 2282 of file common.c.

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

Referenced by get_prompt().

2283 {
2284  const char *val;
2285 
2286  if (!pset.db)
2287  return NULL;
2288 
2289  val = PQparameterStatus(pset.db, "session_authorization");
2290  if (val)
2291  return val;
2292  else
2293  return PQuser(pset.db);
2294 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6071
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:5982
long val
Definition: informix.c:689

◆ SetCancelConn()

void SetCancelConn ( void  )

Definition at line 446 of file common.c.

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

Referenced by do_lo_export(), do_lo_import(), do_lo_unlink(), executeMaintenanceCommand(), GetIdleSlot(), GetQueryResult(), ProcessResult(), PSQLexec(), PSQLexecWatch(), and SendQuery().

447 {
448  PGcancel *oldCancelConn;
449 
450 #ifdef WIN32
451  EnterCriticalSection(&cancelConnLock);
452 #endif
453 
454  /* Free the old one if we have one */
455  oldCancelConn = cancelConn;
456  /* be sure handle_sigint doesn't use pointer while freeing */
457  cancelConn = NULL;
458 
459  if (oldCancelConn != NULL)
460  PQfreeCancel(oldCancelConn);
461 
463 
464 #ifdef WIN32
465  LeaveCriticalSection(&cancelConnLock);
466 #endif
467 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3773
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:3750
static PGcancel *volatile cancelConn
Definition: common.c:278

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 85 of file common.c.

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

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

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

◆ setup_cancel_handler()

void setup_cancel_handler ( void  )

Definition at line 332 of file common.c.

333 {
334  pqsignal(SIGINT, handle_sigint);
335 }
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static void handle_sigint(SIGNAL_ARGS)
Definition: common.c:301

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2258 of file common.c.

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

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

2259 {
2260  const char *val;
2261 
2262  if (!pset.db)
2263  return false;
2264 
2265  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2266 
2267  if (val && strcmp(val, "on") == 0)
2268  return true;
2269 
2270  return false;
2271 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6071
long val
Definition: informix.c:689

Variable Documentation

◆ sigint_interrupt_enabled

volatile bool sigint_interrupt_enabled

◆ sigint_interrupt_jmp

sigjmp_buf sigint_interrupt_jmp

Definition at line 276 of file common.c.

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