Skip to content

Commit

Permalink
New feature tier policy
Browse files Browse the repository at this point in the history
  • Loading branch information
shhnwz committed Jun 14, 2024
1 parent b8a3119 commit 5434dae
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
57 changes: 56 additions & 1 deletion sql/timeseries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ CREATE TABLE @extschema@.ts_config(
partition_duration interval NOT NULL,
partition_lead_time interval NOT NULL,
retention_duration interval,
compression_duration interval);
tier_duration interval,
compression_duration interval,
CONSTRAINT policy_duration_order_check
CHECK(compression_duration < tier_duration
AND tier_duration < retention_duration));

-- Enhances an existing table with our time-series best practices. Basically
-- the entry point to this extension. Minimally, a user must create a table
Expand Down Expand Up @@ -613,3 +617,54 @@ CREATE AGGREGATE locf(anyelement) (
SFUNC = locf_agg,
STYPE = anyelement
);

-- This function sets a tier to object store policy on an existing time-series table, which
-- ensures that all partitions older than a particular offset (from present) are
-- automatically moved to a S3 storage.
--
-- Returns the previous tier duration, or NULL if none was set.
CREATE OR REPLACE FUNCTION @extschema@.set_ts_tier_policy(target_table_id regclass, new_tier interval)
RETURNS interval
LANGUAGE plpgsql
AS $function$
DECLARE
table_name text;
prev_tier interval;
BEGIN
SELECT tier_duration
INTO prev_tier
FROM @extschema@.ts_config
WHERE "table_id"=target_table_id
FOR UPDATE;
IF NOT FOUND THEN
RAISE object_not_in_prerequisite_state USING
MESSAGE = 'could not fetch tier policy',
DETAIL = 'Target table was not time-series enhanced',
HINT = format('Call %L to enable time-series enhancements', 'enable_ts_table');
END IF;

UPDATE @extschema@.ts_config
SET "tier_duration"=new_tier
WHERE "table_id"=target_table_id;

RETURN prev_tier;
END;
$function$;


-- Unsets any tier policy on the specified table. Returns the old policy,
-- if one was set.
CREATE OR REPLACE
FUNCTION @extschema@.clear_ts_tier_policy(target_table_id regclass)
RETURNS interval
LANGUAGE plpgsql
AS $function$
DECLARE
prev_tier interval;
BEGIN
SELECT set_ts_tier_policy(target_table_id, NULL) INTO prev_tier;

RETURN prev_tier;
END;
$function$;

2 changes: 1 addition & 1 deletion timeseries.control
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
comment = 'Convenience API for Tembo time series stack'
default_version = '0.1.5'
module_pathname = '$libdir/timeseries'
requires = 'columnar, pg_cron, pg_partman'
requires = 'columnar, pg_cron, pg_partman, pg_tier'

0 comments on commit 5434dae

Please sign in to comment.