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

Commit85b506b

Browse files
committed
Get rid of SET LOGGED indexes persistence kludge
This removes ATChangeIndexesPersistence() introduced byf41872dwhich was too ugly to live for long. Instead, the correct persistencemarking is passed all the way down to reindex_index, so that thetransient relation built to contain the index relfilenode canget marked correctly right from the start.Author: Fabrízio de Royes MelloReview and editorialization by Michael Paquier and Álvaro Herrera
1 parente4d1e26 commit85b506b

File tree

8 files changed

+56
-69
lines changed

8 files changed

+56
-69
lines changed

‎src/backend/catalog/index.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ index_build(Relation heapRelation,
19801980
* created it, or truncated twice in a subsequent transaction, the
19811981
* relfilenode won't change, and nothing needs to be done here.
19821982
*/
1983-
if (heapRelation->rd_rel->relpersistence==RELPERSISTENCE_UNLOGGED&&
1983+
if (indexRelation->rd_rel->relpersistence==RELPERSISTENCE_UNLOGGED&&
19841984
!smgrexists(indexRelation->rd_smgr,INIT_FORKNUM))
19851985
{
19861986
RegProcedureambuildempty=indexRelation->rd_am->ambuildempty;
@@ -3130,7 +3130,7 @@ IndexGetRelation(Oid indexId, bool missing_ok)
31303130
* reindex_index - This routine is used to recreate a single index
31313131
*/
31323132
void
3133-
reindex_index(OidindexId,boolskip_constraint_checks)
3133+
reindex_index(OidindexId,boolskip_constraint_checks,charpersistence)
31343134
{
31353135
RelationiRel,
31363136
heapRelation;
@@ -3191,6 +3191,9 @@ reindex_index(Oid indexId, bool skip_constraint_checks)
31913191
indexInfo->ii_ExclusionStrats=NULL;
31923192
}
31933193

3194+
/* Set the relpersistence of the new index */
3195+
iRel->rd_rel->relpersistence=persistence;
3196+
31943197
/* We'll build a new physical relation for the index */
31953198
RelationSetNewRelfilenode(iRel,InvalidTransactionId,
31963199
InvalidMultiXactId);
@@ -3310,6 +3313,12 @@ reindex_index(Oid indexId, bool skip_constraint_checks)
33103313
* performance, other callers should include the flag only after transforming
33113314
* the data in a manner that risks a change in constraint validity.
33123315
*
3316+
* REINDEX_REL_FORCE_INDEXES_UNLOGGED: if true, set the persistence of the
3317+
* rebuilt indexes to unlogged.
3318+
*
3319+
* REINDEX_REL_FORCE_INDEXES_LOGGED: if true, set the persistence of the
3320+
* rebuilt indexes to permanent.
3321+
*
33133322
* Returns true if any indexes were rebuilt (including toast table's index
33143323
* when relevant). Note that a CommandCounterIncrement will occur after each
33153324
* index rebuild.
@@ -3371,6 +3380,7 @@ reindex_relation(Oid relid, int flags)
33713380
{
33723381
List*doneIndexes;
33733382
ListCell*indexId;
3383+
charpersistence;
33743384

33753385
if (flags&REINDEX_REL_SUPPRESS_INDEX_USE)
33763386
{
@@ -3384,6 +3394,17 @@ reindex_relation(Oid relid, int flags)
33843394
CommandCounterIncrement();
33853395
}
33863396

3397+
/*
3398+
* Compute persistence of indexes: same as that of owning rel, unless
3399+
* caller specified otherwise.
3400+
*/
3401+
if (flags&REINDEX_REL_FORCE_INDEXES_UNLOGGED)
3402+
persistence=RELPERSISTENCE_UNLOGGED;
3403+
elseif (flags&REINDEX_REL_FORCE_INDEXES_PERMANENT)
3404+
persistence=RELPERSISTENCE_PERMANENT;
3405+
else
3406+
persistence=rel->rd_rel->relpersistence;
3407+
33873408
/* Reindex all the indexes. */
33883409
doneIndexes=NIL;
33893410
foreach(indexId,indexIds)
@@ -3393,7 +3414,8 @@ reindex_relation(Oid relid, int flags)
33933414
if (is_pg_class)
33943415
RelationSetIndexList(rel,doneIndexes,InvalidOid);
33953416

3396-
reindex_index(indexOid, !(flags&REINDEX_REL_CHECK_CONSTRAINTS));
3417+
reindex_index(indexOid, !(flags&REINDEX_REL_CHECK_CONSTRAINTS),
3418+
persistence);
33973419

33983420
CommandCounterIncrement();
33993421

‎src/backend/commands/cluster.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,8 @@ rebuild_relation(Relation OldHeap, Oid indexOid, bool verbose)
589589
*/
590590
finish_heap_swap(tableOid,OIDNewHeap,is_system_catalog,
591591
swap_toast_by_content, false, true,
592-
frozenXid,cutoffMulti);
592+
frozenXid,cutoffMulti,
593+
OldHeap->rd_rel->relpersistence);
593594
}
594595

595596

@@ -1475,7 +1476,8 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
14751476
boolcheck_constraints,
14761477
boolis_internal,
14771478
TransactionIdfrozenXid,
1478-
MultiXactIdcutoffMulti)
1479+
MultiXactIdcutoffMulti,
1480+
charnewrelpersistence)
14791481
{
14801482
ObjectAddressobject;
14811483
Oidmapped_tables[4];
@@ -1519,6 +1521,16 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
15191521
reindex_flags=REINDEX_REL_SUPPRESS_INDEX_USE;
15201522
if (check_constraints)
15211523
reindex_flags |=REINDEX_REL_CHECK_CONSTRAINTS;
1524+
1525+
/*
1526+
* Ensure that the indexes have the same persistence as the parent
1527+
* relation.
1528+
*/
1529+
if (newrelpersistence==RELPERSISTENCE_UNLOGGED)
1530+
reindex_flags |=REINDEX_REL_FORCE_INDEXES_UNLOGGED;
1531+
elseif (newrelpersistence==RELPERSISTENCE_PERMANENT)
1532+
reindex_flags |=REINDEX_REL_FORCE_INDEXES_PERMANENT;
1533+
15221534
reindex_relation(OIDOldHeap,reindex_flags);
15231535

15241536
/*

‎src/backend/commands/indexcmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,7 @@ ReindexIndex(RangeVar *indexRelation)
16891689
RangeVarCallbackForReindexIndex,
16901690
(void*)&heapOid);
16911691

1692-
reindex_index(indOid, false);
1692+
reindex_index(indOid, false,indexRelation->relpersistence);
16931693

16941694
returnindOid;
16951695
}

‎src/backend/commands/matview.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void mv_GenerateOper(StringInfo buf, Oid opoid);
6767

6868
staticvoidrefresh_by_match_merge(OidmatviewOid,OidtempOid,Oidrelowner,
6969
intsave_sec_context);
70-
staticvoidrefresh_by_heap_swap(OidmatviewOid,OidOIDNewHeap);
70+
staticvoidrefresh_by_heap_swap(OidmatviewOid,OidOIDNewHeap,charrelpersistence);
7171

7272
staticvoidOpenMatViewIncrementalMaintenance(void);
7373
staticvoidCloseMatViewIncrementalMaintenance(void);
@@ -303,7 +303,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
303303
Assert(matview_maintenance_depth==old_depth);
304304
}
305305
else
306-
refresh_by_heap_swap(matviewOid,OIDNewHeap);
306+
refresh_by_heap_swap(matviewOid,OIDNewHeap,relpersistence);
307307

308308
/* Roll back any GUC changes */
309309
AtEOXact_GUC(false,save_nestlevel);
@@ -759,10 +759,10 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
759759
* swapping is handled by the called function, so it is not needed here.
760760
*/
761761
staticvoid
762-
refresh_by_heap_swap(OidmatviewOid,OidOIDNewHeap)
762+
refresh_by_heap_swap(OidmatviewOid,OidOIDNewHeap,charrelpersistence)
763763
{
764764
finish_heap_swap(matviewOid,OIDNewHeap, false, false, true, true,
765-
RecentXmin,ReadNextMultiXactId());
765+
RecentXmin,ReadNextMultiXactId(),relpersistence);
766766
}
767767

768768

‎src/backend/commands/tablecmds.c

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,6 @@ static void ATExecClusterOn(Relation rel, const char *indexName,
393393
LOCKMODElockmode);
394394
staticvoidATExecDropCluster(Relationrel,LOCKMODElockmode);
395395
staticboolATPrepChangePersistence(Relationrel,booltoLogged);
396-
staticvoidATChangeIndexesPersistence(Oidrelid,charrelpersistence);
397396
staticvoidATPrepSetTableSpace(AlteredTableInfo*tab,Relationrel,
398397
char*tablespacename,LOCKMODElockmode);
399398
staticvoidATExecSetTableSpace(OidtableOid,OidnewTableSpace,LOCKMODElockmode);
@@ -3734,16 +3733,6 @@ ATRewriteTables(List **wqueue, LOCKMODE lockmode)
37343733
*/
37353734
ATRewriteTable(tab,OIDNewHeap,lockmode);
37363735

3737-
/*
3738-
* Change the persistence marking of indexes, if necessary. This
3739-
* is so that the new copies are built with the right persistence
3740-
* in the reindex step below. Note we cannot do this earlier,
3741-
* because the rewrite step might read the indexes, and that would
3742-
* cause buffers for them to have the wrong setting.
3743-
*/
3744-
if (tab->chgPersistence)
3745-
ATChangeIndexesPersistence(tab->relid,tab->newrelpersistence);
3746-
37473736
/*
37483737
* Swap the physical files of the old and new heaps, then rebuild
37493738
* indexes and discard the old heap. We can use RecentXmin for
@@ -3756,7 +3745,8 @@ ATRewriteTables(List **wqueue, LOCKMODE lockmode)
37563745
false, false, true,
37573746
!OidIsValid(tab->newTableSpace),
37583747
RecentXmin,
3759-
ReadNextMultiXactId());
3748+
ReadNextMultiXactId(),
3749+
persistence);
37603750
}
37613751
else
37623752
{
@@ -10879,48 +10869,6 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
1087910869
return true;
1088010870
}
1088110871

10882-
/*
10883-
* Update the pg_class entry of each index for the given relation to the
10884-
* given persistence.
10885-
*/
10886-
staticvoid
10887-
ATChangeIndexesPersistence(Oidrelid,charrelpersistence)
10888-
{
10889-
Relationrel;
10890-
Relationpg_class;
10891-
List*indexes;
10892-
ListCell*cell;
10893-
10894-
pg_class=heap_open(RelationRelationId,RowExclusiveLock);
10895-
10896-
/* We already have a lock on the table */
10897-
rel=relation_open(relid,NoLock);
10898-
indexes=RelationGetIndexList(rel);
10899-
foreach(cell,indexes)
10900-
{
10901-
Oidindexid=lfirst_oid(cell);
10902-
HeapTupletuple;
10903-
Form_pg_classpg_class_form;
10904-
10905-
tuple=SearchSysCacheCopy1(RELOID,ObjectIdGetDatum(indexid));
10906-
if (!HeapTupleIsValid(tuple))
10907-
elog(ERROR,"cache lookup failed for relation %u",
10908-
indexid);
10909-
10910-
pg_class_form= (Form_pg_class)GETSTRUCT(tuple);
10911-
pg_class_form->relpersistence=relpersistence;
10912-
simple_heap_update(pg_class,&tuple->t_self,tuple);
10913-
10914-
/* keep catalog indexes current */
10915-
CatalogUpdateIndexes(pg_class,tuple);
10916-
10917-
heap_freetuple(tuple);
10918-
}
10919-
10920-
heap_close(pg_class,RowExclusiveLock);
10921-
heap_close(rel,NoLock);
10922-
}
10923-
1092410872
/*
1092510873
* Execute ALTER TABLE SET SCHEMA
1092610874
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,6 +3078,7 @@ RelationSetNewRelfilenode(Relation relation, TransactionId freezeXid,
30783078
}
30793079
classform->relfrozenxid=freezeXid;
30803080
classform->relminmxid=minmulti;
3081+
classform->relpersistence=relation->rd_rel->relpersistence;
30813082

30823083
simple_heap_update(pg_class,&tuple->t_self,tuple);
30833084
CatalogUpdateIndexes(pg_class,tuple);

‎src/include/catalog/index.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,15 @@ extern void validate_index(Oid heapId, Oid indexId, Snapshot snapshot);
111111

112112
externvoidindex_set_state_flags(OidindexId,IndexStateFlagsActionaction);
113113

114-
externvoidreindex_index(OidindexId,boolskip_constraint_checks);
114+
externvoidreindex_index(OidindexId,boolskip_constraint_checks,
115+
charrelpersistence);
115116

116117
/* Flag bits for reindex_relation(): */
117-
#defineREINDEX_REL_PROCESS_TOAST0x01
118-
#defineREINDEX_REL_SUPPRESS_INDEX_USE0x02
119-
#defineREINDEX_REL_CHECK_CONSTRAINTS0x04
118+
#defineREINDEX_REL_PROCESS_TOAST0x01
119+
#defineREINDEX_REL_SUPPRESS_INDEX_USE0x02
120+
#defineREINDEX_REL_CHECK_CONSTRAINTS0x04
121+
#defineREINDEX_REL_FORCE_INDEXES_UNLOGGED0x08
122+
#defineREINDEX_REL_FORCE_INDEXES_PERMANENT0x10
120123

121124
externboolreindex_relation(Oidrelid,intflags);
122125

‎src/include/commands/cluster.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern void finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
3333
boolcheck_constraints,
3434
boolis_internal,
3535
TransactionIdfrozenXid,
36-
MultiXactIdminMulti);
36+
MultiXactIdminMulti,
37+
charnewrelpersistence);
3738

3839
#endif/* CLUSTER_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp