Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't look up entire Chunk struct for compressed chunks #5970

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -2712,17 +2712,17 @@ chunk_simple_scan_by_name(const char *schema, const char *table, FormData_chunk
}

static bool
chunk_simple_scan_by_relid(Oid relid, FormData_chunk *form, bool missing_ok)
chunk_simple_scan_by_reloid(Oid reloid, FormData_chunk *form, bool missing_ok)
{
bool found = false;

if (OidIsValid(relid))
if (OidIsValid(reloid))
{
const char *table = get_rel_name(relid);
const char *table = get_rel_name(reloid);

if (table != NULL)
{
Oid nspid = get_rel_namespace(relid);
Oid nspid = get_rel_namespace(reloid);
const char *schema = get_namespace_name(nspid);

found = chunk_simple_scan_by_name(schema, table, form, missing_ok);
Expand All @@ -2732,7 +2732,7 @@ chunk_simple_scan_by_relid(Oid relid, FormData_chunk *form, bool missing_ok)
if (!found && !missing_ok)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("chunk with relid %u not found", relid)));
errmsg("chunk with reloid %u not found", reloid)));

return found;
}
Expand Down Expand Up @@ -2765,7 +2765,7 @@ ts_chunk_id_from_relid(PG_FUNCTION_ARGS)
if (last_relid == relid)
return last_id;

chunk_simple_scan_by_relid(relid, &form, false);
chunk_simple_scan_by_reloid(relid, &form, false);

last_relid = relid;
last_id = form.id;
Expand All @@ -2778,7 +2778,7 @@ ts_chunk_get_id_by_relid(Oid relid)
{
FormData_chunk form;

chunk_simple_scan_by_relid(relid, &form, /* missing_ok = */ false);
chunk_simple_scan_by_reloid(relid, &form, /* missing_ok = */ false);
return form.id;
}

Expand All @@ -2787,18 +2787,18 @@ ts_chunk_exists_relid(Oid relid)
{
FormData_chunk form;

return chunk_simple_scan_by_relid(relid, &form, true);
return chunk_simple_scan_by_reloid(relid, &form, true);
}

/*
* Returns 0 if there is no chunk with such reloid.
*/
int32
ts_chunk_get_hypertable_id_by_relid(Oid relid)
ts_chunk_get_hypertable_id_by_reloid(Oid reliod)
{
FormData_chunk form;

if (chunk_simple_scan_by_relid(relid, &form, /* missing_ok = */ true))
if (chunk_simple_scan_by_reloid(reliod, &form, /* missing_ok = */ true))
{
return form.hypertable_id;
}
Expand Down Expand Up @@ -2828,7 +2828,7 @@ ts_chunk_get_hypertable_id_and_status_by_relid(Oid relid, int32 *hypertable_id,
FormData_chunk form;

Assert(hypertable_id != NULL && chunk_status != NULL);
if (chunk_simple_scan_by_relid(relid, &form, /* missing_ok = */ true))
if (chunk_simple_scan_by_reloid(relid, &form, /* missing_ok = */ true))
{
*hypertable_id = form.hypertable_id;
*chunk_status = form.status;
Expand All @@ -2837,6 +2837,14 @@ ts_chunk_get_hypertable_id_and_status_by_relid(Oid relid, int32 *hypertable_id,
return false;
}

FormData_chunk
ts_chunk_get_formdata(int32 chunk_id)
{
FormData_chunk fd;
chunk_simple_scan_by_id(chunk_id, &fd, /* missing_ok = */ false);
return fd;
}

/*
* Get the relid of a chunk given its ID.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ extern TSDLLEXPORT Chunk *ts_chunk_get_by_id(int32 id, bool fail_if_not_found);
extern TSDLLEXPORT Chunk *ts_chunk_get_by_relid(Oid relid, bool fail_if_not_found);
extern TSDLLEXPORT void ts_chunk_free(Chunk *chunk);
extern bool ts_chunk_exists(const char *schema_name, const char *table_name);
extern TSDLLEXPORT int32 ts_chunk_get_hypertable_id_by_relid(Oid relid);
extern TSDLLEXPORT int32 ts_chunk_get_hypertable_id_by_reloid(Oid relid);
extern TSDLLEXPORT int32 ts_chunk_get_compressed_chunk_id(int32 chunk_id);
extern bool ts_chunk_get_hypertable_id_and_status_by_relid(Oid relid, int32 *hypertable_id,
int32 *chunk_status);
extern TSDLLEXPORT FormData_chunk ts_chunk_get_formdata(int32 chunk_id);
extern TSDLLEXPORT Oid ts_chunk_get_relid(int32 chunk_id, bool missing_ok);
extern Oid ts_chunk_get_schema_id(int32 chunk_id, bool missing_ok);
extern bool ts_chunk_get_id(const char *schema, const char *table, int32 *chunk_id,
Expand Down
2 changes: 1 addition & 1 deletion src/planner/expand_hypertable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ ts_plan_expand_hypertable_chunks(Hypertable *ht, PlannerInfo *root, RelOptInfo *
* Add the information about chunks to the baserel info cache for
* classify_relation().
*/
add_baserel_cache_entry_for_chunk(chunks[i]->table_id, ht);
ts_add_baserel_cache_entry_for_chunk(chunks[i]->table_id, ht);
}

/* nothing to do here if we have no chunks and no data nodes */
Expand Down
8 changes: 5 additions & 3 deletions src/planner/planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static struct BaserelInfo_hash *ts_baserel_info = NULL;
* chunk info at the plan time chunk exclusion.
*/
void
add_baserel_cache_entry_for_chunk(Oid chunk_reloid, Hypertable *hypertable)
ts_add_baserel_cache_entry_for_chunk(Oid chunk_reloid, Hypertable *hypertable)
{
Assert(hypertable != NULL);
Assert(ts_baserel_info != NULL);
Expand All @@ -166,6 +166,8 @@ add_baserel_cache_entry_for_chunk(Oid chunk_reloid, Hypertable *hypertable)
return;
}

Assert(ts_chunk_get_hypertable_id_by_reloid(chunk_reloid) == hypertable->fd.id);

/* Fill the cache entry. */
entry->ht = hypertable;
}
Expand Down Expand Up @@ -695,7 +697,7 @@ get_or_add_baserel_from_cache(Oid chunk_reloid, Oid parent_reloid)

#ifdef USE_ASSERT_CHECKING
/* Sanity check on the caller-specified hypertable reloid. */
int32 parent_hypertable_id = ts_chunk_get_hypertable_id_by_relid(chunk_reloid);
int32 parent_hypertable_id = ts_chunk_get_hypertable_id_by_reloid(chunk_reloid);
if (parent_hypertable_id != INVALID_HYPERTABLE_ID)
{
Assert(ts_hypertable_id_to_relid(parent_hypertable_id, false) == parent_reloid);
Expand All @@ -712,7 +714,7 @@ get_or_add_baserel_from_cache(Oid chunk_reloid, Oid parent_reloid)
/* Hypertable reloid not specified by the caller, look it up by
* an expensive metadata scan.
*/
int32 hypertable_id = ts_chunk_get_hypertable_id_by_relid(chunk_reloid);
int32 hypertable_id = ts_chunk_get_hypertable_id_by_reloid(chunk_reloid);

if (hypertable_id != INVALID_HYPERTABLE_ID)
{
Expand Down
5 changes: 3 additions & 2 deletions src/planner/planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ typedef enum PartializeAggFixAggref
TS_FIX_AGGSPLIT_FINAL = 2
} PartializeAggFixAggref;

Hypertable *ts_planner_get_hypertable(const Oid relid, const unsigned int flags);
extern TSDLLEXPORT Hypertable *ts_planner_get_hypertable(const Oid relid, const unsigned int flags);
bool has_partialize_function(Node *node, PartializeAggFixAggref fix_aggref);
bool ts_plan_process_partialize_agg(PlannerInfo *root, RelOptInfo *output_rel);

Expand All @@ -110,6 +110,7 @@ extern Node *ts_constify_now(PlannerInfo *root, List *rtable, Node *node);
extern void ts_planner_constraint_cleanup(PlannerInfo *root, RelOptInfo *rel);
extern Node *ts_add_space_constraints(PlannerInfo *root, List *rtable, Node *node);

extern void add_baserel_cache_entry_for_chunk(Oid chunk_reloid, Hypertable *hypertable);
extern TSDLLEXPORT void ts_add_baserel_cache_entry_for_chunk(Oid chunk_reloid,
Hypertable *hypertable);

#endif /* TIMESCALEDB_PLANNER_H */
2 changes: 1 addition & 1 deletion tsl/src/fdw/modify_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ create_foreign_modify(EState *estate, Relation rel, CmdType operation, Oid check
Oid user_id = OidIsValid(check_as_user) ? check_as_user : GetUserId();
int i = 0;
int num_data_nodes, num_all_data_nodes;
int32 hypertable_id = ts_chunk_get_hypertable_id_by_relid(rel->rd_id);
int32 hypertable_id = ts_chunk_get_hypertable_id_by_reloid(rel->rd_id);
List *all_replicas = NIL, *avail_replicas = NIL;

if (hypertable_id == INVALID_HYPERTABLE_ID)
Expand Down
2 changes: 1 addition & 1 deletion tsl/src/fdw/modify_plan.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ get_chunk_data_nodes(Oid relid)
/* check that alteast one data node is available for this chunk */
if (chunk_data_nodes == NIL)
{
Hypertable *ht = ts_hypertable_get_by_id(ts_chunk_get_hypertable_id_by_relid(relid));
Hypertable *ht = ts_hypertable_get_by_id(ts_chunk_get_hypertable_id_by_reloid(relid));

ereport(ERROR,
(errcode(ERRCODE_TS_INSUFFICIENT_NUM_DATA_NODES),
Expand Down
24 changes: 18 additions & 6 deletions tsl/src/nodes/decompress_chunk/decompress_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <optimizer/pathnode.h>
#include <optimizer/paths.h>
#include <parser/parsetree.h>
#include <planner/planner.h>
#include <utils/builtins.h>
#include <utils/lsyscache.h>
#include <utils/typcache.h>
Expand Down Expand Up @@ -1503,21 +1504,31 @@ static void
decompress_chunk_add_plannerinfo(PlannerInfo *root, CompressionInfo *info, Chunk *chunk,
RelOptInfo *chunk_rel, bool needs_sequence_num)
{
ListCell *lc;
Index compressed_index = root->simple_rel_array_size;
Chunk *compressed_chunk = ts_chunk_get_by_id(chunk->fd.compressed_chunk_id, true);
Oid compressed_relid = compressed_chunk->table_id;
RelOptInfo *compressed_rel;
FormData_chunk compressed_fd = ts_chunk_get_formdata(chunk->fd.compressed_chunk_id);
Oid compressed_reloid = ts_get_relation_relid(NameStr(compressed_fd.schema_name),
NameStr(compressed_fd.table_name),
/* return_invalid = */ false);

/*
* Add the compressed chunk to the baserel cache. Note that it belongs to
* a different hypertable, the internal compression table.
*/
Oid compression_hypertable_reloid =
ts_hypertable_id_to_relid(compressed_fd.hypertable_id, /* return_invalid = */ false);
ts_add_baserel_cache_entry_for_chunk(compressed_reloid,
ts_planner_get_hypertable(compression_hypertable_reloid,
CACHE_FLAG_NONE));

expand_planner_arrays(root, 1);
info->compressed_rte = decompress_chunk_make_rte(compressed_relid, AccessShareLock);
info->compressed_rte = decompress_chunk_make_rte(compressed_reloid, AccessShareLock);
root->simple_rte_array[compressed_index] = info->compressed_rte;

root->parse->rtable = lappend(root->parse->rtable, info->compressed_rte);

root->simple_rel_array[compressed_index] = NULL;

compressed_rel = build_simple_rel(root, compressed_index, NULL);
RelOptInfo *compressed_rel = build_simple_rel(root, compressed_index, NULL);
/* github issue :1558
* set up top_parent_relids for this rel as the same as the
* original hypertable, otherwise eq classes are not computed correctly
Expand All @@ -1529,6 +1540,7 @@ decompress_chunk_add_plannerinfo(PlannerInfo *root, CompressionInfo *info, Chunk

root->simple_rel_array[compressed_index] = compressed_rel;
info->compressed_rel = compressed_rel;
ListCell *lc;
foreach (lc, info->hypertable_compression_info)
{
FormData_hypertable_compression *fd = lfirst(lc);
Expand Down