This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# TimescaleDB Complete Setup Guide for AI Coding Assistants | |
You are tasked with setting up a complete TimescaleDB time-series database solution. This guide provides step-by-step instructions for creating hypertables, configuring compression, setting up retention policies, and implementing continuous aggregates with their associated policies. Adapt the schema and configurations to your specific use case. | |
## Step 1: Create Base Table and Hypertable | |
Create a table schema appropriate for your time-series data, then convert it to a hypertable: | |
```sql | |
-- Create hypertable with compression settings directly using WITH clause |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | |
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE; | |
CREATE OR REPLACE FUNCTION uuid_timestamp(uuid UUID) RETURNS TIMESTAMPTZ AS $$ | |
DECLARE | |
bytes bytea; | |
BEGIN | |
bytes := uuid_send(uuid); | |
if (get_byte(bytes, 6) >> 4)::int2 != 1 then | |
RAISE EXCEPTION 'UUID version is not 1'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\pset pager off | |
\set ECHO all | |
\set ON_ERROR_STOP 0 | |
select version(); | |
\dx | |
select now(); | |
--sanity_check: avg_not_yet_compressed_chunks is < 2 | |
\x on |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\timing on | |
with info as ( | |
SELECT | |
stat.relid as relid, | |
stat.relid::regclass as regclass, | |
age(c.relfrozenxid) > current_setting('vacuum_freeze_min_age')::int | |
and age(c.relfrozenxid) > current_setting('vacuum_freeze_table_age')::int autovacuum_can_freeze, | |
ROUND(100*(age(c.relfrozenxid)/current_setting('vacuum_freeze_table_age')::float)) as perc_to_frozen, | |
ROUND(100*(age(c.relfrozenxid)/ current_setting('autovacuum_freeze_max_age')::float)) perc_to_wrap, | |
CASE WHEN c.reltuples > 0 THEN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\pset pager off | |
\set ECHO all | |
\set ON_ERROR_STOP 0 | |
select version(); | |
\dx | |
select now() | |
\x off | |
select * from _prom_catalog.ha_leases |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\pset pager off | |
\set ECHO all | |
\set ON_ERROR_STOP 0 | |
select version(); | |
\dx | |
select now(); | |
\x off | |
SELECT datname, |