Conditional Statement-Level Triggers #166
Closed
wesleykendall
started this conversation in
Ideas
Replies: 1 comment
-
|
Version 4.15 supports creating conditional statement-level triggers. Docs at https://django-pgtrigger.readthedocs.io/en/4.15.0/statement/ This feature is being rolled out to pghistory in AmbitionEng/django-pghistory#197 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Postgres row-level triggers allow for conditional execution based on the
OLD/NEWrows. If the condition doesn't match, triggers aren't placed in the queue. Conditions are nice for expressing events, especially in libraries like django-pghistory.However, row-level triggers come at a cost of O(N) executions. In the case of django-pghistory, this means a bulk insert of 1000 rows can do 1000 inserts.
Since triggered SQL doesn't incur end-to-end network requests, an O(N) query here isn't as bad as one in the application, however, it still incurs a significant performance cost.
Normally one would use statement-level triggers, but Postgres doesn't have the ability to express conditions on statement-level triggers, making the boilerplate verbose. I'm planning to support this in the trigger definition layer in the following way:
COND_NEWandCOND_OLDvariables, which can be used in queries over the conditional old/new rows. I.e.SELECT EXISTS (SELECT * FROM COND_NEW)(note that this is just pseudocode)
My hope is that this will allow for easier conditional statement-level trigger writing. Once available, I plan to expose this option in pghistory, which could be a 10x type of performance improvement.
Let me know what you all think
Beta Was this translation helpful? Give feedback.
All reactions