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

Commit8493831

Browse files
committed
Preserve pg_attribute.attstattarget across REINDEX CONCURRENTLY
For an index, attstattarget can be updated using ALTER INDEX SETSTATISTICS. This data was lost on the new index after REINDEXCONCURRENTLY.The update of this field is done when the old and new indexes areswapped to make the fix back-patchable. Another approach we could lookafter in the long-term is to change index_create() to pass the wantedvalues of attstattarget when creating the new relation, but, as thiswould cause an ABI breakage this can be done only on HEAD.Reported-by: Ronan DunklauAuthor: Michael PaquierReviewed-by: Ronan Dunklau, Tomas VondraDiscussion:https://postgr.es/m/16628084.uLZWGnKmhe@laptop-ronandBackpatch-through: 12
1 parent3fb4c75 commit8493831

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

‎src/backend/catalog/index.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,62 @@ index_concurrently_swap(Oid newIndexId, Oid oldIndexId, const char *oldName)
17281728
/* Copy data of pg_statistic from the old index to the new one */
17291729
CopyStatistics(oldIndexId,newIndexId);
17301730

1731+
/* Copy pg_attribute.attstattarget for each index attribute */
1732+
{
1733+
HeapTupleattrTuple;
1734+
Relationpg_attribute;
1735+
SysScanDescscan;
1736+
ScanKeyDatakey[1];
1737+
1738+
pg_attribute=table_open(AttributeRelationId,RowExclusiveLock);
1739+
ScanKeyInit(&key[0],
1740+
Anum_pg_attribute_attrelid,
1741+
BTEqualStrategyNumber,F_OIDEQ,
1742+
ObjectIdGetDatum(newIndexId));
1743+
scan=systable_beginscan(pg_attribute,AttributeRelidNumIndexId,
1744+
true,NULL,1,key);
1745+
1746+
while (HeapTupleIsValid((attrTuple=systable_getnext(scan))))
1747+
{
1748+
Form_pg_attributeatt= (Form_pg_attribute)GETSTRUCT(attrTuple);
1749+
Datumrepl_val[Natts_pg_attribute];
1750+
boolrepl_null[Natts_pg_attribute];
1751+
boolrepl_repl[Natts_pg_attribute];
1752+
intattstattarget;
1753+
HeapTuplenewTuple;
1754+
1755+
/* Ignore dropped columns */
1756+
if (att->attisdropped)
1757+
continue;
1758+
1759+
/*
1760+
* Get attstattarget from the old index and refresh the new value.
1761+
*/
1762+
attstattarget=get_attstattarget(oldIndexId,att->attnum);
1763+
1764+
/* no need for a refresh if both match */
1765+
if (attstattarget==att->attstattarget)
1766+
continue;
1767+
1768+
memset(repl_val,0,sizeof(repl_val));
1769+
memset(repl_null, false,sizeof(repl_null));
1770+
memset(repl_repl, false,sizeof(repl_repl));
1771+
1772+
repl_repl[Anum_pg_attribute_attstattarget-1]= true;
1773+
repl_val[Anum_pg_attribute_attstattarget-1]=Int32GetDatum(attstattarget);
1774+
1775+
newTuple=heap_modify_tuple(attrTuple,
1776+
RelationGetDescr(pg_attribute),
1777+
repl_val,repl_null,repl_repl);
1778+
CatalogTupleUpdate(pg_attribute,&newTuple->t_self,newTuple);
1779+
1780+
heap_freetuple(newTuple);
1781+
}
1782+
1783+
systable_endscan(scan);
1784+
table_close(pg_attribute,RowExclusiveLock);
1785+
}
1786+
17311787
/* Close relations */
17321788
table_close(pg_class,RowExclusiveLock);
17331789
table_close(pg_index,RowExclusiveLock);

‎src/backend/utils/cache/lsyscache.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,33 @@ get_attnum(Oid relid, const char *attname)
871871
returnInvalidAttrNumber;
872872
}
873873

874+
/*
875+
* get_attstattarget
876+
*
877+
*Given the relation id and the attribute number,
878+
*return the "attstattarget" field from the attribute relation.
879+
*
880+
*Errors if not found.
881+
*/
882+
int
883+
get_attstattarget(Oidrelid,AttrNumberattnum)
884+
{
885+
HeapTupletp;
886+
Form_pg_attributeatt_tup;
887+
intresult;
888+
889+
tp=SearchSysCache2(ATTNUM,
890+
ObjectIdGetDatum(relid),
891+
Int16GetDatum(attnum));
892+
if (!HeapTupleIsValid(tp))
893+
elog(ERROR,"cache lookup failed for attribute %d of relation %u",
894+
attnum,relid);
895+
att_tup= (Form_pg_attribute)GETSTRUCT(tp);
896+
result=att_tup->attstattarget;
897+
ReleaseSysCache(tp);
898+
returnresult;
899+
}
900+
874901
/*
875902
* get_attgenerated
876903
*

‎src/include/utils/lsyscache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ extern Oidget_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype,
8787
int16procnum);
8888
externchar*get_attname(Oidrelid,AttrNumberattnum,boolmissing_ok);
8989
externAttrNumberget_attnum(Oidrelid,constchar*attname);
90+
externintget_attstattarget(Oidrelid,AttrNumberattnum);
9091
externcharget_attgenerated(Oidrelid,AttrNumberattnum);
9192
externOidget_atttype(Oidrelid,AttrNumberattnum);
9293
externvoidget_atttypetypmodcoll(Oidrelid,AttrNumberattnum,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,7 @@ CREATE UNIQUE INDEX concur_exprs_index_pred ON concur_exprs_tab (c1)
24192419
CREATE UNIQUE INDEX concur_exprs_index_pred_2
24202420
ON concur_exprs_tab ((1 / c1))
24212421
WHERE ('-H') >= (c2::TEXT) COLLATE "C";
2422+
ALTER INDEX concur_exprs_index_expr ALTER COLUMN 1 SET STATISTICS 100;
24222423
ANALYZE concur_exprs_tab;
24232424
SELECT starelid::regclass, count(*) FROM pg_statistic WHERE starelid IN (
24242425
'concur_exprs_index_expr'::regclass,
@@ -2498,6 +2499,20 @@ SELECT starelid::regclass, count(*) FROM pg_statistic WHERE starelid IN (
24982499
concur_exprs_index_expr | 1
24992500
(1 row)
25002501

2502+
-- attstattarget should remain intact
2503+
SELECT attrelid::regclass, attnum, attstattarget
2504+
FROM pg_attribute WHERE attrelid IN (
2505+
'concur_exprs_index_expr'::regclass,
2506+
'concur_exprs_index_pred'::regclass,
2507+
'concur_exprs_index_pred_2'::regclass)
2508+
ORDER BY 'concur_exprs_index_expr'::regclass::text, attnum;
2509+
attrelid | attnum | attstattarget
2510+
---------------------------+--------+---------------
2511+
concur_exprs_index_expr | 1 | 100
2512+
concur_exprs_index_pred | 1 | -1
2513+
concur_exprs_index_pred_2 | 1 | -1
2514+
(3 rows)
2515+
25012516
DROP TABLE concur_exprs_tab;
25022517
-- Temporary tables and on-commit actions, where CONCURRENTLY is ignored.
25032518
-- ON COMMIT PRESERVE ROWS, the default.

‎src/test/regress/sql/create_index.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,7 @@ CREATE UNIQUE INDEX concur_exprs_index_pred ON concur_exprs_tab (c1)
10031003
CREATEUNIQUE INDEXconcur_exprs_index_pred_2
10041004
ON concur_exprs_tab ((1/ c1))
10051005
WHERE ('-H')>= (c2::TEXT) COLLATE"C";
1006+
ALTERINDEX concur_exprs_index_expr ALTER COLUMN1SET STATISTICS100;
10061007
ANALYZE concur_exprs_tab;
10071008
SELECT starelid::regclass,count(*)FROM pg_statisticWHERE starelidIN (
10081009
'concur_exprs_index_expr'::regclass,
@@ -1027,6 +1028,13 @@ SELECT starelid::regclass, count(*) FROM pg_statistic WHERE starelid IN (
10271028
'concur_exprs_index_pred'::regclass,
10281029
'concur_exprs_index_pred_2'::regclass)
10291030
GROUP BY starelidORDER BY starelid::regclass::text;
1031+
-- attstattarget should remain intact
1032+
SELECT attrelid::regclass, attnum, attstattarget
1033+
FROM pg_attributeWHERE attrelidIN (
1034+
'concur_exprs_index_expr'::regclass,
1035+
'concur_exprs_index_pred'::regclass,
1036+
'concur_exprs_index_pred_2'::regclass)
1037+
ORDER BY'concur_exprs_index_expr'::regclass::text, attnum;
10301038
DROPTABLE concur_exprs_tab;
10311039

10321040
-- Temporary tables and on-commit actions, where CONCURRENTLY is ignored.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp