Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5dfd564

Browse files
committed
Fix IF NOT EXISTS in CREATE STATISTICS
I misplaced the IF NOT EXISTS clause in commit 7b504eb, 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/[email protected]
1 parent 2c77903 commit 5dfd564

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/backend/parser/gram.y

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3834,7 +3834,7 @@ ExistingIndex: USING INDEX index_name { $$ = $3; }
38343834
/*****************************************************************************
38353835
*
38363836
* QUERY :
3837-
* CREATE STATISTICS stats_name [(stat types)]
3837+
* CREATE STATISTICS [IF NOT EXISTS] stats_name [(stat types)]
38383838
* ON expression-list FROM from_list
38393839
*
38403840
* Note: the expectation here is that the clauses after ON are a subset of
@@ -3846,15 +3846,26 @@ ExistingIndex: USING INDEX index_name { $$ = $3; }
38463846
*****************************************************************************/
38473847

38483848
CreateStatsStmt:
3849-
CREATE opt_if_not_exists STATISTICS any_name
3849+
CREATE STATISTICS any_name
38503850
opt_name_list ON expr_list FROM from_list
38513851
{
38523852
CreateStatsStmt *n = makeNode(CreateStatsStmt);
3853-
n->defnames = $4;
3854-
n->stat_types = $5;
3855-
n->exprs = $7;
3856-
n->relations = $9;
3857-
n->if_not_exists = $2;
3853+
n->defnames = $3;
3854+
n->stat_types = $4;
3855+
n->exprs = $6;
3856+
n->relations = $8;
3857+
n->if_not_exists = false;
3858+
$$ = (Node *)n;
3859+
}
3860+
| CREATE STATISTICS IF_P NOT EXISTS any_name
3861+
opt_name_list ON expr_list FROM from_list
3862+
{
3863+
CreateStatsStmt *n = makeNode(CreateStatsStmt);
3864+
n->defnames = $6;
3865+
n->stat_types = $7;
3866+
n->exprs = $9;
3867+
n->relations = $11;
3868+
n->if_not_exists = true;
38583869
$$ = (Node *)n;
38593870
}
38603871
;

src/test/regress/expected/stats_ext.out

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ CREATE STATISTICS tst ON (relpages, reltuples) FROM pg_class;
3030
ERROR: only simple column references are allowed in CREATE STATISTICS
3131
CREATE STATISTICS tst (unrecognized) ON relname, relnatts FROM pg_class;
3232
ERROR: unrecognized statistic type "unrecognized"
33-
-- Ensure stats are dropped sanely
33+
-- Ensure stats are dropped sanely, and test IF NOT EXISTS while at it
3434
CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER);
35-
CREATE STATISTICS ab1_a_b_stats ON a, b FROM ab1;
35+
CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;
36+
CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;
37+
NOTICE: statistics object "ab1_a_b_stats" already exists, skipping
3638
DROP STATISTICS ab1_a_b_stats;
3739
CREATE SCHEMA regress_schema_2;
3840
CREATE STATISTICS regress_schema_2.ab1_a_b_stats ON a, b FROM ab1;

src/test/regress/sql/stats_ext.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ CREATE STATISTICS tst ON relnatts + relpages FROM pg_class;
1818
CREATE STATISTICS tst ON (relpages, reltuples) FROM pg_class;
1919
CREATE STATISTICS tst (unrecognized) ON relname, relnatts FROM pg_class;
2020

21-
-- Ensure stats are dropped sanely
21+
-- Ensure stats are dropped sanely, and test IF NOT EXISTS while at it
2222
CREATE TABLE ab1 (a INTEGER, b INTEGER, c INTEGER);
23-
CREATE STATISTICS ab1_a_b_stats ON a, b FROM ab1;
23+
CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;
24+
CREATE STATISTICS IF NOT EXISTS ab1_a_b_stats ON a, b FROM ab1;
2425
DROP STATISTICS ab1_a_b_stats;
2526

2627
CREATE SCHEMA regress_schema_2;

0 commit comments

Comments
 (0)