PostgreSQL Source Code  git master
common.h File Reference
#include <setjmp.h>
#include "fe_utils/print.h"
#include "fe_utils/psqlscan.h"
#include "libpq-fe.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 psql_setup_cancel_handler (void)
 
PGresultPSQLexec (const char *query)
 
int PSQLexecWatch (const char *query, const printQueryOpt *opt, FILE *printQueryFout)
 
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 2223 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().

2224 {
2225  if (!filename || !(*filename))
2226  return;
2227 
2228  /*
2229  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2230  * for short versions of long file names, though the tilde is usually
2231  * toward the end, not at the beginning.
2232  */
2233 #ifndef WIN32
2234 
2235  /* try tilde expansion */
2236  if (**filename == '~')
2237  {
2238  char *fn;
2239  char oldp,
2240  *p;
2241  struct passwd *pw;
2242  char home[MAXPGPATH];
2243 
2244  fn = *filename;
2245  *home = '\0';
2246 
2247  p = fn + 1;
2248  while (*p != '/' && *p != '\0')
2249  p++;
2250 
2251  oldp = *p;
2252  *p = '\0';
2253 
2254  if (*(fn + 1) == '\0')
2255  get_home_path(home); /* ~ or ~/ only */
2256  else if ((pw = getpwnam(fn + 1)) != NULL)
2257  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2258 
2259  *p = oldp;
2260  if (strlen(home) != 0)
2261  {
2262  char *newfn;
2263 
2264  newfn = psprintf("%s%s", home, p);
2265  free(fn);
2266  *filename = newfn;
2267  }
2268  }
2269 #endif
2270 }
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:92
bool get_home_path(char *ret_path)
Definition: path.c:807

◆ is_superuser()

bool is_superuser ( void  )

Definition at line 2162 of file common.c.

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

2163 {
2164  const char *val;
2165 
2166  if (!pset.db)
2167  return false;
2168 
2169  val = PQparameterStatus(pset.db, "is_superuser");
2170 
2171  if (val && strcmp(val, "on") == 0)
2172  return true;
2173 
2174  return false;
2175 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:32
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6709
long val
Definition: informix.c:664

◆ NoticeProcessor()

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

Definition at line 220 of file common.c.

References pg_log_info.

Referenced by do_connect(), and main().

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

◆ openQueryOutputFile()

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

Definition at line 50 of file common.c.

References pg_log_error, and generate_unaccent_rules::stdout.

Referenced by ExecQueryUsingCursor(), PrintQueryTuples(), ProcessResult(), 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  pg_log_error("%s: %m", fname);
71  return false;
72  }
73 
74  return true;
75 }
#define pg_log_error(...)
Definition: logging.h:80

◆ 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_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.

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  pg_log_error("cannot escape without active connection");
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  pg_log_info("%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  pg_log_error("shell command argument contains a newline or carriage return: \"%s\"",
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:6744
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:128
PsqlSettings pset
Definition: startup.c:32
static void error(void)
Definition: sql-dyntest.c:147
#define pg_log_error(...)
Definition: logging.h:80
bool appendShellStringNoError(PQExpBuffer buf, const char *str)
Definition: string_utils.c:441
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4075
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:71
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4069
static struct @143 value
#define free(a)
Definition: header.h:65
void PQfreemem(void *ptr)
Definition: fe-exec.c:3796
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:92
#define pg_log_info(...)
Definition: logging.h:88
VariableSpace vars
Definition: settings.h:118

◆ psql_setup_cancel_handler()

void psql_setup_cancel_handler ( void  )

Definition at line 266 of file common.c.

References psql_cancel_callback(), and setup_cancel_handler().

Referenced by main().

267 {
269 }
static void setup_cancel_handler(void)
Definition: parallel.c:613
static void psql_cancel_callback(void)
Definition: common.c:250

◆ PSQLexec()

PGresult* PSQLexec ( const char *  query)

Definition at line 540 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(), listExtendedStats(), listExtensionContents(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), listOneExtensionContents(), listOperatorClasses(), listOperatorFamilies(), listOpFamilyFunctions(), listOpFamilyOperators(), listPartitionedTables(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSConfigsVerbose(), listTSDictionaries(), listTSParsers(), listTSParsersVerbose(), listTSTemplates(), listUserMappings(), main(), objectDescription(), permissionsList(), and start_lo_xact().

541 {
542  PGresult *res;
543 
544  if (!pset.db)
545  {
546  pg_log_error("You are currently not connected to a database.");
547  return NULL;
548  }
549 
551  {
552  printf(_("********* QUERY **********\n"
553  "%s\n"
554  "**************************\n\n"), query);
555  fflush(stdout);
556  if (pset.logfile)
557  {
559  _("********* QUERY **********\n"
560  "%s\n"
561  "**************************\n\n"), query);
562  fflush(pset.logfile);
563  }
564 
566  return NULL;
567  }
568 
570 
571  res = PQexec(pset.db, query);
572 
573  ResetCancelConn();
574 
575  if (!AcceptResult(res))
576  {
577  ClearOrSaveResult(res);
578  res = NULL;
579  }
580 
581  return res;
582 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:32
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:455
#define pg_log_error(...)
Definition: logging.h:80
void ResetCancelConn(void)
Definition: cancel.c:100
#define printf(...)
Definition: port.h:222
#define fprintf
Definition: port.h:220
static bool AcceptResult(const PGresult *result)
Definition: common.c:356
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:143
void SetCancelConn(PGconn *conn)
Definition: cancel.c:70
FILE * logfile
Definition: settings.h:116
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
#define _(x)
Definition: elog.c:89

◆ PSQLexecWatch()

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

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

596 {
597  PGresult *res;
598  double elapsed_msec = 0;
600  instr_time after;
601  FILE *fout;
602 
603  if (!pset.db)
604  {
605  pg_log_error("You are currently not connected to a database.");
606  return 0;
607  }
608 
610 
611  if (pset.timing)
612  INSTR_TIME_SET_CURRENT(before);
613 
614  res = PQexec(pset.db, query);
615 
616  ResetCancelConn();
617 
618  if (!AcceptResult(res))
619  {
620  ClearOrSaveResult(res);
621  return 0;
622  }
623 
624  if (pset.timing)
625  {
626  INSTR_TIME_SET_CURRENT(after);
627  INSTR_TIME_SUBTRACT(after, before);
628  elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
629  }
630 
631  /*
632  * If SIGINT is sent while the query is processing, the interrupt will be
633  * consumed. The user's intention, though, is to cancel the entire watch
634  * process, so detect a sent cancellation request and exit in this case.
635  */
636  if (cancel_pressed)
637  {
638  PQclear(res);
639  return 0;
640  }
641 
642  fout = printQueryFout ? printQueryFout : pset.queryFout;
643 
644  switch (PQresultStatus(res))
645  {
646  case PGRES_TUPLES_OK:
647  printQuery(res, opt, fout, false, pset.logfile);
648  break;
649 
650  case PGRES_COMMAND_OK:
651  fprintf(fout, "%s\n%s\n\n", opt->title, PQcmdStatus(res));
652  break;
653 
654  case PGRES_EMPTY_QUERY:
655  pg_log_error("\\watch cannot be used with an empty query");
656  PQclear(res);
657  return -1;
658 
659  case PGRES_COPY_OUT:
660  case PGRES_COPY_IN:
661  case PGRES_COPY_BOTH:
662  pg_log_error("\\watch cannot be used with COPY");
663  PQclear(res);
664  return -1;
665 
666  default:
667  pg_log_error("unexpected result status for \\watch");
668  PQclear(res);
669  return -1;
670  }
671 
672  PQclear(res);
673 
674  fflush(fout);
675 
676  /* Possible microtiming output */
677  if (pset.timing)
678  PrintTiming(elapsed_msec);
679 
680  return 1;
681 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:32
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:455
#define pg_log_error(...)
Definition: logging.h:80
void ResetCancelConn(void)
Definition: cancel.c:100
#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:481
FILE * queryFout
Definition: settings.h:84
#define fprintf
Definition: port.h:220
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
static bool AcceptResult(const PGresult *result)
Definition: common.c:356
static int before(chr x, chr y)
Definition: regc_locale.c:492
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
void SetCancelConn(PGconn *conn)
Definition: cancel.c:70
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3519
FILE * logfile
Definition: settings.h:116
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * title
Definition: print.h:171
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3420
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193

◆ recognized_connection_string()

bool recognized_connection_string ( const char *  connstr)

Definition at line 2308 of file common.c.

References uri_prefix_length().

Referenced by do_connect().

2309 {
2310  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2311 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2281
static char * connstr
Definition: pg_dumpall.c:62

◆ SendQuery()

bool SendQuery ( const char *  query)

Definition at line 1193 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::gdesc_flag, _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gsavepopt, _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(), restorePsetInfo(), 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().

1194 {
1195  PGresult *results;
1196  PGTransactionStatusType transaction_status;
1197  double elapsed_msec = 0;
1198  bool OK = false;
1199  int i;
1200  bool on_error_rollback_savepoint = false;
1201  static bool on_error_rollback_warning = false;
1202 
1203  if (!pset.db)
1204  {
1205  pg_log_error("You are currently not connected to a database.");
1206  goto sendquery_cleanup;
1207  }
1208 
1209  if (pset.singlestep)
1210  {
1211  char buf[3];
1212 
1213  fflush(stderr);
1214  printf(_("***(Single step mode: verify command)*******************************************\n"
1215  "%s\n"
1216  "***(press return to proceed or enter x and return to cancel)********************\n"),
1217  query);
1218  fflush(stdout);
1219  if (fgets(buf, sizeof(buf), stdin) != NULL)
1220  if (buf[0] == 'x')
1221  goto sendquery_cleanup;
1222  if (cancel_pressed)
1223  goto sendquery_cleanup;
1224  }
1225  else if (pset.echo == PSQL_ECHO_QUERIES)
1226  {
1227  puts(query);
1228  fflush(stdout);
1229  }
1230 
1231  if (pset.logfile)
1232  {
1234  _("********* QUERY **********\n"
1235  "%s\n"
1236  "**************************\n\n"), query);
1237  fflush(pset.logfile);
1238  }
1239 
1241 
1242  transaction_status = PQtransactionStatus(pset.db);
1243 
1244  if (transaction_status == PQTRANS_IDLE &&
1245  !pset.autocommit &&
1246  !command_no_begin(query))
1247  {
1248  results = PQexec(pset.db, "BEGIN");
1249  if (PQresultStatus(results) != PGRES_COMMAND_OK)
1250  {
1252  ClearOrSaveResult(results);
1253  ResetCancelConn();
1254  goto sendquery_cleanup;
1255  }
1256  ClearOrSaveResult(results);
1257  transaction_status = PQtransactionStatus(pset.db);
1258  }
1259 
1260  if (transaction_status == PQTRANS_INTRANS &&
1264  {
1265  if (on_error_rollback_warning == false && pset.sversion < 80000)
1266  {
1267  char sverbuf[32];
1268 
1269  pg_log_warning("The server (version %s) does not support savepoints for ON_ERROR_ROLLBACK.",
1271  sverbuf, sizeof(sverbuf)));
1272  on_error_rollback_warning = true;
1273  }
1274  else
1275  {
1276  results = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1277  if (PQresultStatus(results) != PGRES_COMMAND_OK)
1278  {
1280  ClearOrSaveResult(results);
1281  ResetCancelConn();
1282  goto sendquery_cleanup;
1283  }
1284  ClearOrSaveResult(results);
1285  on_error_rollback_savepoint = true;
1286  }
1287  }
1288 
1289  if (pset.gdesc_flag)
1290  {
1291  /* Describe query's result columns, without executing it */
1292  OK = DescribeQuery(query, &elapsed_msec);
1293  ResetCancelConn();
1294  results = NULL; /* PQclear(NULL) does nothing */
1295  }
1296  else if (pset.fetch_count <= 0 || pset.gexec_flag ||
1298  {
1299  /* Default fetch-it-all-and-print mode */
1301  after;
1302 
1303  if (pset.timing)
1304  INSTR_TIME_SET_CURRENT(before);
1305 
1306  results = PQexec(pset.db, query);
1307 
1308  /* these operations are included in the timing result: */
1309  ResetCancelConn();
1310  OK = ProcessResult(&results);
1311 
1312  if (pset.timing)
1313  {
1314  INSTR_TIME_SET_CURRENT(after);
1315  INSTR_TIME_SUBTRACT(after, before);
1316  elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1317  }
1318 
1319  /* but printing results isn't: */
1320  if (OK && results)
1321  OK = PrintQueryResults(results);
1322  }
1323  else
1324  {
1325  /* Fetch-in-segments mode */
1326  OK = ExecQueryUsingCursor(query, &elapsed_msec);
1327  ResetCancelConn();
1328  results = NULL; /* PQclear(NULL) does nothing */
1329  }
1330 
1331  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1332  pg_log_info("STATEMENT: %s", query);
1333 
1334  /* If we made a temporary savepoint, possibly release/rollback */
1335  if (on_error_rollback_savepoint)
1336  {
1337  const char *svptcmd = NULL;
1338 
1339  transaction_status = PQtransactionStatus(pset.db);
1340 
1341  switch (transaction_status)
1342  {
1343  case PQTRANS_INERROR:
1344  /* We always rollback on an error */
1345  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1346  break;
1347 
1348  case PQTRANS_IDLE:
1349  /* If they are no longer in a transaction, then do nothing */
1350  break;
1351 
1352  case PQTRANS_INTRANS:
1353 
1354  /*
1355  * Do nothing if they are messing with savepoints themselves:
1356  * If the user did COMMIT AND CHAIN, RELEASE or ROLLBACK, our
1357  * savepoint is gone. If they issued a SAVEPOINT, releasing
1358  * ours would remove theirs.
1359  */
1360  if (results &&
1361  (strcmp(PQcmdStatus(results), "COMMIT") == 0 ||
1362  strcmp(PQcmdStatus(results), "SAVEPOINT") == 0 ||
1363  strcmp(PQcmdStatus(results), "RELEASE") == 0 ||
1364  strcmp(PQcmdStatus(results), "ROLLBACK") == 0))
1365  svptcmd = NULL;
1366  else
1367  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1368  break;
1369 
1370  case PQTRANS_ACTIVE:
1371  case PQTRANS_UNKNOWN:
1372  default:
1373  OK = false;
1374  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1375  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1376  pg_log_error("unexpected transaction status (%d)",
1377  transaction_status);
1378  break;
1379  }
1380 
1381  if (svptcmd)
1382  {
1383  PGresult *svptres;
1384 
1385  svptres = PQexec(pset.db, svptcmd);
1386  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1387  {
1389  ClearOrSaveResult(svptres);
1390  OK = false;
1391 
1392  PQclear(results);
1393  ResetCancelConn();
1394  goto sendquery_cleanup;
1395  }
1396  PQclear(svptres);
1397  }
1398  }
1399 
1400  ClearOrSaveResult(results);
1401 
1402  /* Possible microtiming output */
1403  if (pset.timing)
1404  PrintTiming(elapsed_msec);
1405 
1406  /* check for events that may occur during query execution */
1407 
1408  if (pset.encoding != PQclientEncoding(pset.db) &&
1409  PQclientEncoding(pset.db) >= 0)
1410  {
1411  /* track effects of SET CLIENT_ENCODING */
1414  SetVariable(pset.vars, "ENCODING",
1416  }
1417 
1419 
1420  /* perform cleanup that should occur after any attempted query */
1421 
1422 sendquery_cleanup:
1423 
1424  /* reset \g's output-to-filename trigger */
1425  if (pset.gfname)
1426  {
1427  free(pset.gfname);
1428  pset.gfname = NULL;
1429  }
1430 
1431  /* restore print settings if \g changed them */
1432  if (pset.gsavepopt)
1433  {
1435  pset.gsavepopt = NULL;
1436  }
1437 
1438  /* reset \gset trigger */
1439  if (pset.gset_prefix)
1440  {
1442  pset.gset_prefix = NULL;
1443  }
1444 
1445  /* reset \gdesc trigger */
1446  pset.gdesc_flag = false;
1447 
1448  /* reset \gexec trigger */
1449  pset.gexec_flag = false;
1450 
1451  /* reset \crosstabview trigger */
1452  pset.crosstab_flag = false;
1453  for (i = 0; i < lengthof(pset.ctv_args); i++)
1454  {
1455  pg_free(pset.ctv_args[i]);
1456  pset.ctv_args[i] = NULL;
1457  }
1458 
1459  return OK;
1460 }
PSQL_ECHO echo
Definition: settings.h:142
bool singlestep
Definition: settings.h:136
char * gset_prefix
Definition: settings.h:96
static bool PrintQueryResults(PGresult *results)
Definition: common.c:1118
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
int encoding
Definition: print.h:122
PsqlSettings pset
Definition: startup.c:32
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:455
#define pg_log_error(...)
Definition: logging.h:80
void ResetCancelConn(void)
Definition: cancel.c:100
#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:169
static void PrintTiming(double elapsed_msec)
Definition: common.c:481
printQueryOpt * gsavepopt
Definition: settings.h:94
#define printf(...)
Definition: port.h:222
static bool ConnectionUp(void)
Definition: common.c:277
bool autocommit
Definition: settings.h:132
#define lengthof(array)
Definition: c.h:734
#define fprintf
Definition: port.h:220
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:144
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6821
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
static void PrintNotifications(void)
Definition: common.c:688
char * ctv_args[4]
Definition: settings.h:100
static int before(chr x, chr y)
Definition: regc_locale.c:492
bool cur_cmd_interactive
Definition: settings.h:107
static bool is_select_command(const char *query)
Definition: common.c:2125
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:170
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:4700
bool gdesc_flag
Definition: settings.h:97
static char * buf
Definition: pg_test_fsync.c:68
int fetch_count
Definition: settings.h:139
static bool ProcessResult(PGresult **results)
Definition: common.c:913
void SetCancelConn(PGconn *conn)
Definition: cancel.c:70
static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec)
Definition: common.c:1588
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6699
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1472
char * PQcmdStatus(PGresult *res)
Definition: fe-exec.c:3519
char * gfname
Definition: settings.h:93
FILE * logfile
Definition: settings.h:116
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
PGTransactionStatusType
Definition: libpq-fe.h:114
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:156
int i
bool crosstab_flag
Definition: settings.h:99
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
#define pg_log_warning(...)
Definition: pgfnames.c:24
int encoding
Definition: settings.h:83
static bool command_no_begin(const char *query)
Definition: common.c:1910
#define _(x)
Definition: elog.c:89
#define pg_log_info(...)
Definition: logging.h:88
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
VariableSpace vars
Definition: settings.h:118

◆ session_username()

const char* session_username ( void  )

Definition at line 2202 of file common.c.

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

Referenced by get_prompt().

2203 {
2204  const char *val;
2205 
2206  if (!pset.db)
2207  return NULL;
2208 
2209  val = PQparameterStatus(pset.db, "session_authorization");
2210  if (val)
2211  return val;
2212  else
2213  return PQuser(pset.db);
2214 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:32
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6709
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:6598
long val
Definition: informix.c:664

◆ setQFout()

bool setQFout ( const char *  fname)

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

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:32
bool queryFoutPipe
Definition: settings.h:85
FILE * queryFout
Definition: settings.h:84
void set_sigpipe_trap_state(bool ignore)
Definition: print.c:2961
void restore_sigpipe_trap(void)
Definition: print.c:2948
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:50

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2182 of file common.c.

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

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

2183 {
2184  const char *val;
2185 
2186  if (!pset.db)
2187  return false;
2188 
2189  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2190 
2191  if (val && strcmp(val, "on") == 0)
2192  return true;
2193 
2194  return false;
2195 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:32
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6709
long val
Definition: informix.c:664

Variable Documentation

◆ sigint_interrupt_enabled

volatile bool sigint_interrupt_enabled

◆ sigint_interrupt_jmp

sigjmp_buf sigint_interrupt_jmp

Definition at line 247 of file common.c.

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