-
-
Save nyimbi/6591656d31d4fca602703b249e03e6aa to your computer and use it in GitHub Desktop.
Postgresql: PL/Python trigger for logging row changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE EXTENSION plpython2u; | |
| -- TODO: | |
| -- 1) Кэш соединения с ZMQ | |
| -- 2) Найти оптимальный способ сериализаци данных | |
| -- 3) Рассмотреть вариант с LISTEN/NOTIFY и PGQ | |
| CREATE OR REPLACE FUNCTION row_trigger() | |
| RETURNS TRIGGER | |
| AS $$ | |
| import json, zmq | |
| txid = plpy.execute('SELECT txid_current() AS txid')[0]['txid'] | |
| payload = { | |
| 'transaction': txid, | |
| 'event': TD['event'], | |
| 'table': TD['table_name'], | |
| 'old': TD['old'], | |
| 'new': TD['new']} | |
| ctx = zmq.Context() | |
| socket = ctx.socket(zmq.PUSH) | |
| socket.connect("ipc:///tmp/zmq.sock") | |
| socket.send(json.dumps(payload)) | |
| socket.close() | |
| $$ LANGUAGE plpython2u; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment