From 180ff0bcc0304d093d2e1fc40886b4a0ff4b5d56 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <bossartn@amazon.com>
Date: Wed, 19 Dec 2018 01:18:02 +0000
Subject: [PATCH v1 2/4] Add --skip-locked option to vacuumdb.

---
 doc/src/sgml/ref/vacuumdb.sgml    | 16 ++++++++++++++++
 src/bin/scripts/t/100_vacuumdb.pl |  6 +++++-
 src/bin/scripts/vacuumdb.c        | 17 +++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/ref/vacuumdb.sgml b/doc/src/sgml/ref/vacuumdb.sgml
index 955a17a849..942c8ba874 100644
--- a/doc/src/sgml/ref/vacuumdb.sgml
+++ b/doc/src/sgml/ref/vacuumdb.sgml
@@ -167,6 +167,22 @@ PostgreSQL documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--skip-locked</option></term>
+      <listitem>
+       <para>
+        Skip relations that cannot be immediately locked for processing.
+       </para>
+       <note>
+        <para>
+         This option is available for servers running PostgreSQL 12 and later.
+         If specified for a server running an older version of PostgreSQL, this
+         option is silently ignored.
+        </para>
+       </note>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>-t <replaceable class="parameter">table</replaceable> [ (<replaceable class="parameter">column</replaceable> [,...]) ]</option></term>
       <term><option>--table=<replaceable class="parameter">table</replaceable> [ (<replaceable class="parameter">column</replaceable> [,...]) ]</option></term>
diff --git a/src/bin/scripts/t/100_vacuumdb.pl b/src/bin/scripts/t/100_vacuumdb.pl
index 4c477a27aa..239ed76834 100644
--- a/src/bin/scripts/t/100_vacuumdb.pl
+++ b/src/bin/scripts/t/100_vacuumdb.pl
@@ -3,7 +3,7 @@ use warnings;
 
 use PostgresNode;
 use TestLib;
-use Test::More tests => 23;
+use Test::More tests => 25;
 
 program_help_ok('vacuumdb');
 program_version_ok('vacuumdb');
@@ -33,6 +33,10 @@ $node->issues_sql_like(
 	[ 'vacuumdb', '-Z', 'postgres' ],
 	qr/statement: ANALYZE;/,
 	'vacuumdb -Z');
+$node->issues_sql_like(
+	[ 'vacuumdb', '--skip-locked', 'postgres' ],
+	qr/statement: VACUUM \(SKIP_LOCKED\);/,
+	'vacuumdb --skip-locked');
 $node->command_ok([qw(vacuumdb -Z --table=pg_am dbname=template1)],
 	'vacuumdb with connection string');
 
diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c
index 886895ed2f..5aa2833a73 100644
--- a/src/bin/scripts/vacuumdb.c
+++ b/src/bin/scripts/vacuumdb.c
@@ -40,6 +40,7 @@ typedef struct vacuumingOptions
 	bool		and_analyze;
 	bool		full;
 	bool		freeze;
+	bool		skip_locked;
 } vacuumingOptions;
 
 
@@ -110,6 +111,7 @@ main(int argc, char *argv[])
 		{"jobs", required_argument, NULL, 'j'},
 		{"maintenance-db", required_argument, NULL, 2},
 		{"analyze-in-stages", no_argument, NULL, 3},
+		{"skip-locked", no_argument, NULL, 4},
 		{NULL, 0, NULL, 0}
 	};
 
@@ -213,6 +215,9 @@ main(int argc, char *argv[])
 			case 3:
 				analyze_in_stages = vacopts.analyze_only = true;
 				break;
+			case 4:
+				vacopts.skip_locked = true;
+				break;
 			default:
 				fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
 				exit(1);
@@ -673,6 +678,17 @@ prepare_vacuum_command(PQExpBuffer sql, PGconn *conn,
 				appendPQExpBuffer(sql, "%sANALYZE", sep);
 				sep = comma;
 			}
+
+			/*
+			 * The SKIP_LOCKED option was added for VACUUM and ANALYZE in v12.
+			 * Silently ignore it if specified for an older version.
+			 */
+			if (vacopts->skip_locked && PQserverVersion(conn) >= 120000)
+			{
+				appendPQExpBuffer(sql, "%sSKIP_LOCKED", sep);
+				sep = comma;
+			}
+
 			if (sep != paren)
 				appendPQExpBufferChar(sql, ')');
 		}
@@ -1011,6 +1027,7 @@ help(const char *progname)
 	printf(_("  -F, --freeze                    freeze row transaction information\n"));
 	printf(_("  -j, --jobs=NUM                  use this many concurrent connections to vacuum\n"));
 	printf(_("  -q, --quiet                     don't write any messages\n"));
+	printf(_("      --skip-locked               skip relations that cannot be immediately locked\n"));
 	printf(_("  -t, --table='TABLE[(COLUMNS)]'  vacuum specific table(s) only\n"));
 	printf(_("  -v, --verbose                   write a lot of output\n"));
 	printf(_("  -V, --version                   output version information, then exit\n"));
-- 
2.16.5

