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 2322 of file common.c.

2323 {
2324  if (!filename || !(*filename))
2325  return;
2326 
2327  /*
2328  * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2329  * for short versions of long file names, though the tilde is usually
2330  * toward the end, not at the beginning.
2331  */
2332 #ifndef WIN32
2333 
2334  /* try tilde expansion */
2335  if (**filename == '~')
2336  {
2337  char *fn;
2338  char oldp,
2339  *p;
2340  struct passwd *pw;
2341  char home[MAXPGPATH];
2342 
2343  fn = *filename;
2344  *home = '\0';
2345 
2346  p = fn + 1;
2347  while (*p != '/' && *p != '\0')
2348  p++;
2349 
2350  oldp = *p;
2351  *p = '\0';
2352 
2353  if (*(fn + 1) == '\0')
2354  get_home_path(home); /* ~ or ~/ only */
2355  else if ((pw = getpwnam(fn + 1)) != NULL)
2356  strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2357 
2358  *p = oldp;
2359  if (strlen(home) != 0)
2360  {
2361  char *newfn;
2362 
2363  newfn = psprintf("%s%s", home, p);
2364  free(fn);
2365  *filename = newfn;
2366  }
2367  }
2368 #endif
2369 }
#define free(a)
Definition: header.h:65
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:94
bool get_home_path(char *ret_path)
Definition: path.c:928
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void * fn(void *arg)

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

◆ is_superuser()

bool is_superuser ( void  )

Definition at line 2261 of file common.c.

2262 {
2263  const char *val;
2264 
2265  if (!pset.db)
2266  return false;
2267 
2268  val = PQparameterStatus(pset.db, "is_superuser");
2269 
2270  if (val && strcmp(val, "on") == 0)
2271  return true;
2272 
2273  return false;
2274 }
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6873
long val
Definition: informix.c:664
PsqlSettings pset
Definition: startup.c:32
PGconn * db
Definition: settings.h:82

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

◆ NoticeProcessor()

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

Definition at line 223 of file common.c.

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

References arg, and pg_log_info.

Referenced by do_connect(), ExecQueryAndProcessResults(), and main().

◆ openQueryOutputFile()

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

Definition at line 53 of file common.c.

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

References pg_log_error, and generate_unaccent_rules::stdout.

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

◆ psql_get_variable()

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

Definition at line 132 of file common.c.

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

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

◆ psql_setup_cancel_handler()

void psql_setup_cancel_handler ( void  )

Definition at line 269 of file common.c.

270 {
272 }
static void setup_cancel_handler(void)
Definition: parallel.c:610
static void psql_cancel_callback(void)
Definition: common.c:253

References psql_cancel_callback(), and setup_cancel_handler().

Referenced by main().

◆ PSQLexec()

PGresult* PSQLexec ( const char *  query)

Definition at line 556 of file common.c.

557 {
558  PGresult *res;
559 
560  if (!pset.db)
561  {
562  pg_log_error("You are currently not connected to a database.");
563  return NULL;
564  }
565 
567  {
568  printf(_("********* QUERY **********\n"
569  "%s\n"
570  "**************************\n\n"), query);
571  fflush(stdout);
572  if (pset.logfile)
573  {
575  _("********* QUERY **********\n"
576  "%s\n"
577  "**************************\n\n"), query);
579  }
580 
582  return NULL;
583  }
584 
586 
587  res = PQexec(pset.db, query);
588 
589  ResetCancelConn();
590 
591  if (!AcceptResult(res, true))
592  {
594  res = NULL;
595  }
596 
597  return res;
598 }
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:458
static bool AcceptResult(const PGresult *result, bool show_error)
Definition: common.c:359
void ResetCancelConn(void)
Definition: cancel.c:107
void SetCancelConn(PGconn *conn)
Definition: cancel.c:77
#define _(x)
Definition: elog.c:89
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2273
static void const char fflush(stdout)
#define fprintf
Definition: port.h:229
#define printf(...)
Definition: port.h:231
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition: settings.h:47
@ PSQL_ECHO_HIDDEN_OFF
Definition: settings.h:45
FILE * logfile
Definition: settings.h:116
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:143

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

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

◆ PSQLexecWatch()

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

Definition at line 611 of file common.c.

612 {
613  bool timing = pset.timing;
614  double elapsed_msec = 0;
615  int res;
616 
617  if (!pset.db)
618  {
619  pg_log_error("You are currently not connected to a database.");
620  return 0;
621  }
622 
624 
625  res = ExecQueryAndProcessResults(query, &elapsed_msec, NULL, true, opt, printQueryFout);
626 
627  ResetCancelConn();
628 
629  /* Possible microtiming output */
630  if (timing)
631  PrintTiming(elapsed_msec);
632 
633  return res;
634 }
static int ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:1478
static void PrintTiming(double elapsed_msec)
Definition: common.c:497

References _psqlSettings::db, ExecQueryAndProcessResults(), pg_log_error, PrintTiming(), pset, res, ResetCancelConn(), SetCancelConn(), and _psqlSettings::timing.

Referenced by do_watch().

◆ recognized_connection_string()

bool recognized_connection_string ( const char *  connstr)

Definition at line 2407 of file common.c.

2408 {
2409  return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2410 }
static int uri_prefix_length(const char *connstr)
Definition: common.c:2380
static char * connstr
Definition: pg_dumpall.c:63

References connstr, and uri_prefix_length().

Referenced by do_connect().

◆ SendQuery()

bool SendQuery ( const char *  query)

Definition at line 1115 of file common.c.

1116 {
1117  bool timing = pset.timing;
1118  PGTransactionStatusType transaction_status;
1119  double elapsed_msec = 0;
1120  bool OK = false;
1121  int i;
1122  bool on_error_rollback_savepoint = false;
1123  bool svpt_gone = false;
1124 
1125  if (!pset.db)
1126  {
1127  pg_log_error("You are currently not connected to a database.");
1128  goto sendquery_cleanup;
1129  }
1130 
1131  if (pset.singlestep)
1132  {
1133  char buf[3];
1134 
1135  fflush(stderr);
1136  printf(_("***(Single step mode: verify command)*******************************************\n"
1137  "%s\n"
1138  "***(press return to proceed or enter x and return to cancel)********************\n"),
1139  query);
1140  fflush(stdout);
1141  if (fgets(buf, sizeof(buf), stdin) != NULL)
1142  if (buf[0] == 'x')
1143  goto sendquery_cleanup;
1144  if (cancel_pressed)
1145  goto sendquery_cleanup;
1146  }
1147  else if (pset.echo == PSQL_ECHO_QUERIES)
1148  {
1149  puts(query);
1150  fflush(stdout);
1151  }
1152 
1153  if (pset.logfile)
1154  {
1156  _("********* QUERY **********\n"
1157  "%s\n"
1158  "**************************\n\n"), query);
1159  fflush(pset.logfile);
1160  }
1161 
1163 
1164  transaction_status = PQtransactionStatus(pset.db);
1165 
1166  if (transaction_status == PQTRANS_IDLE &&
1167  !pset.autocommit &&
1168  !command_no_begin(query))
1169  {
1170  PGresult *result;
1171 
1172  result = PQexec(pset.db, "BEGIN");
1173  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1174  {
1176  ClearOrSaveResult(result);
1177  goto sendquery_cleanup;
1178  }
1179  ClearOrSaveResult(result);
1180  transaction_status = PQtransactionStatus(pset.db);
1181  }
1182 
1183  if (transaction_status == PQTRANS_INTRANS &&
1187  {
1188  PGresult *result;
1189 
1190  result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1191  if (PQresultStatus(result) != PGRES_COMMAND_OK)
1192  {
1194  ClearOrSaveResult(result);
1195  goto sendquery_cleanup;
1196  }
1197  ClearOrSaveResult(result);
1198  on_error_rollback_savepoint = true;
1199  }
1200 
1201  if (pset.gdesc_flag)
1202  {
1203  /* Describe query's result columns, without executing it */
1204  OK = DescribeQuery(query, &elapsed_msec);
1205  }
1206  else if (pset.fetch_count <= 0 || pset.gexec_flag ||
1208  {
1209  /* Default fetch-it-all-and-print mode */
1210  OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, NULL, NULL) >= 0);
1211  }
1212  else
1213  {
1214  /* Fetch-in-segments mode */
1215  OK = ExecQueryUsingCursor(query, &elapsed_msec);
1216  }
1217 
1218  if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1219  pg_log_info("STATEMENT: %s", query);
1220 
1221  /* If we made a temporary savepoint, possibly release/rollback */
1222  if (on_error_rollback_savepoint)
1223  {
1224  const char *svptcmd = NULL;
1225 
1226  transaction_status = PQtransactionStatus(pset.db);
1227 
1228  switch (transaction_status)
1229  {
1230  case PQTRANS_INERROR:
1231  /* We always rollback on an error */
1232  svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1233  break;
1234 
1235  case PQTRANS_IDLE:
1236  /* If they are no longer in a transaction, then do nothing */
1237  break;
1238 
1239  case PQTRANS_INTRANS:
1240 
1241  /*
1242  * Release our savepoint, but do nothing if they are messing
1243  * with savepoints themselves
1244  */
1245  if (!svpt_gone)
1246  svptcmd = "RELEASE pg_psql_temporary_savepoint";
1247  break;
1248 
1249  case PQTRANS_ACTIVE:
1250  case PQTRANS_UNKNOWN:
1251  default:
1252  OK = false;
1253  /* PQTRANS_UNKNOWN is expected given a broken connection. */
1254  if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1255  pg_log_error("unexpected transaction status (%d)",
1256  transaction_status);
1257  break;
1258  }
1259 
1260  if (svptcmd)
1261  {
1262  PGresult *svptres;
1263 
1264  svptres = PQexec(pset.db, svptcmd);
1265  if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1266  {
1268  ClearOrSaveResult(svptres);
1269  OK = false;
1270 
1271  goto sendquery_cleanup;
1272  }
1273  PQclear(svptres);
1274  }
1275  }
1276 
1277  /* Possible microtiming output */
1278  if (timing)
1279  PrintTiming(elapsed_msec);
1280 
1281  /* check for events that may occur during query execution */
1282 
1283  if (pset.encoding != PQclientEncoding(pset.db) &&
1284  PQclientEncoding(pset.db) >= 0)
1285  {
1286  /* track effects of SET CLIENT_ENCODING */
1289  SetVariable(pset.vars, "ENCODING",
1291  }
1292 
1294 
1295  /* perform cleanup that should occur after any attempted query */
1296 
1297 sendquery_cleanup:
1298 
1299  /* global cancellation reset */
1300  ResetCancelConn();
1301 
1302  /* reset \g's output-to-filename trigger */
1303  if (pset.gfname)
1304  {
1305  free(pset.gfname);
1306  pset.gfname = NULL;
1307  }
1308 
1309  /* restore print settings if \g changed them */
1310  if (pset.gsavepopt)
1311  {
1313  pset.gsavepopt = NULL;
1314  }
1315 
1316  /* reset \gset trigger */
1317  if (pset.gset_prefix)
1318  {
1320  pset.gset_prefix = NULL;
1321  }
1322 
1323  /* reset \gdesc trigger */
1324  pset.gdesc_flag = false;
1325 
1326  /* reset \gexec trigger */
1327  pset.gexec_flag = false;
1328 
1329  /* reset \crosstabview trigger */
1330  pset.crosstab_flag = false;
1331  for (i = 0; i < lengthof(pset.ctv_args); i++)
1332  {
1333  pg_free(pset.ctv_args[i]);
1334  pset.ctv_args[i] = NULL;
1335  }
1336 
1337  return OK;
1338 }
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1350
static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec)
Definition: common.c:1686
static void PrintNotifications(void)
Definition: common.c:641
static bool is_select_command(const char *query)
Definition: common.c:2224
static bool ConnectionUp(void)
Definition: common.c:280
static bool command_no_begin(const char *query)
Definition: common.c:2009
#define lengthof(array)
Definition: c.h:734
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:4778
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6863
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6985
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3270
void PQclear(PGresult *res)
Definition: fe-exec.c:718
void pg_free(void *ptr)
Definition: fe_memutils.c:105
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
int i
Definition: isn.c:73
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
PGTransactionStatusType
Definition: libpq-fe.h:117
@ PQTRANS_INTRANS
Definition: libpq-fe.h:120
@ PQTRANS_IDLE
Definition: libpq-fe.h:118
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:119
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:122
@ PQTRANS_INERROR
Definition: libpq-fe.h:121
@ PSQL_ERROR_ROLLBACK_ON
Definition: settings.h:54
@ PSQL_ERROR_ROLLBACK_OFF
Definition: settings.h:52
@ PSQL_ECHO_ERRORS
Definition: settings.h:39
@ PSQL_ECHO_QUERIES
Definition: settings.h:38
printQueryOpt popt
Definition: settings.h:91
char * gset_prefix
Definition: settings.h:96
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:144
int encoding
Definition: settings.h:83
bool autocommit
Definition: settings.h:132
char * ctv_args[4]
Definition: settings.h:100
PSQL_ECHO echo
Definition: settings.h:142
bool singlestep
Definition: settings.h:136
printQueryOpt * gsavepopt
Definition: settings.h:94
char * gfname
Definition: settings.h:93
int fetch_count
Definition: settings.h:139
bool cur_cmd_interactive
Definition: settings.h:107
bool gexec_flag
Definition: settings.h:98
bool crosstab_flag
Definition: settings.h:99
bool gdesc_flag
Definition: settings.h:97
printTableOpt topt
Definition: print.h:169
int encoding
Definition: print.h:122
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211

References _, _psqlSettings::autocommit, 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, ExecQueryAndProcessResults(), ExecQueryUsingCursor(), _psqlSettings::fetch_count, fflush(), fprintf, free, _psqlSettings::gdesc_flag, _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gsavepopt, _psqlSettings::gset_prefix, i, is_select_command(), lengthof, _psqlSettings::logfile, _psqlSettings::on_error_rollback, pg_encoding_to_char(), pg_free(), pg_log_error, pg_log_info, PGRES_COMMAND_OK, _psqlSettings::popt, PQclear(), PQclientEncoding(), PQerrorMessage(), PQexec(), PQresultStatus(), PQTRANS_ACTIVE, PQTRANS_IDLE, PQTRANS_INERROR, PQTRANS_INTRANS, PQTRANS_UNKNOWN, PQtransactionStatus(), printf, PrintNotifications(), PrintTiming(), 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::timing, printQueryOpt::topt, and _psqlSettings::vars.

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

◆ session_username()

const char* session_username ( void  )

Definition at line 2301 of file common.c.

2302 {
2303  const char *val;
2304 
2305  if (!pset.db)
2306  return NULL;
2307 
2308  val = PQparameterStatus(pset.db, "session_authorization");
2309  if (val)
2310  return val;
2311  else
2312  return PQuser(pset.db);
2313 }
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:6762

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

Referenced by get_prompt().

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 88 of file common.c.

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

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

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2281 of file common.c.

2282 {
2283  const char *val;
2284 
2285  if (!pset.db)
2286  return false;
2287 
2288  val = PQparameterStatus(pset.db, "standard_conforming_strings");
2289 
2290  if (val && strcmp(val, "on") == 0)
2291  return true;
2292 
2293  return false;
2294 }

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

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

Variable Documentation

◆ sigint_interrupt_enabled

◆ sigint_interrupt_jmp

sigjmp_buf sigint_interrupt_jmp
extern