Skip to content

Commit b572b43

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 e5b8b77 commit b572b43

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
@@ -370,10 +370,14 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
370370
/*
371371
* Determine which columns to analyze
372372
*
373-
* Note that system attributes are never analyzed.
373+
* Note that system attributes are never analyzed, so we just reject them
374+
* at the lookup stage. We also reject duplicate column mentions. (We
375+
* could alternatively ignore duplicates, but analyzing a column twice
376+
* won't work; we'd end up making a conflicting update in pg_statistic.)
374377
*/
375378
if (vacstmt->va_cols != NIL)
376379
{
380+
Bitmapset *unique_cols = NULL;
377381
ListCell *le;
378382

379383
vacattrstats = (VacAttrStats **) palloc(list_length(vacstmt->va_cols) *
@@ -389,6 +393,13 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
389393
(errcode(ERRCODE_UNDEFINED_COLUMN),
390394
errmsg("column \"%s\" of relation \"%s\" does not exist",
391395
col, RelationGetRelationName(onerel))));
396+
if (bms_is_member(i, unique_cols))
397+
ereport(ERROR,
398+
(errcode(ERRCODE_DUPLICATE_COLUMN),
399+
errmsg("column \"%s\" of relation \"%s\" is specified twice",
400+
col, RelationGetRelationName(onerel))));
401+
unique_cols = bms_add_member(unique_cols, i);
402+
392403
vacattrstats[tcnt] = examine_attribute(onerel, i, NULL);
393404
if (vacattrstats[tcnt] != NULL)
394405
tcnt++;

src/test/regress/expected/vacuum.out

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

src/test/regress/sql/vacuum.sql

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

63+
-- check behavior with duplicate column mentions
64+
VACUUM ANALYZE vaccluster(i,i);
65+
ANALYZE vaccluster(i,i);
66+
6367
DROP TABLE vaccluster;
6468
DROP TABLE vactst;

0 commit comments

Comments
 (0)