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

Commit85edb1f

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 parent1b9eb7c commit85edb1f

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
@@ -1717,6 +1717,62 @@ index_concurrently_swap(Oid newIndexId, Oid oldIndexId, const char *oldName)
17171717
/* Copy data of pg_statistic from the old index to the new one */
17181718
CopyStatistics(oldIndexId,newIndexId);
17191719

1720+
/* Copy pg_attribute.attstattarget for each index attribute */
1721+
{
1722+
HeapTupleattrTuple;
1723+
Relationpg_attribute;
1724+
SysScanDescscan;
1725+
ScanKeyDatakey[1];
1726+
1727+
pg_attribute=table_open(AttributeRelationId,RowExclusiveLock);
1728+
ScanKeyInit(&key[0],
1729+
Anum_pg_attribute_attrelid,
1730+
BTEqualStrategyNumber,F_OIDEQ,
1731+
ObjectIdGetDatum(newIndexId));
1732+
scan=systable_beginscan(pg_attribute,AttributeRelidNumIndexId,
1733+
true,NULL,1,key);
1734+
1735+
while (HeapTupleIsValid((attrTuple=systable_getnext(scan))))
1736+
{
1737+
Form_pg_attributeatt= (Form_pg_attribute)GETSTRUCT(attrTuple);
1738+
Datumrepl_val[Natts_pg_attribute];
1739+
boolrepl_null[Natts_pg_attribute];
1740+
boolrepl_repl[Natts_pg_attribute];
1741+
intattstattarget;
1742+
HeapTuplenewTuple;
1743+
1744+
/* Ignore dropped columns */
1745+
if (att->attisdropped)
1746+
continue;
1747+
1748+
/*
1749+
* Get attstattarget from the old index and refresh the new value.
1750+
*/
1751+
attstattarget=get_attstattarget(oldIndexId,att->attnum);
1752+
1753+
/* no need for a refresh if both match */
1754+
if (attstattarget==att->attstattarget)
1755+
continue;
1756+
1757+
memset(repl_val,0,sizeof(repl_val));
1758+
memset(repl_null, false,sizeof(repl_null));
1759+
memset(repl_repl, false,sizeof(repl_repl));
1760+
1761+
repl_repl[Anum_pg_attribute_attstattarget-1]= true;
1762+
repl_val[Anum_pg_attribute_attstattarget-1]=Int32GetDatum(attstattarget);
1763+
1764+
newTuple=heap_modify_tuple(attrTuple,
1765+
RelationGetDescr(pg_attribute),
1766+
repl_val,repl_null,repl_repl);
1767+
CatalogTupleUpdate(pg_attribute,&newTuple->t_self,newTuple);
1768+
1769+
heap_freetuple(newTuple);
1770+
}
1771+
1772+
systable_endscan(scan);
1773+
table_close(pg_attribute,RowExclusiveLock);
1774+
}
1775+
17201776
/* Close relations */
17211777
table_close(pg_class,RowExclusiveLock);
17221778
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
@@ -821,6 +821,33 @@ get_attnum(Oid relid, const char *attname)
821821
returnInvalidAttrNumber;
822822
}
823823

824+
/*
825+
* get_attstattarget
826+
*
827+
*Given the relation id and the attribute number,
828+
*return the "attstattarget" field from the attribute relation.
829+
*
830+
*Errors if not found.
831+
*/
832+
int
833+
get_attstattarget(Oidrelid,AttrNumberattnum)
834+
{
835+
HeapTupletp;
836+
Form_pg_attributeatt_tup;
837+
intresult;
838+
839+
tp=SearchSysCache2(ATTNUM,
840+
ObjectIdGetDatum(relid),
841+
Int16GetDatum(attnum));
842+
if (!HeapTupleIsValid(tp))
843+
elog(ERROR,"cache lookup failed for attribute %d of relation %u",
844+
attnum,relid);
845+
att_tup= (Form_pg_attribute)GETSTRUCT(tp);
846+
result=att_tup->attstattarget;
847+
ReleaseSysCache(tp);
848+
returnresult;
849+
}
850+
824851
/*
825852
* get_attgenerated
826853
*

‎src/include/utils/lsyscache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ extern Oidget_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype,
8686
int16procnum);
8787
externchar*get_attname(Oidrelid,AttrNumberattnum,boolmissing_ok);
8888
externAttrNumberget_attnum(Oidrelid,constchar*attname);
89+
externintget_attstattarget(Oidrelid,AttrNumberattnum);
8990
externcharget_attgenerated(Oidrelid,AttrNumberattnum);
9091
externOidget_atttype(Oidrelid,AttrNumberattnum);
9192
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
@@ -2406,6 +2406,7 @@ CREATE UNIQUE INDEX concur_exprs_index_pred ON concur_exprs_tab (c1)
24062406
CREATE UNIQUE INDEX concur_exprs_index_pred_2
24072407
ON concur_exprs_tab ((1 / c1))
24082408
WHERE ('-H') >= (c2::TEXT) COLLATE "C";
2409+
ALTER INDEX concur_exprs_index_expr ALTER COLUMN 1 SET STATISTICS 100;
24092410
ANALYZE concur_exprs_tab;
24102411
SELECT starelid::regclass, count(*) FROM pg_statistic WHERE starelid IN (
24112412
'concur_exprs_index_expr'::regclass,
@@ -2485,6 +2486,20 @@ SELECT starelid::regclass, count(*) FROM pg_statistic WHERE starelid IN (
24852486
concur_exprs_index_expr | 1
24862487
(1 row)
24872488

2489+
-- attstattarget should remain intact
2490+
SELECT attrelid::regclass, attnum, attstattarget
2491+
FROM pg_attribute WHERE attrelid IN (
2492+
'concur_exprs_index_expr'::regclass,
2493+
'concur_exprs_index_pred'::regclass,
2494+
'concur_exprs_index_pred_2'::regclass)
2495+
ORDER BY 'concur_exprs_index_expr'::regclass::text, attnum;
2496+
attrelid | attnum | attstattarget
2497+
---------------------------+--------+---------------
2498+
concur_exprs_index_expr | 1 | 100
2499+
concur_exprs_index_pred | 1 | -1
2500+
concur_exprs_index_pred_2 | 1 | -1
2501+
(3 rows)
2502+
24882503
DROP TABLE concur_exprs_tab;
24892504
-- Temporary tables and on-commit actions, where CONCURRENTLY is ignored.
24902505
-- 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
@@ -997,6 +997,7 @@ CREATE UNIQUE INDEX concur_exprs_index_pred ON concur_exprs_tab (c1)
997997
CREATEUNIQUE INDEXconcur_exprs_index_pred_2
998998
ON concur_exprs_tab ((1/ c1))
999999
WHERE ('-H')>= (c2::TEXT) COLLATE"C";
1000+
ALTERINDEX concur_exprs_index_expr ALTER COLUMN1SET STATISTICS100;
10001001
ANALYZE concur_exprs_tab;
10011002
SELECT starelid::regclass,count(*)FROM pg_statisticWHERE starelidIN (
10021003
'concur_exprs_index_expr'::regclass,
@@ -1021,6 +1022,13 @@ SELECT starelid::regclass, count(*) FROM pg_statistic WHERE starelid IN (
10211022
'concur_exprs_index_pred'::regclass,
10221023
'concur_exprs_index_pred_2'::regclass)
10231024
GROUP BY starelidORDER BY starelid::regclass::text;
1025+
-- attstattarget should remain intact
1026+
SELECT attrelid::regclass, attnum, attstattarget
1027+
FROM pg_attributeWHERE attrelidIN (
1028+
'concur_exprs_index_expr'::regclass,
1029+
'concur_exprs_index_pred'::regclass,
1030+
'concur_exprs_index_pred_2'::regclass)
1031+
ORDER BY'concur_exprs_index_expr'::regclass::text, attnum;
10241032
DROPTABLE concur_exprs_tab;
10251033

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp