Skip to content

Commit a09d8be

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 149cfdb commit a09d8be

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

378382
vacattrstats = (VacAttrStats **) palloc(list_length(vacstmt->va_cols) *
@@ -388,6 +392,13 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
388392
(errcode(ERRCODE_UNDEFINED_COLUMN),
389393
errmsg("column \"%s\" of relation \"%s\" does not exist",
390394
col, RelationGetRelationName(onerel))));
395+
if (bms_is_member(i, unique_cols))
396+
ereport(ERROR,
397+
(errcode(ERRCODE_DUPLICATE_COLUMN),
398+
errmsg("column \"%s\" of relation \"%s\" is specified twice",
399+
col, RelationGetRelationName(onerel))));
400+
unique_cols = bms_add_member(unique_cols, i);
401+
391402
vacattrstats[tcnt] = examine_attribute(onerel, i, NULL);
392403
if (vacattrstats[tcnt] != NULL)
393404
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)