Skip to content

Commit 0434c46

Browse files
committed
Invent an assign-hook mechanism for psql variables similar to the one
existing for backend GUC variables, and use this to eliminate repeated fetching/parsing of psql variables in psql's inner loops. In a trivial test with lots of 'select 1;' commands, psql's CPU time went down almost 10%, although of course the effect on total elapsed time was much less. Per discussion about how to ensure the upcoming FETCH_COUNT patch doesn't cost any performance when not being used.
1 parent b681bfd commit 0434c46

File tree

12 files changed

+407
-266
lines changed

12 files changed

+407
-266
lines changed

src/bin/psql/command.c

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.171 2006/07/18 17:42:01 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.172 2006/08/29 15:19:50 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -55,8 +55,6 @@ static backslashResult exec_command(const char *cmd,
5555
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf);
5656
static bool do_connect(char *dbname, char *user, char *host, char *port);
5757
static bool do_shell(const char *command);
58-
static void SyncVerbosityVariable(void);
59-
6058

6159

6260
/*----------
@@ -196,7 +194,6 @@ exec_command(const char *cmd,
196194
{
197195
bool success = true; /* indicate here if the command ran ok or
198196
* failed */
199-
bool quiet = QUIET();
200197
backslashResult status = PSQL_CMD_SKIP_LINE;
201198

202199
/*
@@ -206,9 +203,9 @@ exec_command(const char *cmd,
206203
if (strcmp(cmd, "a") == 0)
207204
{
208205
if (pset.popt.topt.format != PRINT_ALIGNED)
209-
success = do_pset("format", "aligned", &pset.popt, quiet);
206+
success = do_pset("format", "aligned", &pset.popt, pset.quiet);
210207
else
211-
success = do_pset("format", "unaligned", &pset.popt, quiet);
208+
success = do_pset("format", "unaligned", &pset.popt, pset.quiet);
212209
}
213210

214211
/* \C -- override table title (formerly change HTML caption) */
@@ -217,7 +214,7 @@ exec_command(const char *cmd,
217214
char *opt = psql_scan_slash_option(scan_state,
218215
OT_NORMAL, NULL, true);
219216

220-
success = do_pset("title", opt, &pset.popt, quiet);
217+
success = do_pset("title", opt, &pset.popt, pset.quiet);
221218
free(opt);
222219
}
223220

@@ -493,7 +490,7 @@ exec_command(const char *cmd,
493490
char *fname = psql_scan_slash_option(scan_state,
494491
OT_NORMAL, NULL, false);
495492

496-
success = do_pset("fieldsep", fname, &pset.popt, quiet);
493+
success = do_pset("fieldsep", fname, &pset.popt, pset.quiet);
497494
free(fname);
498495
}
499496

@@ -528,9 +525,9 @@ exec_command(const char *cmd,
528525
else if (strcmp(cmd, "H") == 0 || strcmp(cmd, "html") == 0)
529526
{
530527
if (pset.popt.topt.format != PRINT_HTML)
531-
success = do_pset("format", "html", &pset.popt, quiet);
528+
success = do_pset("format", "html", &pset.popt, pset.quiet);
532529
else
533-
success = do_pset("format", "aligned", &pset.popt, quiet);
530+
success = do_pset("format", "aligned", &pset.popt, pset.quiet);
534531
}
535532

536533

@@ -638,7 +635,7 @@ exec_command(const char *cmd,
638635
{
639636
if (query_buf && query_buf->len > 0)
640637
puts(query_buf->data);
641-
else if (!quiet)
638+
else if (!pset.quiet)
642639
puts(_("Query buffer is empty."));
643640
fflush(stdout);
644641
}
@@ -712,7 +709,7 @@ exec_command(const char *cmd,
712709
success = false;
713710
}
714711
else
715-
success = do_pset(opt0, opt1, &pset.popt, quiet);
712+
success = do_pset(opt0, opt1, &pset.popt, pset.quiet);
716713

717714
free(opt0);
718715
free(opt1);
@@ -727,7 +724,7 @@ exec_command(const char *cmd,
727724
{
728725
resetPQExpBuffer(query_buf);
729726
psql_scan_reset(scan_state);
730-
if (!quiet)
727+
if (!pset.quiet)
731728
puts(_("Query buffer reset (cleared)."));
732729
}
733730

@@ -740,7 +737,7 @@ exec_command(const char *cmd,
740737
expand_tilde(&fname);
741738
/* This scrolls off the screen when using /dev/tty */
742739
success = saveHistory(fname ? fname : DEVTTY, false);
743-
if (success && !quiet && fname)
740+
if (success && !pset.quiet && fname)
744741
printf(gettext("Wrote history to file \"%s/%s\".\n"),
745742
pset.dirname ? pset.dirname : ".", fname);
746743
if (!fname)
@@ -786,13 +783,7 @@ exec_command(const char *cmd,
786783
free(opt);
787784
}
788785

789-
if (SetVariable(pset.vars, opt0, newval))
790-
{
791-
/* Check for special variables */
792-
if (strcmp(opt0, "VERBOSITY") == 0)
793-
SyncVerbosityVariable();
794-
}
795-
else
786+
if (!SetVariable(pset.vars, opt0, newval))
796787
{
797788
psql_error("\\%s: error\n", cmd);
798789
success = false;
@@ -804,7 +795,7 @@ exec_command(const char *cmd,
804795

805796
/* \t -- turn off headers and row count */
806797
else if (strcmp(cmd, "t") == 0)
807-
success = do_pset("tuples_only", NULL, &pset.popt, quiet);
798+
success = do_pset("tuples_only", NULL, &pset.popt, pset.quiet);
808799

809800

810801
/* \T -- define html <table ...> attributes */
@@ -813,15 +804,15 @@ exec_command(const char *cmd,
813804
char *value = psql_scan_slash_option(scan_state,
814805
OT_NORMAL, NULL, false);
815806

816-
success = do_pset("tableattr", value, &pset.popt, quiet);
807+
success = do_pset("tableattr", value, &pset.popt, pset.quiet);
817808
free(value);
818809
}
819810

820811
/* \timing -- toggle timing of queries */
821812
else if (strcmp(cmd, "timing") == 0)
822813
{
823814
pset.timing = !pset.timing;
824-
if (!quiet)
815+
if (!pset.quiet)
825816
{
826817
if (pset.timing)
827818
puts(_("Timing is on."));
@@ -916,7 +907,7 @@ exec_command(const char *cmd,
916907

917908
/* \x -- toggle expanded table representation */
918909
else if (strcmp(cmd, "x") == 0)
919-
success = do_pset("expanded", NULL, &pset.popt, quiet);
910+
success = do_pset("expanded", NULL, &pset.popt, pset.quiet);
920911

921912
/* \z -- list table rights (equivalent to \dp) */
922913
else if (strcmp(cmd, "z") == 0)
@@ -1114,7 +1105,7 @@ do_connect(char *dbname, char *user, char *host, char *port)
11141105
SyncVariables();
11151106

11161107
/* Tell the user about the new connection */
1117-
if (!QUIET())
1108+
if (!pset.quiet)
11181109
{
11191110
printf(_("You are now connected to database \"%s\""), PQdb(pset.db));
11201111

@@ -1148,6 +1139,7 @@ SyncVariables(void)
11481139
/* get stuff from connection */
11491140
pset.encoding = PQclientEncoding(pset.db);
11501141
pset.popt.topt.encoding = pset.encoding;
1142+
pset.sversion = PQserverVersion(pset.db);
11511143

11521144
SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
11531145
SetVariable(pset.vars, "USER", PQuser(pset.db));
@@ -1156,7 +1148,7 @@ SyncVariables(void)
11561148
SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
11571149

11581150
/* send stuff to it, too */
1159-
SyncVerbosityVariable();
1151+
PQsetErrorVerbosity(pset.db, pset.verbosity);
11601152
}
11611153

11621154
/*
@@ -1174,32 +1166,6 @@ UnsyncVariables(void)
11741166
SetVariable(pset.vars, "ENCODING", NULL);
11751167
}
11761168

1177-
/*
1178-
* Update connection state from VERBOSITY variable
1179-
*/
1180-
static void
1181-
SyncVerbosityVariable(void)
1182-
{
1183-
switch (SwitchVariable(pset.vars, "VERBOSITY",
1184-
"default", "terse", "verbose", NULL))
1185-
{
1186-
case 1: /* default */
1187-
pset.verbosity = PQERRORS_DEFAULT;
1188-
break;
1189-
case 2: /* terse */
1190-
pset.verbosity = PQERRORS_TERSE;
1191-
break;
1192-
case 3: /* verbose */
1193-
pset.verbosity = PQERRORS_VERBOSE;
1194-
break;
1195-
default: /* not set or unrecognized value */
1196-
pset.verbosity = PQERRORS_DEFAULT;
1197-
break;
1198-
}
1199-
1200-
PQsetErrorVerbosity(pset.db, pset.verbosity);
1201-
}
1202-
12031169

12041170
/*
12051171
* do_edit -- handler for \e

src/bin/psql/common.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.125 2006/08/25 04:06:54 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.126 2006/08/29 15:19:50 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -515,16 +515,14 @@ PGresult *
515515
PSQLexec(const char *query, bool start_xact)
516516
{
517517
PGresult *res;
518-
int echo_hidden;
519518

520519
if (!pset.db)
521520
{
522521
psql_error("You are currently not connected to a database.\n");
523522
return NULL;
524523
}
525524

526-
echo_hidden = SwitchVariable(pset.vars, "ECHO_HIDDEN", "noexec", NULL);
527-
if (echo_hidden != VAR_NOTSET)
525+
if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
528526
{
529527
printf(_("********* QUERY **********\n"
530528
"%s\n"
@@ -539,14 +537,15 @@ PSQLexec(const char *query, bool start_xact)
539537
fflush(pset.logfile);
540538
}
541539

542-
if (echo_hidden == 1) /* noexec? */
540+
if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
543541
return NULL;
544542
}
545543

546544
SetCancelConn();
547545

548-
if (start_xact && PQtransactionStatus(pset.db) == PQTRANS_IDLE &&
549-
!GetVariableBool(pset.vars, "AUTOCOMMIT"))
546+
if (start_xact &&
547+
!pset.autocommit &&
548+
PQtransactionStatus(pset.db) == PQTRANS_IDLE)
550549
{
551550
res = PQexec(pset.db, "BEGIN");
552551
if (PQresultStatus(res) != PGRES_COMMAND_OK)
@@ -693,7 +692,7 @@ PrintQueryStatus(PGresult *results)
693692
{
694693
char buf[16];
695694

696-
if (!QUIET())
695+
if (!pset.quiet)
697696
{
698697
if (pset.popt.topt.format == PRINT_HTML)
699698
{
@@ -789,15 +788,14 @@ SendQuery(const char *query)
789788
on_error_rollback_savepoint = false;
790789
PGTransactionStatusType transaction_status;
791790
static bool on_error_rollback_warning = false;
792-
const char *rollback_str;
793791

794792
if (!pset.db)
795793
{
796794
psql_error("You are currently not connected to a database.\n");
797795
return false;
798796
}
799797

800-
if (GetVariableBool(pset.vars, "SINGLESTEP"))
798+
if (pset.singlestep)
801799
{
802800
char buf[3];
803801

@@ -810,7 +808,7 @@ SendQuery(const char *query)
810808
if (buf[0] == 'x')
811809
return false;
812810
}
813-
else if (VariableEquals(pset.vars, "ECHO", "queries"))
811+
else if (pset.echo == PSQL_ECHO_QUERIES)
814812
{
815813
puts(query);
816814
fflush(stdout);
@@ -830,7 +828,7 @@ SendQuery(const char *query)
830828
transaction_status = PQtransactionStatus(pset.db);
831829

832830
if (transaction_status == PQTRANS_IDLE &&
833-
!GetVariableBool(pset.vars, "AUTOCOMMIT") &&
831+
!pset.autocommit &&
834832
!command_no_begin(query))
835833
{
836834
results = PQexec(pset.db, "BEGIN");
@@ -846,11 +844,9 @@ SendQuery(const char *query)
846844
}
847845

848846
if (transaction_status == PQTRANS_INTRANS &&
849-
(rollback_str = GetVariable(pset.vars, "ON_ERROR_ROLLBACK")) != NULL &&
850-
/* !off and !interactive is 'on' */
851-
pg_strcasecmp(rollback_str, "off") != 0 &&
847+
pset.on_error_rollback != PSQL_ERROR_ROLLBACK_OFF &&
852848
(pset.cur_cmd_interactive ||
853-
pg_strcasecmp(rollback_str, "interactive") != 0))
849+
pset.on_error_rollback == PSQL_ERROR_ROLLBACK_ON))
854850
{
855851
if (on_error_rollback_warning == false && pset.sversion < 80000)
856852
{

src/bin/psql/copy.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.66 2006/06/14 16:49:02 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.67 2006/08/29 15:19:50 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "copy.h"
@@ -704,7 +704,7 @@ handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary)
704704
/* Prompt if interactive input */
705705
if (isatty(fileno(copystream)))
706706
{
707-
if (!QUIET())
707+
if (!pset.quiet)
708708
puts(_("Enter data to be copied followed by a newline.\n"
709709
"End with a backslash and a period on a line by itself."));
710710
prompt = get_prompt(PROMPT_COPY);

src/bin/psql/describe.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.143 2006/08/25 04:06:54 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.144 2006/08/29 15:19:51 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -673,7 +673,7 @@ describeTableDetails(const char *pattern, bool verbose)
673673

674674
if (PQntuples(res) == 0)
675675
{
676-
if (!QUIET())
676+
if (!pset.quiet)
677677
fprintf(stderr, _("Did not find any relation named \"%s\".\n"),
678678
pattern);
679679
PQclear(res);
@@ -768,7 +768,7 @@ describeOneTableDetails(const char *schemaname,
768768
/* Did we get anything? */
769769
if (PQntuples(res) == 0)
770770
{
771-
if (!QUIET())
771+
if (!pset.quiet)
772772
fprintf(stderr, _("Did not find any relation with OID %s.\n"),
773773
oid);
774774
goto error_return;
@@ -1582,7 +1582,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose)
15821582
if (!res)
15831583
return false;
15841584

1585-
if (PQntuples(res) == 0 && !QUIET())
1585+
if (PQntuples(res) == 0 && !pset.quiet)
15861586
{
15871587
if (pattern)
15881588
fprintf(pset.queryFout, _("No matching relations found.\n"));

0 commit comments

Comments
 (0)