From 8f1476db0a11adb3cdf155b420b483ca8e613025 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Mon, 5 Mar 2018 16:21:05 -0800 Subject: [PATCH 1/2] Add parenthesized options syntax for ANALYZE. This is analogous to the syntax allowed for VACUUM. This allows us to avoid making new options reserved keywords and makes it easier to allow arbitrary argument order. Oh, and it's consistent with the other commands, too. Author: Nathan Bossart Reviewed-By: Michael Paquier, Masahiko Sawada Discussion: https://postgr.es/m/D3FC73E2-9B1A-4DB4-8180-55F57D116B4E@amazon.com --- doc/src/sgml/ref/analyze.sgml | 18 ++++++++++++++++- src/backend/parser/gram.y | 19 +++++++++++++++++- src/test/regress/expected/vacuum.out | 30 ++++++++++++++++++++++++++++ src/test/regress/sql/vacuum.sql | 4 ++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/doc/src/sgml/ref/analyze.sgml b/doc/src/sgml/ref/analyze.sgml index eec7599f148..40b43ee5a9c 100755 --- a/doc/src/sgml/ref/analyze.sgml +++ b/doc/src/sgml/ref/analyze.sgml @@ -21,7 +21,16 @@ PostgreSQL documentation -ANALYZE [ VERBOSE ] [ table_name [ ( column_name [, ...] ) ] ] +ANALYZE [ ( option [, ...] ) ] [ table_and_columns [, ...] ] +ANALYZE [ VERBOSE ] [ table_and_columns [, ...] ] + +where option can be one of: + + VERBOSE + +and table_and_columns is: + + table_name [ ( column_name [, ...] ) ] @@ -43,6 +52,13 @@ ANALYZE [ VERBOSE ] [ table_name [ only that table. It is further possible to give a list of column names, in which case only the statistics for those columns are collected. + + + When the option list is surrounded by parentheses, the options can be + written in any order. The parenthesized syntax was added in + PostgreSQL 11; the unparenthesized syntax + is deprecated. + diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index b329952b24c..391ab1dfde8 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -304,7 +304,8 @@ static Node *makeIsNotDistinctFromNode(Node *expr, int position); %type opt_lock lock_type cast_context %type vacuum_option_list vacuum_option_elem -%type opt_force opt_or_replace + analyze_option_list analyze_option_elem +%type opt_or_replace opt_force opt_grant_grant_option opt_grant_admin_option opt_nowait opt_if_exists opt_with_data opt_masteronly @@ -11487,6 +11488,22 @@ AnalyzeStmt: n->va_cols = $5; $$ = (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 60364cd6bf9..4bbf88d6d3e 100755 --- a/src/test/regress/expected/vacuum.out +++ b/src/test/regress/expected/vacuum.out @@ -84,5 +84,35 @@ VACUUM ANALYZE vaccluster(i,i); ERROR: column "i" of relation "vaccluster" appears more than once ANALYZE vaccluster(i,i); ERROR: column "i" of relation "vaccluster" appears more than once +VACUUM ANALYZE vacparted(a,b,a); +ERROR: column "a" of relation "vacparted" appears more than once +ANALYZE vacparted(a,b,b); +ERROR: column "b" of relation "vacparted" appears more than once +-- multiple tables specified +VACUUM vaccluster, vactst; +VACUUM vacparted, does_not_exist; +ERROR: relation "does_not_exist" does not exist +VACUUM (FREEZE) vacparted, vaccluster, vactst; +VACUUM (FREEZE) does_not_exist, vaccluster; +ERROR: relation "does_not_exist" does not exist +VACUUM ANALYZE vactst, vacparted (a); +VACUUM ANALYZE vactst (does_not_exist), vacparted (b); +ERROR: column "does_not_exist" of relation "vactst" does not exist +VACUUM FULL vacparted, vactst; +VACUUM FULL vactst, vacparted (a, b), vaccluster (i); +ERROR: ANALYZE option must be specified when a column list is provided +ANALYZE vactst, vacparted; +ANALYZE vacparted (b), vactst; +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 +ANALYZE (nonexistant-arg) does_not_exist; +ERROR: syntax error at or near "nonexistant" +LINE 1: ANALYZE (nonexistant-arg) does_not_exist; + ^ DROP TABLE vaccluster; DROP TABLE vactst; diff --git a/src/test/regress/sql/vacuum.sql b/src/test/regress/sql/vacuum.sql index 2f657269c6f..20bedbc905e 100644 --- a/src/test/regress/sql/vacuum.sql +++ b/src/test/regress/sql/vacuum.sql @@ -67,5 +67,9 @@ VACUUM FULL vactst; VACUUM ANALYZE vaccluster(i,i); ANALYZE vaccluster(i,i); +-- parenthesized syntax for ANALYZE +ANALYZE (VERBOSE) does_not_exist; +ANALYZE (nonexistant-arg) does_not_exist; + DROP TABLE vaccluster; DROP TABLE vactst; From 48b9862c2f167d26fdb63e0cf4ba8d09a4e31e9c Mon Sep 17 00:00:00 2001 From: Victor Kremensky Date: Fri, 28 Mar 2025 10:18:45 +0000 Subject: [PATCH 2/2] grammar fixes --- src/backend/parser/gram.y | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 391ab1dfde8..e8f4eea23fb 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -11488,11 +11488,10 @@ AnalyzeStmt: n->va_cols = $5; $$ = (Node *)n; } - | analyze_keyword '(' analyze_option_list ')' opt_vacuum_relation_list + | analyze_keyword '(' analyze_option_list ')' { VacuumStmt *n = makeNode(VacuumStmt); n->options = VACOPT_ANALYZE | $3; - n->rels = $5; $$ = (Node *) n; } ;