Skip to content

Commit 169143d

Browse files
committed
Allow extension to overwrite about-to-be created TOAST tablespace (#278)
* Allow extension to overwrite about-to-be created TOAST tablespace * remove abi change
1 parent 95b8d76 commit 169143d

7 files changed

Lines changed: 34 additions & 20 deletions

File tree

src/backend/catalog/toasting.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
/* Potentially set by contrib/pg_upgrade_support functions */
3838
Oid binary_upgrade_next_toast_pg_type_oid = InvalidOid;
3939

40-
static void CheckAndCreateToastTable(Oid relOid, Datum reloptions,
40+
static void CheckAndCreateToastTable(Oid relOid, Oid tablespaceOid, Datum reloptions,
4141
LOCKMODE lockmode, bool check,
4242
bool is_part_child, bool is_part_parent);
43-
static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
43+
static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Oid tablespaceOid,
4444
Datum reloptions, LOCKMODE lockmode, bool check,
4545
bool is_part_child, bool is_part_parent);
4646
static bool needs_toast_table(Relation rel);
@@ -75,15 +75,15 @@ void
7575
AlterTableCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
7676
bool is_part_child, bool is_part_parent)
7777
{
78-
CheckAndCreateToastTable(relOid, reloptions, lockmode, true,
78+
CheckAndCreateToastTable(relOid, InvalidOid, reloptions, lockmode, true,
7979
is_part_child, is_part_parent);
8080
}
8181

8282
void
83-
NewHeapCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode,
83+
NewHeapCreateToastTable(Oid relOid, Oid tablespaceOid, Datum reloptions, LOCKMODE lockmode,
8484
bool is_part_child, bool is_part_parent)
8585
{
86-
CheckAndCreateToastTable(relOid, reloptions, lockmode, false,
86+
CheckAndCreateToastTable(relOid, tablespaceOid, reloptions, lockmode, false,
8787
is_part_child, is_part_parent);
8888
}
8989

@@ -108,20 +108,20 @@ NewRelationCreateToastTable(Oid relOid, Datum reloptions,
108108
else
109109
lockmode = AccessExclusiveLock;
110110

111-
CheckAndCreateToastTable(relOid, reloptions, AccessExclusiveLock, false,
111+
CheckAndCreateToastTable(relOid, InvalidOid, reloptions, AccessExclusiveLock, false,
112112
is_part_child, is_part_parent);
113113
}
114114

115115
static void
116-
CheckAndCreateToastTable(Oid relOid, Datum reloptions, LOCKMODE lockmode, bool check,
116+
CheckAndCreateToastTable(Oid relOid, Oid tablespaceOid, Datum reloptions, LOCKMODE lockmode, bool check,
117117
bool is_part_child, bool is_part_parent)
118118
{
119119
Relation rel;
120120

121121
rel = heap_open(relOid, lockmode);
122122

123123
/* create_toast_table does all the work */
124-
(void) create_toast_table(rel, InvalidOid, InvalidOid, reloptions, lockmode, check,
124+
(void) create_toast_table(rel, InvalidOid, InvalidOid, tablespaceOid, reloptions, lockmode, check,
125125
is_part_child, is_part_parent);
126126

127127
heap_close(rel, NoLock);
@@ -147,7 +147,7 @@ BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
147147
relName)));
148148

149149
/* create_toast_table does all the work */
150-
if (!create_toast_table(rel, toastOid, toastIndexOid, (Datum) 0,
150+
if (!create_toast_table(rel, toastOid, toastIndexOid, InvalidOid, (Datum) 0,
151151
AccessExclusiveLock, false,
152152
false, false))
153153
elog(ERROR, "\"%s\" does not require a toast table",
@@ -165,7 +165,7 @@ BootstrapToastTable(char *relName, Oid toastOid, Oid toastIndexOid)
165165
* bootstrap they can be nonzero to specify hand-assigned OIDs
166166
*/
167167
static bool
168-
create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
168+
create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Oid tablespaceOid,
169169
Datum reloptions, LOCKMODE lockmode, bool check,
170170
bool is_part_child, bool is_part_parent)
171171
{
@@ -319,7 +319,7 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
319319

320320
toast_relid = heap_create_with_catalog(toast_relname,
321321
namespaceid,
322-
rel->rd_rel->reltablespace,
322+
tablespaceOid ? tablespaceOid : rel->rd_rel->reltablespace,
323323
toastOid,
324324
toast_typid,
325325
InvalidOid,
@@ -397,7 +397,7 @@ create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid,
397397
indexInfo,
398398
list_make2("chunk_id", "chunk_seq"),
399399
BTREE_AM_OID,
400-
rel->rd_rel->reltablespace,
400+
tablespaceOid ? tablespaceOid : rel->rd_rel->reltablespace,
401401
collationObjectId, classObjectId, coloptions, (Datum) 0,
402402
true, false, false, false,
403403
true, false, false, true, NULL);

src/backend/commands/cluster.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ rebuild_relation(Relation OldHeap, Oid indexOid, bool verbose)
664664
heap_close(OldHeap, NoLock);
665665

666666
/* Create the transient table that will receive the re-ordered data */
667-
OIDNewHeap = make_new_heap(tableOid, tableSpace,false,
667+
OIDNewHeap = make_new_heap(tableOid, tableSpace, InvalidOid, false,
668668
AccessExclusiveLock,
669669
true /* createAoBlockDirectory */,
670670
false);
@@ -696,7 +696,7 @@ rebuild_relation(Relation OldHeap, Oid indexOid, bool verbose)
696696
* data, then call finish_heap_swap to complete the operation.
697697
*/
698698
Oid
699-
make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, bool forcetemp,
699+
make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, Oid newTOASTTableSpace, bool forcetemp,
700700
LOCKMODE lockmode,
701701
bool createAoBlockDirectory,
702702
bool makeCdbPolicy)
@@ -834,7 +834,7 @@ make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, bool forcetemp,
834834
&isNull);
835835
if (isNull)
836836
reloptions = (Datum) 0;
837-
NewHeapCreateToastTable(OIDNewHeap, reloptions, lockmode,
837+
NewHeapCreateToastTable(OIDNewHeap, newTOASTTableSpace, reloptions, lockmode,
838838
is_part_child, is_part_parent);
839839

840840
ReleaseSysCache(tuple);

src/backend/commands/matview.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
345345
* it against access by any other process until commit (by which time it
346346
* will be gone).
347347
*/
348-
OIDNewHeap = make_new_heap(matviewOid, tableSpace, concurrent,
348+
OIDNewHeap = make_new_heap(matviewOid, tableSpace, InvalidOid, concurrent,
349349
ExclusiveLock, createAoBlockDirectory, true);
350350
LockRelationOid(OIDNewHeap, AccessExclusiveLock);
351351
dest = CreateTransientRelDestReceiver(OIDNewHeap, matviewOid, concurrent, stmt->skipData);
@@ -563,7 +563,7 @@ transientrel_init(QueryDesc *queryDesc)
563563
* it against access by any other process until commit (by which time it
564564
* will be gone).
565565
*/
566-
OIDNewHeap = make_new_heap(matviewOid, tableSpace, concurrent,
566+
OIDNewHeap = make_new_heap(matviewOid, tableSpace, InvalidOid, concurrent,
567567
ExclusiveLock, createAoBlockDirectory, false);
568568
LockRelationOid(OIDNewHeap, AccessExclusiveLock);
569569

src/backend/commands/tablecmds.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ typedef struct OnCommitItem
152152

153153
static List *on_commits = NIL;
154154

155+
Oid newTOASTTableSpace = InvalidOid;
155156

156157
/*
157158
* State information for ALTER TABLE
@@ -197,6 +198,7 @@ typedef struct AlteredTableInfo
197198
bool dist_opfamily_changed; /* T if changing datatype of distribution key column and new opclass is in different opfamily than old one */
198199
Oid new_opclass; /* new opclass, if changing a distribution key column */
199200
Oid newTableSpace; /* new tablespace; 0 means no change */
201+
Oid newTOASTTableSpace; /* new TOAST tablespace; 0 means no change */
200202
Oid exchange_relid; /* for EXCHANGE, the exchanged in rel */
201203
/* Objects to rebuild after completing ALTER TYPE operations */
202204
List *changedConstraintOids; /* OIDs of constraints to rebuild */
@@ -4350,6 +4352,11 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
43504352
/* Find or create work queue entry for this table */
43514353
tab = ATGetQueueEntry(wqueue, rel);
43524354

4355+
tab->newTOASTTableSpace = newTOASTTableSpace;
4356+
4357+
/* Reset it */
4358+
newTOASTTableSpace = InvalidOid;
4359+
43534360
/*
43544361
* Copy the original subcommand for each table. This avoids conflicts
43554362
* when different child tables need to make different parse
@@ -5981,6 +5988,7 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode)
59815988
Relation OldHeap;
59825989
Oid newTableSpace;
59835990
Oid oldTableSpace;
5991+
Oid newTOASTTableSpace;
59845992
bool hasIndexes;
59855993

59865994
/* We will lock the table iff we decide to actually rewrite it */
@@ -5993,6 +6001,7 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode)
59936001

59946002
oldTableSpace = OldHeap->rd_rel->reltablespace;
59956003
newTableSpace = tab->newTableSpace ? tab->newTableSpace : oldTableSpace;
6004+
newTOASTTableSpace = tab->newTOASTTableSpace ? tab->newTOASTTableSpace : newTableSpace;
59966005
relstorage = OldHeap->rd_rel->relstorage;
59976006
{
59986007
List *indexIds;
@@ -6081,7 +6090,7 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode)
60816090
* unlogged anyway.
60826091
*/
60836092
/* Create transient table that will receive the modified data */
6084-
OIDNewHeap = make_new_heap(tab->relid, newTableSpace, false,
6093+
OIDNewHeap = make_new_heap(tab->relid, newTableSpace, newTOASTTableSpace, false,
60856094
lockmode, hasIndexes, false);
60866095

60876096
/*

src/include/catalog/toasting.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
extern void NewRelationCreateToastTable(Oid relOid, Datum reloptions,
2323
bool is_part_child, bool is_part_parent);
24-
extern void NewHeapCreateToastTable(Oid relOid, Datum reloptions,
24+
extern void NewHeapCreateToastTable(Oid relOid, Oid tablespaceOid, Datum reloptions,
2525
LOCKMODE lockmode,
2626
bool is_part_child, bool is_part_parent);
2727
extern void AlterTableCreateToastTable(Oid relOid, Datum reloptions,

src/include/commands/cluster.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern void check_index_is_clusterable(Relation OldHeap, Oid indexOid,
2525
bool recheck, LOCKMODE lockmode);
2626
extern void mark_index_clustered(Relation rel, Oid indexOid, bool is_internal);
2727

28-
extern Oid make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, bool forcetemp,
28+
extern Oid make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, Oid newTOASTTableSpace, bool forcetemp,
2929
LOCKMODE lockmode,
3030
bool createAoBlockDirectory,
3131
bool makeCdbPolicy);

src/include/nodes/parsenodes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,11 @@ typedef struct AlterTableCmd /* one subcommand of an ALTER TABLE */
15491549
} AlterTableCmd;
15501550

15511551

1552+
/* XXX: OGPDB: Hacky global variable to overwrite TOAST tablespaceoid of about-to-be
1553+
* created (or rewrited) relation. The main usage scenario is for ALTER
1554+
* TABLE with rewrite, when extension want more control of kenrel logic */
1555+
extern Oid newTOASTTableSpace;
1556+
15521557
typedef struct SetDistributionCmd
15531558
{
15541559
NodeTag type;

0 commit comments

Comments
 (0)