Skip to content

Commit

Permalink
Load OSM extension in retention background worker to drop tiered chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
zilder committed Feb 21, 2025
1 parent f7e626b commit b24b4ec
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "hypercube.h"
#include "hypertable.h"
#include "hypertable_cache.h"
#include "loader/loader.h"
#include "osm_callbacks.h"
#include "partitioning.h"
#include "process_utility.h"
Expand Down Expand Up @@ -3954,6 +3955,15 @@ ts_chunk_do_drop_chunks(Hypertable *ht, int64 older_than, int64 newer_than, int3
{
hypertable_drop_chunks_hook_type osm_drop_chunks_hook =
ts_get_osm_hypertable_drop_chunks_hook();

/*
* OSM library may not be loaded at the moment if `ts_chunk_do_drop_chunks`
* is called from the a background worker. If this is the case try to
* load it now.
*/
if (!osm_drop_chunks_hook && ts_try_load_osm())
osm_drop_chunks_hook = ts_get_osm_hypertable_drop_chunks_hook();

Check warning on line 3965 in src/chunk.c

View check run for this annotation

Codecov / codecov/patch

src/chunk.c#L3965

Added line #L3965 was not covered by tests

if (osm_drop_chunks_hook)
{
ListCell *lc;
Expand Down
1 change: 1 addition & 0 deletions src/extension_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define EXTENSION_SO TS_LIBDIR "" EXTENSION_NAME
#define EXTENSION_TSL_SO TS_LIBDIR TSL_LIBRARY_NAME "-" TIMESCALEDB_VERSION_MOD
#define TS_HYPERCORE_TAM_NAME "hypercore"
#define OSM_EXTENSION_NAME "timescaledb_osm"

#define MAKE_EXTOPTION(NAME) (EXTENSION_NAMESPACE "." NAME)

Expand Down
6 changes: 6 additions & 0 deletions src/loader/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ ts_loader_extension_version(void)
return extension_version(EXTENSION_NAME);
}

TSDLLEXPORT char *
ts_osm_extension_version(void)

Check warning on line 191 in src/loader/loader.c

View check run for this annotation

Codecov / codecov/patch

src/loader/loader.c#L191

Added line #L191 was not covered by tests
{
return extension_version(OSM_EXTENSION_NAME);

Check warning on line 193 in src/loader/loader.c

View check run for this annotation

Codecov / codecov/patch

src/loader/loader.c#L193

Added line #L193 was not covered by tests
}

extern bool
ts_loader_extension_exists(void)
{
Expand Down
2 changes: 2 additions & 0 deletions src/loader/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ extern bool ts_loader_extension_exists(void);

extern void ts_loader_extension_check(void);

extern char *ts_osm_extension_version(void);

/* WaitLatch expects a long, so make sure to cast the value */
/* Default value for timescaledb.launcher_poll_time */
#ifdef TS_DEBUG
Expand Down
50 changes: 50 additions & 0 deletions src/osm_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
*/
#include "osm_callbacks.h"

#include <commands/extension.h>
#include <fmgr.h>

#include "export.h"
#include "extension_constants.h"
#include "loader/loader.h"

#define OSM_CALLBACKS "osm_callbacks"
#define OSM_CALLBACKS_VAR_NAME "osm_callbacks_versioned"

Expand Down Expand Up @@ -74,3 +79,48 @@ ts_get_osm_hypertable_drop_chunks_hook()
return ptr->hypertable_drop_chunks_hook;
return NULL;
}

TSDLLEXPORT bool
ts_try_load_osm(void) {
bool res = false;
Oid osm_extension_oid = get_extension_oid(OSM_EXTENSION_NAME, true);

Check warning on line 86 in src/osm_callbacks.c

View check run for this annotation

Codecov / codecov/patch

src/osm_callbacks.c#L84-L86

Added lines #L84 - L86 were not covered by tests

if (OidIsValid(osm_extension_oid))
{
MemoryContext old_mcxt = CurrentMemoryContext;

Check warning on line 90 in src/osm_callbacks.c

View check run for this annotation

Codecov / codecov/patch

src/osm_callbacks.c#L90

Added line #L90 was not covered by tests

PG_TRY();
{
char version[MAX_VERSION_LEN];
char soname[MAX_SO_NAME_LEN];

// Form library path
strlcpy(version, ts_osm_extension_version(), MAX_VERSION_LEN);
snprintf(soname, MAX_SO_NAME_LEN, "%s%s-%s",

Check warning on line 99 in src/osm_callbacks.c

View check run for this annotation

Codecov / codecov/patch

src/osm_callbacks.c#L98-L99

Added lines #L98 - L99 were not covered by tests
TS_LIBDIR,
OSM_EXTENSION_NAME,
version);

// Load library
load_external_function(soname, "pfdw_handler_wrapper", false, NULL);
res = true;

Check warning on line 106 in src/osm_callbacks.c

View check run for this annotation

Codecov / codecov/patch

src/osm_callbacks.c#L105-L106

Added lines #L105 - L106 were not covered by tests
}
PG_CATCH();

Check warning on line 108 in src/osm_callbacks.c

View check run for this annotation

Codecov / codecov/patch

src/osm_callbacks.c#L108

Added line #L108 was not covered by tests
{
ErrorData *error;

/* Switch to the original context & copy edata */
MemoryContextSwitchTo(old_mcxt);
error = CopyErrorData();
FlushErrorState();

Check warning on line 115 in src/osm_callbacks.c

View check run for this annotation

Codecov / codecov/patch

src/osm_callbacks.c#L113-L115

Added lines #L113 - L115 were not covered by tests

elog(LOG, "failed to load OSM extension: %s", error->message);

/* Finally, free error data */
FreeErrorData(error);

Check warning on line 120 in src/osm_callbacks.c

View check run for this annotation

Codecov / codecov/patch

src/osm_callbacks.c#L120

Added line #L120 was not covered by tests
}
PG_END_TRY();
}

return res;

Check warning on line 125 in src/osm_callbacks.c

View check run for this annotation

Codecov / codecov/patch

src/osm_callbacks.c#L125

Added line #L125 was not covered by tests
}
1 change: 1 addition & 0 deletions src/osm_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ typedef struct
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 bool ts_try_load_osm(void);
2 changes: 0 additions & 2 deletions tsl/src/planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@

#include <math.h>

#define OSM_EXTENSION_NAME "timescaledb_osm"

static int osm_present = -1;

static bool
Expand Down

0 comments on commit b24b4ec

Please sign in to comment.