PostgreSQL Source Code
git master
|
#include "postgres_fe.h"
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <pwd.h>
#include <signal.h>
#include <unistd.h>
#include "command.h"
#include "common.h"
#include "common/logging.h"
#include "copy.h"
#include "crosstabview.h"
#include "fe_utils/cancel.h"
#include "fe_utils/mbprint.h"
#include "fe_utils/string_utils.h"
#include "portability/instr_time.h"
#include "settings.h"
Go to the source code of this file.
Functions | |
static bool | DescribeQuery (const char *query, double *elapsed_msec) |
static bool | ExecQueryUsingCursor (const char *query, double *elapsed_msec) |
static int | ExecQueryAndProcessResults (const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, const printQueryOpt *opt, FILE *printQueryFout) |
static bool | command_no_begin (const char *query) |
static bool | is_select_command (const char *query) |
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) |
static void | psql_cancel_callback (void) |
void | psql_setup_cancel_handler (void) |
static bool | ConnectionUp (void) |
static bool | CheckConnection (void) |
static bool | AcceptResult (const PGresult *result, bool show_error) |
static void | SetResultVariables (PGresult *result, bool success) |
void | SetShellResultVariables (int wait_result) |
static void | ClearOrSaveResult (PGresult *result) |
static void | ClearOrSaveAllResults (void) |
static void | PrintTiming (double elapsed_msec) |
PGresult * | PSQLexec (const char *query) |
int | PSQLexecWatch (const char *query, const printQueryOpt *opt, FILE *printQueryFout) |
static void | PrintNotifications (void) |
static bool | PrintQueryTuples (const PGresult *result, const printQueryOpt *opt, FILE *printQueryFout) |
static bool | StoreQueryTuple (const PGresult *result) |
static bool | ExecQueryTuples (const PGresult *result) |
static bool | HandleCopyResult (PGresult **resultp, FILE *copystream) |
static void | PrintQueryStatus (PGresult *result, FILE *printQueryFout) |
static bool | PrintQueryResult (PGresult *result, bool last, const printQueryOpt *opt, FILE *printQueryFout, FILE *printStatusFout) |
bool | SendQuery (const char *query) |
static const char * | skip_white_space (const char *query) |
bool | is_superuser (void) |
bool | standard_strings (void) |
const char * | session_username (void) |
void | expand_tilde (char **filename) |
static int | uri_prefix_length (const char *connstr) |
bool | recognized_connection_string (const char *connstr) |
Variables | |
volatile sig_atomic_t | sigint_interrupt_enabled = false |
sigjmp_buf | sigint_interrupt_jmp |
Definition at line 364 of file common.c.
References CheckConnection(), _psqlSettings::db, error(), pg_log_error, pg_log_info, PGRES_BAD_RESPONSE, PGRES_COMMAND_OK, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_EMPTY_QUERY, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PGRES_TUPLES_OK, PQerrorMessage(), PQresultStatus(), and pset.
Referenced by DescribeQuery(), ExecQueryAndProcessResults(), ExecQueryUsingCursor(), and PSQLexec().
|
static |
Definition at line 303 of file common.c.
References _, connection_warnings(), ConnectionUp(), _psqlSettings::cur_cmd_interactive, _psqlSettings::db, _psqlSettings::dead_conn, exit(), EXIT_BADCONN, fprintf, pg_log_error, PQfinish(), PQreset(), pset, ResetCancelConn(), SyncVariables(), and UnsyncVariables().
Referenced by AcceptResult(), and ExecQueryAndProcessResults().
|
static |
Definition at line 507 of file common.c.
References ClearOrSaveResult(), _psqlSettings::db, PQgetResult(), and pset.
Referenced by ExecQueryAndProcessResults().
|
static |
Definition at line 483 of file common.c.
References _psqlSettings::last_error_result, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PQclear(), PQresultStatus(), and pset.
Referenced by ClearOrSaveAllResults(), DescribeQuery(), ExecQueryAndProcessResults(), ExecQueryUsingCursor(), PSQLexec(), and SendQuery().
|
static |
Definition at line 2026 of file common.c.
References _psqlSettings::encoding, pg_strncasecmp(), PQmblenBounded(), pset, and skip_white_space().
Referenced by SendQuery().
|
static |
Definition at line 285 of file common.c.
References CONNECTION_BAD, _psqlSettings::db, PQstatus(), and pset.
Referenced by CheckConnection(), and SendQuery().
|
static |
Definition at line 1287 of file common.c.
References _, AcceptResult(), appendPQExpBuffer(), appendPQExpBufferStr(), before(), buf, ClearOrSaveResult(), _psqlSettings::db, fprintf, gettext_noop, i, initPQExpBuffer(), INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, INSTR_TIME_SUBTRACT, name, pg_log_info, PGRES_COMMAND_OK, PQclear(), PQdescribePrepared(), PQerrorMessage(), PQescapeLiteral(), PQexec(), PQfmod(), PQfname(), PQfreemem(), PQftype(), PQnfields(), PQprepare(), PQresultStatus(), printfPQExpBuffer(), PrintQueryResult(), pset, _psqlSettings::queryFout, SetResultVariables(), termPQExpBuffer(), and _psqlSettings::timing.
Referenced by SendQuery().
|
static |
Definition at line 1416 of file common.c.
References AcceptResult(), Assert(), before(), _psqlSettings::bind_flag, _psqlSettings::bind_nparams, _psqlSettings::bind_params, cancel_pressed, CheckConnection(), ClearOrSaveAllResults(), ClearOrSaveResult(), _psqlSettings::copyStream, _psqlSettings::db, disable_sigpipe_trap(), error(), _psqlSettings::gfname, HandleCopyResult(), INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, INSTR_TIME_SUBTRACT, openQueryOutputFile(), pg_log_info, PGRES_COPY_BOTH, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_TUPLES_OK, PQcmdStatus(), PQerrorMessage(), PQgetResult(), PQresultErrorMessage(), PQresultStatus(), PQsendQuery(), PQsendQueryParams(), PrintQueryResult(), pset, _psqlSettings::queryFout, restore_sigpipe_trap(), SetResultVariables(), SetShellResultVariables(), success, and _psqlSettings::timing.
Referenced by PSQLexecWatch(), and SendQuery().
Definition at line 786 of file common.c.
References cancel_pressed, _psqlSettings::echo, fflush(), _psqlSettings::gexec_flag, _psqlSettings::on_error_stop, PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), pset, PSQL_ECHO_ALL, SendQuery(), _psqlSettings::singlestep, generate_unaccent_rules::stdout, and success.
Referenced by PrintQueryResult().
|
static |
Definition at line 1701 of file common.c.
References AcceptResult(), appendPQExpBuffer(), Assert(), before(), buf, cancel_pressed, cleanup(), ClearOrSaveResult(), ClosePager(), _psqlSettings::db, disable_sigpipe_trap(), _psqlSettings::fetch_count, fflush(), _psqlSettings::gfname, _psqlSettings::gset_prefix, initPQExpBuffer(), INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, INSTR_TIME_SUBTRACT, INT64_FORMAT, _psqlSettings::logfile, openQueryOutputFile(), PageOutput(), PGRES_COMMAND_OK, PGRES_TUPLES_OK, _psqlSettings::popt, PQclear(), PQexec(), PQntuples(), PQresultStatus(), PQTRANS_IDLE, PQtransactionStatus(), printQuery(), printTableOpt::prior_records, pset, _psqlSettings::queryFout, restore_sigpipe_trap(), SetResultVariables(), SetShellResultVariables(), SetVariable(), snprintf, printTableOpt::start_table, generate_unaccent_rules::stdout, printTableOpt::stop_table, StoreQueryTuple(), termPQExpBuffer(), _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.
Referenced by SendQuery().
void expand_tilde | ( | char ** | filename | ) |
Definition at line 2339 of file common.c.
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().
Definition at line 865 of file common.c.
References Assert(), _psqlSettings::copyStream, _psqlSettings::cur_cmd_source, _psqlSettings::db, handleCopyIn(), handleCopyOut(), PGRES_COPY_IN, PGRES_COPY_OUT, PQbinaryTuples(), PQclear(), PQresultStatus(), pset, _psqlSettings::queryFout, ResetCancelConn(), SetCancelConn(), and success.
Referenced by ExecQueryAndProcessResults().
|
static |
Definition at line 2241 of file common.c.
References _psqlSettings::encoding, pg_strncasecmp(), PQmblenBounded(), pset, and skip_white_space().
Referenced by SendQuery().
bool is_superuser | ( | void | ) |
Definition at line 2278 of file common.c.
References _psqlSettings::db, PQparameterStatus(), pset, and val.
void NoticeProcessor | ( | void * | arg, |
const char * | message | ||
) |
Definition at line 228 of file common.c.
References arg, and pg_log_info.
Referenced by do_connect(), and main().
Definition at line 57 of file common.c.
References fflush(), pg_log_error, and generate_unaccent_rules::stdout.
Referenced by ExecQueryAndProcessResults(), ExecQueryUsingCursor(), and setQFout().
|
static |
Definition at line 665 of file common.c.
References _, pgNotify::be_pid, _psqlSettings::db, pgNotify::extra, fflush(), fprintf, PQconsumeInput(), PQfreemem(), PQnotifies(), pset, _psqlSettings::queryFout, and pgNotify::relname.
Referenced by SendQuery().
|
static |
Definition at line 960 of file common.c.
References _psqlSettings::crosstab_flag, ExecQueryTuples(), _psqlSettings::gexec_flag, _psqlSettings::gset_prefix, pg_log_error, PGRES_BAD_RESPONSE, PGRES_COMMAND_OK, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_EMPTY_QUERY, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PGRES_TUPLES_OK, PQcmdStatus(), PQresultStatus(), PrintQueryStatus(), PrintQueryTuples(), PrintResultInCrosstab(), pset, _psqlSettings::show_all_results, StoreQueryTuple(), and success.
Referenced by DescribeQuery(), and ExecQueryAndProcessResults().
|
static |
Definition at line 922 of file common.c.
References buf, fflush(), printTableOpt::format, fprintf, html_escaped_print(), _psqlSettings::logfile, _psqlSettings::popt, PQcmdStatus(), PQoidValue(), PRINT_HTML, pset, _psqlSettings::queryFout, _psqlSettings::quiet, SetVariable(), snprintf, printQueryOpt::topt, and _psqlSettings::vars.
Referenced by PrintQueryResult().
|
static |
Definition at line 698 of file common.c.
References fflush(), _psqlSettings::logfile, pg_log_error, _psqlSettings::popt, printQuery(), pset, and _psqlSettings::queryFout.
Referenced by PrintQueryResult().
|
static |
Definition at line 521 of file common.c.
References _, days, and printf.
Referenced by PSQLexecWatch(), and SendQuery().
|
static |
Definition at line 258 of file common.c.
References cancel_pressed, sigint_interrupt_enabled, and sigint_interrupt_jmp.
Referenced by psql_setup_cancel_handler().
char* psql_get_variable | ( | const char * | varname, |
PsqlScanQuoteType | quote, | ||
void * | passthrough | ||
) |
Definition at line 137 of file common.c.
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.
void psql_setup_cancel_handler | ( | void | ) |
Definition at line 274 of file common.c.
References psql_cancel_callback(), and setup_cancel_handler().
Referenced by main().
PGresult* PSQLexec | ( | const char * | query | ) |
Definition at line 580 of file common.c.
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().
int PSQLexecWatch | ( | const char * | query, |
const printQueryOpt * | opt, | ||
FILE * | printQueryFout | ||
) |
Definition at line 635 of file common.c.
References _psqlSettings::db, ExecQueryAndProcessResults(), pg_log_error, PrintTiming(), pset, res, ResetCancelConn(), SetCancelConn(), and _psqlSettings::timing.
Referenced by do_watch().
static bool recognized_connection_string | ( | const char * | connstr | ) |
Definition at line 2424 of file common.c.
References connstr, and uri_prefix_length().
Referenced by do_connect().
bool SendQuery | ( | const char * | query | ) |
Definition at line 1042 of file common.c.
References _, _psqlSettings::autocommit, _psqlSettings::bind_flag, _psqlSettings::bind_nparams, _psqlSettings::bind_params, 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().
const char* session_username | ( | void | ) |
Definition at line 2318 of file common.c.
References _psqlSettings::db, PQparameterStatus(), PQuser(), pset, and val.
Referenced by get_prompt().
bool setQFout | ( | const char * | fname | ) |
Definition at line 93 of file common.c.
References openQueryOutputFile(), pset, _psqlSettings::queryFout, _psqlSettings::queryFoutPipe, restore_sigpipe_trap(), set_sigpipe_trap_state(), SetShellResultVariables(), and generate_unaccent_rules::stdout.
Referenced by exec_command_out(), main(), and parse_psql_options().
Definition at line 421 of file common.c.
References PG_DIAG_MESSAGE_PRIMARY, PG_DIAG_SQLSTATE, PQcmdTuples(), PQresultErrorField(), pset, SetVariable(), success, and _psqlSettings::vars.
Referenced by DescribeQuery(), ExecQueryAndProcessResults(), and ExecQueryUsingCursor().
void SetShellResultVariables | ( | int | wait_result | ) |
Definition at line 461 of file common.c.
References buf, pset, SetVariable(), snprintf, _psqlSettings::vars, and wait_result_to_exit_code().
Referenced by do_copy(), do_shell(), exec_command_write(), ExecQueryAndProcessResults(), ExecQueryUsingCursor(), and setQFout().
|
static |
Definition at line 1962 of file common.c.
References _psqlSettings::encoding, PQmblenBounded(), and pset.
Referenced by command_no_begin(), and is_select_command().
bool standard_strings | ( | void | ) |
Definition at line 2298 of file common.c.
References _psqlSettings::db, PQparameterStatus(), pset, and val.
Referenced by get_create_object_cmd(), main(), MainLoop(), and parse_slash_copy().
Definition at line 722 of file common.c.
References free, _psqlSettings::gset_prefix, i, pg_log_error, pg_log_warning, PQfname(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), pset, psprintf(), SetVariable(), success, value, VariableHasHook(), and _psqlSettings::vars.
Referenced by ExecQueryUsingCursor(), and PrintQueryResult().
|
static |
Definition at line 2397 of file common.c.
References connstr, short_uri_designator, and uri_designator.
Referenced by recognized_connection_string().
volatile sig_atomic_t sigint_interrupt_enabled = false |
Definition at line 253 of file common.c.
Referenced by do_watch(), exec_command_password(), exec_command_prompt(), gets_fromFile(), gets_interactive(), handleCopyIn(), MainLoop(), prompt_for_password(), and psql_cancel_callback().
sigjmp_buf sigint_interrupt_jmp |
Definition at line 255 of file common.c.
Referenced by do_watch(), exec_command_password(), exec_command_prompt(), handleCopyIn(), MainLoop(), prompt_for_password(), and psql_cancel_callback().