Skip to content

Commit f8103ed

Browse files
e-ivkovhomper
authored andcommitted
TableAM: add relation_reindex
1 parent 3a15cc3 commit f8103ed

File tree

7 files changed

+72
-28
lines changed

7 files changed

+72
-28
lines changed

src/backend/access/heap/heapam_handler.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,12 @@ heapam_relation_nontransactional_truncate(Relation rel)
982982
RelationTruncate(rel, 0);
983983
}
984984

985+
static bool
986+
heapam_relation_reindex(Relation rel, int flags, ReindexParams *params)
987+
{
988+
return reindex_relation(rel, flags, params);
989+
}
990+
985991
static void
986992
heapam_relation_copy_data(Relation rel, const RelFileLocator *newrlocator)
987993
{
@@ -2955,6 +2961,7 @@ static const TableAmRoutine heapam_methods = {
29552961

29562962
.relation_set_new_filelocator = heapam_relation_set_new_filelocator,
29572963
.relation_nontransactional_truncate = heapam_relation_nontransactional_truncate,
2964+
.relation_reindex = heapam_relation_reindex,
29582965
.relation_copy_data = heapam_relation_copy_data,
29592966
.relation_copy_for_cluster = heapam_relation_copy_for_cluster,
29602967
.relation_vacuum = heap_vacuum_rel,

src/backend/catalog/index.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3891,25 +3891,15 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
38913891
* index rebuild.
38923892
*/
38933893
bool
3894-
reindex_relation(Oid relid, int flags, ReindexParams *params)
3894+
reindex_relation(Relation rel, int flags, ReindexParams *params)
38953895
{
3896-
Relation rel;
38973896
Oid toast_relid;
38983897
List *indexIds;
38993898
char persistence;
39003899
bool result;
39013900
ListCell *indexId;
39023901
int i;
39033902

3904-
/*
3905-
* Open and lock the relation. ShareLock is sufficient since we only need
3906-
* to prevent schema and data changes in it. The lock level used here
3907-
* should match ReindexTable().
3908-
*/
3909-
if ((params->options & REINDEXOPT_MISSING_OK) != 0)
3910-
rel = try_table_open(relid, ShareLock);
3911-
else
3912-
rel = table_open(relid, ShareLock);
39133903

39143904
/* if relation is gone, leave */
39153905
if (!rel)
@@ -4001,11 +3991,6 @@ reindex_relation(Oid relid, int flags, ReindexParams *params)
40013991
i++;
40023992
}
40033993

4004-
/*
4005-
* Close rel, but continue to hold the lock.
4006-
*/
4007-
table_close(rel, NoLock);
4008-
40093994
result = (indexIds != NIL);
40103995

40113996
/*
@@ -4021,10 +4006,20 @@ reindex_relation(Oid relid, int flags, ReindexParams *params)
40214006
* This rule is enforced by setting tablespaceOid to InvalidOid.
40224007
*/
40234008
ReindexParams newparams = *params;
4009+
/*
4010+
* Open and lock the relation. ShareLock is sufficient since we only need
4011+
* to prevent schema and data changes in it. The lock level used here
4012+
* should match ReindexTable().
4013+
*/
4014+
Relation toast_rel = table_open(toast_relid, ShareLock);
40244015

40254016
newparams.options &= ~(REINDEXOPT_MISSING_OK);
40264017
newparams.tablespaceOid = InvalidOid;
4027-
result |= reindex_relation(toast_relid, flags, &newparams);
4018+
result |= reindex_relation(toast_rel, flags, &newparams);
4019+
/*
4020+
* Close rel, but continue to hold the lock.
4021+
*/
4022+
table_close(toast_rel, NoLock);
40284023
}
40294024

40304025
return result;

src/backend/commands/cluster.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,7 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
14621462
int reindex_flags;
14631463
ReindexParams reindex_params = {0};
14641464
int i;
1465+
Relation oldHeap;
14651466

14661467
/* Report that we are now swapping relation files */
14671468
pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE,
@@ -1517,8 +1518,19 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
15171518
/* Report that we are now reindexing relations */
15181519
pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE,
15191520
PROGRESS_CLUSTER_PHASE_REBUILD_INDEX);
1521+
/*
1522+
* Open and lock the relation. ShareLock is sufficient since we only need
1523+
* to prevent schema and data changes in it. The lock level used here
1524+
* should match ReindexTable().
1525+
*/
1526+
oldHeap = table_open(OIDOldHeap, ShareLock);
1527+
1528+
reindex_relation(oldHeap, reindex_flags, &reindex_params);
1529+
/*
1530+
* Close rel, but continue to hold the lock.
1531+
*/
1532+
table_close(oldHeap, NoLock);
15201533

1521-
reindex_relation(OIDOldHeap, reindex_flags, &reindex_params);
15221534

15231535
/* Report that we are now doing clean up */
15241536
pgstat_progress_update_param(PROGRESS_CLUSTER_PHASE,

src/backend/commands/indexcmds.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,12 +2943,23 @@ ReindexTable(RangeVar *relation, ReindexParams *params, bool isTopLevel)
29432943
else
29442944
{
29452945
ReindexParams newparams = *params;
2946+
Relation rel;
29462947

29472948
newparams.options |= REINDEXOPT_REPORT_PROGRESS;
2948-
result = reindex_relation(heapOid,
2949-
REINDEX_REL_PROCESS_TOAST |
2950-
REINDEX_REL_CHECK_CONSTRAINTS,
2951-
&newparams);
2949+
if ((newparams.options & REINDEXOPT_MISSING_OK) != 0)
2950+
rel = try_table_open(heapOid, ShareLock);
2951+
else
2952+
rel = table_open(heapOid, ShareLock);
2953+
2954+
result = table_relation_reindex(rel,
2955+
REINDEX_REL_PROCESS_TOAST |
2956+
REINDEX_REL_CHECK_CONSTRAINTS,
2957+
&newparams);
2958+
/*
2959+
* Close rel, but continue to hold the lock.
2960+
*/
2961+
table_close(rel, NoLock);
2962+
29522963
if (!result)
29532964
ereport(NOTICE,
29542965
(errmsg("table \"%s\" has no indexes to reindex",
@@ -3376,13 +3387,24 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
33763387
{
33773388
bool result;
33783389
ReindexParams newparams = *params;
3390+
Relation rel;
33793391

33803392
newparams.options |=
33813393
REINDEXOPT_REPORT_PROGRESS | REINDEXOPT_MISSING_OK;
3382-
result = reindex_relation(relid,
3394+
/*
3395+
* Open and lock the relation. ShareLock is sufficient since we only need
3396+
* to prevent schema and data changes in it. The lock level used here
3397+
* should match ReindexTable().
3398+
*/
3399+
rel = try_table_open(relid, ShareLock);
3400+
result = table_relation_reindex(rel,
33833401
REINDEX_REL_PROCESS_TOAST |
33843402
REINDEX_REL_CHECK_CONSTRAINTS,
33853403
&newparams);
3404+
/*
3405+
* Close rel, but continue to hold the lock.
3406+
*/
3407+
table_close(rel, NoLock);
33863408

33873409
if (result && (params->options & REINDEXOPT_VERBOSE) != 0)
33883410
ereport(INFO,

src/backend/commands/tablecmds.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,6 @@ ExecuteTruncateGuts(List *explicit_rels,
20372037
}
20382038
else
20392039
{
2040-
Oid heap_relid;
20412040
Oid toast_relid;
20422041
ReindexParams reindex_params = {0};
20432042

@@ -2058,8 +2057,6 @@ ExecuteTruncateGuts(List *explicit_rels,
20582057
*/
20592058
RelationSetNewRelfilenumber(rel, rel->rd_rel->relpersistence);
20602059

2061-
heap_relid = RelationGetRelid(rel);
2062-
20632060
/*
20642061
* The same for the toast table, if any.
20652062
*/
@@ -2077,7 +2074,7 @@ ExecuteTruncateGuts(List *explicit_rels,
20772074
/*
20782075
* Reconstruct the indexes to match, and we're done.
20792076
*/
2080-
reindex_relation(heap_relid, REINDEX_REL_PROCESS_TOAST,
2077+
table_relation_reindex(rel, REINDEX_REL_PROCESS_TOAST,
20812078
&reindex_params);
20822079
}
20832080

src/include/access/tableam.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "utils/guc.h"
2828
#include "utils/rel.h"
2929
#include "utils/snapshot.h"
30+
#include "catalog/index.h"
3031

3132

3233
#define DEFAULT_TABLE_ACCESS_METHOD "heap"
@@ -639,6 +640,9 @@ typedef struct TableAmRoutine
639640
*/
640641
void (*relation_nontransactional_truncate) (Relation rel);
641642

643+
/* See reindex_relation for reference about parameters */
644+
bool (*relation_reindex) (Relation rel, int flags, ReindexParams *params);
645+
642646
/*
643647
* See table_relation_copy_data().
644648
*
@@ -1687,6 +1691,13 @@ table_relation_nontransactional_truncate(Relation rel)
16871691
rel->rd_tableam->relation_nontransactional_truncate(rel);
16881692
}
16891693

1694+
/* See reindex_relation for reference about parameters */
1695+
static inline bool
1696+
table_relation_reindex(Relation rel, int flags, ReindexParams *params)
1697+
{
1698+
return rel->rd_tableam->relation_reindex(rel, flags, params);
1699+
}
1700+
16901701
/*
16911702
* Copy data from `rel` into the new relfilelocator `newrlocator`. The new
16921703
* relfilelocator may not have storage associated before this function is

src/include/catalog/index.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ extern void reindex_index(Oid indexId, bool skip_constraint_checks,
158158
#define REINDEX_REL_FORCE_INDEXES_UNLOGGED 0x08
159159
#define REINDEX_REL_FORCE_INDEXES_PERMANENT 0x10
160160

161-
extern bool reindex_relation(Oid relid, int flags, ReindexParams *params);
161+
extern bool reindex_relation(Relation rel, int flags, ReindexParams *params);
162162

163163
extern bool ReindexIsProcessingHeap(Oid heapOid);
164164
extern bool ReindexIsProcessingIndex(Oid indexOid);

0 commit comments

Comments
 (0)