···11+-- Adjust compression policy to run more frequently with shorter compress_after
22+--
33+-- CHANGE: Update existing compression policy (job 1000) to:
44+-- - Run every 10 minutes (instead of daily)
55+-- - Compress chunks older than 4 hours (instead of 2 days)
66+--
77+-- RATIONALE: With larger chunk sizes, we want to compress
88+-- chunks that are no longer receiving active writes. Since chunks span multiple
99+-- days, setting compress_after to 4 hours ensures we compress all but the latest
1010+-- active chunk, while running every 10 minutes ensures timely compression.
1111+1212+-- Change schedule to run every 10 minutes
1313+SELECT alter_job(1000, schedule_interval => interval '10 minutes');
1414+1515+-- Change compress_after to 4 hours
1616+-- 4 hours = 4 * 3600 seconds = 14,400 seconds
1717+-- In TID format: 14,400 * 1,000,000 microseconds << 10 = 14,745,600,000,000 ticks
1818+SELECT alter_job(1000,
1919+ config => jsonb_set(
2020+ (SELECT config FROM timescaledb_information.jobs WHERE job_id = 1000),
2121+ '{compress_after}',
2222+ to_jsonb((4::bigint * 3600 * 1000000::bigint << 10)::text)
2323+ )
2424+);
···11+-- Reverse the integer_now function setup
22+-- This will cause compression policy to fail again, but allows rollback if needed
33+44+-- Remove the integer_now function from the posts hypertable
55+SELECT set_integer_now_func('posts', NULL);
66+77+-- Drop the tid_now function
88+DROP FUNCTION IF EXISTS public.tid_now();
···11+-- Fix TimescaleDB compression policy by setting integer_now function
22+--
33+-- PROBLEM: The compression policy fails with "integer_now function not set"
44+-- because the posts table uses integer-based time (TID/rkey), and TimescaleDB needs
55+-- to know how to calculate "now" in TID units to determine which chunks to compress.
66+--
77+-- SOLUTION: Create and register a function that converts current time to TID format.
88+99+-- Create function that returns current timestamp as TID
1010+-- TID format: (microseconds_since_epoch << 10)
1111+-- This matches the tid_now() function already used elsewhere in the codebase
1212+CREATE OR REPLACE FUNCTION public.tid_now() RETURNS BIGINT AS $$
1313+ SELECT (EXTRACT(EPOCH FROM NOW()) * 1000000)::BIGINT << 10;
1414+$$ LANGUAGE SQL STABLE;
1515+1616+-- Register the function with the posts hypertable
1717+-- This allows TimescaleDB compression policies to work with integer-based time
1818+SELECT set_integer_now_func('posts', 'tid_now');
1919+2020+-- Note: The existing compression policy (job_id 1000) will now start working
2121+-- It compresses chunks based on the compress_after configuration
2222+-- The policy runs on a scheduled interval and converts old chunks from rowstore to columnstore format