Commit 5dfd564b authored by Alvaro Herrera's avatar Alvaro Herrera

Fix IF NOT EXISTS in CREATE STATISTICS

I misplaced the IF NOT EXISTS clause in commit 7b504eb2, before the
word STATISTICS.  Put it where it belongs.

Patch written independently by Amit Langote and myself.  I adopted his
submitted test case with a slight edit also.

Reported-by: Bruno Wolff III
Discussion: https://postgr.es/m/20170621004237.GB8337@wolff.to
parent 2c77903b
...@@ -3834,7 +3834,7 @@ ExistingIndex: USING INDEX index_name { $$ = $3; } ...@@ -3834,7 +3834,7 @@ ExistingIndex: USING INDEX index_name { $$ = $3; }
/***************************************************************************** /*****************************************************************************
* *
* QUERY : * QUERY :
* CREATE STATISTICS stats_name [(stat types)] * CREATE STATISTICS [IF NOT EXISTS] stats_name [(stat types)]
* ON expression-list FROM from_list * ON expression-list FROM from_list
* *
* Note: the expectation here is that the clauses after ON are a subset of * Note: the expectation here is that the clauses after ON are a subset of
...@@ -3846,15 +3846,26 @@ ExistingIndex: USING INDEX index_name { $$ = $3; } ...@@ -3846,15 +3846,26 @@ ExistingIndex: USING INDEX index_name { $$ = $3; }
*****************************************************************************/ *****************************************************************************/
CreateStatsStmt: CreateStatsStmt:
CREATE opt_if_not_exists STATISTICS any_name CREATE STATISTICS any_name
opt_name_list ON expr_list FROM from_list opt_name_list ON expr_list FROM from_list
{ {
CreateStatsStmt *n = makeNode(CreateStatsStmt); CreateStatsStmt *n = makeNode(CreateStatsStmt);
n->defnames = $4; n->defnames = $3;
n->stat_types = $5; n->stat_types = $4;
n->exprs = $7; n->exprs = $6;
n->relations = $9; n->relations = $8;
n->if_not_exists = $2; n->if_not_exists = false;
$$ = (Node *)n;
}
| CREATE STATISTICS IF_P NOT EXISTS any_name
opt_name_list ON expr_list FROM from_list
{
CreateStatsStmt *n = makeNode(CreateStatsStmt);
n->defnames = $6;
n->stat_types = $7;
n->exprs = $9;
n->relations = $11;
n->if_not_exists = true;
$$ = (Node *)n; $$ = (Node *)n;
} }
; ;
......
...@@ -30,9 +30,11 @@ CREATE STATISTICS tst ON (relpages, reltuples) FROM pg_class; ...@@ -30,9 +30,11 @@ CREATE STATISTICS tst ON (relpages, reltuples) FROM pg_class;
ERROR: only simple column references are allowed in CREATE STATISTICS ERROR: only simple column references are allowed in CREATE STATISTICS
CREATE STATISTICS tst (unrecognized) ON relname, relnatts FROM pg_class; CREATE STATISTICS tst (unrecognized) ON relname, relnatts FROM pg_class;
ERROR: unrecognized statistic type "unrecognized" ERROR: unrecognized statistic type "unrecognized"
-- Ensure stats are dropped sanely -- Ensure stats are dropped sanely, and test IF NOT EXISTS while at it
CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER); CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER);
CREATE STATISTICS ab1_a_b_stats ON a, b FROM ab1; CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;
CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;
NOTICE: statistics object "ab1_a_b_stats" already exists, skipping
DROP STATISTICS ab1_a_b_stats; DROP STATISTICS ab1_a_b_stats;
CREATE SCHEMA regress_schema_2; CREATE SCHEMA regress_schema_2;
CREATE STATISTICS regress_schema_2.ab1_a_b_stats ON a, b FROM ab1; CREATE STATISTICS regress_schema_2.ab1_a_b_stats ON a, b FROM ab1;
......
...@@ -18,9 +18,10 @@ CREATE STATISTICS tst ON relnatts + relpages FROM pg_class; ...@@ -18,9 +18,10 @@ CREATE STATISTICS tst ON relnatts + relpages FROM pg_class;
CREATE STATISTICS tst ON (relpages, reltuples) FROM pg_class; CREATE STATISTICS tst ON (relpages, reltuples) FROM pg_class;
CREATE STATISTICS tst (unrecognized) ON relname, relnatts FROM pg_class; CREATE STATISTICS tst (unrecognized) ON relname, relnatts FROM pg_class;
-- Ensure stats are dropped sanely -- Ensure stats are dropped sanely, and test IF NOT EXISTS while at it
CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER); CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER);
CREATE STATISTICS ab1_a_b_stats ON a, b FROM ab1; CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;
CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;
DROP STATISTICS ab1_a_b_stats; DROP STATISTICS ab1_a_b_stats;
CREATE SCHEMA regress_schema_2; CREATE SCHEMA regress_schema_2;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment