Skip to content

Instantly share code, notes, and snippets.

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