@@ -107,7 +107,7 @@ static TupleDesc ConstructTupleDescriptor(Relation heapRelation,
107107const Oid * opclassIds );
108108static void InitializeAttributeOids (Relation indexRelation ,
109109int numatts ,Oid indexoid );
110- static void AppendAttributeTuples (Relation indexRelation ,const Datum * attopts );
110+ static void AppendAttributeTuples (Relation indexRelation ,const Datum * attopts , const NullableDatum * stattargets );
111111static void UpdateIndexRelation (Oid indexoid ,Oid heapoid ,
112112Oid parentIndexId ,
113113const IndexInfo * indexInfo ,
@@ -507,7 +507,7 @@ InitializeAttributeOids(Relation indexRelation,
507507 * ----------------------------------------------------------------
508508 */
509509static void
510- AppendAttributeTuples (Relation indexRelation ,const Datum * attopts )
510+ AppendAttributeTuples (Relation indexRelation ,const Datum * attopts , const NullableDatum * stattargets )
511511{
512512Relation pg_attribute ;
513513CatalogIndexState indstate ;
@@ -524,6 +524,11 @@ AppendAttributeTuples(Relation indexRelation, const Datum *attopts)
524524attrs_extra [i ].attoptions .value = attopts [i ];
525525else
526526attrs_extra [i ].attoptions .isnull = true;
527+
528+ if (stattargets )
529+ attrs_extra [i ].attstattarget = stattargets [i ];
530+ else
531+ attrs_extra [i ].attstattarget .isnull = true;
527532}
528533}
529534
@@ -730,6 +735,7 @@ index_create(Relation heapRelation,
730735const Oid * opclassIds ,
731736const Datum * opclassOptions ,
732737const int16 * coloptions ,
738+ const NullableDatum * stattargets ,
733739Datum reloptions ,
734740bits16 flags ,
735741bits16 constr_flags ,
@@ -1024,7 +1030,7 @@ index_create(Relation heapRelation,
10241030/*
10251031 * append ATTRIBUTE tuples for the index
10261032 */
1027- AppendAttributeTuples (indexRelation ,opclassOptions );
1033+ AppendAttributeTuples (indexRelation ,opclassOptions , stattargets );
10281034
10291035/* ----------------
10301036 * update pg_index
@@ -1303,6 +1309,7 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId,
13031309Datum * opclassOptions ;
13041310oidvector * indclass ;
13051311int2vector * indcoloptions ;
1312+ NullableDatum * stattargets ;
13061313bool isnull ;
13071314List * indexColNames = NIL ;
13081315List * indexExprs = NIL ;
@@ -1407,6 +1414,23 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId,
14071414for (int i = 0 ;i < newInfo -> ii_NumIndexAttrs ;i ++ )
14081415opclassOptions [i ]= get_attoptions (oldIndexId ,i + 1 );
14091416
1417+ /* Extract statistic targets for each attribute */
1418+ stattargets = palloc0_array (NullableDatum ,newInfo -> ii_NumIndexAttrs );
1419+ for (int i = 0 ;i < newInfo -> ii_NumIndexAttrs ;i ++ )
1420+ {
1421+ HeapTuple tp ;
1422+ Datum dat ;
1423+
1424+ tp = SearchSysCache2 (ATTNUM ,ObjectIdGetDatum (oldIndexId ),Int16GetDatum (i + 1 ));
1425+ if (!HeapTupleIsValid (tp ))
1426+ elog (ERROR ,"cache lookup failed for attribute %d of relation %u" ,
1427+ i + 1 ,oldIndexId );
1428+ dat = SysCacheGetAttr (ATTNUM ,tp ,Anum_pg_attribute_attstattarget ,& isnull );
1429+ ReleaseSysCache (tp );
1430+ stattargets [i ].value = dat ;
1431+ stattargets [i ].isnull = isnull ;
1432+ }
1433+
14101434/*
14111435 * Now create the new index.
14121436 *
@@ -1428,6 +1452,7 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId,
14281452indclass -> values ,
14291453opclassOptions ,
14301454indcoloptions -> values ,
1455+ stattargets ,
14311456reloptionsDatum ,
14321457INDEX_CREATE_SKIP_BUILD |INDEX_CREATE_CONCURRENT ,
143314580 ,
@@ -1771,72 +1796,6 @@ index_concurrently_swap(Oid newIndexId, Oid oldIndexId, const char *oldName)
17711796/* Copy data of pg_statistic from the old index to the new one */
17721797CopyStatistics (oldIndexId ,newIndexId );
17731798
1774- /* Copy pg_attribute.attstattarget for each index attribute */
1775- {
1776- HeapTuple attrTuple ;
1777- Relation pg_attribute ;
1778- SysScanDesc scan ;
1779- ScanKeyData key [1 ];
1780-
1781- pg_attribute = table_open (AttributeRelationId ,RowExclusiveLock );
1782- ScanKeyInit (& key [0 ],
1783- Anum_pg_attribute_attrelid ,
1784- BTEqualStrategyNumber ,F_OIDEQ ,
1785- ObjectIdGetDatum (newIndexId ));
1786- scan = systable_beginscan (pg_attribute ,AttributeRelidNumIndexId ,
1787- true,NULL ,1 ,key );
1788-
1789- while (HeapTupleIsValid ((attrTuple = systable_getnext (scan ))))
1790- {
1791- Form_pg_attribute att = (Form_pg_attribute )GETSTRUCT (attrTuple );
1792- HeapTuple tp ;
1793- Datum dat ;
1794- bool isnull ;
1795- Datum repl_val [Natts_pg_attribute ];
1796- bool repl_null [Natts_pg_attribute ];
1797- bool repl_repl [Natts_pg_attribute ];
1798- HeapTuple newTuple ;
1799-
1800- /* Ignore dropped columns */
1801- if (att -> attisdropped )
1802- continue ;
1803-
1804- /*
1805- * Get attstattarget from the old index and refresh the new value.
1806- */
1807- tp = SearchSysCache2 (ATTNUM ,ObjectIdGetDatum (oldIndexId ),Int16GetDatum (att -> attnum ));
1808- if (!HeapTupleIsValid (tp ))
1809- elog (ERROR ,"cache lookup failed for attribute %d of relation %u" ,
1810- att -> attnum ,oldIndexId );
1811- dat = SysCacheGetAttr (ATTNUM ,tp ,Anum_pg_attribute_attstattarget ,& isnull );
1812- ReleaseSysCache (tp );
1813-
1814- /*
1815- * No need for a refresh if old index value is null. (All new
1816- * index values are null at this point.)
1817- */
1818- if (isnull )
1819- continue ;
1820-
1821- memset (repl_val ,0 ,sizeof (repl_val ));
1822- memset (repl_null , false,sizeof (repl_null ));
1823- memset (repl_repl , false,sizeof (repl_repl ));
1824-
1825- repl_repl [Anum_pg_attribute_attstattarget - 1 ]= true;
1826- repl_val [Anum_pg_attribute_attstattarget - 1 ]= dat ;
1827-
1828- newTuple = heap_modify_tuple (attrTuple ,
1829- RelationGetDescr (pg_attribute ),
1830- repl_val ,repl_null ,repl_repl );
1831- CatalogTupleUpdate (pg_attribute ,& newTuple -> t_self ,newTuple );
1832-
1833- heap_freetuple (newTuple );
1834- }
1835-
1836- systable_endscan (scan );
1837- table_close (pg_attribute ,RowExclusiveLock );
1838- }
1839-
18401799/* Close relations */
18411800table_close (pg_class ,RowExclusiveLock );
18421801table_close (pg_index ,RowExclusiveLock );