33static bool DescribeQuery(
const char *query,
double *elapsed_msec);
40 FILE *printQueryFout);
58 if (!fname || fname[0] ==
'\0')
63 else if (*fname ==
'|')
66 *fout = popen(fname + 1,
"w");
71 *fout = fopen(fname,
"w");
93 if (
pset.
gfname != NULL && *gfile_fout == NULL)
206 pg_log_error(
"cannot escape without active connection");
217 if (escaped_value == NULL)
247 pg_log_error(
"shell command argument contains a newline or carriage return: \"%s\"",
355 fprintf(stderr,
_(
"The connection to the server was lost. Attempting reset: "));
435 if (!OK && show_error)
506 (wait_result == 0) ?
"false" :
"true");
568 if (elapsed_msec < 1000.0)
571 printf(
_(
"Time: %.3f ms\n"), elapsed_msec);
581 seconds = elapsed_msec / 1000.0;
582 minutes = floor(seconds / 60.0);
583 seconds -= 60.0 * minutes;
586 printf(
_(
"Time: %.3f ms (%02d:%06.3f)\n"),
587 elapsed_msec, (
int) minutes, seconds);
591 hours = floor(minutes / 60.0);
592 minutes -= 60.0 * hours;
595 printf(
_(
"Time: %.3f ms (%02d:%02d:%06.3f)\n"),
596 elapsed_msec, (
int) hours, (
int) minutes, seconds);
600 days = floor(hours / 24.0);
601 hours -= 24.0 *
days;
602 printf(
_(
"Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"),
603 elapsed_msec,
days, (
int) hours, (
int) minutes, seconds);
626 pg_log_error(
"You are currently not connected to a database.");
632 printf(
_(
"/******** QUERY *********/\n"
634 "/************************/\n\n"), query);
639 _(
"/******** QUERY *********/\n"
641 "/************************/\n\n"), query);
678 double elapsed_msec = 0;
683 pg_log_error(
"You are currently not connected to a database.");
713 if (notify->
extra[0])
714 fprintf(
pset.
queryFout,
_(
"Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
717 fprintf(
pset.
queryFout,
_(
"Asynchronous notification \"%s\" received from server process with PID %d.\n"),
739 FILE *printQueryFout)
742 FILE *fout = printQueryFout ? printQueryFout :
pset.
queryFout;
791 pg_log_warning(
"attempt to \\gset into specially treated variable \"%s\" ignored",
839 for (r = 0; r < nrows; r++)
841 for (
c = 0;
c < ncolumns;
c++)
918 && (copystream != NULL);
948 *resultp = copy_result;
961 FILE *fout = printQueryFout ? printQueryFout :
pset.
queryFout;
966 if (!(strncmp(cmdstatus,
"INSERT", 6) == 0 ||
967 strncmp(cmdstatus,
"UPDATE", 6) == 0 ||
968 strncmp(cmdstatus,
"DELETE", 6) == 0 ||
969 strncmp(cmdstatus,
"MERGE", 5) == 0))
979 fputs(
"</p>\n", fout);
982 fprintf(fout,
"%s\n", cmdstatus);
1006 FILE *printStatusFout)
1086 double elapsed_msec = 0;
1089 bool on_error_rollback_savepoint =
false;
1090 bool svpt_gone =
false;
1094 pg_log_error(
"You are currently not connected to a database.");
1095 goto sendquery_cleanup;
1103 printf(
_(
"/**(Single step mode: verify command)******************************************/\n"
1105 "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1108 if (fgets(
buf,
sizeof(
buf), stdin) != NULL)
1110 goto sendquery_cleanup;
1112 goto sendquery_cleanup;
1123 _(
"/******** QUERY *********/\n"
1125 "/************************/\n\n"), query);
1144 goto sendquery_cleanup;
1157 result =
PQexec(
pset.
db,
"SAVEPOINT pg_psql_temporary_savepoint");
1162 goto sendquery_cleanup;
1165 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;
1360 "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1376 if (escname == NULL)
1412 _(
"The command has no result, or the result has no columns.\n"));
1447 double *elapsed_msec,
bool *svpt_gone_p,
1448 bool is_watch,
int min_rows,
1453 bool return_early =
false;
1457 FILE *gfile_fout = NULL;
1458 bool gfile_is_pipe =
false;
1542 if (min_rows > 0 &&
PQntuples(result) < min_rows)
1544 return_early =
true;
1547 while (result != NULL)
1550 bool is_chunked_result =
false;
1603 else if (svpt_gone_p && !*svpt_gone_p)
1613 *svpt_gone_p = (strcmp(cmd,
"COMMIT") == 0 ||
1614 strcmp(cmd,
"SAVEPOINT") == 0 ||
1615 strcmp(cmd,
"RELEASE") == 0 ||
1616 strcmp(cmd,
"ROLLBACK") == 0);
1626 FILE *copy_stream = NULL;
1639 copy_stream = printQueryFout ? printQueryFout :
pset.
queryFout;
1651 copy_stream = gfile_fout;
1671 FILE *tuples_fout = printQueryFout ? printQueryFout :
pset.
queryFout;
1673 int64 total_tuples = 0;
1674 bool is_pager =
false;
1675 int flush_error = 0;
1685 tuples_fout = gfile_fout;
1708 flush_error =
fflush(tuples_fout);
1757 is_chunked_result =
true;
1780 last = (next_result == NULL);
1810 FILE *tuples_fout = printQueryFout;
1815 tuples_fout = gfile_fout;
1818 tuples_fout, printQueryFout);
1822 if (last && !is_watch && !is_chunked_result)
1826 result = next_result;
1871 if (isspace((
unsigned char) *query))
1873 else if (query[0] ==
'/' && query[1] ==
'*')
1878 else if (cnestlevel > 0 && query[0] ==
'*' && query[1] ==
'/')
1883 else if (cnestlevel == 0 && query[0] ==
'-' && query[1] ==
'-')
1901 else if (cnestlevel > 0)
1932 while (isalpha((
unsigned char) query[wordlen]))
1963 while (isalpha((
unsigned char) query[wordlen]))
1966 if (wordlen == 11 &&
pg_strncasecmp(query,
"transaction", 11) == 0)
1985 if (isalpha((
unsigned char) query[0]))
1997 while (isalpha((
unsigned char) query[wordlen]))
2002 if (wordlen == 10 &&
pg_strncasecmp(query,
"tablespace", 10) == 0)
2013 while (isalpha((
unsigned char) query[wordlen]))
2024 while (isalpha((
unsigned char) query[wordlen]))
2027 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2041 while (isalpha((
unsigned char) query[wordlen]))
2064 while (isalpha((
unsigned char) query[wordlen]))
2071 if (wordlen == 10 &&
pg_strncasecmp(query,
"tablespace", 10) == 0)
2079 while (isalpha((
unsigned char) query[wordlen]))
2086 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2098 while (isalpha((
unsigned char) query[wordlen]))
2101 if (wordlen == 12 &&
pg_strncasecmp(query,
"concurrently", 12) == 0)
2118 while (isalpha((
unsigned char) query[wordlen]))
2143 if (
val && strcmp(
val,
"on") == 0)
2163 if (
val && strcmp(
val,
"on") == 0)
2220 while (*p !=
'/' && *p !=
'\0')
2226 if (*(
fn + 1) ==
'\0')
2228 else if ((pw = getpwnam(
fn + 1)) != NULL)
2229 strlcpy(home, pw->pw_dir,
sizeof(home));
2232 if (strlen(home) != 0)
void expand_tilde(char **filename)
PGresult * PSQLexec(const char *query)
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)
const char * session_username(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 void ClearOrSaveAllResults(void)
static bool ExecQueryTuples(const PGresult *result)
static void PrintNotifications(void)
static void SetResultVariables(PGresult *result, bool success)
char * psql_get_variable(const char *varname, PsqlScanQuoteType quote, void *passthrough)
sigjmp_buf sigint_interrupt_jmp
static void ClearOrSaveResult(PGresult *result)
static bool SetupGOutput(FILE **gfile_fout, bool *is_pipe)
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)
void clean_extended_state(void)
static bool StoreQueryTuple(const PGresult *result)
static void CloseGOutput(FILE *gfile_fout, bool is_pipe)
static const char * skip_white_space(const char *query)
bool standard_strings(void)
static bool PrintQueryTuples(const PGresult *result, const printQueryOpt *opt, FILE *printQueryFout)
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)
#define Assert(condition)
void ResetCancelConn(void)
void SetCancelConn(PGconn *conn)
void setup_cancel_handler(void(*query_cancel_callback)(void))
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)
#define fprintf(file, fmt, msg)
static void PGresult * res
void PQreset(PGconn *conn)
static const char short_uri_designator[]
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
static const char uri_designator[]
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
ConnStatusType PQstatus(const PGconn *conn)
int PQclientEncoding(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQuser(const PGconn *conn)
char * PQerrorMessage(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)
Oid PQftype(const PGresult *res, int field_num)
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
PGresult * PQgetResult(PGconn *conn)
ExecStatusType PQresultStatus(const PGresult *res)
int PQsendClosePrepared(PGconn *conn, const char *stmt)
char * PQcmdTuples(PGresult *res)
int PQntuples(const PGresult *res)
PGresult * PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
char * PQresultErrorMessage(const PGresult *res)
char * PQfname(const PGresult *res, int field_num)
int PQconsumeInput(PGconn *conn)
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
int PQfmod(const PGresult *res, int field_num)
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
int PQsetChunkedRowsMode(PGconn *conn, int chunkSize)
char * PQresultErrorField(const PGresult *res, int fieldcode)
int PQsendQuery(PGconn *conn, const char *query)
char * PQcmdStatus(PGresult *res)
PGresult * PQexec(PGconn *conn, const char *query)
int PQsendQueryPrepared(PGconn *conn, const char *stmtName, int nParams, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Oid PQoidValue(const PGresult *res)
int PQnfields(const PGresult *res)
PGnotify * PQnotifies(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)
FILE * PageOutput(int lines, const printTableOpt *topt)
void ClosePager(FILE *pagerpipe)
void disable_sigpipe_trap(void)
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)
#define pg_log_error(...)
#define pg_encoding_to_char
#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
@ PSQL_SEND_EXTENDED_QUERY_PARAMS
@ PSQL_SEND_EXTENDED_PARSE
@ PSQL_SEND_EXTENDED_CLOSE
@ PSQL_SEND_EXTENDED_QUERY_PREPARED
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)