Skip to content

Commit

Permalink
Add hook for OSM runtime chunk exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
konskov committed Feb 22, 2024
1 parent b039465 commit 2bf2095
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/nodes/chunk_append/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include <optimizer/prep.h>
#include <optimizer/restrictinfo.h>
#include <parser/parsetree.h>
#include <parser/parse_expr.h>
#include <parser/parse_relation.h>
#include <parser/parse_coerce.h>
#include <parser/parse_collate.h>
#include <rewrite/rewriteManip.h>
#include <utils/builtins.h>
#include <utils/memutils.h>
Expand All @@ -30,6 +34,9 @@
#include "loader/lwlocks.h"
#include "planner/planner.h"
#include "transform.h"
#include "dimension_slice.h"
#include "chunk.h"
#include "osm_callbacks.h"

#define INVALID_SUBPLAN_INDEX (-1)
#define NO_MATCHING_SUBPLANS (-2)
Expand Down Expand Up @@ -256,6 +263,37 @@ do_startup_exclusion(ChunkAppendState *state)
}
restrictinfos = constify_restrictinfos(&root, restrictinfos);

#if PG14_GE
// this is where we run the OSM chunk exclusion code
chunk_startup_exclusion_hook_type osm_chunk_exclusion_hook = ts_get_osm_chunk_startup_exclusion_hook();

if (osm_chunk_exclusion_hook)
{
Index rt_index = scan->scanrelid;
EState *estate = state->csstate.ss.ps.state;
RangeTblEntry *rte = rt_fetch(rt_index, estate->es_range_table);

Check warning on line 274 in src/nodes/chunk_append/exec.c

View check run for this annotation

Codecov / codecov/patch

src/nodes/chunk_append/exec.c#L272-L274

Added lines #L272 - L274 were not covered by tests
// relation_constraints = ca_get_relation_constraints(rte->relid, rt_index, true);
// need to get chunk from the relid
Oid relid = rte->relid;
Chunk *chunk = ts_chunk_get_by_relid(relid, false);
Hypertable *ht = ts_hypertable_get_by_id(chunk->fd.hypertable_id);
int tiered_chunks_match = 0;

Check warning on line 280 in src/nodes/chunk_append/exec.c

View check run for this annotation

Codecov / codecov/patch

src/nodes/chunk_append/exec.c#L277-L280

Added lines #L277 - L280 were not covered by tests
// Index varno = rt_index;
if (chunk && IS_OSM_CHUNK(chunk))
{
tiered_chunks_match = osm_chunk_exclusion_hook(NameStr(ht->fd.schema_name), NameStr(ht->fd.table_name), relid, (ForeignScan *)scan, restrictinfos, rt_index);

Check warning on line 284 in src/nodes/chunk_append/exec.c

View check run for this annotation

Codecov / codecov/patch

src/nodes/chunk_append/exec.c#L284

Added line #L284 was not covered by tests
if (tiered_chunks_match == 0)
{
// the OSM chunk can be skipped entirely
if (i < state->first_partial_plan)
filtered_first_partial_plan--;

Check warning on line 289 in src/nodes/chunk_append/exec.c

View check run for this annotation

Codecov / codecov/patch

src/nodes/chunk_append/exec.c#L289

Added line #L289 was not covered by tests

continue;

Check warning on line 291 in src/nodes/chunk_append/exec.c

View check run for this annotation

Codecov / codecov/patch

src/nodes/chunk_append/exec.c#L291

Added line #L291 was not covered by tests
}
}
// root->simple_rte_array[scan->scanrelid]->relid
}
#endif
if (can_exclude_chunk(lfirst(lc_constraints), restrictinfos))
{
if (i < state->first_partial_plan)
Expand Down Expand Up @@ -1030,6 +1068,7 @@ constify_param_mutator(Node *node, void *context)
return expression_tree_mutator(node, constify_param_mutator, context);
}


/*
* stripped down version of postgres get_relation_constraints
*/
Expand Down
9 changes: 9 additions & 0 deletions src/osm_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,12 @@ ts_get_osm_hypertable_drop_chunks_hook()
return ptr->hypertable_drop_chunks_hook;
return NULL;
}

chunk_startup_exclusion_hook_type
ts_get_osm_chunk_startup_exclusion_hook()
{
OsmCallbacks_Versioned *ptr = ts_get_osm_callbacks();
if (ptr && ptr->version_num == 1)
return ptr->chunk_startup_exclusion_hook;

Check warning on line 83 in src/osm_callbacks.c

View check run for this annotation

Codecov / codecov/patch

src/osm_callbacks.c#L83

Added line #L83 was not covered by tests
return NULL;
}
9 changes: 9 additions & 0 deletions src/osm_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <postgres.h>
#include <catalog/objectaddress.h>
#include <nodes/plannodes.h>

/* range_start and range_end are in PG internal timestamp format. */
typedef int (*chunk_insert_check_hook_type)(Oid ht_oid, int64 range_start, int64 range_end);
Expand All @@ -15,6 +16,12 @@ typedef List *(*hypertable_drop_chunks_hook_type)(Oid osm_chunk_oid,
const char *hypertable_schema_name,
const char *hypertable_name, int64 range_start,
int64 range_end);
typedef int (*chunk_startup_exclusion_hook_type)(const char *hypertable_schema_name,
const char *hypertable_name,
Oid relid,
ForeignScan *scan,
List *constified_restrictinfos,
int32 varno); // scan->plan->fdw_private

/*
* Object Storage Manager callbacks.
Expand All @@ -37,8 +44,10 @@ typedef struct
chunk_insert_check_hook_type chunk_insert_check_hook;
hypertable_drop_hook_type hypertable_drop_hook;
hypertable_drop_chunks_hook_type hypertable_drop_chunks_hook;
chunk_startup_exclusion_hook_type chunk_startup_exclusion_hook;
} OsmCallbacks_Versioned;

extern chunk_insert_check_hook_type ts_get_osm_chunk_insert_hook(void);
extern hypertable_drop_hook_type ts_get_osm_hypertable_drop_hook(void);
extern hypertable_drop_chunks_hook_type ts_get_osm_hypertable_drop_chunks_hook(void);
extern chunk_startup_exclusion_hook_type ts_get_osm_chunk_startup_exclusion_hook(void);

0 comments on commit 2bf2095

Please sign in to comment.