Use pg_trigger_depth to avoid re-trigger in update (#1708)

Some of the `update_osm_${LAYER}`-functions, which are executed by triggers on updates, execute an UPDATE statement on the same tables that have these triggers. This causes the trigger functions `flag` and `store` to run multiple times for one record. 

For instance, if I add log in the trigger functions and run an INSERT on `osm_housenumber_point`, this output is generated:  

```
NOTICE:  Store
NOTICE:  Flag
NOTICE:  Refresh housenumber
NOTICE:  Flag
NOTICE:  Store
NOTICE:  Flag
INSERT 0 1
```

If we limit the triggers from executing if they are called from another trigger using `pg_trigger_depth() < 1`, the triggers will only be triggered once per record:

```
NOTICE:  Store
NOTICE:  Flag
NOTICE:  Refresh housenumber
INSERT 0 1
```

This will prevent redunant executions and might improve update performance.  

Co-authored-by: Patrik Sylve <patrik.sylve@t-kartor.com>
This commit is contained in:
Patrik Sylve
2025-02-18 11:18:04 +01:00
committed by GitHub
parent d32d74aaac
commit e6a1000155
15 changed files with 31 additions and 0 deletions

View File

@@ -110,12 +110,14 @@ CREATE TRIGGER trigger_store
AFTER INSERT OR UPDATE
ON osm_state_point
FOR EACH ROW
WHEN (pg_trigger_depth() < 1)
EXECUTE PROCEDURE place_state.store();
CREATE TRIGGER trigger_flag
AFTER INSERT OR UPDATE
ON osm_state_point
FOR EACH STATEMENT
WHEN (pg_trigger_depth() < 1)
EXECUTE PROCEDURE place_state.flag();
CREATE CONSTRAINT TRIGGER trigger_refresh