-- Custom migration (ELEVA-78): set TOAST compression to lz4 on the heavy jsonb
-- columns of `keywords`. lz4 decompresses near-free vs the pglz default and on
-- real jsonb often stores smaller too. This is a metadata-only DDL: it does NOT
-- recompress existing rows (per the ALTER TABLE docs) — only NEW writes take
-- lz4. The hot columns (serp/metrics refreshed on each import) migrate naturally.
--
-- lz4 requires Postgres compiled `--with-lz4` (PG >= 14). The official Docker
-- images (prod) are built with it. Some builds are not, in which case the ALTER
-- raises `feature_not_supported`. We guard each column so a missing-lz4 build
-- degrades to a no-op NOTICE instead of hard-failing the whole migration chain.
DO $$
BEGIN
  ALTER TABLE keywords
    ALTER COLUMN serp SET COMPRESSION lz4,
    ALTER COLUMN briefs SET COMPRESSION lz4,
    ALTER COLUMN metrics SET COMPRESSION lz4,
    ALTER COLUMN gsc_by_country SET COMPRESSION lz4,
    ALTER COLUMN trend SET COMPRESSION lz4;
EXCEPTION
  WHEN feature_not_supported THEN
    RAISE NOTICE 'lz4 TOAST compression not supported by this Postgres build; keeping default (pglz). Rebuild --with-lz4 (or use the official Docker image) to enable.';
END $$;
