33 static bool DescribeQuery(
const char *query,
double *elapsed_msec);
36 bool is_watch,
const printQueryOpt *opt, FILE *printQueryFout);
55 if (!fname || fname[0] ==
'\0')
60 else if (*fname ==
'|')
62 *fout = popen(fname + 1,
"w");
67 *fout = fopen(fname,
"w");
162 pg_log_error(
"cannot escape without active connection");
173 if (escaped_value == NULL)
203 pg_log_error(
"shell command argument contains a newline or carriage return: \"%s\"",
311 fprintf(stderr,
_(
"The connection to the server was lost. Attempting reset: "));
390 if (!OK && show_error)
503 if (elapsed_msec < 1000.0)
506 printf(
_(
"Time: %.3f ms\n"), elapsed_msec);
516 seconds = elapsed_msec / 1000.0;
517 minutes = floor(seconds / 60.0);
518 seconds -= 60.0 * minutes;
521 printf(
_(
"Time: %.3f ms (%02d:%06.3f)\n"),
522 elapsed_msec, (
int) minutes, seconds);
526 hours = floor(minutes / 60.0);
527 minutes -= 60.0 * hours;
530 printf(
_(
"Time: %.3f ms (%02d:%02d:%06.3f)\n"),
531 elapsed_msec, (
int) hours, (
int) minutes, seconds);
535 days = floor(hours / 24.0);
536 hours -= 24.0 *
days;
537 printf(
_(
"Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"),
538 elapsed_msec,
days, (
int) hours, (
int) minutes, seconds);
561 pg_log_error(
"You are currently not connected to a database.");
567 printf(
_(
"********* QUERY **********\n"
569 "**************************\n\n"), query);
574 _(
"********* QUERY **********\n"
576 "**************************\n\n"), query);
613 double elapsed_msec = 0;
618 pg_log_error(
"You are currently not connected to a database.");
648 if (notify->
extra[0])
649 fprintf(
pset.
queryFout,
_(
"Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
652 fprintf(
pset.
queryFout,
_(
"Asynchronous notification \"%s\" received from server process with PID %d.\n"),
699 FILE *fout = printQueryFout ? printQueryFout :
pset.
queryFout;
748 pg_log_warning(
"attempt to \\gset into specially treated variable \"%s\" ignored",
799 for (r = 0; r < nrows; r++)
801 for (
c = 0;
c < ncolumns;
c++)
877 bool need_close =
false;
878 bool is_pipe =
false;
889 ©stream, &is_pipe))
907 && (copystream != NULL);
950 *resultp = copy_result;
964 FILE *fout = printQueryFout ? printQueryFout :
pset.
queryFout;
972 fputs(
"</p>\n", fout);
997 const char *cmdstatus;
1021 if (strncmp(cmdstatus,
"INSERT", 6) == 0 ||
1022 strncmp(cmdstatus,
"UPDATE", 6) == 0 ||
1023 strncmp(cmdstatus,
"DELETE", 6) == 0)
1080 double elapsed_msec = 0;
1083 bool on_error_rollback_savepoint =
false;
1084 bool svpt_gone =
false;
1088 pg_log_error(
"You are currently not connected to a database.");
1089 goto sendquery_cleanup;
1097 printf(
_(
"***(Single step mode: verify command)*******************************************\n"
1099 "***(press return to proceed or enter x and return to cancel)********************\n"),
1102 if (fgets(
buf,
sizeof(
buf), stdin) != NULL)
1104 goto sendquery_cleanup;
1106 goto sendquery_cleanup;
1117 _(
"********* QUERY **********\n"
1119 "**************************\n\n"), query);
1138 goto sendquery_cleanup;
1151 result =
PQexec(
pset.
db,
"SAVEPOINT pg_psql_temporary_savepoint");
1156 goto sendquery_cleanup;
1159 on_error_rollback_savepoint =
true;
1183 if (on_error_rollback_savepoint)
1185 const char *svptcmd = NULL;
1189 switch (transaction_status)
1193 svptcmd =
"ROLLBACK TO pg_psql_temporary_savepoint";
1207 svptcmd =
"RELEASE pg_psql_temporary_savepoint";
1217 transaction_status);
1232 goto sendquery_cleanup;
1355 "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1371 if (escname == NULL)
1407 _(
"The command has no result, or the result has no columns.\n"));
1440 bool is_watch,
const printQueryOpt *opt, FILE *printQueryFout)
1479 while (result != NULL)
1534 else if (svpt_gone_p && !*svpt_gone_p)
1544 *svpt_gone_p = (strcmp(cmd,
"COMMIT") == 0 ||
1545 strcmp(cmd,
"SAVEPOINT") == 0 ||
1546 strcmp(cmd,
"RELEASE") == 0 ||
1547 strcmp(cmd,
"ROLLBACK") == 0);
1573 last = (next_result == NULL);
1599 if (!is_watch && last &&
success)
1603 result = next_result;
1640 bool is_pager =
false;
1641 bool started_txn =
false;
1642 int64 total_tuples = 0;
1703 snprintf(fetch_cmd,
sizeof(fetch_cmd),
1704 "FETCH FORWARD %d FROM _psql_cursor",
1771 total_tuples += ntuples;
1773 if (ntuples < fetch_count)
1778 else if (fout ==
stdout && !is_pager)
1802 flush_error =
fflush(fout);
1906 if (isspace((
unsigned char) *query))
1908 else if (query[0] ==
'/' && query[1] ==
'*')
1913 else if (cnestlevel > 0 && query[0] ==
'*' && query[1] ==
'/')
1918 else if (cnestlevel == 0 && query[0] ==
'-' && query[1] ==
'-')
1936 else if (cnestlevel > 0)
1967 while (isalpha((
unsigned char) query[wordlen]))
1998 while (isalpha((
unsigned char) query[wordlen]))
2001 if (wordlen == 11 &&
pg_strncasecmp(query,
"transaction", 11) == 0)
2020 if (isalpha((
unsigned char) query[0]))
2032 while (isalpha((
unsigned char) query[wordlen]))
2037 if (wordlen == 10 &&
pg_strncasecmp(query,
"tablespace", 10) == 0)
2048 while (isalpha((
unsigned char) query[wordlen]))
2059 while (isalpha((
unsigned char) query[wordlen]))
2062 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2076 while (isalpha((
unsigned char) query[wordlen]))
2099 while (isalpha((
unsigned char) query[wordlen]))
2106 if (wordlen == 10 &&
pg_strncasecmp(query,
"tablespace", 10) == 0)
2114 while (isalpha((
unsigned char) query[wordlen]))
2121 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2133 while (isalpha((
unsigned char) query[wordlen]))
2136 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2153 while (isalpha((
unsigned char) query[wordlen]))
2179 if (query[0] ==
'(')
2189 while (isalpha((
unsigned char) query[wordlen]))
2215 if (
val && strcmp(
val,
"on") == 0)
2235 if (
val && strcmp(
val,
"on") == 0)
2292 while (*p !=
'/' && *p !=
'\0')
2298 if (*(
fn + 1) ==
'\0')
2300 else if ((pw = getpwnam(
fn + 1)) != NULL)
2301 strlcpy(home, pw->pw_dir,
sizeof(home));
2304 if (strlen(home) != 0)
static void setup_cancel_handler(void)
void expand_tilde(char **filename)
void psql_setup_cancel_handler(void)
static bool CheckConnection(void)
static void PrintQueryStatus(PGresult *result, FILE *printQueryFout)
static int uri_prefix_length(const char *connstr)
static bool DescribeQuery(const char *query, double *elapsed_msec)
static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec)
static void ClearOrSaveAllResults(void)
static bool ExecQueryTuples(const PGresult *result)
static void PrintNotifications(void)
static bool is_select_command(const char *query)
char * psql_get_variable(const char *varname, PsqlScanQuoteType quote, void *passthrough)
const char * session_username(void)
static bool PrintQueryResult(PGresult *result, bool last, bool is_watch, const printQueryOpt *opt, FILE *printQueryFout)
static void SetResultVariables(PGresult *result, bool success)
PGresult * PSQLexec(const char *query)
sigjmp_buf sigint_interrupt_jmp
static void ClearOrSaveResult(PGresult *result)
static void psql_cancel_callback(void)
static bool AcceptResult(const PGresult *result, bool show_error)
void NoticeProcessor(void *arg, const char *message)
volatile bool sigint_interrupt_enabled
static bool StoreQueryTuple(const PGresult *result)
bool standard_strings(void)
static bool PrintQueryTuples(const PGresult *result, const printQueryOpt *opt, FILE *printQueryFout)
static const char * skip_white_space(const char *query)
static bool ConnectionUp(void)
static int ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, const printQueryOpt *opt, FILE *printQueryFout)
int PSQLexecWatch(const char *query, const printQueryOpt *opt, FILE *printQueryFout)
static bool HandleCopyResult(PGresult **resultp)
bool setQFout(const char *fname)
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
bool recognized_connection_string(const char *connstr)
static void PrintTiming(double elapsed_msec)
bool SendQuery(const char *query)
static bool command_no_begin(const char *query)
bool handleCopyOut(PGconn *conn, FILE *copystream, PGresult **res)
bool handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary, PGresult **res)
static void cleanup(void)
void ResetCancelConn(void)
void SetCancelConn(PGconn *conn)
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
void UnsyncVariables(void)
void connection_warnings(bool in_startup)
bool conditional_active(ConditionalStack cstack)
bool PrintResultInCrosstab(const PGresult *res)
static void PGresult * res
const char * pg_encoding_to_char(int encoding)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
void PQreset(PGconn *conn)
static const char short_uri_designator[]
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
static const char uri_designator[]
char * PQerrorMessage(const PGconn *conn)
ConnStatusType PQstatus(const PGconn *conn)
int PQclientEncoding(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQuser(const PGconn *conn)
int PQbinaryTuples(const PGresult *res)
void PQfreemem(void *ptr)
PGnotify * PQnotifies(PGconn *conn)
PGresult * PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Oid PQftype(const PGresult *res, int field_num)
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
ExecStatusType PQresultStatus(const PGresult *res)
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
char * PQcmdTuples(PGresult *res)
char * PQresultErrorMessage(const PGresult *res)
int PQntuples(const PGresult *res)
char * PQfname(const PGresult *res, int field_num)
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
PGresult * PQexec(PGconn *conn, const char *query)
int PQconsumeInput(PGconn *conn)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
char * PQcmdStatus(PGresult *res)
int PQfmod(const PGresult *res, int field_num)
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
int PQsendQuery(PGconn *conn, const char *query)
char * PQresultErrorField(const PGresult *res, int fieldcode)
Oid PQoidValue(const PGresult *res)
int PQnfields(const PGresult *res)
PGresult * PQgetResult(PGconn *conn)
int PQmblenBounded(const char *s, int encoding)
char * pg_strdup(const char *in)
void restore_sigpipe_trap(void)
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
void ClosePager(FILE *pagerpipe)
void disable_sigpipe_trap(void)
FILE * PageOutput(int lines, const printTableOpt *topt)
void html_escaped_print(const char *in, FILE *fout)
void set_sigpipe_trap_state(bool ignore)
volatile sig_atomic_t cancel_pressed
#define INSTR_TIME_SET_CURRENT(t)
#define INSTR_TIME_SUBTRACT(x, y)
struct timeval instr_time
#define INSTR_TIME_GET_MILLISEC(t)
static void const char fflush(stdout)
Assert(fmt[strlen(fmt) - 1] !='\n')
#define pg_log_error(...)
#define pg_log_warning(...)
bool get_home_path(char *ret_path)
size_t strlcpy(char *dst, const char *src, size_t siz)
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
#define PG_DIAG_MESSAGE_PRIMARY
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void initPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
char * psprintf(const char *fmt,...)
static int before(chr x, chr y)
@ PSQL_ERROR_ROLLBACK_OFF
@ PSQL_ECHO_HIDDEN_NOEXEC
bool appendShellStringNoError(PQExpBuffer buf, const char *str)
PSQL_ERROR_ROLLBACK on_error_rollback
PGresult * last_error_result
PSQL_ECHO_HIDDEN echo_hidden
printQueryOpt * gsavepopt
unsigned long prior_records
static void * fn(void *arg)
bool VariableHasHook(VariableSpace space, const char *name)
bool SetVariable(VariableSpace space, const char *name, const char *value)
const char * GetVariable(VariableSpace space, const char *name)