Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ea31541

Browse files
committed
Give a better error for duplicate entries in VACUUM/ANALYZE column list.
Previously, the code didn't think about this case and would just try to analyze such a column twice. That would fail at the point of inserting the second version of the pg_statistic row, with obscure error messsages like "duplicate key value violates unique constraint" or "tuple already updated by self", depending on context and PG version. We could allow the case by ignoring duplicate column specifications, but it seems better to reject it explicitly. The bogus error messages seem like arguably a bug, so back-patch to all supported versions. Nathan Bossart, per a report from Michael Paquier, and whacked around a bit by me. Discussion: https://postgr.es/m/E061A8E3-5E3D-494D-94F0-E8A9B312BBFC@amazon.com
1 parent 33dd10e commit ea31541

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/backend/commands/analyze.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,14 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params,
356356
/*
357357
* Determine which columns to analyze
358358
*
359-
* Note that system attributes are never analyzed.
359+
* Note that system attributes are never analyzed, so we just reject them
360+
* at the lookup stage. We also reject duplicate column mentions. (We
361+
* could alternatively ignore duplicates, but analyzing a column twice
362+
* won't work; we'd end up making a conflicting update in pg_statistic.)
360363
*/
361364
if (va_cols != NIL)
362365
{
366+
Bitmapset *unique_cols = NULL;
363367
ListCell *le;
364368

365369
vacattrstats = (VacAttrStats **) palloc(list_length(va_cols) *
@@ -375,6 +379,13 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params,
375379
(errcode(ERRCODE_UNDEFINED_COLUMN),
376380
errmsg("column \"%s\" of relation \"%s\" does not exist",
377381
col, RelationGetRelationName(onerel))));
382+
if (bms_is_member(i, unique_cols))
383+
ereport(ERROR,
384+
(errcode(ERRCODE_DUPLICATE_COLUMN),
385+
errmsg("column \"%s\" of relation \"%s\" is specified twice",
386+
col, RelationGetRelationName(onerel))));
387+
unique_cols = bms_add_member(unique_cols, i);
388+
378389
vacattrstats[tcnt] = examine_attribute(onerel, i, NULL);
379390
if (vacattrstats[tcnt] != NULL)
380391
tcnt++;

src/test/regress/expected/vacuum.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,10 @@ CONTEXT: SQL function "do_analyze" statement 1
8080
SQL function "wrap_do_analyze" statement 1
8181
VACUUM FULL vactst;
8282
VACUUM (DISABLE_PAGE_SKIPPING) vaccluster;
83+
-- check behavior with duplicate column mentions
84+
VACUUM ANALYZE vaccluster(i,i);
85+
ERROR: column "i" of relation "vaccluster" is specified twice
86+
ANALYZE vaccluster(i,i);
87+
ERROR: column "i" of relation "vaccluster" is specified twice
8388
DROP TABLE vaccluster;
8489
DROP TABLE vactst;

src/test/regress/sql/vacuum.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,9 @@ VACUUM FULL vactst;
6262

6363
VACUUM (DISABLE_PAGE_SKIPPING) vaccluster;
6464

65+
-- check behavior with duplicate column mentions
66+
VACUUM ANALYZE vaccluster(i,i);
67+
ANALYZE vaccluster(i,i);
68+
6569
DROP TABLE vaccluster;
6670
DROP TABLE vactst;

0 commit comments

Comments
 (0)