Skip to content

fix(migration): use PERFORM for add_continuous_aggregate_policy inside DO block#183

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

fix(migration): use PERFORM for add_continuous_aggregate_policy inside DO block#183
TerrifiedBug merged 1 commit intomainfrom
fix/migration-perform-continuous-aggregate-policy

Conversation

@TerrifiedBug
Copy link
Copy Markdown
Owner

Summary

Direct follow-up to #182 — same bug class, next migration in the sequence.

After #182 unblocked 20260329000001_timescaledb_compression, the next migration 20260329000002_timescaledb_continuous_aggregates failed against timescale/timescaledb:2.16.0-pg16 with the same PL/pgSQL syntax error:

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 28 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_continuous_aggregate_policy(...) calls in this migration use SELECT, so the very first one aborts the whole block.

The four CREATE MATERIALIZED VIEW ... WITH (timescaledb.continuous) statements in the same block are fine — only the add_continuous_aggregate_policy calls need fixing.

Fix

Four-line change. Each SELECT add_continuous_aggregate_policy( becomes PERFORM add_continuous_aggregate_policy(. View names, intervals, options, indentation untouched.

Sweep status

I (well — the prompt author) checked the rest of prisma/migrations/ for any other bare SELECT of a TimescaleDB management function (add_compression_policy, add_continuous_aggregate_policy, add_retention_policy, add_reorder_policy, add_job, alter_job, create_hypertable, drop_chunks, reorder_chunk, refresh_continuous_aggregate) inside a DO block. These four are the last remaining instances. After this PR merges, the migration set should apply cleanly on a fresh TimescaleDB database.

Why this didn't surface earlier

Same as #179 / #180 / #181 / #182: every prior deployment ran on plain Postgres, where the IF EXISTS (... timescaledb) guard makes this whole migration a no-op. Now that #182 cleared the previous migration under TimescaleDB, this one is next 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 20260329000002_timescaledb_continuous_aggregates
npx prisma migrate deploy

Drift warning for plain-Postgres users

Same caveat as the prior PRs: 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 20260329000002_timescaledb_continuous_aggregates

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

Test plan

  • Visual inspection — exactly 4 lines changed, all SELECT add_continuous_aggregate_policy(PERFORM add_continuous_aggregate_policy(
  • Drop the demo postgres volume
  • docker compose up against timescale/timescaledb:2.16.0-pg1620260329000002 applies cleanly through to completion
  • Confirm the four continuous-aggregate refresh jobs exist:
    SELECT hypertable_name, proc_name FROM timescaledb_information.jobs
    WHERE proc_name = 'policy_refresh_continuous_aggregate';
    — should return rows for the four cagg views (pipeline_metrics_1m, pipeline_metrics_1h, node_metrics_1m, node_metrics_1h).

Prior PRs in this series

…e DO block

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_continuous_aggregate_policy calls to PERFORM
so the migration applies cleanly on TimescaleDB.

Same fix as #182, applied to the next migration in the sequence.
@github-actions github-actions Bot added the fix label Apr 26, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 26, 2026

Greptile Summary

Fixes the PL/pgSQL syntax error in the 20260329000002_timescaledb_continuous_aggregates migration by replacing four bare SELECT add_continuous_aggregate_policy(...) calls with PERFORM, which is required inside a DO block when the function's return value is intentionally discarded. The change is minimal and correct — all four policy calls are updated, idempotency guards (if_not_exists => true and IF NOT EXISTS on the views) are preserved, and the plain-Postgres no-op guard remains intact.

Confidence Score: 5/5

Safe to merge — targeted four-line fix with no functional side effects and correct idempotency preserved.

All four affected lines are correctly updated, the fix exactly matches the stated intent, no logic was altered beyond the keyword change, and the migration remains idempotent and guarded for non-TimescaleDB environments.

No files require special attention.

Important Files Changed

Filename Overview
prisma/migrations/20260329000002_timescaledb_continuous_aggregates/migration.sql Replaces four bare SELECT add_continuous_aggregate_policy(...) calls with PERFORM inside the PL/pgSQL DO block — the correct fix for the "query has no destination for result data" error on TimescaleDB.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Migration DO block starts] --> B{timescaledb extension exists?}
    B -- No --> C[RAISE NOTICE: skipping]
    B -- Yes --> D[CREATE MATERIALIZED VIEW pipeline_metrics_1m IF NOT EXISTS]
    D --> E[PERFORM add_continuous_aggregate_policy pipeline_metrics_1m]
    E --> F[CREATE MATERIALIZED VIEW pipeline_metrics_1h IF NOT EXISTS]
    F --> G[PERFORM add_continuous_aggregate_policy pipeline_metrics_1h]
    G --> H[CREATE MATERIALIZED VIEW node_metrics_1m IF NOT EXISTS]
    H --> I[PERFORM add_continuous_aggregate_policy node_metrics_1m]
    I --> J[CREATE MATERIALIZED VIEW node_metrics_1h IF NOT EXISTS]
    J --> K[PERFORM add_continuous_aggregate_policy node_metrics_1h]
    K --> L[RAISE NOTICE: success]

    style E fill:#90EE90
    style G fill:#90EE90
    style I fill:#90EE90
    style K fill:#90EE90
Loading

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

@TerrifiedBug TerrifiedBug merged commit c547f17 into main Apr 26, 2026
13 checks passed
@TerrifiedBug TerrifiedBug deleted the fix/migration-perform-continuous-aggregate-policy branch April 26, 2026 20:07
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