Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8493831

Browse files
committed
Preserve pg_attribute.attstattarget across REINDEX CONCURRENTLY
For an index, attstattarget can be updated using ALTER INDEX SET STATISTICS. This data was lost on the new index after REINDEX CONCURRENTLY. The update of this field is done when the old and new indexes are swapped to make the fix back-patchable. Another approach we could look after in the long-term is to change index_create() to pass the wanted values of attstattarget when creating the new relation, but, as this would cause an ABI breakage this can be done only on HEAD. Reported-by: Ronan Dunklau Author: Michael Paquier Reviewed-by: Ronan Dunklau, Tomas Vondra Discussion: https://postgr.es/m/16628084.uLZWGnKmhe@laptop-ronand Backpatch-through: 12
1 parent 3fb4c75 commit 8493831

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

src/backend/catalog/index.c

+56
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+
HeapTuple attrTuple;
1734+
Relation pg_attribute;
1735+
SysScanDesc scan;
1736+
ScanKeyData key[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_attribute att = (Form_pg_attribute) GETSTRUCT(attrTuple);
1749+
Datum repl_val[Natts_pg_attribute];
1750+
bool repl_null[Natts_pg_attribute];
1751+
bool repl_repl[Natts_pg_attribute];
1752+
int attstattarget;
1753+
HeapTuple newTuple;
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

+27
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,33 @@ get_attnum(Oid relid, const char *attname)
871871
return InvalidAttrNumber;
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(Oid relid, AttrNumber attnum)
884+
{
885+
HeapTuple tp;
886+
Form_pg_attribute att_tup;
887+
int result;
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+
return result;
899+
}
900+
874901
/*
875902
* get_attgenerated
876903
*

src/include/utils/lsyscache.h

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ extern Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype,
8787
int16 procnum);
8888
extern char *get_attname(Oid relid, AttrNumber attnum, bool missing_ok);
8989
extern AttrNumber get_attnum(Oid relid, const char *attname);
90+
extern int get_attstattarget(Oid relid, AttrNumber attnum);
9091
extern char get_attgenerated(Oid relid, AttrNumber attnum);
9192
extern Oid get_atttype(Oid relid, AttrNumber attnum);
9293
extern void get_atttypetypmodcoll(Oid relid, AttrNumber attnum,

src/test/regress/expected/create_index.out

+15
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

+8
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
CREATE UNIQUE INDEX concur_exprs_index_pred_2
10041004
ON concur_exprs_tab ((1 / c1))
10051005
WHERE ('-H') >= (c2::TEXT) COLLATE "C";
1006+
ALTER INDEX concur_exprs_index_expr ALTER COLUMN 1 SET STATISTICS 100;
10061007
ANALYZE concur_exprs_tab;
10071008
SELECT starelid::regclass, count(*) FROM pg_statistic WHERE starelid IN (
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 starelid ORDER BY starelid::regclass::text;
1031+
-- attstattarget should remain intact
1032+
SELECT attrelid::regclass, attnum, attstattarget
1033+
FROM pg_attribute WHERE attrelid IN (
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
DROP TABLE concur_exprs_tab;
10311039

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

0 commit comments

Comments
 (0)