Refactor reorder policy

This patch changes the reorder policy to store it's configuration
in the bgw_job table and removes the bgw_policy_reorder table.
This commit is contained in:
Sven Klemm 2020-07-27 22:43:47 +02:00 committed by Sven Klemm
parent 09d37fa4f7
commit 3e83577916
29 changed files with 347 additions and 491 deletions

View File

@ -31,9 +31,17 @@ RETURNS INTEGER AS '@MODULE_PATHNAME@', 'ts_add_drop_chunks_policy'
LANGUAGE C VOLATILE STRICT; LANGUAGE C VOLATILE STRICT;
CREATE OR REPLACE FUNCTION add_reorder_policy(hypertable REGCLASS, index_name NAME, if_not_exists BOOL = false) RETURNS INTEGER CREATE OR REPLACE FUNCTION add_reorder_policy(hypertable REGCLASS, index_name NAME, if_not_exists BOOL = false) RETURNS INTEGER
AS '@MODULE_PATHNAME@', 'ts_add_reorder_policy' AS '@MODULE_PATHNAME@', 'ts_policy_reorder_add'
LANGUAGE C VOLATILE STRICT; LANGUAGE C VOLATILE STRICT;
CREATE OR REPLACE FUNCTION remove_reorder_policy(hypertable REGCLASS, if_exists BOOL = false) RETURNS VOID
AS '@MODULE_PATHNAME@', 'ts_policy_reorder_remove'
LANGUAGE C VOLATILE STRICT;
CREATE OR REPLACE PROCEDURE _timescaledb_internal.policy_reorder(job_id INTEGER, config JSONB)
AS '@MODULE_PATHNAME@', 'ts_policy_reorder_proc'
LANGUAGE C;
CREATE OR REPLACE FUNCTION add_compress_chunks_policy(hypertable REGCLASS, older_than "any", if_not_exists BOOL = false) CREATE OR REPLACE FUNCTION add_compress_chunks_policy(hypertable REGCLASS, older_than "any", if_not_exists BOOL = false)
RETURNS INTEGER RETURNS INTEGER
AS '@MODULE_PATHNAME@', 'ts_add_compress_chunks_policy' AS '@MODULE_PATHNAME@', 'ts_add_compress_chunks_policy'
@ -43,11 +51,7 @@ CREATE OR REPLACE FUNCTION remove_drop_chunks_policy(hypertable REGCLASS, if_exi
AS '@MODULE_PATHNAME@', 'ts_remove_drop_chunks_policy' AS '@MODULE_PATHNAME@', 'ts_remove_drop_chunks_policy'
LANGUAGE C VOLATILE STRICT; LANGUAGE C VOLATILE STRICT;
CREATE OR REPLACE FUNCTION remove_reorder_policy(hypertable REGCLASS, if_exists BOOL = false) RETURNS VOID CREATE OR REPLACE FUNCTION remove_compress_chunks_policy(hypertable REGCLASS, if_exists BOOL = false) RETURNS BOOL
AS '@MODULE_PATHNAME@', 'ts_remove_reorder_policy'
LANGUAGE C VOLATILE STRICT;
CREATE OR REPLACE FUNCTION remove_compress_chunks_policy(hypertable REGCLASS, if_exists BOOL = false) RETURNS BOOL
AS '@MODULE_PATHNAME@', 'ts_remove_compress_chunks_policy' AS '@MODULE_PATHNAME@', 'ts_remove_compress_chunks_policy'
LANGUAGE C VOLATILE STRICT; LANGUAGE C VOLATILE STRICT;

View File

@ -229,13 +229,6 @@ CREATE TABLE IF NOT EXISTS _timescaledb_internal.bgw_job_stat (
--the statistics probably aren't very meaningful across instances. --the statistics probably aren't very meaningful across instances.
--Now we define the argument tables for available BGW policies. --Now we define the argument tables for available BGW policies.
CREATE TABLE IF NOT EXISTS _timescaledb_config.bgw_policy_reorder (
job_id INTEGER PRIMARY KEY REFERENCES _timescaledb_config.bgw_job(id) ON DELETE CASCADE,
hypertable_id INTEGER UNIQUE NOT NULL REFERENCES _timescaledb_catalog.hypertable(id) ON DELETE CASCADE,
hypertable_index_name NAME NOT NULL
);
SELECT pg_catalog.pg_extension_config_dump('_timescaledb_config.bgw_policy_reorder', '');
CREATE TABLE IF NOT EXISTS _timescaledb_config.bgw_policy_drop_chunks ( CREATE TABLE IF NOT EXISTS _timescaledb_config.bgw_policy_drop_chunks (
job_id INTEGER PRIMARY KEY REFERENCES _timescaledb_config.bgw_job(id) ON DELETE CASCADE, job_id INTEGER PRIMARY KEY REFERENCES _timescaledb_config.bgw_job(id) ON DELETE CASCADE,
hypertable_id INTEGER UNIQUE NOT NULL REFERENCES _timescaledb_catalog.hypertable(id) ON DELETE CASCADE, hypertable_id INTEGER UNIQUE NOT NULL REFERENCES _timescaledb_catalog.hypertable(id) ON DELETE CASCADE,

View File

@ -118,6 +118,12 @@ ALTER TABLE IF EXISTS _timescaledb_catalog.compression_chunk_size ADD COLUMN IF
CLUSTER _timescaledb_catalog.compression_chunk_size USING compression_chunk_size_pkey; CLUSTER _timescaledb_catalog.compression_chunk_size USING compression_chunk_size_pkey;
ALTER TABLE _timescaledb_catalog.compression_chunk_size SET WITHOUT CLUSTER; ALTER TABLE _timescaledb_catalog.compression_chunk_size SET WITHOUT CLUSTER;
---Clean up constraints on hypertable catalog table ---
ALTER TABLE _timescaledb_catalog.hypertable ADD CONSTRAINT hypertable_table_name_schema_name_key UNIQUE(table_name, schema_name);
ALTER TABLE _timescaledb_catalog.hypertable DROP CONSTRAINT hypertable_schema_name_table_name_key;
ALTER TABLE _timescaledb_catalog.hypertable DROP CONSTRAINT hypertable_id_schema_name_key;
-- add fields for custom jobs/generic configuration to bgw_job table
ALTER TABLE _timescaledb_config.bgw_job ADD COLUMN proc_name NAME NOT NULL DEFAULT ''; ALTER TABLE _timescaledb_config.bgw_job ADD COLUMN proc_name NAME NOT NULL DEFAULT '';
ALTER TABLE _timescaledb_config.bgw_job ADD COLUMN proc_schema NAME NOT NULL DEFAULT ''; ALTER TABLE _timescaledb_config.bgw_job ADD COLUMN proc_schema NAME NOT NULL DEFAULT '';
ALTER TABLE _timescaledb_config.bgw_job ADD COLUMN owner NAME NOT NULL DEFAULT CURRENT_ROLE; ALTER TABLE _timescaledb_config.bgw_job ADD COLUMN owner NAME NOT NULL DEFAULT CURRENT_ROLE;
@ -128,13 +134,29 @@ ALTER TABLE _timescaledb_config.bgw_job ADD COLUMN config JSONB;
ALTER TABLE _timescaledb_config.bgw_job DROP CONSTRAINT valid_job_type; ALTER TABLE _timescaledb_config.bgw_job DROP CONSTRAINT valid_job_type;
ALTER TABLE _timescaledb_config.bgw_job ADD CONSTRAINT valid_job_type CHECK (job_type IN ('telemetry_and_version_check_if_enabled', 'reorder', 'drop_chunks', 'continuous_aggregate', 'compress_chunks', 'custom')); ALTER TABLE _timescaledb_config.bgw_job ADD CONSTRAINT valid_job_type CHECK (job_type IN ('telemetry_and_version_check_if_enabled', 'reorder', 'drop_chunks', 'continuous_aggregate', 'compress_chunks', 'custom'));
-- migrate reorder jobs
UPDATE
_timescaledb_config.bgw_job job
SET proc_name = 'policy_reorder',
proc_schema = '_timescaledb_internal',
config = jsonb_build_object('hypertable_id', reorder.hypertable_id, 'index_name', reorder.hypertable_index_name),
hypertable_id = reorder.hypertable_id,
OWNER = (
SELECT relowner::regrole::text
FROM _timescaledb_catalog.hypertable ht,
pg_class cl
WHERE ht.id = reorder.hypertable_id
AND cl.oid = format('%I.%I', schema_name, table_name)::regclass)
FROM _timescaledb_config.bgw_policy_reorder reorder
WHERE job_type = 'reorder'
AND job.id = reorder.job_id;
--rewrite catalog table to not break catalog scans on tables with missingval optimization --rewrite catalog table to not break catalog scans on tables with missingval optimization
CLUSTER _timescaledb_config.bgw_job USING bgw_job_pkey; CLUSTER _timescaledb_config.bgw_job USING bgw_job_pkey;
ALTER TABLE _timescaledb_config.bgw_job SET WITHOUT CLUSTER; ALTER TABLE _timescaledb_config.bgw_job SET WITHOUT CLUSTER;
CREATE INDEX IF NOT EXISTS bgw_job_proc_hypertable_id_idx ON _timescaledb_config.bgw_job(proc_name,proc_schema,hypertable_id); CREATE INDEX IF NOT EXISTS bgw_job_proc_hypertable_id_idx ON _timescaledb_config.bgw_job(proc_name,proc_schema,hypertable_id);
---Clean up constraints on hypertable catalog table --- ALTER EXTENSION timescaledb DROP TABLE _timescaledb_config.bgw_policy_reorder;
ALTER TABLE _timescaledb_catalog.hypertable ADD CONSTRAINT hypertable_table_name_schema_name_key UNIQUE(table_name, schema_name); DROP TABLE _timescaledb_config.bgw_policy_reorder CASCADE;
ALTER TABLE _timescaledb_catalog.hypertable DROP CONSTRAINT hypertable_schema_name_table_name_key;
ALTER TABLE _timescaledb_catalog.hypertable DROP CONSTRAINT hypertable_id_schema_name_key;

View File

@ -77,17 +77,23 @@ CREATE OR REPLACE VIEW timescaledb_information.drop_chunks_policies as
INNER JOIN _timescaledb_catalog.hypertable ht ON p.hypertable_id = ht.id INNER JOIN _timescaledb_catalog.hypertable ht ON p.hypertable_id = ht.id
INNER JOIN _timescaledb_config.bgw_job j ON p.job_id = j.id; INNER JOIN _timescaledb_config.bgw_job j ON p.job_id = j.id;
CREATE OR REPLACE VIEW timescaledb_information.reorder_policies as CREATE OR REPLACE VIEW timescaledb_information.reorder_policies AS
SELECT format('%1$I.%2$I', ht.schema_name, ht.table_name)::regclass as hypertable, p.hypertable_index_name, p.job_id, j.schedule_interval, SELECT format('%1$I.%2$I', ht.schema_name, ht.table_name)::regclass AS hypertable,
j.max_runtime, j.max_retries, j.retry_period jsonb_object_field_text (j.config, 'index_name') AS hypertable_index_name,
FROM _timescaledb_config.bgw_policy_reorder p j.id AS job_id,
INNER JOIN _timescaledb_catalog.hypertable ht ON p.hypertable_id = ht.id j.schedule_interval,
INNER JOIN _timescaledb_config.bgw_job j ON p.job_id = j.id; j.max_runtime,
j.max_retries,
j.retry_period
FROM _timescaledb_config.bgw_job j
INNER JOIN _timescaledb_catalog.hypertable ht ON j.hypertable_id = ht.id
WHERE job_type = 'reorder';
CREATE OR REPLACE VIEW timescaledb_information.policy_stats as CREATE OR REPLACE VIEW timescaledb_information.policy_stats as
SELECT format('%1$I.%2$I', ht.schema_name, ht.table_name)::regclass as hypertable, p.job_id, j.job_type, js.last_run_success, js.last_finish, js.last_successful_finish, js.last_start, js.next_start, SELECT format('%1$I.%2$I', ht.schema_name, ht.table_name)::regclass as hypertable, p.job_id, j.job_type, js.last_run_success, js.last_finish, js.last_successful_finish, js.last_start, js.next_start,
js.total_runs, js.total_failures js.total_runs, js.total_failures
FROM (SELECT job_id, hypertable_id FROM _timescaledb_config.bgw_policy_reorder FROM (SELECT id AS job_id, hypertable_id FROM _timescaledb_config.bgw_job WHERE job_type IN ('reorder')
UNION SELECT job_id, hypertable_id FROM _timescaledb_config.bgw_policy_drop_chunks UNION SELECT job_id, hypertable_id FROM _timescaledb_config.bgw_policy_drop_chunks
UNION SELECT job_id, hypertable_id FROM _timescaledb_config.bgw_policy_compress_chunks UNION SELECT job_id, hypertable_id FROM _timescaledb_config.bgw_policy_compress_chunks
UNION SELECT job_id, raw_hypertable_id FROM _timescaledb_catalog.continuous_agg) p UNION SELECT job_id, raw_hypertable_id FROM _timescaledb_catalog.continuous_agg) p

View File

@ -33,7 +33,7 @@
#include "bgw_policy/chunk_stats.h" #include "bgw_policy/chunk_stats.h"
#include "bgw_policy/drop_chunks.h" #include "bgw_policy/drop_chunks.h"
#include "bgw_policy/compress_chunks.h" #include "bgw_policy/compress_chunks.h"
#include "bgw_policy/reorder.h" #include "bgw_policy/policy.h"
#include "scan_iterator.h" #include "scan_iterator.h"
#include <cross_module_fn.h> #include <cross_module_fn.h>
@ -111,12 +111,7 @@ ts_bgw_job_owner(BgwJob *job)
return ts_catalog_database_info_get()->owner_uid; return ts_catalog_database_info_get()->owner_uid;
case JOB_TYPE_REORDER: case JOB_TYPE_REORDER:
{ {
BgwPolicyReorder *policy = ts_bgw_policy_reorder_find_by_job(job->fd.id); return get_role_oid(NameStr(job->fd.owner), false);
if (policy == NULL)
elog(ERROR, "reorder policy for job with id \"%d\" not found", job->fd.id);
return ts_rel_get_owner(ts_hypertable_id_to_relid(policy->fd.hypertable_id));
} }
case JOB_TYPE_DROP_CHUNKS: case JOB_TYPE_DROP_CHUNKS:
{ {
@ -572,7 +567,6 @@ bgw_job_tuple_delete(TupleInfo *ti, void *data)
ts_bgw_job_stat_delete(job_id); ts_bgw_job_stat_delete(job_id);
/* Delete any policy args associated with this job */ /* Delete any policy args associated with this job */
ts_bgw_policy_reorder_delete_row_only_by_job_id(job_id);
ts_bgw_policy_drop_chunks_delete_row_only_by_job_id(job_id); ts_bgw_policy_drop_chunks_delete_row_only_by_job_id(job_id);
ts_bgw_policy_compress_chunks_delete_row_only_by_job_id(job_id); ts_bgw_policy_compress_chunks_delete_row_only_by_job_id(job_id);

View File

@ -1,5 +1,4 @@
set(SOURCES set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/reorder.c
${CMAKE_CURRENT_SOURCE_DIR}/drop_chunks.c ${CMAKE_CURRENT_SOURCE_DIR}/drop_chunks.c
${CMAKE_CURRENT_SOURCE_DIR}/compress_chunks.c ${CMAKE_CURRENT_SOURCE_DIR}/compress_chunks.c
${CMAKE_CURRENT_SOURCE_DIR}/policy.c ${CMAKE_CURRENT_SOURCE_DIR}/policy.c

View File

@ -3,26 +3,25 @@
* Please see the included NOTICE for copyright information and * Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license. * LICENSE-APACHE for a copy of the license.
*/ */
#include <postgres.h> #include <postgres.h>
#include "policy.h"
#include "bgw_policy/reorder.h"
#include "bgw_policy/drop_chunks.h"
#include "bgw_policy/compress_chunks.h"
#include "bgw/job.h" #include "bgw/job.h"
#include "bgw_policy/compress_chunks.h"
#include "bgw_policy/drop_chunks.h"
#include "policy.h"
void void
ts_bgw_policy_delete_by_hypertable_id(int32 hypertable_id) ts_bgw_policy_delete_by_hypertable_id(int32 hypertable_id)
{ {
List *jobs;
ListCell *lc;
/* /*
* Because this is used in a cascaded delete from hypertable deletion, we * Because this is used in a cascaded delete from hypertable deletion, we
* also need to delete the job. This means we don't actually call the * also need to delete the job. This means we don't actually call the
* delete on the individual policy, but call the bgw_job delete function. * delete on the individual policy, but call the bgw_job delete function.
*/ */
void *policy = ts_bgw_policy_reorder_find_by_hypertable(hypertable_id); void *policy;
if (policy)
ts_bgw_job_delete_by_id(((BgwPolicyReorder *) policy)->fd.job_id);
policy = ts_bgw_policy_drop_chunks_find_by_hypertable(hypertable_id); policy = ts_bgw_policy_drop_chunks_find_by_hypertable(hypertable_id);
@ -33,6 +32,13 @@ ts_bgw_policy_delete_by_hypertable_id(int32 hypertable_id)
if (policy) if (policy)
ts_bgw_job_delete_by_id(((BgwPolicyCompressChunks *) policy)->fd.job_id); ts_bgw_job_delete_by_id(((BgwPolicyCompressChunks *) policy)->fd.job_id);
jobs = ts_bgw_job_find_by_hypertable_id(hypertable_id);
foreach (lc, jobs)
{
BgwJob *job = lfirst(lc);
ts_bgw_job_delete_by_id(job->fd.id);
}
} }
/* This function does NOT cascade deletes to the bgw_job table. */ /* This function does NOT cascade deletes to the bgw_job table. */
@ -47,3 +53,11 @@ ts_bgw_policy_delete_row_only_tuple_found(TupleInfo *ti, void *const data)
return SCAN_CONTINUE; return SCAN_CONTINUE;
} }
int32
ts_bgw_policy_reorder_count()
{
List *jobs = ts_bgw_job_find_by_proc("policy_reorder", INTERNAL_SCHEMA_NAME);
return list_length(jobs);
}

View File

@ -9,9 +9,11 @@
#include "scanner.h" #include "scanner.h"
#include "catalog.h" #include "catalog.h"
#include "export.h"
ScanTupleResult ts_bgw_policy_delete_row_only_tuple_found(TupleInfo *ti, void *const data); extern ScanTupleResult ts_bgw_policy_delete_row_only_tuple_found(TupleInfo *ti, void *const data);
void ts_bgw_policy_delete_by_hypertable_id(int32 hypertable_id); extern void ts_bgw_policy_delete_by_hypertable_id(int32 hypertable_id);
extern int32 ts_bgw_policy_reorder_count(void);
#endif /* TIMESCALEDB_BGW_POLICY_POLICY_H */ #endif /* TIMESCALEDB_BGW_POLICY_POLICY_H */

View File

@ -1,149 +0,0 @@
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include <postgres.h>
#include <utils/builtins.h>
#include <utils/timestamp.h>
#include <utils/lsyscache.h>
#include <utils/syscache.h>
#include "catalog.h"
#include "policy.h"
#include "reorder.h"
#include "scanner.h"
#include "utils.h"
#include "hypertable.h"
#include "bgw/job.h"
#include "scan_iterator.h"
#include "compat.h"
static ScanTupleResult
bgw_policy_reorder_tuple_found(TupleInfo *ti, void *const data)
{
BgwPolicyReorder **policy = data;
*policy = STRUCT_FROM_SLOT(ti->slot, ti->mctx, BgwPolicyReorder, FormData_bgw_policy_reorder);
return SCAN_CONTINUE;
}
/*
* To prevent infinite recursive calls from the job <-> policy tables, we do not cascade deletes in
* this function. Instead, the caller must be responsible for making sure that the delete cascades
* to the job corresponding to this policy.
*/
bool
ts_bgw_policy_reorder_delete_row_only_by_job_id(int32 job_id)
{
ScanKeyData scankey[1];
ScanKeyInit(&scankey[0],
Anum_bgw_policy_reorder_pkey_idx_job_id,
BTEqualStrategyNumber,
F_INT4EQ,
Int32GetDatum(job_id));
return ts_catalog_scan_one(BGW_POLICY_REORDER,
BGW_POLICY_REORDER_PKEY_IDX,
scankey,
1,
ts_bgw_policy_delete_row_only_tuple_found,
RowExclusiveLock,
BGW_POLICY_REORDER_TABLE_NAME,
NULL);
}
BgwPolicyReorder *
ts_bgw_policy_reorder_find_by_job(int32 job_id)
{
ScanKeyData scankey[1];
BgwPolicyReorder *ret = NULL;
ScanKeyInit(&scankey[0],
Anum_bgw_policy_reorder_pkey_idx_job_id,
BTEqualStrategyNumber,
F_INT4EQ,
Int32GetDatum(job_id));
ts_catalog_scan_one(BGW_POLICY_REORDER,
BGW_POLICY_REORDER_PKEY_IDX,
scankey,
1,
bgw_policy_reorder_tuple_found,
AccessShareLock,
BGW_POLICY_REORDER_TABLE_NAME,
(void *) &ret);
return ret;
}
BgwPolicyReorder *
ts_bgw_policy_reorder_find_by_hypertable(int32 hypertable_id)
{
ScanKeyData scankey[1];
BgwPolicyReorder *ret = NULL;
ScanKeyInit(&scankey[0],
Anum_bgw_policy_reorder_hypertable_id_idx_hypertable_id,
BTEqualStrategyNumber,
F_INT4EQ,
Int32GetDatum(hypertable_id));
ts_catalog_scan_one(BGW_POLICY_REORDER,
BGW_POLICY_REORDER_HYPERTABLE_ID_IDX,
scankey,
1,
bgw_policy_reorder_tuple_found,
AccessShareLock,
BGW_POLICY_REORDER_TABLE_NAME,
(void *) &ret);
return ret;
}
static void
ts_bgw_policy_reorder_insert_with_relation(Relation rel, BgwPolicyReorder *policy)
{
TupleDesc tupdesc;
CatalogSecurityContext sec_ctx;
Datum values[Natts_bgw_policy_reorder];
bool nulls[Natts_bgw_policy_reorder] = { false };
tupdesc = RelationGetDescr(rel);
values[AttrNumberGetAttrOffset(Anum_bgw_policy_reorder_job_id)] =
Int32GetDatum(policy->fd.job_id);
values[AttrNumberGetAttrOffset(Anum_bgw_policy_reorder_hypertable_id)] =
Int32GetDatum(policy->fd.hypertable_id);
values[AttrNumberGetAttrOffset(Anum_bgw_policy_reorder_hypertable_index_name)] =
NameGetDatum(&policy->fd.hypertable_index_name);
ts_catalog_database_info_become_owner(ts_catalog_database_info_get(), &sec_ctx);
ts_catalog_insert_values(rel, tupdesc, values, nulls);
ts_catalog_restore_user(&sec_ctx);
}
void
ts_bgw_policy_reorder_insert(BgwPolicyReorder *policy)
{
Catalog *catalog = ts_catalog_get();
Relation rel = table_open(catalog_get_table_id(catalog, BGW_POLICY_REORDER), RowExclusiveLock);
ts_bgw_policy_reorder_insert_with_relation(rel, policy);
table_close(rel, RowExclusiveLock);
}
TSDLLEXPORT int32
ts_bgw_policy_reorder_count()
{
int32 count = 0;
ScanIterator iterator =
ts_scan_iterator_create(BGW_POLICY_REORDER, AccessShareLock, CurrentMemoryContext);
ts_scanner_foreach(&iterator) { count++; }
return count;
}

View File

@ -1,24 +0,0 @@
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#ifndef TIMESCALEDB_BGW_POLICY_REORDER_H
#define TIMESCALEDB_BGW_POLICY_REORDER_H
#include "catalog.h"
#include "export.h"
typedef struct BgwPolicyReorder
{
FormData_bgw_policy_reorder fd;
} BgwPolicyReorder;
extern TSDLLEXPORT BgwPolicyReorder *ts_bgw_policy_reorder_find_by_job(int32 job_id);
extern TSDLLEXPORT BgwPolicyReorder *ts_bgw_policy_reorder_find_by_hypertable(int32 hypertable_id);
extern TSDLLEXPORT void ts_bgw_policy_reorder_insert(BgwPolicyReorder *policy);
extern TSDLLEXPORT bool ts_bgw_policy_reorder_delete_row_only_by_job_id(int32 job_id);
extern TSDLLEXPORT int32 ts_bgw_policy_reorder_count(void);
#endif /* TIMESCALEDB_BGW_POLICY_REORDER_H */

View File

@ -71,10 +71,6 @@ static const TableInfoDef catalog_table_names[_MAX_CATALOG_TABLES + 1] = {
.schema_name = CATALOG_SCHEMA_NAME, .schema_name = CATALOG_SCHEMA_NAME,
.table_name = METADATA_TABLE_NAME, .table_name = METADATA_TABLE_NAME,
}, },
[BGW_POLICY_REORDER] = {
.schema_name = CONFIG_SCHEMA_NAME,
.table_name = BGW_POLICY_REORDER_TABLE_NAME,
},
[BGW_POLICY_DROP_CHUNKS] = { [BGW_POLICY_DROP_CHUNKS] = {
.schema_name = CONFIG_SCHEMA_NAME, .schema_name = CONFIG_SCHEMA_NAME,
.table_name = BGW_POLICY_DROP_CHUNKS_TABLE_NAME, .table_name = BGW_POLICY_DROP_CHUNKS_TABLE_NAME,
@ -210,13 +206,6 @@ static const TableIndexDef catalog_table_index_definitions[_MAX_CATALOG_TABLES]
[METADATA_PKEY_IDX] = "metadata_pkey", [METADATA_PKEY_IDX] = "metadata_pkey",
}, },
}, },
[BGW_POLICY_REORDER] = {
.length = _MAX_BGW_POLICY_REORDER_INDEX,
.names = (char *[]) {
[BGW_POLICY_REORDER_PKEY_IDX] = "bgw_policy_reorder_pkey",
[BGW_POLICY_REORDER_HYPERTABLE_ID_IDX] = "bgw_policy_reorder_hypertable_id_key",
},
},
[BGW_POLICY_DROP_CHUNKS] = { [BGW_POLICY_DROP_CHUNKS] = {
.length = _MAX_BGW_POLICY_DROP_CHUNKS_INDEX, .length = _MAX_BGW_POLICY_DROP_CHUNKS_INDEX,
.names = (char *[]) { .names = (char *[]) {
@ -304,7 +293,6 @@ static const char *catalog_table_serial_id_names[_MAX_CATALOG_TABLES] = {
[TABLESPACE] = CATALOG_SCHEMA_NAME ".tablespace_id_seq", [TABLESPACE] = CATALOG_SCHEMA_NAME ".tablespace_id_seq",
[BGW_JOB] = CONFIG_SCHEMA_NAME ".bgw_job_id_seq", [BGW_JOB] = CONFIG_SCHEMA_NAME ".bgw_job_id_seq",
[BGW_JOB_STAT] = NULL, [BGW_JOB_STAT] = NULL,
[BGW_POLICY_REORDER] = NULL,
[BGW_POLICY_DROP_CHUNKS] = NULL, [BGW_POLICY_DROP_CHUNKS] = NULL,
[CONTINUOUS_AGGS_COMPLETED_THRESHOLD] = NULL, [CONTINUOUS_AGGS_COMPLETED_THRESHOLD] = NULL,
[CONTINUOUS_AGGS_HYPERTABLE_INVALIDATION_LOG] = NULL, [CONTINUOUS_AGGS_HYPERTABLE_INVALIDATION_LOG] = NULL,

View File

@ -46,7 +46,6 @@ typedef enum CatalogTable
BGW_JOB, BGW_JOB,
BGW_JOB_STAT, BGW_JOB_STAT,
METADATA, METADATA,
BGW_POLICY_REORDER,
BGW_POLICY_DROP_CHUNKS, BGW_POLICY_DROP_CHUNKS,
BGW_POLICY_CHUNK_STATS, BGW_POLICY_CHUNK_STATS,
CONTINUOUS_AGG, CONTINUOUS_AGG,
@ -806,57 +805,6 @@ enum
_MAX_METADATA_INDEX, _MAX_METADATA_INDEX,
}; };
/****** BGW_POLICY_REORDER TABLE definitions */
#define BGW_POLICY_REORDER_TABLE_NAME "bgw_policy_reorder"
enum Anum_bgw_policy_reorder
{
Anum_bgw_policy_reorder_job_id = 1,
Anum_bgw_policy_reorder_hypertable_id,
Anum_bgw_policy_reorder_hypertable_index_name,
_Anum_bgw_policy_reorder_max,
};
#define Natts_bgw_policy_reorder (_Anum_bgw_policy_reorder_max - 1)
typedef struct FormData_bgw_policy_reorder
{
int32 job_id;
int32 hypertable_id;
NameData hypertable_index_name;
} FormData_bgw_policy_reorder;
typedef FormData_bgw_policy_reorder *Form_bgw_policy_reorder;
enum
{
BGW_POLICY_REORDER_PKEY_IDX = 0,
BGW_POLICY_REORDER_HYPERTABLE_ID_IDX,
_MAX_BGW_POLICY_REORDER_INDEX,
};
enum Anum_bgw_policy_reorder_pkey_idx
{
Anum_bgw_policy_reorder_pkey_idx_job_id = 1,
_Anum_bgw_policy_reorder_pkey_idx_max,
};
typedef struct FormData_bgw_policy_reorder_pkey_idx
{
int32 job_id;
} FormData_bgw_policy_reorder_pkey_idx;
enum Anum_bgw_policy_reorder_hypertable_id_idx
{
Anum_bgw_policy_reorder_hypertable_id_idx_hypertable_id = 1,
_Anum_bgw_policy_reorder_hypertable_id_idx_max,
};
typedef struct FormData_bgw_policy_reorder_hypertable_id_idx
{
int32 hypertable_id;
} FormData_bgw_policy_reorder_hypertable_id_idx;
/****************************************** /******************************************
* *
* bgw_policy_drop_chunks table definitions * bgw_policy_drop_chunks table definitions

View File

@ -23,11 +23,13 @@
/* bgw policy functions */ /* bgw policy functions */
CROSSMODULE_WRAPPER(add_compress_chunks_policy); CROSSMODULE_WRAPPER(add_compress_chunks_policy);
CROSSMODULE_WRAPPER(add_drop_chunks_policy); CROSSMODULE_WRAPPER(add_drop_chunks_policy);
CROSSMODULE_WRAPPER(add_reorder_policy); CROSSMODULE_WRAPPER(policy_reorder_add);
CROSSMODULE_WRAPPER(policy_reorder_proc);
CROSSMODULE_WRAPPER(policy_reorder_remove);
CROSSMODULE_WRAPPER(alter_job_schedule); CROSSMODULE_WRAPPER(alter_job_schedule);
CROSSMODULE_WRAPPER(remove_compress_chunks_policy); CROSSMODULE_WRAPPER(remove_compress_chunks_policy);
CROSSMODULE_WRAPPER(remove_drop_chunks_policy); CROSSMODULE_WRAPPER(remove_drop_chunks_policy);
CROSSMODULE_WRAPPER(remove_reorder_policy);
CROSSMODULE_WRAPPER(reorder_chunk); CROSSMODULE_WRAPPER(reorder_chunk);
CROSSMODULE_WRAPPER(move_chunk); CROSSMODULE_WRAPPER(move_chunk);
@ -335,13 +337,15 @@ TSDLLEXPORT CrossModuleFunctions ts_cm_functions_default = {
/* bgw policies */ /* bgw policies */
.add_compress_chunks_policy = error_no_default_fn_pg_community, .add_compress_chunks_policy = error_no_default_fn_pg_community,
.add_drop_chunks_policy = error_no_default_fn_pg_community, .add_drop_chunks_policy = error_no_default_fn_pg_community,
.add_reorder_policy = error_no_default_fn_pg_community, .policy_reorder_add = error_no_default_fn_pg_community,
.policy_reorder_proc = error_no_default_fn_pg_community,
.policy_reorder_remove = error_no_default_fn_pg_community,
.alter_job_schedule = error_no_default_fn_pg_community, .alter_job_schedule = error_no_default_fn_pg_community,
.bgw_policy_job_execute = bgw_policy_job_execute_default_fn, .bgw_policy_job_execute = bgw_policy_job_execute_default_fn,
.move_chunk = error_no_default_fn_pg_enterprise,
.remove_compress_chunks_policy = error_no_default_fn_pg_community, .remove_compress_chunks_policy = error_no_default_fn_pg_community,
.remove_drop_chunks_policy = error_no_default_fn_pg_community, .remove_drop_chunks_policy = error_no_default_fn_pg_community,
.remove_reorder_policy = error_no_default_fn_pg_community,
.move_chunk = error_no_default_fn_pg_enterprise,
.reorder_chunk = error_no_default_fn_pg_community, .reorder_chunk = error_no_default_fn_pg_community,
.partialize_agg = error_no_default_fn_pg_community, .partialize_agg = error_no_default_fn_pg_community,

View File

@ -47,18 +47,23 @@ typedef struct CrossModuleFunctions
void (*add_tsl_telemetry_info)(JsonbParseState **parseState); void (*add_tsl_telemetry_info)(JsonbParseState **parseState);
bool (*bgw_policy_job_execute)(BgwJob *job); bool (*bgw_policy_job_execute)(BgwJob *job);
bool (*continuous_agg_materialize)(int32 materialization_id, ContinuousAggMatOptions *options); bool (*continuous_agg_materialize)(int32 materialization_id, ContinuousAggMatOptions *options);
Datum (*add_drop_chunks_policy)(PG_FUNCTION_ARGS);
Datum (*add_reorder_policy)(PG_FUNCTION_ARGS); PGFunction add_drop_chunks_policy;
Datum (*add_compress_chunks_policy)(PG_FUNCTION_ARGS); PGFunction policy_reorder_add;
Datum (*remove_drop_chunks_policy)(PG_FUNCTION_ARGS); PGFunction policy_reorder_proc;
Datum (*remove_reorder_policy)(PG_FUNCTION_ARGS); PGFunction policy_reorder_remove;
Datum (*remove_compress_chunks_policy)(PG_FUNCTION_ARGS); PGFunction add_compress_chunks_policy;
PGFunction remove_drop_chunks_policy;
PGFunction remove_compress_chunks_policy;
void (*create_upper_paths_hook)(PlannerInfo *, UpperRelationKind, RelOptInfo *, RelOptInfo *, void (*create_upper_paths_hook)(PlannerInfo *, UpperRelationKind, RelOptInfo *, RelOptInfo *,
TsRelType input_reltype, Hypertable *ht, void *extra); TsRelType input_reltype, Hypertable *ht, void *extra);
void (*set_rel_pathlist_dml)(PlannerInfo *, RelOptInfo *, Index, RangeTblEntry *, Hypertable *); void (*set_rel_pathlist_dml)(PlannerInfo *, RelOptInfo *, Index, RangeTblEntry *, Hypertable *);
void (*set_rel_pathlist_query)(PlannerInfo *, RelOptInfo *, Index, RangeTblEntry *, void (*set_rel_pathlist_query)(PlannerInfo *, RelOptInfo *, Index, RangeTblEntry *,
Hypertable *); Hypertable *);
void (*set_rel_pathlist)(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte); void (*set_rel_pathlist)(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte);
/* gapfill */
PGFunction gapfill_marker; PGFunction gapfill_marker;
PGFunction gapfill_int16_time_bucket; PGFunction gapfill_int16_time_bucket;
PGFunction gapfill_int32_time_bucket; PGFunction gapfill_int32_time_bucket;
@ -66,6 +71,7 @@ typedef struct CrossModuleFunctions
PGFunction gapfill_date_time_bucket; PGFunction gapfill_date_time_bucket;
PGFunction gapfill_timestamp_time_bucket; PGFunction gapfill_timestamp_time_bucket;
PGFunction gapfill_timestamptz_time_bucket; PGFunction gapfill_timestamptz_time_bucket;
PGFunction alter_job_schedule; PGFunction alter_job_schedule;
PGFunction reorder_chunk; PGFunction reorder_chunk;
PGFunction move_chunk; PGFunction move_chunk;

View File

@ -56,6 +56,26 @@ ts_jsonb_add_int32(JsonbParseState *state, const char *key, const int32 int_valu
ts_jsonb_add_numeric(state, key, value); ts_jsonb_add_numeric(state, key, value);
} }
void
ts_jsonb_add_int64(JsonbParseState *state, const char *key, const int64 int_value)
{
Numeric value;
value = DatumGetNumeric(DirectFunctionCall1(int4_numeric, Int64GetDatum(int_value)));
ts_jsonb_add_numeric(state, key, value);
}
void
ts_jsonb_add_interval(JsonbParseState *state, const char *key, Interval *interval)
{
char *value;
value = DatumGetCString(DirectFunctionCall1(interval_out, IntervalPGetDatum(interval)));
ts_jsonb_add_str(state, key, value);
}
void void
ts_jsonb_add_numeric(JsonbParseState *state, const char *key, const Numeric value) ts_jsonb_add_numeric(JsonbParseState *state, const char *key, const Numeric value)
{ {
@ -153,3 +173,35 @@ ts_jsonb_get_int32_field(Jsonb *json, const char *key, bool *field_found)
*field_found = true; *field_found = true;
return DatumGetInt32(int_datum); return DatumGetInt32(int_datum);
} }
int64
ts_jsonb_get_int64_field(Jsonb *json, const char *key, bool *field_found)
{
Datum int_datum;
char *int_str = ts_jsonb_get_str_field(json, key);
if (int_str == NULL)
{
*field_found = false;
return 0;
}
int_datum = DirectFunctionCall1(int8in, CStringGetDatum(int_str));
*field_found = true;
return DatumGetInt64(int_datum);
}
Interval *
ts_jsonb_get_interval_field(Jsonb *json, const char *key)
{
Datum interval_datum;
char *interval_str = ts_jsonb_get_str_field(json, key);
if (interval_str == NULL)
return NULL;
interval_datum = DirectFunctionCall3(interval_in, CStringGetDatum("1 day"), InvalidOid, -1);
return DatumGetIntervalP(interval_datum);
}

View File

@ -15,16 +15,22 @@
extern TSDLLEXPORT void ts_jsonb_add_bool(JsonbParseState *state, const char *key, bool boolean); extern TSDLLEXPORT void ts_jsonb_add_bool(JsonbParseState *state, const char *key, bool boolean);
extern TSDLLEXPORT void ts_jsonb_add_str(JsonbParseState *state, const char *key, extern TSDLLEXPORT void ts_jsonb_add_str(JsonbParseState *state, const char *key,
const char *value); const char *value);
extern TSDLLEXPORT void ts_jsonb_add_interval(JsonbParseState *state, const char *key,
Interval *value);
extern TSDLLEXPORT void ts_jsonb_add_int32(JsonbParseState *state, const char *key, extern TSDLLEXPORT void ts_jsonb_add_int32(JsonbParseState *state, const char *key,
const int32 value); const int32 value);
extern TSDLLEXPORT void ts_jsonb_add_int64(JsonbParseState *state, const char *key,
const int64 value);
extern TSDLLEXPORT void ts_jsonb_add_numeric(JsonbParseState *state, const char *key, extern TSDLLEXPORT void ts_jsonb_add_numeric(JsonbParseState *state, const char *key,
const Numeric value); const Numeric value);
extern void ts_jsonb_add_value(JsonbParseState *state, const char *key, JsonbValue *value); extern void ts_jsonb_add_value(JsonbParseState *state, const char *key, JsonbValue *value);
extern TSDLLEXPORT char *ts_jsonb_get_str_field(Jsonb *jsonb, const char *key); extern TSDLLEXPORT char *ts_jsonb_get_str_field(Jsonb *jsonb, const char *key);
extern TSDLLEXPORT Interval *ts_jsonb_get_interval_field(Jsonb *jsonb, const char *key);
extern TSDLLEXPORT TimestampTz ts_jsonb_get_time_field(Jsonb *jsonb, const char *key, extern TSDLLEXPORT TimestampTz ts_jsonb_get_time_field(Jsonb *jsonb, const char *key,
bool *field_found); bool *field_found);
extern TSDLLEXPORT int32 ts_jsonb_get_int32_field(Jsonb *json, const char *key, bool *field_found); extern TSDLLEXPORT int32 ts_jsonb_get_int32_field(Jsonb *json, const char *key, bool *field_found);
extern TSDLLEXPORT int64 ts_jsonb_get_int64_field(Jsonb *json, const char *key, bool *field_found);
#endif /* TIMESCALEDB_JSONB_UTILS_H */ #endif /* TIMESCALEDB_JSONB_UTILS_H */

View File

@ -25,7 +25,7 @@
#include "jsonb_utils.h" #include "jsonb_utils.h"
#include "license_guc.h" #include "license_guc.h"
#include "bgw_policy/drop_chunks.h" #include "bgw_policy/drop_chunks.h"
#include "bgw_policy/reorder.h" #include "bgw_policy/policy.h"
#include "compression_chunk_size.h" #include "compression_chunk_size.h"
#include "cross_module_fn.h" #include "cross_module_fn.h"

View File

@ -25,7 +25,7 @@
#include "bgw_policy/chunk_stats.h" #include "bgw_policy/chunk_stats.h"
#include "bgw_policy/drop_chunks.h" #include "bgw_policy/drop_chunks.h"
#include "bgw_policy/compress_chunks.h" #include "bgw_policy/compress_chunks.h"
#include "bgw_policy/reorder.h" #include "bgw_policy/reorder_api.h"
#include "compression/compress_utils.h" #include "compression/compress_utils.h"
#include "continuous_aggs/materialize.h" #include "continuous_aggs/materialize.h"
#include "continuous_aggs/job.h" #include "continuous_aggs/job.h"
@ -104,14 +104,12 @@ get_chunk_to_compress(Hypertable *ht, FormData_ts_interval *older_than)
} }
bool bool
execute_reorder_policy(BgwJob *job, reorder_func reorder, bool fast_continue) execute_reorder_policy(int32 job_id, Jsonb *config, reorder_func reorder, bool fast_continue)
{ {
int chunk_id; int chunk_id;
bool started = false; bool started = false;
BgwPolicyReorder *args;
Hypertable *ht; Hypertable *ht;
Chunk *chunk; Chunk *chunk;
int32 job_id = job->fd.id;
if (!IsTransactionOrTransactionBlock()) if (!IsTransactionOrTransactionBlock())
{ {
@ -119,19 +117,10 @@ execute_reorder_policy(BgwJob *job, reorder_func reorder, bool fast_continue)
StartTransactionCommand(); StartTransactionCommand();
} }
/* Get the arguments from the reorder_policy table */ ht = ts_hypertable_get_by_id(policy_reorder_get_hypertable_id(config));
args = ts_bgw_policy_reorder_find_by_job(job_id);
if (args == NULL)
ereport(ERROR,
(errcode(ERRCODE_TS_INTERNAL_ERROR),
errmsg("could not run reorder policy #%d because no args in policy table",
job_id)));
ht = ts_hypertable_get_by_id(args->fd.hypertable_id);
/* Find a chunk to reorder in the selected hypertable */ /* Find a chunk to reorder in the selected hypertable */
chunk_id = get_chunk_id_to_reorder(args->fd.job_id, ht); chunk_id = get_chunk_id_to_reorder(job_id, ht);
if (chunk_id == -1) if (chunk_id == -1)
{ {
@ -150,7 +139,7 @@ execute_reorder_policy(BgwJob *job, reorder_func reorder, bool fast_continue)
chunk = ts_chunk_get_by_id(chunk_id, false); chunk = ts_chunk_get_by_id(chunk_id, false);
elog(LOG, "reordering chunk %s.%s", chunk->fd.schema_name.data, chunk->fd.table_name.data); elog(LOG, "reordering chunk %s.%s", chunk->fd.schema_name.data, chunk->fd.table_name.data);
reorder(chunk->table_id, reorder(chunk->table_id,
get_relname_relid(NameStr(args->fd.hypertable_index_name), get_relname_relid(policy_reorder_get_index_name(config),
get_namespace_oid(NameStr(ht->fd.schema_name), false)), get_namespace_oid(NameStr(ht->fd.schema_name), false)),
false, false,
InvalidOid, InvalidOid,
@ -162,12 +151,13 @@ execute_reorder_policy(BgwJob *job, reorder_func reorder, bool fast_continue)
chunk->fd.table_name.data); chunk->fd.table_name.data);
/* Now update chunk_stats table */ /* Now update chunk_stats table */
ts_bgw_policy_chunk_stats_record_job_run(args->fd.job_id, ts_bgw_policy_chunk_stats_record_job_run(job_id, chunk_id, ts_timer_get_current_timestamp());
chunk_id,
ts_timer_get_current_timestamp());
if (fast_continue && get_chunk_id_to_reorder(args->fd.job_id, ht) != -1) if (fast_continue && get_chunk_id_to_reorder(job_id, ht) != -1)
{
BgwJob *job = ts_bgw_job_find(job_id, CurrentMemoryContext, true);
enable_fast_restart(job, "reorder"); enable_fast_restart(job, "reorder");
}
commit: commit:
if (started) if (started)
@ -397,7 +387,7 @@ tsl_bgw_policy_job_execute(BgwJob *job)
switch (job->bgw_type) switch (job->bgw_type)
{ {
case JOB_TYPE_REORDER: case JOB_TYPE_REORDER:
return execute_reorder_policy(job, reorder_chunk, true); return execute_reorder_policy(job->fd.id, job->fd.config, reorder_chunk, true);
case JOB_TYPE_DROP_CHUNKS: case JOB_TYPE_DROP_CHUNKS:
return execute_drop_chunks_policy(job->fd.id); return execute_drop_chunks_policy(job->fd.id);
case JOB_TYPE_CONTINUOUS_AGGREGATE: case JOB_TYPE_CONTINUOUS_AGGREGATE:

View File

@ -18,7 +18,8 @@ typedef void (*reorder_func)(Oid tableOid, Oid indexOid, bool verbose, Oid wait_
Oid destination_tablespace, Oid index_tablespace); Oid destination_tablespace, Oid index_tablespace);
/* Functions exposed only for testing */ /* Functions exposed only for testing */
extern bool execute_reorder_policy(BgwJob *job, reorder_func reorder, bool fast_continue); extern bool execute_reorder_policy(int32 job_id, Jsonb *config, reorder_func reorder,
bool fast_continue);
extern bool execute_drop_chunks_policy(int32 job_id); extern bool execute_drop_chunks_policy(int32 job_id);
extern bool execute_compress_chunks_policy(BgwJob *job); extern bool execute_compress_chunks_policy(BgwJob *job);
extern bool tsl_bgw_policy_job_execute(BgwJob *job); extern bool tsl_bgw_policy_job_execute(BgwJob *job);

View File

@ -14,16 +14,17 @@
#include <utils/syscache.h> #include <utils/syscache.h>
#include <miscadmin.h> #include <miscadmin.h>
#include <compat.h>
#include <dimension.h> #include <dimension.h>
#include <jsonb_utils.h>
#include "compat.h"
#include "bgw/job.h" #include "bgw/job.h"
#include "bgw_policy/reorder.h" #include "bgw_policy/job.h"
#include "bgw_policy/reorder_api.h"
#include "errors.h" #include "errors.h"
#include "hypertable.h" #include "hypertable.h"
#include "license.h" #include "license.h"
#include "reorder_api.h" #include "reorder.h"
#include "utils.h" #include "utils.h"
/* /*
@ -32,7 +33,10 @@
* the default is 4 days, which is approximately 1/2 of the default chunk size, 7 days. * the default is 4 days, which is approximately 1/2 of the default chunk size, 7 days.
*/ */
#define DEFAULT_SCHEDULE_INTERVAL \ #define DEFAULT_SCHEDULE_INTERVAL \
DatumGetIntervalP(DirectFunctionCall3(interval_in, CStringGetDatum("4 days"), InvalidOid, -1)) { \
.day = 4 \
}
/* Default max runtime for a reorder job is unlimited for now */ /* Default max runtime for a reorder job is unlimited for now */
#define DEFAULT_MAX_RUNTIME \ #define DEFAULT_MAX_RUNTIME \
DatumGetIntervalP(DirectFunctionCall3(interval_in, CStringGetDatum("0"), InvalidOid, -1)) DatumGetIntervalP(DirectFunctionCall3(interval_in, CStringGetDatum("0"), InvalidOid, -1))
@ -42,6 +46,41 @@
#define DEFAULT_RETRY_PERIOD \ #define DEFAULT_RETRY_PERIOD \
DatumGetIntervalP(DirectFunctionCall3(interval_in, CStringGetDatum("5 min"), InvalidOid, -1)) DatumGetIntervalP(DirectFunctionCall3(interval_in, CStringGetDatum("5 min"), InvalidOid, -1))
#define CONFIG_KEY_HYPERTABLE_ID "hypertable_id"
#define CONFIG_KEY_INDEX_NAME "index_name"
#define POLICY_REORDER_PROC_NAME "policy_reorder"
int32
policy_reorder_get_hypertable_id(Jsonb *config)
{
bool found;
int32 hypertable_id = ts_jsonb_get_int32_field(config, CONFIG_KEY_HYPERTABLE_ID, &found);
if (!found)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("could not find hypertable_id in config for job")));
return hypertable_id;
}
char *
policy_reorder_get_index_name(Jsonb *config)
{
char *index_name = NULL;
if (config != NULL)
index_name = ts_jsonb_get_str_field(config, CONFIG_KEY_INDEX_NAME);
if (index_name == NULL)
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("could not find index_name in config for job")));
return index_name;
}
static void static void
check_valid_index(Hypertable *ht, Name index_name) check_valid_index(Hypertable *ht, Name index_name)
{ {
@ -67,16 +106,26 @@ check_valid_index(Hypertable *ht, Name index_name)
} }
Datum Datum
reorder_add_policy(PG_FUNCTION_ARGS) policy_reorder_proc(PG_FUNCTION_ARGS)
{
int32 job_id = PG_GETARG_INT32(0);
Jsonb *config = PG_GETARG_JSONB_P(1);
execute_reorder_policy(job_id, config, reorder_chunk, true);
PG_RETURN_VOID();
}
Datum
policy_reorder_add(PG_FUNCTION_ARGS)
{ {
NameData application_name; NameData application_name;
NameData reorder_name; NameData reorder_name;
NameData proc_name, proc_schema, owner; NameData proc_name, proc_schema, owner;
int32 job_id; int32 job_id;
BgwPolicyReorder *existing;
Dimension *dim; Dimension *dim;
Interval *default_schedule_interval = DEFAULT_SCHEDULE_INTERVAL; Interval schedule_interval = DEFAULT_SCHEDULE_INTERVAL;
Oid ht_oid = PG_GETARG_OID(0); Oid ht_oid = PG_GETARG_OID(0);
Name index_name = PG_GETARG_NAME(1); Name index_name = PG_GETARG_NAME(1);
bool if_not_exists = PG_GETARG_BOOL(2); bool if_not_exists = PG_GETARG_BOOL(2);
@ -84,11 +133,7 @@ reorder_add_policy(PG_FUNCTION_ARGS)
Hypertable *ht = ts_hypertable_get_by_id(hypertable_id); Hypertable *ht = ts_hypertable_get_by_id(hypertable_id);
Oid partitioning_type; Oid partitioning_type;
Oid owner_id; Oid owner_id;
List *jobs;
BgwPolicyReorder policy = { .fd = {
.hypertable_id = hypertable_id,
.hypertable_index_name = *index_name,
} };
owner_id = ts_hypertable_permissions_check(ht_oid, GetUserId()); owner_id = ts_hypertable_permissions_check(ht_oid, GetUserId());
@ -105,11 +150,16 @@ reorder_add_policy(PG_FUNCTION_ARGS)
/* Verify that the hypertable owner can create a background worker */ /* Verify that the hypertable owner can create a background worker */
ts_bgw_job_validate_job_owner(owner_id, JOB_TYPE_REORDER); ts_bgw_job_validate_job_owner(owner_id, JOB_TYPE_REORDER);
/* Make sure that an existing policy doesn't exist on this hypertable */ /* Make sure that an existing reorder policy doesn't exist on this hypertable */
existing = ts_bgw_policy_reorder_find_by_hypertable(ts_hypertable_relid_to_id(ht_oid)); jobs = ts_bgw_job_find_by_proc_and_hypertable_id(POLICY_REORDER_PROC_NAME,
INTERNAL_SCHEMA_NAME,
ht->fd.id);
if (existing != NULL) if (jobs != NIL)
{ {
BgwJob *existing = linitial(jobs);
Assert(list_length(jobs) == 1);
if (!if_not_exists) if (!if_not_exists)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT), (errcode(ERRCODE_DUPLICATE_OBJECT),
@ -118,7 +168,8 @@ reorder_add_policy(PG_FUNCTION_ARGS)
if (!DatumGetBool(DirectFunctionCall2Coll(nameeq, if (!DatumGetBool(DirectFunctionCall2Coll(nameeq,
C_COLLATION_OID, C_COLLATION_OID,
NameGetDatum(&existing->fd.hypertable_index_name), CStringGetDatum(policy_reorder_get_index_name(
existing->fd.config)),
NameGetDatum(index_name)))) NameGetDatum(index_name))))
{ {
elog(WARNING, elog(WARNING,
@ -136,8 +187,8 @@ reorder_add_policy(PG_FUNCTION_ARGS)
/* Next, insert a new job into jobs table */ /* Next, insert a new job into jobs table */
namestrcpy(&application_name, "Reorder Background Job"); namestrcpy(&application_name, "Reorder Background Job");
namestrcpy(&reorder_name, "reorder"); namestrcpy(&reorder_name, "reorder");
namestrcpy(&proc_name, ""); namestrcpy(&proc_name, POLICY_REORDER_PROC_NAME);
namestrcpy(&proc_schema, ""); namestrcpy(&proc_schema, INTERNAL_SCHEMA_NAME);
namestrcpy(&owner, GetUserNameFromId(owner_id, false)); namestrcpy(&owner, GetUserNameFromId(owner_id, false));
/* /*
@ -148,19 +199,23 @@ reorder_add_policy(PG_FUNCTION_ARGS)
partitioning_type = ts_dimension_get_partition_type(dim); partitioning_type = ts_dimension_get_partition_type(dim);
if (dim && IS_TIMESTAMP_TYPE(partitioning_type)) if (dim && IS_TIMESTAMP_TYPE(partitioning_type))
default_schedule_interval = DatumGetIntervalP( {
DirectFunctionCall7(make_interval, schedule_interval.time = dim->fd.interval_length / 2;
Int32GetDatum(0), schedule_interval.day = 0;
Int32GetDatum(0), schedule_interval.month = 0;
Int32GetDatum(0), }
Int32GetDatum(0),
Int32GetDatum(0), JsonbParseState *parseState = NULL;
Int32GetDatum(0),
Float8GetDatum(dim->fd.interval_length / 2000000))); pushJsonbValue(&parseState, WJB_BEGIN_OBJECT, NULL);
ts_jsonb_add_int32(parseState, CONFIG_KEY_HYPERTABLE_ID, ht->fd.id);
ts_jsonb_add_str(parseState, CONFIG_KEY_INDEX_NAME, NameStr(*index_name));
JsonbValue *result = pushJsonbValue(&parseState, WJB_END_OBJECT, NULL);
Jsonb *config = JsonbValueToJsonb(result);
job_id = ts_bgw_job_insert_relation(&application_name, job_id = ts_bgw_job_insert_relation(&application_name,
&reorder_name, &reorder_name,
default_schedule_interval, &schedule_interval,
DEFAULT_MAX_RUNTIME, DEFAULT_MAX_RUNTIME,
DEFAULT_MAX_RETRIES, DEFAULT_MAX_RETRIES,
DEFAULT_RETRY_PERIOD, DEFAULT_RETRY_PERIOD,
@ -169,26 +224,24 @@ reorder_add_policy(PG_FUNCTION_ARGS)
&owner, &owner,
true, true,
hypertable_id, hypertable_id,
NULL); config);
/* Now, insert a new row in the reorder args table */
policy.fd.job_id = job_id;
ts_bgw_policy_reorder_insert(&policy);
PG_RETURN_INT32(job_id); PG_RETURN_INT32(job_id);
} }
Datum Datum
reorder_remove_policy(PG_FUNCTION_ARGS) policy_reorder_remove(PG_FUNCTION_ARGS)
{ {
Oid hypertable_oid = PG_GETARG_OID(0); Oid hypertable_oid = PG_GETARG_OID(0);
bool if_exists = PG_GETARG_BOOL(1); bool if_exists = PG_GETARG_BOOL(1);
/* Remove the job, then remove the policy */ /* Remove the job, then remove the policy */
int ht_id = ts_hypertable_relid_to_id(hypertable_oid); int ht_id = ts_hypertable_relid_to_id(hypertable_oid);
BgwPolicyReorder *policy = ts_bgw_policy_reorder_find_by_hypertable(ht_id);
if (policy == NULL) List *jobs = ts_bgw_job_find_by_proc_and_hypertable_id(POLICY_REORDER_PROC_NAME,
INTERNAL_SCHEMA_NAME,
ht_id);
if (jobs == NIL)
{ {
if (!if_exists) if (!if_exists)
ereport(ERROR, ereport(ERROR,
@ -208,10 +261,12 @@ reorder_remove_policy(PG_FUNCTION_ARGS)
PG_RETURN_NULL(); PG_RETURN_NULL();
} }
} }
Assert(list_length(jobs) == 1);
BgwJob *job = linitial(jobs);
ts_hypertable_permissions_check(hypertable_oid, GetUserId()); ts_hypertable_permissions_check(hypertable_oid, GetUserId());
ts_bgw_job_delete_by_id(policy->fd.job_id); ts_bgw_job_delete_by_id(job->fd.id);
PG_RETURN_NULL(); PG_RETURN_NULL();
} }

View File

@ -10,7 +10,11 @@
#include <postgres.h> #include <postgres.h>
/* User-facing API functions */ /* User-facing API functions */
extern Datum reorder_add_policy(PG_FUNCTION_ARGS); extern Datum policy_reorder_add(PG_FUNCTION_ARGS);
extern Datum reorder_remove_policy(PG_FUNCTION_ARGS); extern Datum policy_reorder_remove(PG_FUNCTION_ARGS);
extern Datum policy_reorder_proc(PG_FUNCTION_ARGS);
extern int32 policy_reorder_get_hypertable_id(Jsonb *config);
extern char *policy_reorder_get_index_name(Jsonb *config);
#endif /* TIMESCALEDB_TSL_BGW_POLICY_REORDER_API_H */ #endif /* TIMESCALEDB_TSL_BGW_POLICY_REORDER_API_H */

View File

@ -92,10 +92,11 @@ CrossModuleFunctions tsl_cm_functions = {
.bgw_policy_job_execute = tsl_bgw_policy_job_execute, .bgw_policy_job_execute = tsl_bgw_policy_job_execute,
.continuous_agg_materialize = continuous_agg_materialize, .continuous_agg_materialize = continuous_agg_materialize,
.add_drop_chunks_policy = drop_chunks_add_policy, .add_drop_chunks_policy = drop_chunks_add_policy,
.add_reorder_policy = reorder_add_policy, .policy_reorder_add = policy_reorder_add,
.policy_reorder_proc = policy_reorder_proc,
.policy_reorder_remove = policy_reorder_remove,
.add_compress_chunks_policy = compress_chunks_add_policy, .add_compress_chunks_policy = compress_chunks_add_policy,
.remove_drop_chunks_policy = drop_chunks_remove_policy, .remove_drop_chunks_policy = drop_chunks_remove_policy,
.remove_reorder_policy = reorder_remove_policy,
.remove_compress_chunks_policy = compress_chunks_remove_policy, .remove_compress_chunks_policy = compress_chunks_remove_policy,
.create_upper_paths_hook = tsl_create_upper_paths_hook, .create_upper_paths_hook = tsl_create_upper_paths_hook,
.set_rel_pathlist_dml = tsl_set_rel_pathlist_dml, .set_rel_pathlist_dml = tsl_set_rel_pathlist_dml,

View File

@ -56,16 +56,10 @@ SELECT COUNT(*) FROM _timescaledb_catalog.chunk as c, _timescaledb_catalog.hyper
-- Make sure reorder correctly selects chunks to reorder -- Make sure reorder correctly selects chunks to reorder
-- by starting with oldest chunks -- by starting with oldest chunks
select add_reorder_policy('test_table', 'test_table_time_idx') as reorder_job_id \gset select add_reorder_policy('test_table', 'test_table_time_idx') as reorder_job_id \gset
select * from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
job_id | hypertable_id | hypertable_index_name
--------+---------------+-----------------------
1000 | 1 | test_table_time_idx
(1 row)
select * from _timescaledb_config.bgw_job where job_type IN ('reorder'); select * from _timescaledb_config.bgw_job where job_type IN ('reorder');
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-----------------------------------------------------------
1000 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 1 | 1000 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 1 | {"index_name": "test_table_time_idx", "hypertable_id": 1}
(1 row) (1 row)
select job_id, chunk_id, num_times_job_run from _timescaledb_internal.bgw_policy_chunk_stats; select job_id, chunk_id, num_times_job_run from _timescaledb_internal.bgw_policy_chunk_stats;

View File

@ -99,18 +99,11 @@ SELECT json_object_field(get_telemetry_report(always_display_report := true)::js
"1" "1"
(1 row) (1 row)
-- policy was created
select * from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
job_id | hypertable_id | hypertable_index_name
--------+---------------+-----------------------------
1000 | 1 | test_reorder_table_time_idx
(1 row)
-- job was created -- job was created
SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id; SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-------------------------------------------------------------------
1000 | Reorder Background Job | reorder | @ 4 days | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 1 | 1000 | Reorder Background Job | reorder | @ 4 days | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 1 | {"index_name": "test_reorder_table_time_idx", "hypertable_id": 1}
(1 row) (1 row)
-- no stats -- no stats
@ -144,9 +137,9 @@ SELECT * FROM sorted_bgw_log;
(2 rows) (2 rows)
SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id; SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-------------------------------------------------------------------
1000 | Reorder Background Job | reorder | @ 4 days | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 1 | 1000 | Reorder Background Job | reorder | @ 4 days | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 1 | {"index_name": "test_reorder_table_time_idx", "hypertable_id": 1}
(1 row) (1 row)
-- job ran once, successfully -- job ran once, successfully
@ -184,9 +177,9 @@ SELECT * FROM sorted_bgw_log;
(4 rows) (4 rows)
SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id; SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-------------------------------------------------------------------
1000 | Reorder Background Job | reorder | @ 4 days | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 1 | 1000 | Reorder Background Job | reorder | @ 4 days | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 1 | {"index_name": "test_reorder_table_time_idx", "hypertable_id": 1}
(1 row) (1 row)
-- two runs -- two runs
@ -228,9 +221,9 @@ SELECT * FROM sorted_bgw_log;
(6 rows) (6 rows)
SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id; SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-------------------------------------------------------------------
1000 | Reorder Background Job | reorder | @ 4 days | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 1 | 1000 | Reorder Background Job | reorder | @ 4 days | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 1 | {"index_name": "test_reorder_table_time_idx", "hypertable_id": 1}
(1 row) (1 row)
SELECT * SELECT *
@ -272,9 +265,9 @@ SELECT * FROM sorted_bgw_log;
(7 rows) (7 rows)
SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id; SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-------------------------------------------------------------------
1000 | Reorder Background Job | reorder | @ 4 days | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 1 | 1000 | Reorder Background Job | reorder | @ 4 days | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 1 | {"index_name": "test_reorder_table_time_idx", "hypertable_id": 1}
(1 row) (1 row)
SELECT * SELECT *
@ -316,11 +309,6 @@ SELECT remove_reorder_policy('test_reorder_table');
(1 row) (1 row)
select * from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
job_id | hypertable_id | hypertable_index_name
--------+---------------+-----------------------
(0 rows)
SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id; SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
----+------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------+-----------+---------------+-------- ----+------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------+-----------+---------------+--------

View File

@ -15,9 +15,9 @@ select * from _timescaledb_config.bgw_policy_drop_chunks;
--------+---------------+------------+----------------------------- --------+---------------+------------+-----------------------------
(0 rows) (0 rows)
select * from _timescaledb_config.bgw_policy_reorder; select * from _timescaledb_config.bgw_job WHERE job_type IN ('reorder');
job_id | hypertable_id | hypertable_index_name id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
--------+---------------+----------------------- ----+------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------+-----------+---------------+--------
(0 rows) (0 rows)
CREATE TABLE test_table(time timestamptz, junk int); CREATE TABLE test_table(time timestamptz, junk int);
@ -74,10 +74,10 @@ ERROR: reorder policy already exists for hypertable "test_table"
select add_reorder_policy('test_table', 'third_index'); select add_reorder_policy('test_table', 'third_index');
ERROR: reorder policy already exists for hypertable "test_table" ERROR: reorder policy already exists for hypertable "test_table"
\set ON_ERROR_STOP 1 \set ON_ERROR_STOP 1
select * from _timescaledb_config.bgw_policy_reorder where job_id=:job_id; select * from _timescaledb_config.bgw_job where id=:job_id;
job_id | hypertable_id | hypertable_index_name id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
--------+---------------+----------------------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-----------------------------------------------------------
1000 | 1 | test_table_time_idx 1000 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 1 | {"index_name": "test_table_time_idx", "hypertable_id": 1}
(1 row) (1 row)
-- Now check that default scheduling interval for reorder policy is calculated correctly -- Now check that default scheduling interval for reorder policy is calculated correctly
@ -96,19 +96,19 @@ select add_reorder_policy('test_table2', 'test_table2_time_idx');
1001 1001
(1 row) (1 row)
select * from _timescaledb_config.bgw_job where job_type IN ('drop_chunks', 'reorder'); SELECT * FROM _timescaledb_config.bgw_job WHERE job_type IN ('drop_chunks', 'reorder') ORDER BY id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+------------------------------------------------------------
1000 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 1 | 1000 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 1 | {"index_name": "test_table_time_idx", "hypertable_id": 1}
1001 | Reorder Background Job | reorder | @ 12 hours | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 3 | 1001 | Reorder Background Job | reorder | @ 12 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 3 | {"index_name": "test_table2_time_idx", "hypertable_id": 3}
(2 rows) (2 rows)
DROP TABLE test_table2; DROP TABLE test_table2;
-- Make sure that test_table2 reorder policy gets dropped -- Make sure that test_table2 reorder policy gets dropped
select * from _timescaledb_config.bgw_job where job_type IN ('drop_chunks', 'reorder'); SELECT * FROM _timescaledb_config.bgw_job WHERE job_type IN ('drop_chunks', 'reorder') ORDER BY id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-----------------------------------------------------------
1000 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 1 | 1000 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 1 | {"index_name": "test_table_time_idx", "hypertable_id": 1}
(1 row) (1 row)
-- Error whenever incorrect arguments are applied (must have table and interval) -- Error whenever incorrect arguments are applied (must have table and interval)
@ -245,14 +245,9 @@ select remove_reorder_policy('test_table');
(1 row) (1 row)
select * from _timescaledb_config.bgw_policy_reorder; select * from _timescaledb_config.bgw_job WHERE job_type IN ('reorder');
job_id | hypertable_id | hypertable_index_name id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
--------+---------------+----------------------- ----+------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------+-----------+---------------+--------
(0 rows)
select r.job_id,r.hypertable_id,r.hypertable_index_name from _timescaledb_config.bgw_policy_reorder as r, _timescaledb_catalog.hypertable as h where r.hypertable_id=h.id and h.table_name='test_table';
job_id | hypertable_id | hypertable_index_name
--------+---------------+-----------------------
(0 rows) (0 rows)
select add_drop_chunks_policy('test_table', INTERVAL '3 month'); select add_drop_chunks_policy('test_table', INTERVAL '3 month');
@ -380,12 +375,6 @@ select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:jo
1 1
(1 row) (1 row)
select count(*) from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
count
-------
1
(1 row)
select delete_job(:job_id); select delete_job(:job_id);
delete_job delete_job
------------ ------------
@ -406,7 +395,7 @@ select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:jo
(1 row) (1 row)
-- Job args should still be there -- Job args should still be there
select count(*) from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id; select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id;
count count
------- -------
1 1
@ -424,13 +413,6 @@ select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id;
0 0
(1 row) (1 row)
-- Job args should be gone
select count(*) from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
count
-------
0
(1 row)
-- Now make sure policy args have correct job deletion dependency -- Now make sure policy args have correct job deletion dependency
select add_drop_chunks_policy('test_table', INTERVAL '2 month') as job_id \gset select add_drop_chunks_policy('test_table', INTERVAL '2 month') as job_id \gset
select add_reorder_policy('test_table', 'third_index') as reorder_job_id \gset select add_reorder_policy('test_table', 'third_index') as reorder_job_id \gset
@ -452,18 +434,12 @@ select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:jo
1 1
(1 row) (1 row)
select count(*) from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id; SELECT * FROM _timescaledb_config.bgw_job ORDER BY id;
count id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------- ------+----------------------------+----------------------------------------+-------------------+-----------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+---------------------------------------------------
1 1 | Telemetry Reporter | telemetry_and_version_check_if_enabled | @ 24 hours | @ 1 min 40 secs | -1 | @ 1 hour | | | super_user | t | |
(1 row) 1007 | Drop Chunks Background Job | drop_chunks | @ 1 day | @ 5 mins | -1 | @ 5 mins | | | default_perm_user | t | 1 |
1008 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 1 | {"index_name": "third_index", "hypertable_id": 1}
select * from _timescaledb_config.bgw_job;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+----------------------------+----------------------------------------+-------------------+-----------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+--------
1 | Telemetry Reporter | telemetry_and_version_check_if_enabled | @ 24 hours | @ 1 min 40 secs | -1 | @ 1 hour | | | super_user | t | |
1007 | Drop Chunks Background Job | drop_chunks | @ 1 day | @ 5 mins | -1 | @ 5 mins | | | default_perm_user | t | 1 |
1008 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 1 |
(3 rows) (3 rows)
DROP TABLE test_table; DROP TABLE test_table;
@ -485,12 +461,6 @@ select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:jo
0 0
(1 row) (1 row)
select count(*) from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
count
-------
0
(1 row)
-- Check that we can't add policies on non-hypertables -- Check that we can't add policies on non-hypertables
CREATE TABLE non_hypertable(junk int, more_junk int); CREATE TABLE non_hypertable(junk int, more_junk int);
CREATE INDEX non_ht_index on non_hypertable(junk); CREATE INDEX non_ht_index on non_hypertable(junk);
@ -626,9 +596,9 @@ select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_c
(1 row) (1 row)
select * from _timescaledb_config.bgw_job where job_type='reorder'; select * from _timescaledb_config.bgw_job where job_type='reorder';
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+----------------------------------------------------
1011 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 6 | 1011 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 6 | {"index_name": "second_index", "hypertable_id": 6}
(1 row) (1 row)
-- Deleting a chunk that has nothing to do with the job should do nothing -- Deleting a chunk that has nothing to do with the job should do nothing
@ -642,9 +612,9 @@ select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_c
(1 row) (1 row)
select * from _timescaledb_config.bgw_job where job_type='reorder'; select * from _timescaledb_config.bgw_job where job_type='reorder';
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+----------------------------------------------------
1011 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 6 | 1011 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 6 | {"index_name": "second_index", "hypertable_id": 6}
(1 row) (1 row)
-- Dropping the hypertable should drop the chunk, which should drop the reorder policy -- Dropping the hypertable should drop the chunk, which should drop the reorder policy
@ -687,11 +657,11 @@ select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_c
1012 | 123 | 1 1012 | 123 | 1
(1 row) (1 row)
select * from _timescaledb_config.bgw_job where job_type in ('drop_chunks', 'reorder'); SELECT * FROM _timescaledb_config.bgw_job WHERE job_type IN ('drop_chunks', 'reorder') ORDER BY id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+----------------------------+-------------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+----------------------------+-------------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-----------------------------------------------------------
1012 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 7 | 1012 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 7 | {"index_name": "test_table_time_idx", "hypertable_id": 7}
1013 | Drop Chunks Background Job | drop_chunks | @ 1 day | @ 5 mins | -1 | @ 5 mins | | | default_perm_user | t | 7 | 1013 | Drop Chunks Background Job | drop_chunks | @ 1 day | @ 5 mins | -1 | @ 5 mins | | | default_perm_user | t | 7 |
(2 rows) (2 rows)
-- Dropping the drop_chunks job should not affect the chunk_stats row -- Dropping the drop_chunks job should not affect the chunk_stats row
@ -707,10 +677,10 @@ select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_c
1012 | 123 | 1 1012 | 123 | 1
(1 row) (1 row)
select * from _timescaledb_config.bgw_job where job_type in ('drop_chunks', 'reorder'); SELECT * FROM _timescaledb_config.bgw_job WHERE job_type IN ('drop_chunks', 'reorder') ORDER BY id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-----------------------------------------------------------
1012 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 7 | 1012 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 7 | {"index_name": "test_table_time_idx", "hypertable_id": 7}
(1 row) (1 row)
select remove_reorder_policy('test_table'); select remove_reorder_policy('test_table');
@ -725,7 +695,7 @@ select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_c
--------+----------+------------------- --------+----------+-------------------
(0 rows) (0 rows)
select * from _timescaledb_config.bgw_job where job_type in ('drop_chunks', 'reorder'); SELECT * FROM _timescaledb_config.bgw_job WHERE job_type IN ('drop_chunks', 'reorder') ORDER BY id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
----+------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------+-----------+---------------+-------- ----+------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------+-----------+---------------+--------
(0 rows) (0 rows)
@ -733,9 +703,9 @@ select * from _timescaledb_config.bgw_job where job_type in ('drop_chunks', 'reo
-- Now test if alter_job_schedule works -- Now test if alter_job_schedule works
select add_reorder_policy('test_table', 'test_table_time_idx') as job_id \gset select add_reorder_policy('test_table', 'test_table_time_idx') as job_id \gset
select * from _timescaledb_config.bgw_job where id=:job_id; select * from _timescaledb_config.bgw_job where id=:job_id;
id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config id | application_name | job_type | schedule_interval | max_runtime | max_retries | retry_period | proc_name | proc_schema | owner | scheduled | hypertable_id | config
------+------------------------+----------+-------------------+-------------+-------------+--------------+-----------+-------------+-------------------+-----------+---------------+-------- ------+------------------------+----------+-------------------+-------------+-------------+--------------+----------------+-----------------------+-------------------+-----------+---------------+-----------------------------------------------------------
1014 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | | | default_perm_user | t | 7 | 1014 | Reorder Background Job | reorder | @ 84 hours | @ 0 | -1 | @ 5 mins | policy_reorder | _timescaledb_internal | default_perm_user | t | 7 | {"index_name": "test_table_time_idx", "hypertable_id": 7}
(1 row) (1 row)
-- No change -- No change

View File

@ -55,7 +55,6 @@ SELECT COUNT(*) FROM _timescaledb_catalog.chunk as c, _timescaledb_catalog.hyper
-- Make sure reorder correctly selects chunks to reorder -- Make sure reorder correctly selects chunks to reorder
-- by starting with oldest chunks -- by starting with oldest chunks
select add_reorder_policy('test_table', 'test_table_time_idx') as reorder_job_id \gset select add_reorder_policy('test_table', 'test_table_time_idx') as reorder_job_id \gset
select * from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
select * from _timescaledb_config.bgw_job where job_type IN ('reorder'); select * from _timescaledb_config.bgw_job where job_type IN ('reorder');
select job_id, chunk_id, num_times_job_run from _timescaledb_internal.bgw_policy_chunk_stats; select job_id, chunk_id, num_times_job_run from _timescaledb_internal.bgw_policy_chunk_stats;

View File

@ -78,9 +78,6 @@ SELECT json_object_field(get_telemetry_report(always_display_report := true)::js
select add_reorder_policy('test_reorder_table', 'test_reorder_table_time_idx') as reorder_job_id \gset select add_reorder_policy('test_reorder_table', 'test_reorder_table_time_idx') as reorder_job_id \gset
SELECT json_object_field(get_telemetry_report(always_display_report := true)::json,'num_reorder_policies'); SELECT json_object_field(get_telemetry_report(always_display_report := true)::json,'num_reorder_policies');
-- policy was created
select * from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
-- job was created -- job was created
SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id; SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id;
@ -170,8 +167,6 @@ SELECT * FROM timescaledb_information.policy_stats;
-- test deleting the policy -- test deleting the policy
SELECT remove_reorder_policy('test_reorder_table'); SELECT remove_reorder_policy('test_reorder_table');
select * from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id; SELECT * FROM _timescaledb_config.bgw_job where id=:reorder_job_id;
SELECT job_id, next_start, last_finish as until_next, last_run_success, total_runs, total_successes, total_failures, total_crashes SELECT job_id, next_start, last_finish as until_next, last_run_success, total_runs, total_successes, total_failures, total_crashes

View File

@ -16,7 +16,7 @@ LANGUAGE C VOLATILE STRICT;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER \c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
select * from _timescaledb_config.bgw_policy_drop_chunks; select * from _timescaledb_config.bgw_policy_drop_chunks;
select * from _timescaledb_config.bgw_policy_reorder; select * from _timescaledb_config.bgw_job WHERE job_type IN ('reorder');
CREATE TABLE test_table(time timestamptz, junk int); CREATE TABLE test_table(time timestamptz, junk int);
CREATE TABLE test_table_int(time bigint, junk int); CREATE TABLE test_table_int(time bigint, junk int);
@ -43,7 +43,7 @@ select add_reorder_policy('test_table', 'second_index');
select add_reorder_policy('test_table', 'third_index'); select add_reorder_policy('test_table', 'third_index');
\set ON_ERROR_STOP 1 \set ON_ERROR_STOP 1
select * from _timescaledb_config.bgw_policy_reorder where job_id=:job_id; select * from _timescaledb_config.bgw_job where id=:job_id;
-- Now check that default scheduling interval for reorder policy is calculated correctly -- Now check that default scheduling interval for reorder policy is calculated correctly
-- Should be 1/2 default chunk interval length -- Should be 1/2 default chunk interval length
@ -51,11 +51,11 @@ CREATE TABLE test_table2(time timestamptz, junk int);
SELECT create_hypertable('test_table2', 'time', chunk_time_interval=>INTERVAL '1 day'); SELECT create_hypertable('test_table2', 'time', chunk_time_interval=>INTERVAL '1 day');
select add_reorder_policy('test_table2', 'test_table2_time_idx'); select add_reorder_policy('test_table2', 'test_table2_time_idx');
select * from _timescaledb_config.bgw_job where job_type IN ('drop_chunks', 'reorder'); SELECT * FROM _timescaledb_config.bgw_job WHERE job_type IN ('drop_chunks', 'reorder') ORDER BY id;
DROP TABLE test_table2; DROP TABLE test_table2;
-- Make sure that test_table2 reorder policy gets dropped -- Make sure that test_table2 reorder policy gets dropped
select * from _timescaledb_config.bgw_job where job_type IN ('drop_chunks', 'reorder'); SELECT * FROM _timescaledb_config.bgw_job WHERE job_type IN ('drop_chunks', 'reorder') ORDER BY id;
-- Error whenever incorrect arguments are applied (must have table and interval) -- Error whenever incorrect arguments are applied (must have table and interval)
\set ON_ERROR_STOP 0 \set ON_ERROR_STOP 0
@ -108,8 +108,7 @@ select * from _timescaledb_config.bgw_policy_drop_chunks;
select r.job_id,r.hypertable_id,r.older_than from _timescaledb_config.bgw_policy_drop_chunks as r, _timescaledb_catalog.hypertable as h where r.hypertable_id=h.id and h.table_name='test_table'; select r.job_id,r.hypertable_id,r.older_than from _timescaledb_config.bgw_policy_drop_chunks as r, _timescaledb_catalog.hypertable as h where r.hypertable_id=h.id and h.table_name='test_table';
select remove_reorder_policy('test_table'); select remove_reorder_policy('test_table');
select * from _timescaledb_config.bgw_policy_reorder; select * from _timescaledb_config.bgw_job WHERE job_type IN ('reorder');
select r.job_id,r.hypertable_id,r.hypertable_index_name from _timescaledb_config.bgw_policy_reorder as r, _timescaledb_catalog.hypertable as h where r.hypertable_id=h.id and h.table_name='test_table';
select add_drop_chunks_policy('test_table', INTERVAL '3 month'); select add_drop_chunks_policy('test_table', INTERVAL '3 month');
select * from _timescaledb_config.bgw_policy_drop_chunks; select * from _timescaledb_config.bgw_policy_drop_chunks;
@ -152,7 +151,6 @@ select add_reorder_policy('test_table', 'third_index') as reorder_job_id \gset
select count(*) from _timescaledb_config.bgw_job where id=:job_id; select count(*) from _timescaledb_config.bgw_job where id=:job_id;
select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id; select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id;
select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:job_id; select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:job_id;
select count(*) from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
select delete_job(:job_id); select delete_job(:job_id);
@ -160,12 +158,10 @@ select count(*) from _timescaledb_config.bgw_job where id=:job_id;
-- Job args should be gone -- Job args should be gone
select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:job_id; select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:job_id;
-- Job args should still be there -- Job args should still be there
select count(*) from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id; select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id;
select delete_job(:reorder_job_id); select delete_job(:reorder_job_id);
select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id; select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id;
-- Job args should be gone
select count(*) from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
-- Now make sure policy args have correct job deletion dependency -- Now make sure policy args have correct job deletion dependency
select add_drop_chunks_policy('test_table', INTERVAL '2 month') as job_id \gset select add_drop_chunks_policy('test_table', INTERVAL '2 month') as job_id \gset
@ -174,15 +170,13 @@ select add_reorder_policy('test_table', 'third_index') as reorder_job_id \gset
select count(*) from _timescaledb_config.bgw_job where id=:job_id; select count(*) from _timescaledb_config.bgw_job where id=:job_id;
select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id; select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id;
select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:job_id; select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:job_id;
select count(*) from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id; SELECT * FROM _timescaledb_config.bgw_job ORDER BY id;
select * from _timescaledb_config.bgw_job;
DROP TABLE test_table; DROP TABLE test_table;
select count(*) from _timescaledb_config.bgw_job where id=:job_id; select count(*) from _timescaledb_config.bgw_job where id=:job_id;
select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id; select count(*) from _timescaledb_config.bgw_job where id=:reorder_job_id;
select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:job_id; select count(*) from _timescaledb_config.bgw_policy_drop_chunks where job_id=:job_id;
select count(*) from _timescaledb_config.bgw_policy_reorder where job_id=:reorder_job_id;
-- Check that we can't add policies on non-hypertables -- Check that we can't add policies on non-hypertables
CREATE TABLE non_hypertable(junk int, more_junk int); CREATE TABLE non_hypertable(junk int, more_junk int);
@ -270,17 +264,17 @@ select add_drop_chunks_policy('test_table', INTERVAL '2 days', true);
select ts_test_chunk_stats_insert(:job_id, 123, 1); select ts_test_chunk_stats_insert(:job_id, 123, 1);
select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_chunk_stats; select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_chunk_stats;
select * from _timescaledb_config.bgw_job where job_type in ('drop_chunks', 'reorder'); SELECT * FROM _timescaledb_config.bgw_job WHERE job_type IN ('drop_chunks', 'reorder') ORDER BY id;
-- Dropping the drop_chunks job should not affect the chunk_stats row -- Dropping the drop_chunks job should not affect the chunk_stats row
select remove_drop_chunks_policy('test_table'); select remove_drop_chunks_policy('test_table');
select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_chunk_stats; select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_chunk_stats;
select * from _timescaledb_config.bgw_job where job_type in ('drop_chunks', 'reorder'); SELECT * FROM _timescaledb_config.bgw_job WHERE job_type IN ('drop_chunks', 'reorder') ORDER BY id;
select remove_reorder_policy('test_table'); select remove_reorder_policy('test_table');
-- Row should be gone -- Row should be gone
select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_chunk_stats; select job_id,chunk_id,num_times_job_run from _timescaledb_internal.bgw_policy_chunk_stats;
select * from _timescaledb_config.bgw_job where job_type in ('drop_chunks', 'reorder'); SELECT * FROM _timescaledb_config.bgw_job WHERE job_type IN ('drop_chunks', 'reorder') ORDER BY id;
-- Now test if alter_job_schedule works -- Now test if alter_job_schedule works
select add_reorder_policy('test_table', 'test_table_time_idx') as job_id \gset select add_reorder_policy('test_table', 'test_table_time_idx') as job_id \gset

View File

@ -42,7 +42,7 @@ ts_test_auto_reorder(PG_FUNCTION_ARGS)
int32 job_id = PG_GETARG_INT32(0); int32 job_id = PG_GETARG_INT32(0);
Datum values[NUM_REORDER_RET_VALS]; Datum values[NUM_REORDER_RET_VALS];
bool nulls[NUM_REORDER_RET_VALS] = { false }; bool nulls[NUM_REORDER_RET_VALS] = { false };
BgwJob job = { .fd = { .id = job_id } }; BgwJob *job = ts_bgw_job_find(job_id, CurrentMemoryContext, true);
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
{ {
@ -52,7 +52,7 @@ ts_test_auto_reorder(PG_FUNCTION_ARGS)
"that cannot accept type record"))); "that cannot accept type record")));
} }
execute_reorder_policy(&job, dummy_reorder_func, false); execute_reorder_policy(job->fd.id, job->fd.config, dummy_reorder_func, false);
values[0] = ObjectIdGetDatum(chunk_oid); values[0] = ObjectIdGetDatum(chunk_oid);
values[1] = ObjectIdGetDatum(index_oid); values[1] = ObjectIdGetDatum(index_oid);