Skip to content

fix(migration): use PERFORM for add_compression_policy inside DO block#182

Merged
TerrifiedBug merged 1 commit intomainfrom
fix/migration-perform-compression-policy
Apr 26, 2026
Merged

fix(migration): use PERFORM for add_compression_policy inside DO block#182
TerrifiedBug merged 1 commit intomainfrom
fix/migration-perform-compression-policy

Conversation

@TerrifiedBug
Copy link
Copy Markdown
Owner

Summary

Fourth in the TimescaleDB-demo bring-up bug series (after #179, #180, #181). Once #181 fixed the camelCase identifier quoting, the migration hit yet another error on a fresh TimescaleDB volume:

ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
WHERE: PL/pgSQL function inline_code_block line 12 at SQL statement

Inside a PL/pgSQL DO block, a bare SELECT is illegal unless its result is captured (SELECT ... INTO). To discard the result, you must use PERFORM. The four add_compression_policy(...) calls in this migration use SELECT, so the very first one aborts the whole block.

The four ALTER TABLE ... SET (timescaledb.compress, ...) statements in the same block are fine — only the add_compression_policy calls need fixing.

Fix

Four-line change. Each SELECT add_compression_policy( becomes PERFORM add_compression_policy(. Table names, options, indentation untouched.

Why this didn't surface earlier

Same as #179 / #180 / #181: every prior deployment ran on plain Postgres, where the IF EXISTS (... timescaledb) guard makes this whole migration a no-op. Now that the prior three PRs let the body actually execute under TimescaleDB, this latent PL/pgSQL syntax bug is the next one in line.

Recovery instructions

For anyone whose DB is currently in the failed state on this migration:

Option A — wipe and reapply (cleanest if no real data, e.g. demo):

docker compose down -v
docker volume ls | grep vf-demo-pg   # verify gone, manual rm if needed
docker compose up

Option B — preserve data:

npx prisma migrate resolve --rolled-back 20260329000001_timescaledb_compression
npx prisma migrate deploy

Drift warning for plain-Postgres users

Same caveat as #179 / #180 / #181: the migration's checksum changes. On next prisma migrate deploy you may see a "migration was modified after applied" warning. Resolve with:

npx prisma migrate resolve --applied 20260329000001_timescaledb_compression

One-time fix; the no-op behaviour on plain Postgres is unchanged.

Test plan

  • Visual inspection — exactly 4 lines changed, all SELECT add_compression_policy(PERFORM add_compression_policy(
  • Drop the demo postgres volume
  • docker compose up against timescale/timescaledb:2.16.0-pg16 — both 20260329000000 and 20260329000001 apply cleanly through to completion
  • Confirm the four compression policies exist: SELECT hypertable_name, proc_name FROM timescaledb_information.jobs WHERE proc_name = 'policy_compression'; — should return rows for PipelineMetric, NodeMetric, PipelineLog, NodeStatusEvent

Prior PRs in this series

Bare SELECT inside a PL/pgSQL DO block is invalid when the result is
discarded — Postgres raises 42601 "query has no destination for result
data". Switch the four add_compression_policy calls to PERFORM so the
migration applies cleanly on TimescaleDB.

Follows #179, #180, #181 — same file, same DO block.
@github-actions github-actions Bot added the fix label Apr 26, 2026
@TerrifiedBug TerrifiedBug merged commit 206a9b8 into main Apr 26, 2026
7 checks passed
@TerrifiedBug TerrifiedBug deleted the fix/migration-perform-compression-policy branch April 26, 2026 18:41
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 26, 2026

Greptile Summary

Replaces four SELECT add_compression_policy(...) calls with PERFORM add_compression_policy(...) inside a PL/pgSQL DO block. This is the correct fix: PL/pgSQL requires PERFORM to discard a function's result when no INTO target is provided, and the bare SELECT was causing the entire block to abort with "query has no destination for result data". The change is exactly scoped to the four affected lines, with no other modifications.

Confidence Score: 5/5

Safe to merge — the four-line change is correct PL/pgSQL and precisely fixes the stated runtime error.

The fix is surgically scoped, PL/pgSQL-correct, and the existing if_not_exists => true guard ensures idempotency. No logic, security, or correctness issues introduced.

No files require special attention.

Important Files Changed

Filename Overview
prisma/migrations/20260329000001_timescaledb_compression/migration.sql Four SELECT → PERFORM substitutions in the DO block; fix is minimal, correct, and leaves all surrounding SQL untouched.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[DO $$ BEGIN] --> B{timescaledb extension\nexists?}
    B -- No --> C[RAISE NOTICE: skipping]
    B -- Yes --> D[ALTER TABLE PipelineMetric SET compress options]
    D --> E[PERFORM add_compression_policy PipelineMetric]
    E --> F[ALTER TABLE NodeMetric SET compress options]
    F --> G[PERFORM add_compression_policy NodeMetric]
    G --> H[ALTER TABLE PipelineLog SET compress options]
    H --> I[PERFORM add_compression_policy PipelineLog]
    I --> J[ALTER TABLE NodeStatusEvent SET compress options]
    J --> K[PERFORM add_compression_policy NodeStatusEvent]
    K --> L[RAISE NOTICE: policies enabled]
    C --> M[END $$]
    L --> M
Loading

Reviews (1): Last reviewed commit: "fix(migration): use PERFORM for add_comp..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant