From 5a7dbb38167c226dcc093e5190b0ab4437c138df Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Wed, 28 Nov 2018 11:24:53 +0900
Subject: [PATCH 2/2] Add psql -g option to control debug print

Added -g option and related help messages. TABCOMPLETION_DEBUG was
removed and the previously ifdef'ed code became always active.
---
 src/bin/psql/help.c         |  2 +-
 src/bin/psql/settings.h     |  1 +
 src/bin/psql/startup.c      | 12 +++++++++++-
 src/bin/psql/tab-complete.c | 14 +-------------
 4 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 2e9fe760eb..43f224f3e8 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -123,7 +123,7 @@ usage(unsigned short int pager)
 					  "                           set field separator for unaligned output to zero byte\n"));
 	fprintf(output, _("  -0, --record-separator-zero\n"
 					  "                           set record separator for unaligned output to zero byte\n"));
-
+	fprintf(output, _("  -g, --debug              turn on debug print to session log file\n"));
 	fprintf(output, _("\nConnection options:\n"));
 	/* Display default host */
 	env = getenv("PGHOST");
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 176c85afd0..c0bc96bf20 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -140,6 +140,7 @@ typedef struct _psqlSettings
 	const char *prompt3;
 	PGVerbosity verbosity;		/* current error verbosity level */
 	PGContextVisibility show_context;	/* current context display level */
+	bool		debug;
 } PsqlSettings;
 
 extern PsqlSettings pset;
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index e7536a8a06..25f78bcc2c 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -310,6 +310,12 @@ main(int argc, char *argv[])
 			exit(EXIT_FAILURE);
 		}
 	}
+	else if (pset.debug)
+	{
+		fprintf(stderr, _("%s: no session log file, turn off debug print\n"),
+				pset.progname);
+		pset.debug = false;
+	}
 
 	if (!options.no_psqlrc)
 		process_psqlrc(argv[0]);
@@ -440,6 +446,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
 		{"no-align", no_argument, NULL, 'A'},
 		{"command", required_argument, NULL, 'c'},
 		{"dbname", required_argument, NULL, 'd'},
+		{"debug", no_argument, NULL, 'g'},
 		{"echo-queries", no_argument, NULL, 'e'},
 		{"echo-errors", no_argument, NULL, 'b'},
 		{"echo-hidden", no_argument, NULL, 'E'},
@@ -480,7 +487,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
 
 	memset(options, 0, sizeof *options);
 
-	while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
+	while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:gh:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
 							long_options, &optindex)) != -1)
 	{
 		switch (c)
@@ -518,6 +525,9 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
 										  ACT_FILE,
 										  optarg);
 				break;
+			case 'g':
+				pset.debug = true;
+				break;
 			case 'F':
 				pset.popt.topt.fieldSep.separator = pg_strdup(optarg);
 				pset.popt.topt.fieldSep.separator_zero = false;
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 8addca60a6..e034ff6da2 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -56,17 +56,6 @@
 extern char *filename_completion_function();
 #endif
 
-#ifdef HAVE_RL_COMPLETION_MATCHES
-#define completion_matches rl_completion_matches
-#endif
-
-/*
- * By enabling TABCOMPLETION_DEBUG, every completion attempt is logged in
- * session log file if any.
- */
-#ifdef TABCOMPLETION_DEBUG
-
-#undef completion_matches
 #define completion_matches(text, func) \
 	completion_debug(__LINE__, (text), (func), \
 					 previous_words, previous_words_count)
@@ -83,7 +72,7 @@ completion_debug(int line, const char *text, rl_compentry_func_t *func,
 	char **list = completion_matches(text, func);
 #endif
 
-	if (pset.logfile)
+	if (pset.debug && pset.logfile)
 	{
 		/* Emit completion log */
 
@@ -119,7 +108,6 @@ completion_debug(int line, const char *text, rl_compentry_func_t *func,
 
 	return list;
 }
-#endif	/* TABCOMPLETION_DEBUG */
 
 /* word break characters */
 #define WORD_BREAKS		"\t\n@$><=;|&{() "
-- 
2.16.3

