Pergunta

I'm trying to implement something similar to replication with python trigger procedures.

procedure

CREATE OR REPLACE FUNCTION foo.send_payload()
RETURNS trigger AS
$$
  import json, zmq

  try:
    payload = json.dumps(TD)
    ctx = zmq.Context()
    socket = ctx.socket(zmq.PUSH)
    socket.connect("ipc:///tmp/feeds/0")
    socket.send(payload)
    socket.close()
  except:
    pass
$$
LANGUAGE plpython VOLATILE;

trigger

CREATE TRIGGER foo.my_trigger
  AFTER INSERT
  ON foo.my_table
  FOR EACH ROW
  EXECUTE PROCEDURE foo.send_payload();

This does work, but it's not very efficient. Rows are inserted in bulk and I want to reuse the socket to send all of them. However, when I do a statement level trigger I don't have access to the rows.

I was thinking about defining a sequence which would be the last row id processed. Then use that to grab all the data in the procedure with a SELECT inside the statement level trigger. The problem is that there doesn't seem to be a way of getting a sequence value without incrementing it.

Any suggestions on how to approach this problem?

Foi útil?

Solução

Use two triggers. "FOR EACH ROW" would stack the rows in some temporary place (maybe SD), and "FOR EACH STATEMENT" would get data from shared place, send, and clear the shared place.

Alternatively (and I think it's better idea), you can use LISTEN/NOTIFY, as I once described in my blog.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top