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.

Macros

#define atooid(x)   ((Oid) strtoul((x), NULL, 10))
 

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 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
 

Macro Definition Documentation

#define atooid (   x)    ((Oid) strtoul((x), NULL, 10))

Definition at line 16 of file common.h.

Function Documentation

void expand_tilde ( char **  filename)

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

2054 {
2055  if (!filename || !(*filename))
2056  return;
2057 
2058  /*
2059  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2060  * for short versions of long file names, though the tilde is usually
2061  * toward the end, not at the beginning.
2062  */
2063 #ifndef WIN32
2064 
2065  /* try tilde expansion */
2066  if (**filename == '~')
2067  {
2068  char *fn;
2069  char oldp,
2070  *p;
2071  struct passwd *pw;
2072  char home[MAXPGPATH];
2073 
2074  fn = *filename;
2075  *home = '\0';
2076 
2077  p = fn + 1;
2078  while (*p != '/' && *p != '\0')
2079  p++;
2080 
2081  oldp = *p;
2082  *p = '\0';
2083 
2084  if (*(fn + 1) == '\0')
2085  get_home_path(home); /* ~ or ~/ only */
2086  else if ((pw = getpwnam(fn + 1)) != NULL)
2087  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2088 
2089  *p = oldp;
2090  if (strlen(home) != 0)
2091  {
2092  char *newfn;
2093 
2094  newfn = psprintf("%s%s", home, p);
2095  free(fn);
2096  *filename = newfn;
2097  }
2098  }
2099 #endif
2100 
2101  return;
2102 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define MAXPGPATH
static void * fn(void *arg)
#define free(a)
Definition: header.h:60
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define NULL
Definition: c.h:226
static char * filename
Definition: pg_dumpall.c:84
bool get_home_path(char *ret_path)
Definition: path.c:807
bool is_superuser ( void  )

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

1986 {
1987  const char *val;
1988 
1989  if (!pset.db)
1990  return false;
1991 
1992  val = PQparameterStatus(pset.db, "is_superuser");
1993 
1994  if (val && strcmp(val, "on") == 0)
1995  return true;
1996 
1997  return false;
1998 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:37
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:5925
long val
Definition: informix.c:689
void void NoticeProcessor ( void *  arg,
const char *  message 
)

Definition at line 198 of file common.c.

References psql_error().

Referenced by do_connect(), and main().

199 {
200  (void) arg; /* not used */
201  psql_error("%s", message);
202 }
void psql_error(const char *fmt,...)
Definition: common.c:177
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:177
#define NULL
Definition: c.h:226
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 
)

Definition at line 124 of file common.c.

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

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

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

600 {
601  PGresult *res;
602 
603  if (!pset.db)
604  {
605  psql_error("You are currently not connected to a database.\n");
606  return NULL;
607  }
608 
610  {
611  printf(_("********* QUERY **********\n"
612  "%s\n"
613  "**************************\n\n"), query);
614  fflush(stdout);
615  if (pset.logfile)
616  {
617  fprintf(pset.logfile,
618  _("********* QUERY **********\n"
619  "%s\n"
620  "**************************\n\n"), query);
621  fflush(pset.logfile);
622  }
623 
625  return NULL;
626  }
627 
628  SetCancelConn();
629 
630  res = PQexec(pset.db, query);
631 
632  ResetCancelConn();
633 
634  if (!AcceptResult(res))
635  {
636  ClearOrSaveResult(res);
637  res = NULL;
638  }
639 
640  return res;
641 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:37
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:514
static bool AcceptResult(const PGresult *result)
Definition: common.c:462
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:131
void SetCancelConn(void)
Definition: common.c:402
void psql_error(const char *fmt,...)
Definition: common.c:177
FILE * logfile
Definition: settings.h:113
#define NULL
Definition: c.h:226
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
void ResetCancelConn(void)
Definition: common.c:432
#define _(x)
Definition: elog.c:84
int PSQLexecWatch ( const char *  query,
const printQueryOpt opt 
)

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

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

Definition at line 2140 of file common.c.

References NULL, and uri_prefix_length().

Referenced by do_connect().

2141 {
2142  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2143 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2113
#define NULL
Definition: c.h:226
static char * connstr
Definition: pg_dumpall.c:67
void ResetCancelConn ( void  )

Definition at line 432 of file common.c.

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

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

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

Definition at line 2032 of file common.c.

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

Referenced by get_prompt().

2033 {
2034  const char *val;
2035 
2036  if (!pset.db)
2037  return NULL;
2038 
2039  val = PQparameterStatus(pset.db, "session_authorization");
2040  if (val)
2041  return val;
2042  else
2043  return PQuser(pset.db);
2044 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:37
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:5925
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:5836
#define NULL
Definition: c.h:226
long val
Definition: informix.c:689
void SetCancelConn ( void  )

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

403 {
404  PGcancel *oldCancelConn;
405 
406 #ifdef WIN32
407  EnterCriticalSection(&cancelConnLock);
408 #endif
409 
410  /* Free the old one if we have one */
411  oldCancelConn = cancelConn;
412  /* be sure handle_sigint doesn't use pointer while freeing */
413  cancelConn = NULL;
414 
415  if (oldCancelConn != NULL)
416  PQfreeCancel(oldCancelConn);
417 
419 
420 #ifdef WIN32
421  LeaveCriticalSection(&cancelConnLock);
422 #endif
423 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:37
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3661
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:3638
static PGcancel *volatile cancelConn
Definition: common.c:234
#define NULL
Definition: c.h:226
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:37
bool queryFoutPipe
Definition: settings.h:85
FILE * queryFout
Definition: settings.h:84
void set_sigpipe_trap_state(bool ignore)
Definition: print.c:2838
void restore_sigpipe_trap(void)
Definition: print.c:2825
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:49
void setup_cancel_handler ( void  )

Definition at line 288 of file common.c.

289 {
290  pqsignal(SIGINT, handle_sigint);
291 }
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static void handle_sigint(SIGNAL_ARGS)
Definition: common.c:257
bool standard_strings ( void  )

Definition at line 2008 of file common.c.

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

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

2009 {
2010  const char *val;
2011 
2012  if (!pset.db)
2013  return false;
2014 
2015  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2016 
2017  if (val && strcmp(val, "on") == 0)
2018  return true;
2019 
2020  return false;
2021 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:37
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:5925
long val
Definition: informix.c:689

Variable Documentation

volatile bool sigint_interrupt_enabled
sigjmp_buf sigint_interrupt_jmp

Definition at line 232 of file common.c.

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