Skip to content

Commit

Permalink
Add helper function to get all chunks of a hypertable
Browse files Browse the repository at this point in the history
This patch adds a function `ts_chunk_get_by_hypertable_id` to return
a List of all chunks belonging to a hypertable. The returned chunk
objects will not have any constraints or dimension information filled
in.
  • Loading branch information
svenklemm committed Jan 9, 2024
1 parent df7a8fe commit 5676ace
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
35 changes: 35 additions & 0 deletions src/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -3160,6 +3160,41 @@ ts_chunk_get_chunk_ids_by_hypertable_id(int32 hypertable_id)
return chunkids;
}

/* Return list of chunks that belong to the given hypertable.
*
* The returned chunk objects will not have any constraints or dimension
* information filled in.
*/
List *
ts_chunk_get_by_hypertable_id(int32 hypertable_id)
{
List *chunks = NIL;
Oid hypertable_relid = ts_hypertable_id_to_relid(hypertable_id, false);

ScanIterator iterator = ts_scan_iterator_create(CHUNK, RowExclusiveLock, CurrentMemoryContext);

init_scan_by_hypertable_id(&iterator, hypertable_id);
ts_scanner_foreach(&iterator)
{
TupleInfo *ti = ts_scan_iterator_tuple_info(&iterator);
Chunk *chunk = palloc0(sizeof(Chunk));
ts_chunk_formdata_fill(&chunk->fd, ti);

chunk->hypertable_relid = hypertable_relid;

if (!chunk->fd.dropped)
{
chunk->table_id = ts_get_relation_relid(NameStr(chunk->fd.schema_name),
NameStr(chunk->fd.table_name),
false);
}

chunks = lappend(chunks, chunk);
}

return chunks;
}

static ChunkResult
chunk_recreate_constraint(ChunkScanCtx *ctx, ChunkStub *stub)
{
Expand Down
1 change: 1 addition & 0 deletions src/chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ extern TSDLLEXPORT bool ts_chunk_contains_compressed_data(const Chunk *chunk);
extern TSDLLEXPORT ChunkCompressionStatus ts_chunk_get_compression_status(int32 chunk_id);
extern TSDLLEXPORT Datum ts_chunk_id_from_relid(PG_FUNCTION_ARGS);
extern TSDLLEXPORT List *ts_chunk_get_chunk_ids_by_hypertable_id(int32 hypertable_id);
extern TSDLLEXPORT List *ts_chunk_get_by_hypertable_id(int32 hypertable_id);

extern TSDLLEXPORT Chunk *ts_chunk_create_only_table(Hypertable *ht, Hypercube *cube,
const char *schema_name,
Expand Down
8 changes: 4 additions & 4 deletions src/ts_catalog/compression_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ ts_compression_settings_rename_column_hypertable(Hypertable *ht, char *old, char
if (ht->fd.compressed_hypertable_id)
{
ListCell *lc;
List *chunk_ids = ts_chunk_get_chunk_ids_by_hypertable_id(ht->fd.compressed_hypertable_id);
foreach (lc, chunk_ids)
List *chunks = ts_chunk_get_by_hypertable_id(ht->fd.compressed_hypertable_id);
foreach (lc, chunks)
{
Oid relid = ts_chunk_get_relid(lfirst_int(lc), false);
ts_compression_settings_rename_column(relid, old, new);
Chunk *chunk = lfirst(lc);
ts_compression_settings_rename_column(chunk->table_id, old, new);
}
}
}
Expand Down

0 comments on commit 5676ace

Please sign in to comment.