pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bossart, Nathan <bossartn@amazon.com>
To: Robert Haas <robertmhaas@gmail.com>
To: Lyes Ameddah <lyes.amd@gmail.com>
Cc: Tomas Vondra <tomas.vondra@2ndquadrant.com>
Cc: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14941: Vacuum crashes
Date: Tue, 5 Dec 2017 16:52:40 +0000
Message-ID: <D3FC73E2-9B1A-4DB4-8180-55F57D116B4E@amazon.com> (raw)
In-Reply-To: <CA+TgmobBsYxLT586dHBtcsHHeSsJ7T6tr447tY3C08X5fFFraQ@mail.gmail.com>
References: <20171201160907.27110.74730@wrigleys.postgresql.org>
	<cabd2b8a-ebbc-ad38-3ca2-113882afd12a@2ndquadrant.com>
	<1E4815BF-18D2-4184-989A-975728F59B88@amazon.com>
	<CA+TgmoY3Azn-6cuuuV7KSJ=eFErJiVpyzUXEQF6J32aCzLLkHg@mail.gmail.com>
	<F7AE1E78-4C3A-4D2D-84EE-68E8D8898E6D@amazon.com>
	<CAEcwYAJY_EODBTj20xuZMKZNxdm0W7vVyCj1TQV7Kmcz+BBX3w@mail.gmail.com>
	<CA+TgmobBsYxLT586dHBtcsHHeSsJ7T6tr447tY3C08X5fFFraQ@mail.gmail.com>

On 12/4/17, 8:52 AM, "Robert Haas" <robertmhaas@gmail.com> wrote:
> On Mon, Dec 4, 2017 at 8:59 AM, Lyes Ameddah <lyes.amd@gmail.com> wrote:
>> sorry guys, yes I'm talking about a FULL VACUUM and not about Auto-Vacuum.
>> Thank you very match for your feedback.
>
> OK, but that's not the confusion.  What you said is that it CRASHES,
> but the behavior you described is that it BLOCKS waiting for a lock.
> Blocking and crashing are not the same thing.

Provided that Lyes seems to have described wanting to avoid the blocking
behavior (and since I'm interested in adding this functionality anyway),
here are some patches that open up the VACOPT_NOWAIT option to users for
both VACUUM and ANALYZE commands.

---

0001_fix_unparenthesized_vacuum_grammar_v1.patch

One VacuumStmt grammar rule allows users to specify the VACOPT_ANALYZE
option by including an AnalyzeStmt at the end of the command.  This
appears to have been added as part of the introduction of the ANALYZE
command (f905d65e).  However, this means that users can call VACUUM
commands like

        VACUUM VERBOSE ANALYZE VERBOSE pg_class;

Besides being disallowed according to the documentation, I think this
may give users the idea that there is a difference between the VERBOSE
options for VACUUM versus ANALYZE.  In fact, they are the same option,
and specifying it twice is redundant.

This change fixes the unparenthesized VACUUM syntax to disallow the out-
of-order VACUUM options as described above.  This is a prerequisite
change for opening up VACOPT_NOWAIT to users, as adding it to the
grammar as-is would cause statements like

        VACUUM VERBOSE NOWAIT ANALYZE VERBOSE NOWAIT pg_class;

to be allowed.  Since I am also looking to introduce a parenthesized
syntax for ANALYZE, this patch would prevent statements like

        VACUUM VERBOSE ANALYZE (VERBOSE, NOWAIT) pg_class;

from being accepted as well.

---

0002_add_parenthesized_analyze_syntax_v1.patch

As a prerequisite to giving users access to the VACOPT_NOWAIT option, I
am introducing a parenthesized syntax for ANALYZE that is similar to
VACUUM's.  Following VACUUM's example, any new options will be added to
this syntax, and the old style will become deprecated.

Adding a parenthesized syntax for ANALYZE is not strictly necessary for
providing the NOWAIT option, as NOWAIT is already a keyword in gram.y.
However, I thought it would be good to match VACUUM (since the command
structure is so similar), and this work would be required at some point
anyway if ANALYZE ever accepts options that we do not want to make
reserved keywords.

---

0003_add_nowait_vacuum_option_v1.patch

This change provides the existing VACOPT_NOWAIT option to users.  This
option was previously only used by autovacuum in special cases, but it
seems like a potentially valuable option in other cases as well.  For
example, perhaps a user wants to run a nightly VACUUM job that skips
tables that cannot be locked due to autovacuum (or something else)
already working on it.

I chose to use NOWAIT as the option name because it is already a keyword
for the LOCK command.

This patch follows the existing logging precedent added by 11d8d72c and
ab6eaee8: if a relation is explicitly specified in the command, a log
statement will be emitted if it is skipped.  If the relation is not
specified (e.g. "VACUUM FULL;"), no such log statements will be emitted.

---

I'll be adding an entry to the next commitfest for these patches soon.

Nathan



Attachments: download all (3 files, .tar.gz)

0003_add_nowait_vacuum_option_v1.patch (application/octet-stream, 7.6K) download 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;
0002_add_parenthesized_analyze_syntax_v1.patch (application/octet-stream, 3.6K) download diff --git a/doc/src/sgml/ref/analyze.sgml b/doc/src/sgml/ref/analyze.sgml index 83b07a0..10b3a9a 100644 --- a/doc/src/sgml/ref/analyze.sgml +++ b/doc/src/sgml/ref/analyze.sgml @@ -21,9 +21,14 @@ PostgreSQL documentation <refsynopsisdiv> <synopsis> +ANALYZE [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] [ <replaceable class="parameter">table_and_columns</replaceable> [, ...] ] ANALYZE [ VERBOSE ] [ <replaceable class="parameter">table_and_columns</replaceable> [, ...] ] -<phrase>where <replaceable class="parameter">table_and_columns</replaceable> is:</phrase> +<phrase>where <replaceable class="parameter">option</replaceable> can be one of:</phrase> + + VERBOSE + +<phrase>and <replaceable class="parameter">table_and_columns</replaceable> is:</phrase> <replaceable class="parameter">table_name</replaceable> [ ( <replaceable class="parameter">column_name</replaceable> [, ...] ) ] </synopsis> @@ -49,6 +54,13 @@ ANALYZE [ VERBOSE ] [ <replaceable class="parameter">table_and_columns</replacea It is further possible to give a list of column names for a table, in which case only the statistics for those columns are collected. </para> + + <para> + When the option list is surrounded by parentheses, the options can be + written in any order. The parenthesized syntax was added in + <productname>PostgreSQL</productname> 11; the unparenthesized syntax + is deprecated. + </para> </refsect1> <refsect1> diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 07cc15d..f749722 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -306,6 +306,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type <ival> opt_lock lock_type cast_context %type <ival> vacuum_option_list vacuum_option_elem + analyze_option_list analyze_option_elem %type <boolean> opt_or_replace opt_grant_grant_option opt_grant_admin_option opt_nowait opt_if_exists opt_with_data @@ -10517,6 +10518,22 @@ AnalyzeStmt: analyze_keyword opt_verbose opt_vacuum_relation_list n->rels = $3; $$ = (Node *)n; } + | analyze_keyword '(' analyze_option_list ')' opt_vacuum_relation_list + { + VacuumStmt *n = makeNode(VacuumStmt); + n->options = VACOPT_ANALYZE | $3; + n->rels = $5; + $$ = (Node *) n; + } + ; + +analyze_option_list: + analyze_option_elem { $$ = $1; } + | analyze_option_list ',' analyze_option_elem { $$ = $1 | $3; } + ; + +analyze_option_elem: + VERBOSE { $$ = VACOPT_VERBOSE; } ; analyze_keyword: diff --git a/src/test/regress/expected/vacuum.out b/src/test/regress/expected/vacuum.out index c440c7e..199dda6 100644 --- a/src/test/regress/expected/vacuum.out +++ b/src/test/regress/expected/vacuum.out @@ -112,6 +112,9 @@ ANALYZE vactst, does_not_exist, vacparted; 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; +ERROR: relation "does_not_exist" does not exist 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 92eaca2..e7b7a7f 100644 --- a/src/test/regress/sql/vacuum.sql +++ b/src/test/regress/sql/vacuum.sql @@ -89,6 +89,9 @@ ANALYZE vacparted (b), vactst; ANALYZE vactst, does_not_exist, vacparted; ANALYZE vactst (i), vacparted (does_not_exist); +-- parenthesized syntax for ANALYZE +ANALYZE (VERBOSE) does_not_exist; + DROP TABLE vaccluster; DROP TABLE vactst; DROP TABLE vacparted;
0001_fix_unparenthesized_vacuum_grammar_v1.patch (application/octet-stream, 1.9K) download diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index ebfc94f..07cc15d 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -437,7 +437,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type <boolean> opt_instead %type <boolean> opt_unique opt_concurrently opt_verbose opt_full -%type <boolean> opt_freeze opt_default opt_recheck +%type <boolean> opt_freeze opt_analyze opt_default opt_recheck %type <defelt> opt_binary opt_oids copy_delimiter %type <boolean> copy_from opt_program @@ -10462,7 +10462,7 @@ cluster_index_specification: * *****************************************************************************/ -VacuumStmt: VACUUM opt_full opt_freeze opt_verbose opt_vacuum_relation_list +VacuumStmt: VACUUM opt_full opt_freeze opt_verbose opt_analyze opt_vacuum_relation_list { VacuumStmt *n = makeNode(VacuumStmt); n->options = VACOPT_VACUUM; @@ -10472,19 +10472,9 @@ VacuumStmt: VACUUM opt_full opt_freeze opt_verbose opt_vacuum_relation_list n->options |= VACOPT_FREEZE; if ($4) n->options |= VACOPT_VERBOSE; - n->rels = $5; - $$ = (Node *)n; - } - | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt - { - VacuumStmt *n = (VacuumStmt *) $5; - n->options |= VACOPT_VACUUM; - if ($2) - n->options |= VACOPT_FULL; - if ($3) - n->options |= VACOPT_FREEZE; - if ($4) - n->options |= VACOPT_VERBOSE; + if ($5) + n->options |= VACOPT_ANALYZE; + n->rels = $6; $$ = (Node *)n; } | VACUUM '(' vacuum_option_list ')' opt_vacuum_relation_list @@ -10534,6 +10524,11 @@ analyze_keyword: | ANALYSE /* British */ {} ; +opt_analyze: + analyze_keyword { $$ = true; } + | /*EMPTY*/ { $$ = false; } + ; + opt_verbose: VERBOSE { $$ = true; } | /*EMPTY*/ { $$ = false; }

view thread (42+ messages)  latest in thread

Message-ID: <D3FC73E2-9B1A-4DB4-8180-55F57D116B4E@amazon.com>
Permalink:  ../D3FC73E2-9B1A-4DB4-8180-55F57D116B4E@amazon.com/
Also on:    postgresql.org/message-id/D3FC73E2-9B1A-4DB4-8180-55F57D116B4E@amazon.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bossartn@amazon.com, robertmhaas@gmail.com, lyes.amd@gmail.com, tomas.vondra@2ndquadrant.com, pgsql-hackers@postgresql.org
  Subject: Re: BUG #14941: Vacuum crashes
  In-Reply-To: <D3FC73E2-9B1A-4DB4-8180-55F57D116B4E@amazon.com>
  References: <20171201160907.27110.74730@wrigleys.postgresql.org> <cabd2b8a-ebbc-ad38-3ca2-113882afd12a@2ndquadrant.com> <1E4815BF-18D2-4184-989A-975728F59B88@amazon.com> <CA+TgmoY3Azn-6cuuuV7KSJ=eFErJiVpyzUXEQF6J32aCzLLkHg@mail.gmail.com> <F7AE1E78-4C3A-4D2D-84EE-68E8D8898E6D@amazon.com> <CAEcwYAJY_EODBTj20xuZMKZNxdm0W7vVyCj1TQV7Kmcz+BBX3w@mail.gmail.com> <CA+TgmobBsYxLT586dHBtcsHHeSsJ7T6tr447tY3C08X5fFFraQ@mail.gmail.com> <D3FC73E2-9B1A-4DB4-8180-55F57D116B4E@amazon.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox