33 static bool DescribeQuery(
const char *query,
double *elapsed_msec);
41 FILE *printQueryFout);
60 if (!fname || fname[0] ==
'\0')
65 else if (*fname ==
'|')
68 *fout = popen(fname + 1,
"w");
73 *fout = fopen(fname,
"w");
168 pg_log_error(
"cannot escape without active connection");
179 if (escaped_value == NULL)
209 pg_log_error(
"shell command argument contains a newline or carriage return: \"%s\"",
317 fprintf(stderr,
_(
"The connection to the server was lost. Attempting reset: "));
396 if (!OK && show_error)
467 (wait_result == 0) ?
"false" :
"true");
529 if (elapsed_msec < 1000.0)
532 printf(
_(
"Time: %.3f ms\n"), elapsed_msec);
542 seconds = elapsed_msec / 1000.0;
543 minutes = floor(seconds / 60.0);
544 seconds -= 60.0 * minutes;
547 printf(
_(
"Time: %.3f ms (%02d:%06.3f)\n"),
548 elapsed_msec, (
int) minutes, seconds);
552 hours = floor(minutes / 60.0);
553 minutes -= 60.0 * hours;
556 printf(
_(
"Time: %.3f ms (%02d:%02d:%06.3f)\n"),
557 elapsed_msec, (
int) hours, (
int) minutes, seconds);
561 days = floor(hours / 24.0);
562 hours -= 24.0 *
days;
563 printf(
_(
"Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"),
564 elapsed_msec,
days, (
int) hours, (
int) minutes, seconds);
587 pg_log_error(
"You are currently not connected to a database.");
593 printf(
_(
"/******** QUERY *********/\n"
595 "/************************/\n\n"), query);
600 _(
"/******** QUERY *********/\n"
602 "/************************/\n\n"), query);
639 double elapsed_msec = 0;
644 pg_log_error(
"You are currently not connected to a database.");
674 if (notify->
extra[0])
675 fprintf(
pset.
queryFout,
_(
"Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
678 fprintf(
pset.
queryFout,
_(
"Asynchronous notification \"%s\" received from server process with PID %d.\n"),
700 FILE *printQueryFout)
703 FILE *fout = printQueryFout ? printQueryFout :
pset.
queryFout;
752 pg_log_warning(
"attempt to \\gset into specially treated variable \"%s\" ignored",
803 for (r = 0; r < nrows; r++)
805 for (
c = 0;
c < ncolumns;
c++)
882 && (copystream != NULL);
912 *resultp = copy_result;
926 FILE *fout = printQueryFout ? printQueryFout :
pset.
queryFout;
934 fputs(
"</p>\n", fout);
963 FILE *printStatusFout)
966 const char *cmdstatus;
990 if (strncmp(cmdstatus,
"INSERT", 6) == 0 ||
991 strncmp(cmdstatus,
"UPDATE", 6) == 0 ||
992 strncmp(cmdstatus,
"DELETE", 6) == 0)
1047 double elapsed_msec = 0;
1050 bool on_error_rollback_savepoint =
false;
1051 bool svpt_gone =
false;
1055 pg_log_error(
"You are currently not connected to a database.");
1056 goto sendquery_cleanup;
1064 printf(
_(
"/**(Single step mode: verify command)******************************************/\n"
1066 "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1069 if (fgets(
buf,
sizeof(
buf), stdin) != NULL)
1071 goto sendquery_cleanup;
1073 goto sendquery_cleanup;
1084 _(
"/******** QUERY *********/\n"
1086 "/************************/\n\n"), query);
1105 goto sendquery_cleanup;
1118 result =
PQexec(
pset.
db,
"SAVEPOINT pg_psql_temporary_savepoint");
1123 goto sendquery_cleanup;
1126 on_error_rollback_savepoint =
true;
1150 if (on_error_rollback_savepoint)
1152 const char *svptcmd = NULL;
1156 switch (transaction_status)
1160 svptcmd =
"ROLLBACK TO pg_psql_temporary_savepoint";
1174 svptcmd =
"RELEASE pg_psql_temporary_savepoint";
1184 transaction_status);
1199 goto sendquery_cleanup;
1334 "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1350 if (escname == NULL)
1386 _(
"The command has no result, or the result has no columns.\n"));
1418 double *elapsed_msec,
bool *svpt_gone_p,
1419 bool is_watch,
int min_rows,
1424 bool return_early =
false;
1428 FILE *gfile_fout = NULL;
1429 bool gfile_is_pipe =
false;
1466 if (min_rows > 0 &&
PQntuples(result) < min_rows)
1468 return_early =
true;
1471 while (result != NULL)
1526 else if (svpt_gone_p && !*svpt_gone_p)
1536 *svpt_gone_p = (strcmp(cmd,
"COMMIT") == 0 ||
1537 strcmp(cmd,
"SAVEPOINT") == 0 ||
1538 strcmp(cmd,
"RELEASE") == 0 ||
1539 strcmp(cmd,
"ROLLBACK") == 0);
1549 FILE *copy_stream = NULL;
1562 copy_stream = printQueryFout ? printQueryFout :
pset.
queryFout;
1572 if (gfile_fout == NULL)
1575 &gfile_fout, &gfile_is_pipe))
1579 copy_stream = gfile_fout;
1585 copy_stream = gfile_fout;
1608 last = (next_result == NULL);
1638 FILE *tuples_fout = printQueryFout;
1639 bool do_print =
true;
1644 if (gfile_fout == NULL)
1647 &gfile_fout, &gfile_is_pipe))
1655 tuples_fout = gfile_fout;
1659 tuples_fout, printQueryFout);
1663 if (!is_watch && last &&
success)
1667 result = next_result;
1719 bool is_pager =
false;
1720 bool started_txn =
false;
1721 int64 total_tuples = 0;
1784 snprintf(fetch_cmd,
sizeof(fetch_cmd),
1785 "FETCH FORWARD %d FROM _psql_cursor",
1852 total_tuples += ntuples;
1854 if (ntuples < fetch_count)
1859 else if (fout ==
stdout && !is_pager)
1883 flush_error =
fflush(fout);
1987 if (isspace((
unsigned char) *query))
1989 else if (query[0] ==
'/' && query[1] ==
'*')
1994 else if (cnestlevel > 0 && query[0] ==
'*' && query[1] ==
'/')
1999 else if (cnestlevel == 0 && query[0] ==
'-' && query[1] ==
'-')
2017 else if (cnestlevel > 0)
2048 while (isalpha((
unsigned char) query[wordlen]))
2079 while (isalpha((
unsigned char) query[wordlen]))
2082 if (wordlen == 11 &&
pg_strncasecmp(query,
"transaction", 11) == 0)
2101 if (isalpha((
unsigned char) query[0]))
2113 while (isalpha((
unsigned char) query[wordlen]))
2118 if (wordlen == 10 &&
pg_strncasecmp(query,
"tablespace", 10) == 0)
2129 while (isalpha((
unsigned char) query[wordlen]))
2140 while (isalpha((
unsigned char) query[wordlen]))
2143 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2157 while (isalpha((
unsigned char) query[wordlen]))
2180 while (isalpha((
unsigned char) query[wordlen]))
2187 if (wordlen == 10 &&
pg_strncasecmp(query,
"tablespace", 10) == 0)
2195 while (isalpha((
unsigned char) query[wordlen]))
2202 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2214 while (isalpha((
unsigned char) query[wordlen]))
2217 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2234 while (isalpha((
unsigned char) query[wordlen]))
2260 if (query[0] ==
'(')
2270 while (isalpha((
unsigned char) query[wordlen]))
2296 if (
val && strcmp(
val,
"on") == 0)
2316 if (
val && strcmp(
val,
"on") == 0)
2373 while (*p !=
'/' && *p !=
'\0')
2379 if (*(
fn + 1) ==
'\0')
2381 else if ((pw = getpwnam(
fn + 1)) != NULL)
2382 strlcpy(home, pw->pw_dir,
sizeof(home));
2385 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)
int PSQLexecWatch(const char *query, const printQueryOpt *opt, FILE *printQueryFout, int min_rows)
void SetShellResultVariables(int wait_result)
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)
bool setQFout(const char *fname)
static int ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, int min_rows, const printQueryOpt *opt, FILE *printQueryFout)
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)
int wait_result_to_exit_code(int exit_status)