PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
common.h File Reference
#include <setjmp.h>
#include "libpq-fe.h"
#include "fe_utils/print.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, bool escape, bool as_ident, 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

void expand_tilde ( char **  filename)

Definition at line 2064 of file common.c.

References filename, fn(), free, get_home_path(), MAXPGPATH, NULL, psprintf(), and strlcpy().

Referenced by exec_command(), initializeInput(), parse_slash_copy(), and process_psqlrc().

2065 {
2066  if (!filename || !(*filename))
2067  return;
2068 
2069  /*
2070  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2071  * for short versions of long file names, though the tilde is usually
2072  * toward the end, not at the beginning.
2073  */
2074 #ifndef WIN32
2075 
2076  /* try tilde expansion */
2077  if (**filename == '~')
2078  {
2079  char *fn;
2080  char oldp,
2081  *p;
2082  struct passwd *pw;
2083  char home[MAXPGPATH];
2084 
2085  fn = *filename;
2086  *home = '\0';
2087 
2088  p = fn + 1;
2089  while (*p != '/' && *p != '\0')
2090  p++;
2091 
2092  oldp = *p;
2093  *p = '\0';
2094 
2095  if (*(fn + 1) == '\0')
2096  get_home_path(home); /* ~ or ~/ only */
2097  else if ((pw = getpwnam(fn + 1)) != NULL)
2098  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2099 
2100  *p = oldp;
2101  if (strlen(home) != 0)
2102  {
2103  char *newfn;
2104 
2105  newfn = psprintf("%s%s", home, p);
2106  free(fn);
2107  *filename = newfn;
2108  }
2109  }
2110 #endif
2111 
2112  return;
2113 }
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
#define NULL
Definition: c.h:229
static char * filename
Definition: pg_dumpall.c:87
bool get_home_path(char *ret_path)
Definition: path.c:807
bool is_superuser ( void  )

Definition at line 1996 of file common.c.

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

Referenced by check_role(), check_session_authorization(), get_prompt(), pg_stat_statements_internal(), and SetSessionUserId().

1997 {
1998  const char *val;
1999 
2000  if (!pset.db)
2001  return false;
2002 
2003  val = PQparameterStatus(pset.db, "is_superuser");
2004 
2005  if (val && strcmp(val, "on") == 0)
2006  return true;
2007 
2008  return false;
2009 }
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:5976
long val
Definition: informix.c:689
void void NoticeProcessor ( void *  arg,
const char *  message 
)

Definition at line 202 of file common.c.

References psql_error().

Referenced by do_connect(), and main().

203 {
204  (void) arg; /* not used */
205  psql_error("%s", message);
206 }
void psql_error(const char *fmt,...)
Definition: common.c:181
void * arg
bool openQueryOutputFile ( const char *  fname,
FILE **  fout,
bool is_pipe 
)

Definition at line 49 of file common.c.

References NULL, psql_error(), and strerror().

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

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

Definition at line 127 of file common.c.

References _psqlSettings::db, error(), GetVariable(), NULL, pg_strdup(), PQerrorMessage(), PQescapeIdentifier(), PQescapeLiteral(), PQfreemem(), pset, psql_error(), result, value, and _psqlSettings::vars.

129 {
130  char *result;
131  const char *value;
132 
133  value = GetVariable(pset.vars, varname);
134  if (!value)
135  return NULL;
136 
137  if (escape)
138  {
139  char *escaped_value;
140 
141  if (!pset.db)
142  {
143  psql_error("cannot escape without active connection\n");
144  return NULL;
145  }
146 
147  if (as_ident)
148  escaped_value =
149  PQescapeIdentifier(pset.db, value, strlen(value));
150  else
151  escaped_value =
152  PQescapeLiteral(pset.db, value, strlen(value));
153 
154  if (escaped_value == NULL)
155  {
156  const char *error = PQerrorMessage(pset.db);
157 
158  psql_error("%s", error);
159  return NULL;
160  }
161 
162  /*
163  * Rather than complicate the lexer's API with a notion of which
164  * free() routine to use, just pay the price of an extra strdup().
165  */
166  result = pg_strdup(escaped_value);
167  PQfreemem(escaped_value);
168  }
169  else
170  result = pg_strdup(value);
171 
172  return result;
173 }
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6011
static struct @78 value
PsqlSettings pset
Definition: startup.c:33
static void error(void)
Definition: sql-dyntest.c:147
return result
Definition: formatting.c:1618
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:3474
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:181
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:3468
#define NULL
Definition: c.h:229
void PQfreemem(void *ptr)
Definition: fe-exec.c:3200
VariableSpace vars
Definition: settings.h:116
PGresult* PSQLexec ( const char *  query)

Definition at line 603 of file common.c.

References _, AcceptResult(), ClearOrSaveResult(), _psqlSettings::db, _psqlSettings::echo_hidden, _psqlSettings::logfile, NULL, 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(), 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().

604 {
605  PGresult *res;
606 
607  if (!pset.db)
608  {
609  psql_error("You are currently not connected to a database.\n");
610  return NULL;
611  }
612 
614  {
615  printf(_("********* QUERY **********\n"
616  "%s\n"
617  "**************************\n\n"), query);
618  fflush(stdout);
619  if (pset.logfile)
620  {
621  fprintf(pset.logfile,
622  _("********* QUERY **********\n"
623  "%s\n"
624  "**************************\n\n"), query);
625  fflush(pset.logfile);
626  }
627 
629  return NULL;
630  }
631 
632  SetCancelConn();
633 
634  res = PQexec(pset.db, query);
635 
636  ResetCancelConn();
637 
638  if (!AcceptResult(res))
639  {
640  ClearOrSaveResult(res);
641  res = NULL;
642  }
643 
644  return res;
645 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:518
static bool AcceptResult(const PGresult *result)
Definition: common.c:466
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:132
void SetCancelConn(void)
Definition: common.c:406
void psql_error(const char *fmt,...)
Definition: common.c:181
FILE * logfile
Definition: settings.h:114
#define NULL
Definition: c.h:229
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
void ResetCancelConn(void)
Definition: common.c:436
#define _(x)
Definition: elog.c:84
int PSQLexecWatch ( const char *  query,
const printQueryOpt opt 
)

Definition at line 658 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().

659 {
660  PGresult *res;
661  double elapsed_msec = 0;
663  instr_time after;
664 
665  if (!pset.db)
666  {
667  psql_error("You are currently not connected to a database.\n");
668  return 0;
669  }
670 
671  SetCancelConn();
672 
673  if (pset.timing)
674  INSTR_TIME_SET_CURRENT(before);
675 
676  res = PQexec(pset.db, query);
677 
678  ResetCancelConn();
679 
680  if (!AcceptResult(res))
681  {
682  ClearOrSaveResult(res);
683  return 0;
684  }
685 
686  if (pset.timing)
687  {
688  INSTR_TIME_SET_CURRENT(after);
689  INSTR_TIME_SUBTRACT(after, before);
690  elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
691  }
692 
693  /*
694  * If SIGINT is sent while the query is processing, the interrupt will be
695  * consumed. The user's intention, though, is to cancel the entire watch
696  * process, so detect a sent cancellation request and exit in this case.
697  */
698  if (cancel_pressed)
699  {
700  PQclear(res);
701  return 0;
702  }
703 
704  switch (PQresultStatus(res))
705  {
706  case PGRES_TUPLES_OK:
707  printQuery(res, opt, pset.queryFout, false, pset.logfile);
708  break;
709 
710  case PGRES_COMMAND_OK:
711  fprintf(pset.queryFout, "%s\n%s\n\n", opt->title, PQcmdStatus(res));
712  break;
713 
714  case PGRES_EMPTY_QUERY:
715  psql_error(_("\\watch cannot be used with an empty query\n"));
716  PQclear(res);
717  return -1;
718 
719  case PGRES_COPY_OUT:
720  case PGRES_COPY_IN:
721  case PGRES_COPY_BOTH:
722  psql_error(_("\\watch cannot be used with COPY\n"));
723  PQclear(res);
724  return -1;
725 
726  default:
727  psql_error(_("unexpected result status for \\watch\n"));
728  PQclear(res);
729  return -1;
730  }
731 
732  PQclear(res);
733 
734  fflush(pset.queryFout);
735 
736  /* Possible microtiming output */
737  if (pset.timing)
738  PrintTiming(elapsed_msec);
739 
740  return 1;
741 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:518
#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:544
FILE * queryFout
Definition: settings.h:84
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static bool AcceptResult(const PGresult *result)
Definition: common.c:466
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:406
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:2944
void psql_error(const char *fmt,...)
Definition: common.c:181
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
char * title
Definition: print.h:167
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3285
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:153
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
void ResetCancelConn(void)
Definition: common.c:436
#define _(x)
Definition: elog.c:84
bool recognized_connection_string ( const char *  connstr)

Definition at line 2151 of file common.c.

References NULL, and uri_prefix_length().

Referenced by do_connect().

2152 {
2153  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2154 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2124
#define NULL
Definition: c.h:229
static char * connstr
Definition: pg_dumpall.c:64
void ResetCancelConn ( void  )

Definition at line 436 of file common.c.

437 {
438  PGcancel *oldCancelConn;
439 
440 #ifdef WIN32
441  EnterCriticalSection(&cancelConnLock);
442 #endif
443 
444  oldCancelConn = cancelConn;
445  /* be sure handle_sigint doesn't use pointer while freeing */
446  cancelConn = NULL;
447 
448  if (oldCancelConn != NULL)
449  PQfreeCancel(oldCancelConn);
450 
451 #ifdef WIN32
452  LeaveCriticalSection(&cancelConnLock);
453 #endif
454 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3712
static PGcancel *volatile cancelConn
Definition: common.c:238
#define NULL
Definition: c.h:229
bool SendQuery ( const char *  query)

Definition at line 1191 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, _psqlSettings::echo, _psqlSettings::encoding, printTableOpt::encoding, ExecQueryUsingCursor(), _psqlSettings::fetch_count, formatPGVersionNumber(), free, _psqlSettings::g_expanded, _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, NULL, _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().

1192 {
1193  PGresult *results;
1194  PGTransactionStatusType transaction_status;
1195  double elapsed_msec = 0;
1196  bool OK = false;
1197  int i;
1198  bool on_error_rollback_savepoint = false;
1199  static bool on_error_rollback_warning = false;
1200 
1201  if (!pset.db)
1202  {
1203  psql_error("You are currently not connected to a database.\n");
1204  goto sendquery_cleanup;
1205  }
1206 
1207  if (pset.singlestep)
1208  {
1209  char buf[3];
1210 
1211  fflush(stderr);
1212  printf(_("***(Single step mode: verify command)*******************************************\n"
1213  "%s\n"
1214  "***(press return to proceed or enter x and return to cancel)********************\n"),
1215  query);
1216  fflush(stdout);
1217  if (fgets(buf, sizeof(buf), stdin) != NULL)
1218  if (buf[0] == 'x')
1219  goto sendquery_cleanup;
1220  if (cancel_pressed)
1221  goto sendquery_cleanup;
1222  }
1223  else if (pset.echo == PSQL_ECHO_QUERIES)
1224  {
1225  puts(query);
1226  fflush(stdout);
1227  }
1228 
1229  if (pset.logfile)
1230  {
1231  fprintf(pset.logfile,
1232  _("********* QUERY **********\n"
1233  "%s\n"
1234  "**************************\n\n"), query);
1235  fflush(pset.logfile);
1236  }
1237 
1238  SetCancelConn();
1239 
1240  transaction_status = PQtransactionStatus(pset.db);
1241 
1242  if (transaction_status == PQTRANS_IDLE &&
1243  !pset.autocommit &&
1244  !command_no_begin(query))
1245  {
1246  results = PQexec(pset.db, "BEGIN");
1247  if (PQresultStatus(results) != PGRES_COMMAND_OK)
1248  {
1249  psql_error("%s", PQerrorMessage(pset.db));
1250  ClearOrSaveResult(results);
1251  ResetCancelConn();
1252  goto sendquery_cleanup;
1253  }
1254  ClearOrSaveResult(results);
1255  transaction_status = PQtransactionStatus(pset.db);
1256  }
1257 
1258  if (transaction_status == PQTRANS_INTRANS &&
1262  {
1263  if (on_error_rollback_warning == false && pset.sversion < 80000)
1264  {
1265  char sverbuf[32];
1266 
1267  psql_error("The server (version %s) does not support savepoints for ON_ERROR_ROLLBACK.\n",
1269  sverbuf, sizeof(sverbuf)));
1270  on_error_rollback_warning = true;
1271  }
1272  else
1273  {
1274  results = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1275  if (PQresultStatus(results) != PGRES_COMMAND_OK)
1276  {
1277  psql_error("%s", PQerrorMessage(pset.db));
1278  ClearOrSaveResult(results);
1279  ResetCancelConn();
1280  goto sendquery_cleanup;
1281  }
1282  ClearOrSaveResult(results);
1283  on_error_rollback_savepoint = true;
1284  }
1285  }
1286 
1287  if (pset.fetch_count <= 0 || pset.gexec_flag ||
1289  {
1290  /* Default fetch-it-all-and-print mode */
1292  after;
1293 
1294  if (pset.timing)
1295  INSTR_TIME_SET_CURRENT(before);
1296 
1297  results = PQexec(pset.db, query);
1298 
1299  /* these operations are included in the timing result: */
1300  ResetCancelConn();
1301  OK = ProcessResult(&results);
1302 
1303  if (pset.timing)
1304  {
1305  INSTR_TIME_SET_CURRENT(after);
1306  INSTR_TIME_SUBTRACT(after, before);
1307  elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1308  }
1309 
1310  /* but printing results isn't: */
1311  if (OK && results)
1312  OK = PrintQueryResults(results);
1313  }
1314  else
1315  {
1316  /* Fetch-in-segments mode */
1317  OK = ExecQueryUsingCursor(query, &elapsed_msec);
1318  ResetCancelConn();
1319  results = NULL; /* PQclear(NULL) does nothing */
1320  }
1321 
1322  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1323  psql_error("STATEMENT: %s\n", query);
1324 
1325  /* If we made a temporary savepoint, possibly release/rollback */
1326  if (on_error_rollback_savepoint)
1327  {
1328  const char *svptcmd = NULL;
1329 
1330  transaction_status = PQtransactionStatus(pset.db);
1331 
1332  switch (transaction_status)
1333  {
1334  case PQTRANS_INERROR:
1335  /* We always rollback on an error */
1336  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1337  break;
1338 
1339  case PQTRANS_IDLE:
1340  /* If they are no longer in a transaction, then do nothing */
1341  break;
1342 
1343  case PQTRANS_INTRANS:
1344 
1345  /*
1346  * Do nothing if they are messing with savepoints themselves:
1347  * If the user did RELEASE or ROLLBACK, our savepoint is gone.
1348  * If they issued a SAVEPOINT, releasing ours would remove
1349  * theirs.
1350  */
1351  if (results &&
1352  (strcmp(PQcmdStatus(results), "SAVEPOINT") == 0 ||
1353  strcmp(PQcmdStatus(results), "RELEASE") == 0 ||
1354  strcmp(PQcmdStatus(results), "ROLLBACK") == 0))
1355  svptcmd = NULL;
1356  else
1357  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1358  break;
1359 
1360  case PQTRANS_ACTIVE:
1361  case PQTRANS_UNKNOWN:
1362  default:
1363  OK = false;
1364  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1365  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1366  psql_error("unexpected transaction status (%d)\n",
1367  transaction_status);
1368  break;
1369  }
1370 
1371  if (svptcmd)
1372  {
1373  PGresult *svptres;
1374 
1375  svptres = PQexec(pset.db, svptcmd);
1376  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1377  {
1378  psql_error("%s", PQerrorMessage(pset.db));
1379  ClearOrSaveResult(svptres);
1380  OK = false;
1381 
1382  PQclear(results);
1383  ResetCancelConn();
1384  goto sendquery_cleanup;
1385  }
1386  PQclear(svptres);
1387  }
1388  }
1389 
1390  ClearOrSaveResult(results);
1391 
1392  /* Possible microtiming output */
1393  if (pset.timing)
1394  PrintTiming(elapsed_msec);
1395 
1396  /* check for events that may occur during query execution */
1397 
1398  if (pset.encoding != PQclientEncoding(pset.db) &&
1399  PQclientEncoding(pset.db) >= 0)
1400  {
1401  /* track effects of SET CLIENT_ENCODING */
1404  SetVariable(pset.vars, "ENCODING",
1406  }
1407 
1409 
1410  /* perform cleanup that should occur after any attempted query */
1411 
1412 sendquery_cleanup:
1413 
1414  /* reset \g's output-to-filename trigger */
1415  if (pset.gfname)
1416  {
1417  free(pset.gfname);
1418  pset.gfname = NULL;
1419  }
1420 
1421  /* reset \gx's expanded-mode flag */
1422  pset.g_expanded = false;
1423 
1424  /* reset \gset trigger */
1425  if (pset.gset_prefix)
1426  {
1428  pset.gset_prefix = NULL;
1429  }
1430 
1431  /* reset \gexec trigger */
1432  pset.gexec_flag = false;
1433 
1434  /* reset \crosstabview trigger */
1435  pset.crosstab_flag = false;
1436  for (i = 0; i < lengthof(pset.ctv_args); i++)
1437  {
1438  pg_free(pset.ctv_args[i]);
1439  pset.ctv_args[i] = NULL;
1440  }
1441 
1442  return OK;
1443 }
PSQL_ECHO echo
Definition: settings.h:131
bool singlestep
Definition: settings.h:127
char * gset_prefix
Definition: settings.h:95
static bool PrintQueryResults(PGresult *results)
Definition: common.c:1116
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6011
int encoding
Definition: print.h:118
PsqlSettings pset
Definition: startup.c:33
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:518
#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:96
printTableOpt topt
Definition: print.h:165
static void PrintTiming(double elapsed_msec)
Definition: common.c:544
static bool ConnectionUp(void)
Definition: common.c:349
bool autocommit
Definition: settings.h:123
#define lengthof(array)
Definition: c.h:562
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:133
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6071
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
volatile bool cancel_pressed
Definition: print.c:46
static void PrintNotifications(void)
Definition: common.c:748
char * ctv_args[4]
Definition: settings.h:98
static int before(chr x, chr y)
Definition: regc_locale.c:496
bool cur_cmd_interactive
Definition: settings.h:105
static bool is_select_command(const char *query)
Definition: common.c:1956
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:167
static char * buf
Definition: pg_test_fsync.c:65
int fetch_count
Definition: settings.h:128
static bool ProcessResult(PGresult **results)
Definition: common.c:956
bool g_expanded
Definition: settings.h:94
void SetCancelConn(void)
Definition: common.c:406
static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec)
Definition: common.c:1457
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:5966
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:2944
void psql_error(const char *fmt,...)
Definition: common.c:181
char * gfname
Definition: settings.h:93
FILE * logfile
Definition: settings.h:114
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#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
#define NULL
Definition: c.h:229
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:97
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
int encoding
Definition: settings.h:83
static bool command_no_begin(const char *query)
Definition: common.c:1757
void ResetCancelConn(void)
Definition: common.c:436
#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:116
const char* session_username ( void  )

Definition at line 2043 of file common.c.

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

Referenced by get_prompt().

2044 {
2045  const char *val;
2046 
2047  if (!pset.db)
2048  return NULL;
2049 
2050  val = PQparameterStatus(pset.db, "session_authorization");
2051  if (val)
2052  return val;
2053  else
2054  return PQuser(pset.db);
2055 }
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:5976
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:5887
#define NULL
Definition: c.h:229
long val
Definition: informix.c:689
void SetCancelConn ( void  )

Definition at line 406 of file common.c.

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

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

407 {
408  PGcancel *oldCancelConn;
409 
410 #ifdef WIN32
411  EnterCriticalSection(&cancelConnLock);
412 #endif
413 
414  /* Free the old one if we have one */
415  oldCancelConn = cancelConn;
416  /* be sure handle_sigint doesn't use pointer while freeing */
417  cancelConn = NULL;
418 
419  if (oldCancelConn != NULL)
420  PQfreeCancel(oldCancelConn);
421 
423 
424 #ifdef WIN32
425  LeaveCriticalSection(&cancelConnLock);
426 #endif
427 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3712
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:3689
static PGcancel *volatile cancelConn
Definition: common.c:238
#define NULL
Definition: c.h:229
bool setQFout ( const char *  fname)

Definition at line 84 of file common.c.

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

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

85 {
86  FILE *fout;
87  bool is_pipe;
88 
89  /* First make sure we can open the new output file/pipe */
90  if (!openQueryOutputFile(fname, &fout, &is_pipe))
91  return false;
92 
93  /* Close old file/pipe */
94  if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
95  {
96  if (pset.queryFoutPipe)
97  pclose(pset.queryFout);
98  else
99  fclose(pset.queryFout);
100  }
101 
102  pset.queryFout = fout;
103  pset.queryFoutPipe = is_pipe;
104 
105  /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
106  set_sigpipe_trap_state(is_pipe);
108 
109  return true;
110 }
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:49
void setup_cancel_handler ( void  )

Definition at line 292 of file common.c.

293 {
294  pqsignal(SIGINT, handle_sigint);
295 }
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static void handle_sigint(SIGNAL_ARGS)
Definition: common.c:261
bool standard_strings ( void  )

Definition at line 2019 of file common.c.

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

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

2020 {
2021  const char *val;
2022 
2023  if (!pset.db)
2024  return false;
2025 
2026  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2027 
2028  if (val && strcmp(val, "on") == 0)
2029  return true;
2030 
2031  return false;
2032 }
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:5976
long val
Definition: informix.c:689

Variable Documentation

volatile bool sigint_interrupt_enabled
sigjmp_buf sigint_interrupt_jmp

Definition at line 236 of file common.c.

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