Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Postgres Difficulty: Advanced ClaudeChatGPTCursor

Postgres Materialized View Refresh Strategy Prompt

Design a safe refresh strategy for Postgres materialized views — choosing between plain and CONCURRENTLY refresh, scheduling cadence, staleness tolerance, and the unique index CONCURRENTLY requires — so dashboards stay fast without long ACCESS EXCLUSIVE locks or runaway bloat on the matview.

Target user
DBAs and data engineers running reporting matviews on production Postgres
Difficulty
Advanced
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior PostgreSQL DBA who designs reporting layers. You know the two refresh
modes have very different locking: plain REFRESH MATERIALIZED VIEW takes an ACCESS
EXCLUSIVE lock and blocks all readers for the whole refresh, while REFRESH MATERIALIZED
VIEW CONCURRENTLY keeps readers online but requires a UNIQUE index on the matview, does
a row-by-row diff (slower, and it churns dead tuples so the matview itself needs vacuum),
and cannot be the first refresh. Your job is to pick the right mode, cadence, and safeguards
for a specific matview.

I will paste:
- The matview definition (or its purpose) and its approximate row count and size: [MATVIEW]
- The underlying query cost / how long a full rebuild takes, and how volatile the source
  data is: [REFRESH COST / VOLATILITY]
- Read pattern: who queries the matview, how often, and their staleness tolerance
  (real-time-ish? minutes? hourly? overnight?): [READ PATTERN]
- Any existing unique key candidate on the result set, current indexes on the matview,
  and how refreshes are scheduled today (cron, pg_cron, app job): [KEYS / SCHEDULE]

Work through:

1. **Choose the refresh mode** — recommend CONCURRENTLY when readers must stay online
   during refresh AND a genuinely unique column set exists (give the exact CREATE UNIQUE
   INDEX). Recommend plain REFRESH when a brief blocking window is acceptable (e.g.
   overnight) or no unique key exists, and state the ACCESS EXCLUSIVE lock impact plainly.

2. **Set the cadence against staleness** — match refresh frequency to the read pattern
   and refresh cost. Warn when refresh duration approaches the interval (refreshes
   overlapping or stacking), and when data volatility makes a matview the wrong tool
   versus an indexed view or incremental rollup table.

3. **Handle the CONCURRENTLY costs** — because CONCURRENTLY diffs and leaves dead tuples,
   confirm autovacuum covers the matview (or add per-object autovacuum settings), and note
   the first refresh after creation must be plain (non-concurrent) to populate it.

4. **Make it operable** — give the exact refresh command, the scheduling snippet (pg_cron
   or a systemd timer / cron entry), a lock_timeout or statement_timeout wrapper so a
   refresh can't wedge forever, and a query to detect a stale or failed matview.

Output format: (a) mode decision with the reasoning and lock impact, (b) the exact CREATE
UNIQUE INDEX (if CONCURRENTLY) and REFRESH command, (c) the schedule snippet with a timeout
guard, (d) a monitoring query for last refresh / staleness and matview bloat.

Guardrails: plain REFRESH MATERIALIZED VIEW holds an ACCESS EXCLUSIVE lock for its full
duration — never schedule a long plain refresh during business hours on a matview that
users read. CONCURRENTLY needs a UNIQUE index and a prior non-concurrent populate; verify
both before relying on it. Wrap scheduled refreshes with a timeout so a stuck refresh
doesn't pile up locks or overlap the next run.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

Materialized views are an easy win for slow dashboards right up until the refresh strategy bites back. The trap is that the two refresh modes behave completely differently under load: a plain REFRESH MATERIALIZED VIEW takes an ACCESS EXCLUSIVE lock and freezes every reader for the entire rebuild, while REFRESH ... CONCURRENTLY keeps readers online but only works if the matview has a unique index, has already been populated once non-concurrently, and is being vacuumed to clear the dead tuples its diff leaves behind. This prompt makes the model reason about those constraints explicitly instead of emitting a generic “just refresh it.”

It ties the decision to the thing that actually matters — the read pattern’s staleness tolerance and the refresh cost. That framing surfaces the failure mode teams hit most: a refresh that takes longer than its own interval, so runs overlap and stack up locks. By asking the model to compare refresh duration against cadence, it catches the problem before it becomes a 2 a.m. page.

The output is deliberately operable: the exact unique index, the exact refresh command, a scheduling snippet, a timeout guard, and a staleness/bloat monitoring query. The guardrails keep the blocking plain refresh out of business hours and make sure the CONCURRENTLY prerequisites are actually in place before anyone depends on them.

Related prompts

More Postgres prompts & error guides

Browse every Postgres prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.