Skip to content

Commit bb25c34

Browse files
authored
Add LISTEN/NOTIFY notifications for item operations. (#383)
* Added notification triggers as documentation.
1 parent 497625a commit bb25c34

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

docs/src/pgstac.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,87 @@ WHERE schemaname = 'pgstac'
208208
ORDER BY idx_scan ASC
209209
;
210210
```
211+
212+
### Notification Triggers
213+
214+
You can add notification triggers alongside pgstac to get notified when items are inserted, updated, or deleted.
215+
216+
**Important:** Do NOT create these in the `pgstac` schema as they could be removed in future migrations.
217+
218+
#### Example Notification Setup
219+
220+
Here's an example of how to set up notification triggers for item changes:
221+
222+
```sql
223+
-- Create the notification function
224+
CREATE OR REPLACE FUNCTION notify_items_change_func()
225+
RETURNS TRIGGER AS $$
226+
DECLARE
227+
228+
BEGIN
229+
PERFORM pg_notify('pgstac_items_change'::text, json_build_object(
230+
'operation', TG_OP,
231+
'items', jsonb_agg(
232+
jsonb_build_object(
233+
'collection', data.collection,
234+
'id', data.id
235+
)
236+
)
237+
)::text
238+
)
239+
FROM data
240+
;
241+
RETURN NULL;
242+
END;
243+
$$ LANGUAGE plpgsql;
244+
245+
-- Create triggers for INSERT operations
246+
CREATE OR REPLACE TRIGGER notify_items_change_insert
247+
AFTER INSERT ON pgstac.items
248+
REFERENCING NEW TABLE AS data
249+
FOR EACH STATEMENT EXECUTE FUNCTION notify_items_change_func()
250+
;
251+
252+
-- Create triggers for UPDATE operations
253+
CREATE OR REPLACE TRIGGER notify_items_change_update
254+
AFTER UPDATE ON pgstac.items
255+
REFERENCING NEW TABLE AS data
256+
FOR EACH STATEMENT EXECUTE FUNCTION notify_items_change_func()
257+
;
258+
259+
-- Create triggers for DELETE operations
260+
CREATE OR REPLACE TRIGGER notify_items_change_delete
261+
AFTER DELETE ON pgstac.items
262+
REFERENCING OLD TABLE AS data
263+
FOR EACH STATEMENT EXECUTE FUNCTION notify_items_change_func()
264+
;
265+
```
266+
267+
#### Usage
268+
269+
Listen for notifications:
270+
```sql
271+
LISTEN pgstac_items_change;
272+
```
273+
274+
Payload structure:
275+
276+
```json
277+
{
278+
"operation": "INSERT",
279+
"items": [
280+
{
281+
"collection": "sentinel-2-l2a",
282+
"id": "item-1"
283+
},
284+
{
285+
"collection": "sentinel-2-l2a",
286+
"id": "item-2"
287+
}
288+
]
289+
}
290+
```
291+
292+
#### Customization
293+
294+
You may modify the function to include additional metadata, filter by collection, or use different channels. Only trigger on the `items` table, not `items_staging`.

0 commit comments

Comments
 (0)