Skip to content

Commit af7e518

Browse files
authored
docs(postgres-notify): add docs for Postgres notification (#955)
1 parent 9f2c545 commit af7e518

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

docs/docs/ops/sources.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,27 @@ The spec takes the following fields:
313313
* `included_columns` (`list[str]`, optional): non-primary-key columns to include. If not specified, all non-PK columns are included.
314314
* `ordinal_column` (`str`, optional): to specify a non-primary-key column used for change tracking and ordering, e.g. can be a modified timestamp or a monotonic version number. Supported types are integer-like (`bigint`/`integer`) and timestamps (`timestamp`, `timestamptz`).
315315
`ordinal_column` must not be a primary key column.
316+
* `notification` (`cocoindex.sources.PostgresNotification`, optional): when present, enable change capture based on Postgres LISTEN/NOTIFY. It has the following fields:
317+
* `channel_name` (`str`, optional): the Postgres notification channel to listen on. CocoIndex will automatically create the channel with the given name. If omitted, CocoIndex uses `{flow_name}__{source_name}__cocoindex`.
318+
319+
:::info
320+
321+
If `notification` is provided, CocoIndex listens for row changes using Postgres LISTEN/NOTIFY and creates the required database objects on demand when the flow starts listening:
322+
323+
- Function to create notification message: `{channel_name}_n`.
324+
- Trigger to react to table changes: `{channel_name}_t` on the specified `table_name`.
325+
326+
Creation is automatic when listening begins.
327+
328+
Currently CocoIndex doesn't automatically clean up these objects when the flow is dropped (unlike targets)
329+
It's usually OK to leave them as they are, but if you want to clean them up, you can run the following SQL statements to manually drop them:
330+
331+
```sql
332+
DROP TRIGGER IF EXISTS {channel_name}_t ON "{table_name}";
333+
DROP FUNCTION IF EXISTS {channel_name}_n();
334+
```
335+
336+
:::
316337

317338
### Schema
318339

src/ops/sources/postgres.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,9 @@ impl SourceFactoryBase for Factory {
789789
.await?;
790790

791791
let notification_ctx = spec.notification.map(|spec| {
792-
let channel_name = spec
793-
.channel_name
794-
.unwrap_or_else(|| format!("coco_{}__{}", context.flow_instance_name, source_name));
792+
let channel_name = spec.channel_name.unwrap_or_else(|| {
793+
format!("{}__{}__cocoindex", context.flow_instance_name, source_name)
794+
});
795795
NotificationContext {
796796
function_name: format!("{channel_name}_n"),
797797
trigger_name: format!("{channel_name}_t"),

0 commit comments

Comments
 (0)