Created
August 31, 2014 16:21
-
-
Save movEAX/d128a9602cf93e3dba2e to your computer and use it in GitHub Desktop.
Postgresql: PL/Python trigger for logging row changes.
This file contains 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