0003_add_nowait_vacuum_option_v1.patch

application/octet-stream

Filename: 0003_add_nowait_vacuum_option_v1.patch
Type: application/octet-stream
Part: 0
Message: Re: BUG #14941: Vacuum crashes

Patch

Format: unified
Series: patch v1
File+
doc/src/sgml/ref/analyze.sgml 12 0
doc/src/sgml/ref/vacuum.sgml 12 0
src/backend/parser/gram.y 2 0
src/include/nodes/parsenodes.h 1 1
src/test/isolation/expected/vacuum-nowait.out 64 0
src/test/isolation/isolation_schedule 1 0
src/test/isolation/specs/vacuum-nowait.spec 43 0
src/test/regress/expected/vacuum.out 4 1
src/test/regress/sql/vacuum.sql 5 1
diff --git a/doc/src/sgml/ref/analyze.sgml b/doc/src/sgml/ref/analyze.sgml
index 10b3a9a..05882c3 100644
--- a/doc/src/sgml/ref/analyze.sgml
+++ b/doc/src/sgml/ref/analyze.sgml
@@ -27,6 +27,7 @@ ANALYZE [ VERBOSE ] [ <replaceable class="parameter">table_and_columns</replacea
 <phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase>
 
     VERBOSE
+    NOWAIT
 
 <phrase>and <replaceable class="parameter">table_and_columns</replaceable> is:</phrase>
 
@@ -77,6 +78,17 @@ ANALYZE [ VERBOSE ] [ <replaceable class="parameter">table_and_columns</replacea
    </varlistentry>
 
    <varlistentry>
+    <term><literal>NOWAIT</literal></term>
+    <listitem>
+     <para>
+      Specifies that <command>ANALYZE</command> should not wait for any
+      conflicting locks to be released: if a relation cannot be locked
+      immediately without waiting, the relation is skipped.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
     <term><replaceable class="parameter">table_name</replaceable></term>
     <listitem>
      <para>
diff --git a/doc/src/sgml/ref/vacuum.sgml b/doc/src/sgml/ref/vacuum.sgml
index b760e8e..1d7ffef 100644
--- a/doc/src/sgml/ref/vacuum.sgml
+++ b/doc/src/sgml/ref/vacuum.sgml
@@ -31,6 +31,7 @@ VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ <replaceable class="paramet
     VERBOSE
     ANALYZE
     DISABLE_PAGE_SKIPPING
+    NOWAIT
 
 <phrase>and <replaceable class="parameter">table_and_columns</replaceable> is:</phrase>
 
@@ -161,6 +162,17 @@ VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ ANALYZE ] [ <replaceable class="paramet
    </varlistentry>
 
    <varlistentry>
+    <term><literal>NOWAIT</literal></term>
+    <listitem>
+     <para>
+      Specifies that <command>VACUUM</command> should not wait for any
+      conflicting locks to be released: if a relation cannot be locked
+      immediately without waiting, the relation is skipped.
+     </para>
+    </listitem>
+   </varlistentry>
+
+   <varlistentry>
     <term><replaceable class="parameter">table_name</replaceable></term>
     <listitem>
      <para>
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index f749722..c6cc833 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -10507,6 +10507,7 @@ vacuum_option_elem:
 							 errmsg("unrecognized VACUUM option \"%s\"", $1),
 									 parser_errposition(@1)));
 				}
+			| NOWAIT			{ $$ = VACOPT_NOWAIT; }
 		;
 
 AnalyzeStmt: analyze_keyword opt_verbose opt_vacuum_relation_list
@@ -10534,6 +10535,7 @@ analyze_option_list:
 
 analyze_option_elem:
 			VERBOSE				{ $$ = VACOPT_VERBOSE; }
+			| NOWAIT			{ $$ = VACOPT_NOWAIT; }
 		;
 
 analyze_keyword:
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index 2eaa6b2..27e06fb 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -3116,7 +3116,7 @@ typedef enum VacuumOption
 	VACOPT_VERBOSE = 1 << 2,	/* print progress info */
 	VACOPT_FREEZE = 1 << 3,		/* FREEZE option */
 	VACOPT_FULL = 1 << 4,		/* FULL (non-concurrent) vacuum */
-	VACOPT_NOWAIT = 1 << 5,		/* don't wait to get lock (autovacuum only) */
+	VACOPT_NOWAIT = 1 << 5,		/* don't wait to get lock */
 	VACOPT_SKIPTOAST = 1 << 6,	/* don't process the TOAST table, if any */
 	VACOPT_DISABLE_PAGE_SKIPPING = 1 << 7	/* don't skip any pages */
 } VacuumOption;
diff --git a/src/test/isolation/expected/vacuum-nowait.out b/src/test/isolation/expected/vacuum-nowait.out
new file mode 100644
index 0000000..89c1cf6
--- /dev/null
+++ b/src/test/isolation/expected/vacuum-nowait.out
@@ -0,0 +1,64 @@
+Parsed test spec with 2 sessions
+
+starting permutation: lock vac_specified commit
+step lock: 
+	BEGIN;
+	LOCK test1 IN SHARE MODE;
+
+WARNING:  skipping vacuum of "test1" --- lock not available
+step vac_specified: VACUUM (NOWAIT) test1, test2;
+step commit: 
+	COMMIT;
+
+
+starting permutation: lock vac_all commit
+step lock: 
+	BEGIN;
+	LOCK test1 IN SHARE MODE;
+
+step vac_all: VACUUM (NOWAIT);
+step commit: 
+	COMMIT;
+
+
+starting permutation: lock analyze_specified commit
+step lock: 
+	BEGIN;
+	LOCK test1 IN SHARE MODE;
+
+WARNING:  skipping analyze of "test1" --- lock not available
+step analyze_specified: ANALYZE (NOWAIT) test1, test2;
+step commit: 
+	COMMIT;
+
+
+starting permutation: lock analyze_all commit
+step lock: 
+	BEGIN;
+	LOCK test1 IN SHARE MODE;
+
+step analyze_all: ANALYZE (NOWAIT);
+step commit: 
+	COMMIT;
+
+
+starting permutation: lock vac_analyze_specified commit
+step lock: 
+	BEGIN;
+	LOCK test1 IN SHARE MODE;
+
+WARNING:  skipping vacuum of "test1" --- lock not available
+step vac_analyze_specified: VACUUM (ANALYZE, NOWAIT) test1, test2;
+step commit: 
+	COMMIT;
+
+
+starting permutation: lock vac_analyze_all commit
+step lock: 
+	BEGIN;
+	LOCK test1 IN SHARE MODE;
+
+step vac_analyze_all: VACUUM (ANALYZE, NOWAIT);
+step commit: 
+	COMMIT;
+
diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule
index e41b916..0ebbdbf 100644
--- a/src/test/isolation/isolation_schedule
+++ b/src/test/isolation/isolation_schedule
@@ -63,3 +63,4 @@ test: async-notify
 test: vacuum-reltuples
 test: timeouts
 test: vacuum-concurrent-drop
+test: vacuum-nowait
diff --git a/src/test/isolation/specs/vacuum-nowait.spec b/src/test/isolation/specs/vacuum-nowait.spec
new file mode 100644
index 0000000..cd3d623
--- /dev/null
+++ b/src/test/isolation/specs/vacuum-nowait.spec
@@ -0,0 +1,43 @@
+# Test for the NOWAIT option for VACUUM and ANALYZE commands.
+#
+# If a skipped relation is specified in the command, it should be logged.  If a
+# skipped relation was not explicitly specified in the command, it should not be
+# logged.
+
+setup
+{
+	CREATE TABLE test1 (a INT);
+	CREATE TABLE test2 (a INT);
+}
+
+teardown
+{
+	DROP TABLE IF EXISTS test1;
+	DROP TABLE IF EXISTS test2;
+}
+
+session "s1"
+step "lock"
+{
+	BEGIN;
+	LOCK test1 IN SHARE MODE;
+}
+step "commit"
+{
+	COMMIT;
+}
+
+session "s2"
+step "vac_specified"		{ VACUUM (NOWAIT) test1, test2; }
+step "vac_all"			{ VACUUM (NOWAIT); }
+step "analyze_specified"	{ ANALYZE (NOWAIT) test1, test2; }
+step "analyze_all"		{ ANALYZE (NOWAIT); }
+step "vac_analyze_specified"	{ VACUUM (ANALYZE, NOWAIT) test1, test2; }
+step "vac_analyze_all"		{ VACUUM (ANALYZE, NOWAIT); }
+
+permutation "lock" "vac_specified" "commit"
+permutation "lock" "vac_all" "commit"
+permutation "lock" "analyze_specified" "commit"
+permutation "lock" "analyze_all" "commit"
+permutation "lock" "vac_analyze_specified" "commit"
+permutation "lock" "vac_analyze_all" "commit"
diff --git a/src/test/regress/expected/vacuum.out b/src/test/regress/expected/vacuum.out
index 199dda6..99aad21 100644
--- a/src/test/regress/expected/vacuum.out
+++ b/src/test/regress/expected/vacuum.out
@@ -113,8 +113,11 @@ ERROR:  relation "does_not_exist" does not exist
 ANALYZE vactst (i), vacparted (does_not_exist);
 ERROR:  column "does_not_exist" of relation "vacparted" does not exist
 -- parenthesized syntax for ANALYZE
-ANALYZE (VERBOSE) does_not_exist;
+ANALYZE (NOWAIT, VERBOSE) does_not_exist;
 ERROR:  relation "does_not_exist" does not exist
+-- nowait option
+VACUUM (NOWAIT) vactst;
+ANALYZE (NOWAIT) vactst;
 DROP TABLE vaccluster;
 DROP TABLE vactst;
 DROP TABLE vacparted;
diff --git a/src/test/regress/sql/vacuum.sql b/src/test/regress/sql/vacuum.sql
index e7b7a7f..5e88c68 100644
--- a/src/test/regress/sql/vacuum.sql
+++ b/src/test/regress/sql/vacuum.sql
@@ -90,7 +90,11 @@ ANALYZE vactst, does_not_exist, vacparted;
 ANALYZE vactst (i), vacparted (does_not_exist);
 
 -- parenthesized syntax for ANALYZE
-ANALYZE (VERBOSE) does_not_exist;
+ANALYZE (NOWAIT, VERBOSE) does_not_exist;
+
+-- nowait option
+VACUUM (NOWAIT) vactst;
+ANALYZE (NOWAIT) vactst;
 
 DROP TABLE vaccluster;
 DROP TABLE vactst;