Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7148050

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 toanalyze such a column twice. That would fail at the point of insertingthe second version of the pg_statistic row, with obscure error messsageslike "duplicate key value violates unique constraint" or "tuple alreadyupdated by self", depending on context and PG version. We could allowthe case by ignoring duplicate column specifications, but it seems betterto reject it explicitly.The bogus error messages seem like arguably a bug, so back-patch toall supported versions.Nathan Bossart, per a report from Michael Paquier, and whackedaround a bit by me.Discussion:https://postgr.es/m/E061A8E3-5E3D-494D-94F0-E8A9B312BBFC@amazon.com
1 parent28ae524 commit7148050

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

‎src/backend/commands/analyze.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,14 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params,
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 (va_cols!=NIL)
376379
{
380+
Bitmapset*unique_cols=NULL;
377381
ListCell*le;
378382

379383
vacattrstats= (VacAttrStats**)palloc(list_length(va_cols)*
@@ -389,6 +393,13 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params,
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++;
@@ -527,9 +538,9 @@ do_analyze_rel(Relation onerel, int options, VacuumParams *params,
527538
stats->rows=rows;
528539
stats->tupDesc=onerel->rd_att;
529540
stats->compute_stats(stats,
530-
std_fetch_func,
531-
numrows,
532-
totalrows);
541+
std_fetch_func,
542+
numrows,
543+
totalrows);
533544

534545
/*
535546
* If the appropriate flavor of the n_distinct option is
@@ -831,9 +842,9 @@ compute_index_stats(Relation onerel, double totalrows,
831842
stats->exprnulls=exprnulls+i;
832843
stats->rowstride=attr_cnt;
833844
stats->compute_stats(stats,
834-
ind_fetch_func,
835-
numindexrows,
836-
totalindexrows);
845+
ind_fetch_func,
846+
numindexrows,
847+
totalindexrows);
837848

838849
/*
839850
* If the n_distinct option is specified, it overrides the

‎src/test/regress/expected/vacuum.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,9 @@ UPDATE vacparted SET b = 'b';
9090
VACUUM (ANALYZE) vacparted;
9191
VACUUM (FULL) vacparted;
9292
VACUUM (FREEZE) vacparted;
93+
-- check behavior with duplicate column mentions
94+
VACUUM ANALYZE vacparted(a,b,a);
95+
ERROR: column "a" of relation "vacparted" is specified twice
96+
ANALYZE vacparted(a,b,b);
97+
ERROR: column "b" of relation "vacparted" is specified twice
9398
DROP TABLE vacparted;

‎src/test/regress/sql/vacuum.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,9 @@ UPDATE vacparted SET b = 'b';
7373
VACUUM (ANALYZE) vacparted;
7474
VACUUM (FULL) vacparted;
7575
VACUUM (FREEZE) vacparted;
76+
77+
-- check behavior with duplicate column mentions
78+
VACUUM ANALYZE vacparted(a,b,a);
79+
ANALYZE vacparted(a,b,b);
80+
7681
DROPTABLE vacparted;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp