On 11/15/2014 05:56 PM, Andrew Dunstan wrote:
On 11/13/2014 11:41 AM, Andrew Dunstan wrote:
On 11/13/2014 11:09 AM, Tom Lane wrote:
Andrew Dunstan <and...@dunslane.net> writes:
I often get annoyed because psql is a bit too aggressive when it
decides
whether to put output through the pager, and the only way to avoid
this
is to turn the pager off (in which case your next query might dump
many
thousands of lines to the screen). I'd like a way to be able to
specify
a minumum number of lines of output before psql would invoke the
pager,
rather than just always using the terminal window size.
Are you saying you'd want to set the threshold to *more* than the
window
height? Why?
Because I might be quite happy with 100 or 200 lines I can just
scroll in my terminal's scroll buffer, but want to use the pager for
more than that. This is useful especially if I want to scroll back
and see the results from a query or two ago.
This patch shows more or less what I had in mind.
However, there is more work to do. As Tom noted upthread, psql's
calculation of the number of lines is pretty bad. For example, if I do:
\pset pager_min_lines 100
select * from generate_series(1,50);
the pager still gets invoked, which is unfortunate to say the least.
So I'm going to take a peek at that.
The over-eager invocation of the pager due to double counting of lines
got fixed recently, so here's a slightly updated patch for a
pager_min_lines setting, including docco.
cheers
andrew
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index e7fcc73..4201847 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -2236,6 +2236,18 @@ lo_import 152801
</varlistentry>
<varlistentry>
+ <term><literal>pager_min_lines</literal></term>
+ <listitem>
+ <para>
+ If <literal>pager_min_lines</> is set to a number greater than the
+ page height, the pager program will not be called unless there are
+ at least this many lines of output to show. The default setting
+ is 0.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><literal>recordsep</literal></term>
<listitem>
<para>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index c7a17d7..2703850 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -807,7 +807,7 @@ exec_command(const char *cmd,
opt[--len] = '\0';
}
- helpSQL(opt, pset.popt.topt.pager);
+ helpSQL(opt, pset.popt.topt.pager, pset.popt.topt.pager_min_lines);
free(opt);
}
@@ -1074,7 +1074,8 @@ exec_command(const char *cmd,
static const char *const my_list[] = {
"border", "columns", "expanded", "fieldsep", "fieldsep_zero",
"footer", "format", "linestyle", "null",
- "numericlocale", "pager", "recordsep", "recordsep_zero",
+ "numericlocale", "pager", "pager_min_lines",
+ "recordsep", "recordsep_zero",
"tableattr", "title", "tuples_only",
"unicode_border_linestyle",
"unicode_column_linestyle",
@@ -1268,7 +1269,8 @@ exec_command(const char *cmd,
lines++;
}
- output = PageOutput(lineno, pset.popt.topt.pager);
+ output = PageOutput(lineno, pset.popt.topt.pager,
+ pset.popt.topt.pager_min_lines);
is_pager = true;
}
else
@@ -1520,13 +1522,13 @@ exec_command(const char *cmd,
OT_NORMAL, NULL, false);
if (!opt0 || strcmp(opt0, "commands") == 0)
- slashUsage(pset.popt.topt.pager);
+ slashUsage(pset.popt.topt.pager, pset.popt.topt.pager_min_lines);
else if (strcmp(opt0, "options") == 0)
- usage(pset.popt.topt.pager);
+ usage(pset.popt.topt.pager, pset.popt.topt.pager_min_lines);
else if (strcmp(opt0, "variables") == 0)
- helpVariables(pset.popt.topt.pager);
+ helpVariables(pset.popt.topt.pager, pset.popt.topt.pager_min_lines);
else
- slashUsage(pset.popt.topt.pager);
+ slashUsage(pset.popt.topt.pager, pset.popt.topt.pager_min_lines);
}
#if 0
@@ -2522,6 +2524,13 @@ do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
popt->topt.pager = 1;
}
+ /* set minimum lines for pager use */
+ else if (strcmp(param, "pager_min_lines") == 0)
+ {
+ if (value)
+ popt->topt.pager_min_lines = atoi(value);
+ }
+
/* disable "(x rows)" footer */
else if (strcmp(param, "footer") == 0)
{
@@ -2643,6 +2652,13 @@ printPsetInfo(const char *param, struct printQueryOpt *popt)
printf(_("Pager usage is off.\n"));
}
+ /* show minimum lines for pager use */
+ else if (strcmp(param, "pager_min_lines") == 0)
+ {
+ printf(_("Pager won't be used for less than %d lines\n"),
+ popt->topt.pager_min_lines);
+ }
+
/* show record separator for unaligned text */
else if (strcmp(param, "recordsep") == 0)
{
@@ -2795,6 +2811,8 @@ pset_value_string(const char *param, struct printQueryOpt *popt)
return pstrdup(pset_bool_string(popt->topt.numericLocale));
else if (strcmp(param, "pager") == 0)
return psprintf("%d", popt->topt.pager);
+ else if (strcmp(param, "pager_min_lines") == 0)
+ return psprintf("%d", popt->topt.pager_min_lines);
else if (strcmp(param, "recordsep") == 0)
return pset_quoted_string(popt->topt.recordSep.separator
? popt->topt.recordSep.separator
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 66d80b5..91102f4 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1337,7 +1337,8 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec)
* If query requires multiple result sets, hack to ensure that
* only one pager instance is used for the whole mess
*/
- pset.queryFout = PageOutput(100000, my_popt.topt.pager);
+ pset.queryFout = PageOutput(100000, my_popt.topt.pager,
+ my_popt.topt.pager_min_lines);
did_pager = true;
}
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index ae5fe88..bb166b9 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -46,7 +46,7 @@
#define ON(var) (var ? _("on") : _("off"))
void
-usage(unsigned short int pager)
+usage(unsigned short int pager, int pager_min_lines)
{
const char *env;
const char *user;
@@ -65,7 +65,7 @@ usage(unsigned short int pager)
}
}
- output = PageOutput(59, pager);
+ output = PageOutput(59, pager, pager_min_lines);
fprintf(output, _("psql is the PostgreSQL interactive terminal.\n\n"));
fprintf(output, _("Usage:\n"));
@@ -151,14 +151,14 @@ usage(unsigned short int pager)
* print out help for the backslash commands
*/
void
-slashUsage(unsigned short int pager)
+slashUsage(unsigned short int pager, int pager_min_lines)
{
FILE *output;
char *currdb;
currdb = PQdb(pset.db);
- output = PageOutput(103, pager);
+ output = PageOutput(103, pager, pager_min_lines);
/* if you add/remove a line here, change the row count above */
@@ -301,11 +301,11 @@ slashUsage(unsigned short int pager)
* show list of available variables (options) from command line
*/
void
-helpVariables(unsigned short int pager)
+helpVariables(unsigned short int pager, int pager_min_lines)
{
FILE *output;
- output = PageOutput(85, pager);
+ output = PageOutput(85, pager, pager_min_lines);
fprintf(output, _("List of specially treated variables.\n"));
@@ -406,7 +406,7 @@ helpVariables(unsigned short int pager)
* Note: we assume caller removed any trailing spaces in "topic".
*/
void
-helpSQL(const char *topic, unsigned short int pager)
+helpSQL(const char *topic, unsigned short int pager, int pager_min_lines)
{
#define VALUE_OR_NULL(a) ((a) ? (a) : "")
@@ -435,7 +435,7 @@ helpSQL(const char *topic, unsigned short int pager)
ncolumns = Max(ncolumns, 1);
nrows = (QL_HELP_COUNT + (ncolumns - 1)) / ncolumns;
- output = PageOutput(nrows + 1, pager);
+ output = PageOutput(nrows + 1, pager, pager_min_lines);
fputs(_("Available help:\n"), output);
@@ -488,7 +488,7 @@ helpSQL(const char *topic, unsigned short int pager)
if (wordlen >= len) /* Don't try again if the same word */
{
if (!output)
- output = PageOutput(nl_count, pager);
+ output = PageOutput(nl_count, pager, pager_min_lines);
break;
}
len = wordlen;
@@ -509,7 +509,7 @@ helpSQL(const char *topic, unsigned short int pager)
}
if (!output)
- output = PageOutput(nl_count, pager);
+ output = PageOutput(nl_count, pager, pager_min_lines);
for (i = 0; QL_HELP[i].cmd; i++)
{
diff --git a/src/bin/psql/help.h b/src/bin/psql/help.h
index 3ad374a..646d080 100644
--- a/src/bin/psql/help.h
+++ b/src/bin/psql/help.h
@@ -8,13 +8,14 @@
#ifndef HELP_H
#define HELP_H
-void usage(unsigned short int pager);
+void usage(unsigned short int pager, int pager_min_lines);
-void slashUsage(unsigned short int pager);
+void slashUsage(unsigned short int pager, int pager_min_lines);
-void helpVariables(unsigned short int pager);
+void helpVariables(unsigned short int pager, int pager_min_lines);
-void helpSQL(const char *topic, unsigned short int pager);
+void helpSQL(const char *topic, unsigned short int pager,
+ int pager_min_lines);
void print_copyright(void);
diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c
index 6416ab9..01111e8 100644
--- a/src/bin/psql/input.c
+++ b/src/bin/psql/input.c
@@ -479,7 +479,7 @@ printHistory(const char *fname, unsigned short int pager)
if (fname == NULL)
{
/* use pager, if enabled, when printing to console */
- output = PageOutput(INT_MAX, pager);
+ output = PageOutput(INT_MAX, pager, 0);
is_pager = true;
}
else
diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index fd26d6d..256c333 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -811,7 +811,8 @@ print_aligned_text(const printTableContent *cont, FILE *fout)
if (!is_pager && fout == stdout && output_columns > 0 &&
(output_columns < total_header_width || output_columns < width_total))
{
- fout = PageOutput(INT_MAX, cont->opt->pager); /* force pager */
+ fout = PageOutput(INT_MAX, cont->opt->pager,
+ cont->opt->pager_min_lines); /* force pager */
is_pager = true;
}
@@ -2488,7 +2489,7 @@ print_troff_ms_vertical(const printTableContent *cont, FILE *fout)
* Tests if pager is needed and returns appropriate FILE pointer.
*/
FILE *
-PageOutput(int lines, unsigned short int pager)
+PageOutput(int lines, unsigned short int pager, int min_lines)
{
/* check whether we need / can / are supposed to use pager */
if (pager && isatty(fileno(stdin)) && isatty(fileno(stdout)))
@@ -2503,7 +2504,9 @@ PageOutput(int lines, unsigned short int pager)
result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
/* >= accounts for a one-line prompt */
- if (result == -1 || lines >= screen_size.ws_row || pager > 1)
+ if (result == -1
+ || (lines >= screen_size.ws_row && lines >= min_lines)
+ || pager > 1)
{
#endif
pagerprog = getenv("PAGER");
@@ -2803,7 +2806,8 @@ IsPagerNeeded(const printTableContent *cont, const int extra_lines, bool expande
lines++;
}
- *fout = PageOutput(lines + extra_lines, cont->opt->pager);
+ *fout = PageOutput(lines + extra_lines, cont->opt->pager,
+ cont->opt->pager_min_lines);
*is_pager = (*fout != stdout);
}
else
diff --git a/src/bin/psql/print.h b/src/bin/psql/print.h
index f668b23..3510f7a 100644
--- a/src/bin/psql/print.h
+++ b/src/bin/psql/print.h
@@ -89,6 +89,8 @@ typedef struct printTableOpt
* 1=dividing lines, 2=full */
unsigned short int pager; /* use pager for output (if to stdout and
* stdout is a tty) 0=off 1=on 2=always */
+ int pager_min_lines;/* don't use pager unless there are at least
+ * this many lines */
bool tuples_only; /* don't output headers, row counts, etc. */
bool start_table; /* print start decoration, eg <table> */
bool stop_table; /* print stop decoration, eg </table> */
@@ -164,7 +166,7 @@ extern const printTextFormat pg_asciiformat_old;
extern const printTextFormat pg_utf8format;
-extern FILE *PageOutput(int lines, unsigned short int pager);
+extern FILE *PageOutput(int lines, unsigned short int pager, int min_lines);
extern void ClosePager(FILE *pagerpipe);
extern void html_escaped_print(const char *in, FILE *fout);
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 11a159a..174c481 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -100,7 +100,7 @@ main(int argc, char *argv[])
{
if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
{
- usage(NOPAGER);
+ usage(NOPAGER, 0);
exit(EXIT_SUCCESS);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
@@ -129,6 +129,7 @@ main(int argc, char *argv[])
pset.popt.topt.format = PRINT_ALIGNED;
pset.popt.topt.border = 1;
pset.popt.topt.pager = 1;
+ pset.popt.topt.pager_min_lines = 0;
pset.popt.topt.start_table = true;
pset.popt.topt.stop_table = true;
pset.popt.topt.default_footer = true;
@@ -569,7 +570,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
/* Actual help option given */
if (strcmp(argv[optind - 1], "-?") == 0)
{
- usage(NOPAGER);
+ usage(NOPAGER, 0);
exit(EXIT_SUCCESS);
}
/* unknown option reported by getopt */
@@ -579,11 +580,11 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
case 1:
{
if (!optarg || strcmp(optarg, "options") == 0)
- usage(NOPAGER);
+ usage(NOPAGER, 0);
else if (optarg && strcmp(optarg, "commands") == 0)
- slashUsage(NOPAGER);
+ slashUsage(NOPAGER, 0);
else if (optarg && strcmp(optarg, "variables") == 0)
- helpVariables(NOPAGER);
+ helpVariables(NOPAGER, 0);
else
goto unknown_option;
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers