Skip to main content
What this does: Creates a subscription on a materialized view and consumes change events (insert, update, delete) in your application as they happen. When to use this: Your app or agent needs to react to data changes immediately, without polling a materialized view in a loop.

Setup

1. Create a materialized view to subscribe to

2. Create a subscription

3. Consume changes in Python

4. Consume with a start timestamp (resume from checkpoint)

Key points

  • Change types: 'Insert', 'Delete', 'UpdateInsert' (new value after update), 'UpdateDelete' (old value before update)
  • retention controls how far back you can start consuming (default: 24 hours)
  • FETCH NEXT FROM cur WITH (timeout = 'Ns') blocks for up to N seconds waiting for data, then returns empty — eliminates the need for a polling sleep loop
  • A single subscription can have multiple cursors consuming independently
  • Drop a subscription when no longer needed: DROP SUBSCRIPTION high_value_alerts

Next steps