Skip to content

Commit 3bf676b

Browse files
committed
Remove foreach_compressed_chunk
Refactor foreach_chunk to allow propagation to compressed relation and remove reliance on compressed hypertable when iterating over compressed relations.
1 parent ee25b55 commit 3bf676b

1 file changed

Lines changed: 41 additions & 115 deletions

File tree

src/process_utility.c

Lines changed: 41 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -960,19 +960,15 @@ typedef void (*mt_process_chunk_t)(int32 hypertable_id, Oid chunk_relid, void *a
960960
* Returns the number of processed chunks, or -1 if the table was not a
961961
* hypertable.
962962
*/
963-
static int
964-
foreach_chunk(Hypertable *ht, process_chunk_t process_chunk, void *arg)
963+
static void
964+
foreach_chunk(Hypertable *ht, process_chunk_t process_chunk, void *arg,
965+
bool propagate_to_compressed)
965966
{
966967
List *chunks;
967968
ListCell *lc;
968-
int n = 0;
969969
MemoryContext orig_mcxt = CurrentMemoryContext;
970970
MemoryContext chunk_mcxt;
971-
972-
if (NULL == ht)
973-
{
974-
return -1;
975-
}
971+
Ensure(ht, "hypertable cannot be null");
976972

977973
chunks = find_inheritance_children(ht->main_table_relid, NoLock);
978974

@@ -985,58 +981,24 @@ foreach_chunk(Hypertable *ht, process_chunk_t process_chunk, void *arg)
985981
foreach (lc, chunks)
986982
{
987983
MemoryContextSwitchTo(chunk_mcxt);
984+
if (propagate_to_compressed)
985+
{
986+
Oid compressed_relid = ts_relation_get_compressed_relid(lfirst_oid(lc));
987+
if (OidIsValid(compressed_relid))
988+
{
989+
process_chunk(ht, compressed_relid, arg);
990+
}
991+
}
988992
process_chunk(ht, lfirst_oid(lc), arg);
989993
MemoryContextSwitchTo(orig_mcxt);
990994
MemoryContextReset(chunk_mcxt);
991-
n++;
992995
}
993996

994997
MemoryContextDelete(chunk_mcxt);
995998
list_free(chunks);
996-
997-
return n;
998999
}
9991000

1000-
/*
1001-
* Applies a function to each compressed internal chunk of a hypertable.
1002-
*
1003-
* Returns the number of processed chunks, or -1 if the table was not a
1004-
* hypertable.
1005-
*/
1006-
static int
1007-
foreach_compressed_chunk(Hypertable *ht, process_chunk_t process_chunk, void *arg)
1008-
{
1009-
List *chunks;
1010-
ListCell *lc;
1011-
int n = 0;
1012-
MemoryContext orig_mcxt = CurrentMemoryContext;
1013-
MemoryContext chunk_mcxt;
1014-
1015-
if (!ht || !ht->fd.compressed_hypertable_id)
1016-
{
1017-
return -1;
1018-
}
1019-
1020-
chunks = ts_chunk_get_by_hypertable_id(ht->fd.compressed_hypertable_id);
1021-
1022-
chunk_mcxt = AllocSetContextCreate(orig_mcxt, "foreach_chunk", ALLOCSET_DEFAULT_SIZES);
1023-
foreach (lc, chunks)
1024-
{
1025-
Chunk *chunk = lfirst(lc);
1026-
MemoryContextSwitchTo(chunk_mcxt);
1027-
process_chunk(ht, chunk->table_id, arg);
1028-
MemoryContextReset(chunk_mcxt);
1029-
n++;
1030-
}
1031-
1032-
MemoryContextSwitchTo(orig_mcxt);
1033-
MemoryContextDelete(chunk_mcxt);
1034-
list_free(chunks);
1035-
1036-
return n;
1037-
}
1038-
1039-
static int
1001+
static void
10401002
foreach_chunk_multitransaction(Oid relid, MemoryContext mctx, mt_process_chunk_t process_chunk,
10411003
void *arg)
10421004
{
@@ -1045,7 +1007,6 @@ foreach_chunk_multitransaction(Oid relid, MemoryContext mctx, mt_process_chunk_t
10451007
int32 hypertable_id;
10461008
List *chunks;
10471009
ListCell *lc;
1048-
int num_chunks = -1;
10491010

10501011
StartTransactionCommand();
10511012
MemoryContextSwitchTo(mctx);
@@ -1056,7 +1017,7 @@ foreach_chunk_multitransaction(Oid relid, MemoryContext mctx, mt_process_chunk_t
10561017
{
10571018
ts_cache_release(&hcache);
10581019
CommitTransactionCommand();
1059-
return -1;
1020+
return;
10601021
}
10611022

10621023
hypertable_id = ht->fd.id;
@@ -1065,16 +1026,13 @@ foreach_chunk_multitransaction(Oid relid, MemoryContext mctx, mt_process_chunk_t
10651026
ts_cache_release(&hcache);
10661027
CommitTransactionCommand();
10671028

1068-
num_chunks = list_length(chunks);
1069-
pgstat_progress_update_param(PROGRESS_CREATEIDX_PARTITIONS_TOTAL, num_chunks);
1029+
pgstat_progress_update_param(PROGRESS_CREATEIDX_PARTITIONS_TOTAL, list_length(chunks));
10701030
foreach (lc, chunks)
10711031
{
10721032
process_chunk(hypertable_id, lfirst_oid(lc), arg);
10731033
}
10741034

10751035
list_free(chunks);
1076-
1077-
return num_chunks;
10781036
}
10791037

10801038
typedef struct VacuumCtx
@@ -1326,7 +1284,7 @@ process_vacuum(ProcessUtilityArgs *args)
13261284
if (ht)
13271285
{
13281286
ctx.ht_vacuum_rel = vacuum_rel;
1329-
foreach_chunk(ht, add_chunk_to_vacuum, &ctx);
1287+
foreach_chunk(ht, add_chunk_to_vacuum, &ctx, false);
13301288
}
13311289
}
13321290
vacuum_rels = lappend(vacuum_rels, vacuum_rel);
@@ -1391,7 +1349,7 @@ handle_truncate_hypertable(ProcessUtilityArgs *args, TruncateStmt *stmt, Hyperta
13911349
ts_chunk_delete_by_hypertable_id(ht->fd.id);
13921350

13931351
/* Drop the chunk tables */
1394-
foreach_chunk(ht, process_truncate_chunk, stmt);
1352+
foreach_chunk(ht, process_truncate_chunk, stmt, true);
13951353
}
13961354

13971355
/*
@@ -1808,7 +1766,7 @@ process_drop_hypertable(ProcessUtilityArgs *args, DropStmt *stmt)
18081766
* We need to drop hypertable chunks before the hypertable to avoid the need
18091767
* to CASCADE such drops;
18101768
*/
1811-
foreach_chunk(ht, process_drop_table_chunk, stmt);
1769+
foreach_chunk(ht, process_drop_table_chunk, stmt, true);
18121770
/* The usual path for deleting an associated compressed hypertable uses
18131771
* DROP_RESTRICT But if we are using DROP_CASCADE we should propagate that down to
18141772
* the compressed hypertable.
@@ -1817,22 +1775,6 @@ process_drop_hypertable(ProcessUtilityArgs *args, DropStmt *stmt)
18171775
{
18181776
Hypertable *compressed_hypertable =
18191777
ts_hypertable_get_by_id(ht->fd.compressed_hypertable_id);
1820-
List *chunks = ts_chunk_get_by_hypertable_id(ht->fd.compressed_hypertable_id);
1821-
foreach (lc, chunks)
1822-
{
1823-
Chunk *chunk = lfirst(lc);
1824-
1825-
if (OidIsValid(chunk->table_id))
1826-
{
1827-
ObjectAddress chunk_addr = (ObjectAddress){
1828-
.classId = RelationRelationId,
1829-
.objectId = chunk->table_id,
1830-
};
1831-
1832-
/* Drop the postgres table */
1833-
performDeletion(&chunk_addr, stmt->behavior, 0);
1834-
}
1835-
}
18361778
ts_hypertable_drop(compressed_hypertable, DROP_CASCADE);
18371779
}
18381780
}
@@ -2146,7 +2088,7 @@ process_grant_and_revoke(ProcessUtilityArgs *args)
21462088

21472089
if (ht)
21482090
{
2149-
foreach_chunk(ht, add_chunk_oid, args);
2091+
foreach_chunk(ht, add_chunk_oid, args, false);
21502092
}
21512093
}
21522094

@@ -2360,13 +2302,12 @@ reindex_chunk(Hypertable *ht, Oid chunk_relid, void *arg)
23602302
{
23612303
ProcessUtilityArgs *args = arg;
23622304
ReindexStmt *stmt = (ReindexStmt *) args->parsetree;
2363-
Chunk *chunk = ts_chunk_get_by_relid(chunk_relid, true);
23642305

23652306
switch (stmt->kind)
23662307
{
23672308
case REINDEX_OBJECT_TABLE:
2368-
stmt->relation->relname = NameStr(chunk->fd.table_name);
2369-
stmt->relation->schemaname = NameStr(chunk->fd.schema_name);
2309+
stmt->relation->relname = get_rel_name(chunk_relid);
2310+
stmt->relation->schemaname = get_namespace_name(get_rel_namespace(chunk_relid));
23702311
ExecReindex(NULL, stmt, false);
23712312
break;
23722313
case REINDEX_OBJECT_INDEX:
@@ -2410,7 +2351,7 @@ process_reindex(ProcessUtilityArgs *args)
24102351
case REINDEX_OBJECT_TABLE:
24112352
ht = ts_hypertable_cache_get_entry(hcache, relid, CACHE_FLAG_MISSING_OK);
24122353

2413-
if (NULL != ht)
2354+
if (ht)
24142355
{
24152356
PreventCommandDuringRecovery("REINDEX");
24162357
ts_hypertable_permissions_check_by_id(ht->fd.id);
@@ -2421,10 +2362,8 @@ process_reindex(ProcessUtilityArgs *args)
24212362
errmsg("concurrent index creation on hypertables is not supported")));
24222363
}
24232364

2424-
if (foreach_chunk(ht, reindex_chunk, args) >= 0)
2425-
{
2426-
result = DDL_DONE;
2427-
}
2365+
foreach_chunk(ht, reindex_chunk, args, true);
2366+
result = DDL_DONE;
24282367
}
24292368
break;
24302369

@@ -2871,11 +2810,11 @@ process_rename_constraint_or_trigger(ProcessUtilityArgs *args, Cache *hcache, Oi
28712810

28722811
if (stmt->renameType == OBJECT_TABCONSTRAINT)
28732812
{
2874-
foreach_chunk(ht, rename_hypertable_constraint, stmt);
2813+
foreach_chunk(ht, rename_hypertable_constraint, stmt, false);
28752814
}
28762815
else if (stmt->renameType == OBJECT_TRIGGER)
28772816
{
2878-
foreach_chunk(ht, rename_hypertable_trigger, stmt);
2817+
foreach_chunk(ht, rename_hypertable_trigger, stmt, false);
28792818
}
28802819
}
28812820
else if (stmt->renameType == OBJECT_TABCONSTRAINT)
@@ -2985,20 +2924,13 @@ process_altertable_change_owner(Hypertable *ht, AlterTableCmd *cmd)
29852924
Assert(IsA(cmd->newowner, RoleSpec));
29862925

29872926
process_altertable_change_owner_bgw_jobs(ht->fd.id, newrole_oid);
2988-
foreach_chunk(ht, process_altertable_change_owner_chunk, cmd);
2927+
foreach_chunk(ht, process_altertable_change_owner_chunk, cmd, true);
29892928

29902929
if (TS_HYPERTABLE_HAS_COMPRESSION_TABLE(ht))
29912930
{
29922931
Hypertable *compressed_hypertable =
29932932
ts_hypertable_get_by_id(ht->fd.compressed_hypertable_id);
29942933
AlterTableInternal(compressed_hypertable->main_table_relid, list_make1(cmd), false);
2995-
ListCell *lc;
2996-
List *chunks = ts_chunk_get_by_hypertable_id(ht->fd.compressed_hypertable_id);
2997-
foreach (lc, chunks)
2998-
{
2999-
Chunk *chunk = lfirst(lc);
3000-
AlterTableInternal(chunk->table_id, list_make1(cmd), false);
3001-
}
30022934
process_altertable_change_owner(compressed_hypertable, cmd);
30032935
}
30042936
}
@@ -3277,19 +3209,19 @@ process_altertable_add_constraint(Hypertable *ht, const AlterTableCmd *cmd,
32773209
get_relation_constraint_oid(ht->main_table_relid, constraint_name, false),
32783210
};
32793211

3280-
foreach_chunk(ht, process_add_constraint_chunk, &info);
3212+
foreach_chunk(ht, process_add_constraint_chunk, &info, false);
32813213
}
32823214

32833215
static void
32843216
process_altertable_alter_constraint_end(Hypertable *ht, AlterTableCmd *cmd)
32853217
{
3286-
foreach_chunk(ht, alter_hypertable_constraint, cmd);
3218+
foreach_chunk(ht, alter_hypertable_constraint, cmd, false);
32873219
}
32883220

32893221
static void
32903222
process_altertable_validate_constraint_end(Hypertable *ht, AlterTableCmd *cmd)
32913223
{
3292-
foreach_chunk(ht, validate_hypertable_constraint, cmd);
3224+
foreach_chunk(ht, validate_hypertable_constraint, cmd, false);
32933225
}
32943226

32953227
/*
@@ -3389,7 +3321,7 @@ process_altertable_alter_not_null(Hypertable *ht, AlterTableCmd *cmd)
33893321
{
33903322
if (cmd->subtype == AT_SetNotNull)
33913323
{
3392-
foreach_chunk(ht, validate_set_not_null, cmd);
3324+
foreach_chunk(ht, validate_set_not_null, cmd, false);
33933325
}
33943326

33953327
if (cmd->subtype == AT_DropNotNull)
@@ -3998,7 +3930,7 @@ process_index_start(ProcessUtilityArgs *args)
39983930
list_free(chunks);
39993931

40003932
/* Recurse to each chunk and create a corresponding index. */
4001-
foreach_chunk(ht, process_index_chunk, &info);
3933+
foreach_chunk(ht, process_index_chunk, &info, false);
40023934

40033935
ts_catalog_restore_user(&sec_ctx);
40043936
ts_cache_release(&hcache);
@@ -4705,7 +4637,7 @@ process_altertable_replica_identity(Hypertable *ht, AlterTableCmd *cmd)
47054637
}
47064638
}
47074639

4708-
foreach_chunk(ht, process_altertable_chunk_replica_identity, cmd);
4640+
foreach_chunk(ht, process_altertable_chunk_replica_identity, cmd, false);
47094641
}
47104642

47114643
static void
@@ -4737,20 +4669,12 @@ process_altertable_set_tablespace_end(Hypertable *ht, AlterTableCmd *cmd)
47374669
}
47384670

47394671
ts_tablespace_attach_internal(&tspc_name, ht->main_table_relid, true);
4740-
foreach_chunk(ht, process_altertable_chunk, cmd);
4672+
foreach_chunk(ht, process_altertable_chunk, cmd, true);
47414673
if (TS_HYPERTABLE_HAS_COMPRESSION_TABLE(ht))
47424674
{
47434675
Hypertable *compressed_hypertable =
47444676
ts_hypertable_get_by_id(ht->fd.compressed_hypertable_id);
47454677
AlterTableInternal(compressed_hypertable->main_table_relid, list_make1(cmd), false);
4746-
4747-
List *chunks = ts_chunk_get_by_hypertable_id(ht->fd.compressed_hypertable_id);
4748-
ListCell *lc;
4749-
foreach (lc, chunks)
4750-
{
4751-
Chunk *chunk = lfirst(lc);
4752-
AlterTableInternal(chunk->table_id, list_make1(cmd), false);
4753-
}
47544678
process_altertable_set_tablespace_end(compressed_hypertable, cmd);
47554679
}
47564680
}
@@ -5331,7 +5255,7 @@ process_altertable_end_subcmd(Hypertable *ht, Node *parsetree, ObjectAddress *ob
53315255
case AT_DisableTrigAll:
53325256
case AT_EnableTrigUser:
53335257
case AT_DisableTrigUser:
5334-
foreach_chunk(ht, process_altertable_chunk, cmd);
5258+
foreach_chunk(ht, process_altertable_chunk, cmd, false);
53355259
break;
53365260
case AT_ClusterOn:
53375261
process_altertable_clusteron_end(ht, cmd);
@@ -5360,8 +5284,7 @@ process_altertable_end_subcmd(Hypertable *ht, Node *parsetree, ObjectAddress *ob
53605284
break;
53615285
case AT_SetLogged:
53625286
case AT_SetUnLogged:
5363-
foreach_chunk(ht, process_altertable_chunk, cmd);
5364-
foreach_compressed_chunk(ht, process_altertable_chunk, cmd);
5287+
foreach_chunk(ht, process_altertable_chunk, cmd, true);
53655288
break;
53665289
case AT_DropCluster:
53675290
case AT_SetNotNull:
@@ -5374,7 +5297,7 @@ process_altertable_end_subcmd(Hypertable *ht, Node *parsetree, ObjectAddress *ob
53745297
case AT_ResetOptions:
53755298
case AT_ReAddStatistics:
53765299
case AT_SetCompression:
5377-
foreach_chunk(ht, process_altertable_chunk, cmd);
5300+
foreach_chunk(ht, process_altertable_chunk, cmd, false);
53785301
break;
53795302
case AT_SetTableSpace:
53805303
process_altertable_set_tablespace_end(ht, cmd);
@@ -6335,7 +6258,10 @@ process_drop_table_constraint(EventTriggerDropObject *obj)
63356258
ts_catalog_database_info_become_owner(ts_catalog_database_info_get(), &sec_ctx);
63366259

63376260
/* Recurse to each chunk and drop the corresponding constraint */
6338-
foreach_chunk(ht, process_drop_constraint_on_chunk, (void *) constraint->constraint_name);
6261+
foreach_chunk(ht,
6262+
process_drop_constraint_on_chunk,
6263+
(void *) constraint->constraint_name,
6264+
false);
63396265

63406266
ts_catalog_restore_user(&sec_ctx);
63416267
}

0 commit comments

Comments
 (0)