33 static bool DescribeQuery(
const char *query,
double *elapsed_msec);
40 FILE *printQueryFout);
59 if (!fname || fname[0] ==
'\0')
64 else if (*fname ==
'|')
67 *fout = popen(fname + 1,
"w");
72 *fout = fopen(fname,
"w");
167 pg_log_error(
"cannot escape without active connection");
178 if (escaped_value == NULL)
208 pg_log_error(
"shell command argument contains a newline or carriage return: \"%s\"",
316 fprintf(stderr,
_(
"The connection to the server was lost. Attempting reset: "));
395 if (!OK && show_error)
508 if (elapsed_msec < 1000.0)
511 printf(
_(
"Time: %.3f ms\n"), elapsed_msec);
521 seconds = elapsed_msec / 1000.0;
522 minutes = floor(seconds / 60.0);
523 seconds -= 60.0 * minutes;
526 printf(
_(
"Time: %.3f ms (%02d:%06.3f)\n"),
527 elapsed_msec, (
int) minutes, seconds);
531 hours = floor(minutes / 60.0);
532 minutes -= 60.0 * hours;
535 printf(
_(
"Time: %.3f ms (%02d:%02d:%06.3f)\n"),
536 elapsed_msec, (
int) hours, (
int) minutes, seconds);
540 days = floor(hours / 24.0);
541 hours -= 24.0 *
days;
542 printf(
_(
"Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"),
543 elapsed_msec,
days, (
int) hours, (
int) minutes, seconds);
566 pg_log_error(
"You are currently not connected to a database.");
572 printf(
_(
"********* QUERY **********\n"
574 "**************************\n\n"), query);
579 _(
"********* QUERY **********\n"
581 "**************************\n\n"), query);
618 double elapsed_msec = 0;
623 pg_log_error(
"You are currently not connected to a database.");
653 if (notify->
extra[0])
654 fprintf(
pset.
queryFout,
_(
"Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
657 fprintf(
pset.
queryFout,
_(
"Asynchronous notification \"%s\" received from server process with PID %d.\n"),
679 FILE *printQueryFout)
682 FILE *fout = printQueryFout ? printQueryFout :
pset.
queryFout;
731 pg_log_warning(
"attempt to \\gset into specially treated variable \"%s\" ignored",
782 for (r = 0; r < nrows; r++)
784 for (
c = 0;
c < ncolumns;
c++)
861 && (copystream != NULL);
891 *resultp = copy_result;
905 FILE *fout = printQueryFout ? printQueryFout :
pset.
queryFout;
913 fputs(
"</p>\n", fout);
942 FILE *printStatusFout)
945 const char *cmdstatus;
969 if (strncmp(cmdstatus,
"INSERT", 6) == 0 ||
970 strncmp(cmdstatus,
"UPDATE", 6) == 0 ||
971 strncmp(cmdstatus,
"DELETE", 6) == 0)
1026 double elapsed_msec = 0;
1029 bool on_error_rollback_savepoint =
false;
1030 bool svpt_gone =
false;
1034 pg_log_error(
"You are currently not connected to a database.");
1035 goto sendquery_cleanup;
1043 printf(
_(
"***(Single step mode: verify command)*******************************************\n"
1045 "***(press return to proceed or enter x and return to cancel)********************\n"),
1048 if (fgets(
buf,
sizeof(
buf), stdin) != NULL)
1050 goto sendquery_cleanup;
1052 goto sendquery_cleanup;
1063 _(
"********* QUERY **********\n"
1065 "**************************\n\n"), query);
1084 goto sendquery_cleanup;
1097 result =
PQexec(
pset.
db,
"SAVEPOINT pg_psql_temporary_savepoint");
1102 goto sendquery_cleanup;
1105 on_error_rollback_savepoint =
true;
1129 if (on_error_rollback_savepoint)
1131 const char *svptcmd = NULL;
1135 switch (transaction_status)
1139 svptcmd =
"ROLLBACK TO pg_psql_temporary_savepoint";
1153 svptcmd =
"RELEASE pg_psql_temporary_savepoint";
1163 transaction_status);
1178 goto sendquery_cleanup;
1313 "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1329 if (escname == NULL)
1365 _(
"The command has no result, or the result has no columns.\n"));
1397 double *elapsed_msec,
bool *svpt_gone_p,
1406 FILE *gfile_fout = NULL;
1407 bool gfile_is_pipe =
false;
1445 while (result != NULL)
1500 else if (svpt_gone_p && !*svpt_gone_p)
1510 *svpt_gone_p = (strcmp(cmd,
"COMMIT") == 0 ||
1511 strcmp(cmd,
"SAVEPOINT") == 0 ||
1512 strcmp(cmd,
"RELEASE") == 0 ||
1513 strcmp(cmd,
"ROLLBACK") == 0);
1523 FILE *copy_stream = NULL;
1536 copy_stream = printQueryFout ? printQueryFout :
pset.
queryFout;
1546 if (gfile_fout == NULL)
1549 &gfile_fout, &gfile_is_pipe))
1553 copy_stream = gfile_fout;
1559 copy_stream = gfile_fout;
1582 last = (next_result == NULL);
1612 FILE *tuples_fout = printQueryFout;
1613 bool do_print =
true;
1618 if (gfile_fout == NULL)
1621 &gfile_fout, &gfile_is_pipe))
1629 tuples_fout = gfile_fout;
1633 tuples_fout, printQueryFout);
1637 if (!is_watch && last &&
success)
1641 result = next_result;
1690 bool is_pager =
false;
1691 bool started_txn =
false;
1692 int64 total_tuples = 0;
1755 snprintf(fetch_cmd,
sizeof(fetch_cmd),
1756 "FETCH FORWARD %d FROM _psql_cursor",
1823 total_tuples += ntuples;
1825 if (ntuples < fetch_count)
1830 else if (fout ==
stdout && !is_pager)
1854 flush_error =
fflush(fout);
1958 if (isspace((
unsigned char) *query))
1960 else if (query[0] ==
'/' && query[1] ==
'*')
1965 else if (cnestlevel > 0 && query[0] ==
'*' && query[1] ==
'/')
1970 else if (cnestlevel == 0 && query[0] ==
'-' && query[1] ==
'-')
1988 else if (cnestlevel > 0)
2019 while (isalpha((
unsigned char) query[wordlen]))
2050 while (isalpha((
unsigned char) query[wordlen]))
2053 if (wordlen == 11 &&
pg_strncasecmp(query,
"transaction", 11) == 0)
2072 if (isalpha((
unsigned char) query[0]))
2084 while (isalpha((
unsigned char) query[wordlen]))
2089 if (wordlen == 10 &&
pg_strncasecmp(query,
"tablespace", 10) == 0)
2100 while (isalpha((
unsigned char) query[wordlen]))
2111 while (isalpha((
unsigned char) query[wordlen]))
2114 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2128 while (isalpha((
unsigned char) query[wordlen]))
2151 while (isalpha((
unsigned char) query[wordlen]))
2158 if (wordlen == 10 &&
pg_strncasecmp(query,
"tablespace", 10) == 0)
2166 while (isalpha((
unsigned char) query[wordlen]))
2173 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2185 while (isalpha((
unsigned char) query[wordlen]))
2188 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2205 while (isalpha((
unsigned char) query[wordlen]))
2231 if (query[0] ==
'(')
2241 while (isalpha((
unsigned char) query[wordlen]))
2267 if (
val && strcmp(
val,
"on") == 0)
2287 if (
val && strcmp(
val,
"on") == 0)
2344 while (*p !=
'/' && *p !=
'\0')
2350 if (*(
fn + 1) ==
'\0')
2352 else if ((pw = getpwnam(
fn + 1)) != NULL)
2353 strlcpy(home, pw->pw_dir,
sizeof(home));
2356 if (strlen(home) != 0)
static void setup_cancel_handler(void)
void expand_tilde(char **filename)
void psql_setup_cancel_handler(void)
volatile sig_atomic_t sigint_interrupt_enabled
static bool PrintQueryResult(PGresult *result, bool last, const printQueryOpt *opt, FILE *printQueryFout, FILE *printStatusFout)
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 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)
static bool HandleCopyResult(PGresult **resultp, FILE *copystream)
void NoticeProcessor(void *arg, const char *message)
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)
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 PQsendQueryParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
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)
#define INSTR_TIME_GET_MILLISEC(t)
#define INSTR_TIME_SET_ZERO(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)