Files
trade/data/db/init-db/01-timescaledb.sql
T
Rekey 9351dec226 refactor: migrate API keys to config, extend Kline intervals, add DB extensions
Security:
- Move hardcoded Binance API key/secret from rest.ts to env.yaml (exchange config segment)
- Add ExchangeConfig validation in config/validators.ts
- Export typed exchange config from config/index.ts
- Update AGENTS/07-caveats.md to reflect the new policy

Kline intervals (add 3m / 2h / 6h / 8h / 1mon):
- TypeScript: update KlineInterval type, KLINE_INTERVAL_MS mapping, build_aggregates_sql refresh chain
- Python: sync KlineInterval Literal type, INTERVAL_TO_TABLE and INTERVAL_MS mappings, db_test table list
- SQL: add 5 continuous aggregate materialized views (klines_3m/2h/6h/8h/1mon) with indexes
- SQL: extend default kline_intervals in trading_pairs table
- SQL: add cross-sectional query indexes for klines_1d and klines_1w

DB:
- Enable pg_prewarm extension (backtest warmup)
- Enable pg_stat_statements extension (slow query monitoring)

Other:
- data/run/exchange.ts: graceful pgsql shutdown after backfill completes
- Config path: load from data/env.yaml (symlink) instead of project root
2026-06-14 18:45:01 +08:00

37 lines
1.6 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- ============================================================
-- 01-timescaledb.sql — TimescaleDB 扩展初始化
-- ============================================================
-- Docker 容器首次启动时自动执行(/docker-entrypoint-initdb.d/
-- 确保 TimescaleDB 扩展在数据库级别启用。
--
-- init-db 完整执行链(按字母序自动执行):
-- 01-timescaledb.sql — 本文件:启用 TimescaleDB 扩展
-- 02-init-tables.sql — 核心业务表(exchanges / trading_pairs / klines
-- 03-continuous-aggregates.sql — K 线分层连续聚合视图(5m → 1w)
--
-- 注意:
-- - klines 基表由 02-init-tables.sql 创建为 TimescaleDB hypertable
-- - 连续聚合视图由 03-continuous-aggregates.sql 创建
-- - TypeORM 的 synchronize:true 与 SQL 脚本互为 fallback(开发/生产双路径)
-- - 本脚本为 init-db 链的第一环,负责扩展启用(TimescaleDB / pg_prewarm / pg_stat_statements
-- ============================================================
-- 启用 TimescaleDB 扩展(必须最先执行)
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
-- 启用 pg_prewarm 扩展(回测预热,减少首轮查询延迟)
CREATE EXTENSION IF NOT EXISTS pg_prewarm;
-- 启用 pg_stat_statements 扩展(慢查询监控,零成本)
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
-- 验证扩展已启用
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_extension WHERE extname = 'timescaledb'
) THEN
RAISE EXCEPTION 'TimescaleDB extension is not installed';
END IF;
END $$;