Database Reference
In-Depth Information
If the key is not present, meaning this is the first call to this particular trigger, we have
to create an appropriate log function and store it. To do this, we examine the first ar-
gument for the log destination type.
For the udp log type, we create a UDP socket for writing. Then, we define a function,
passing in this socket and also the other two trigger arguments as default arguments
for the function. This is the most convenient way to create a closure, and to bundle a
function with some data values in Python.
For the file type, we just open this file in the append mode ( a+ ) and also create
a log function. The log function writes a message to this file and flushes the write,
so the data is written to the file immediately and not some time later when the
write buffer fills up. The log function created in either of these cases is stored in
SD[tuple(TD["args"])] .
At this point, we also prepare and save a query plan for getting other data we want to
log and save this in SD['env_plan'] . Now that we are done with the one-time pre-
parations, we can proceed with the actual logging part, which is really very simple.
Next, we retrieve the logging function ( logfunc = SD[args] ) and get the row of
the other logged data:
env_info_row = plpy.execute(SD['env_plan'])[0]
Finally, we convert all the logged data into one JSON object (log_msg =
json.dumps({...})) and then use the logging function to send it to the log, log-
func(log_msg) .
And that's it.
Next, let's test it out to see how it works by adding another trigger to our test table
we created earlier:
CREATE TRIGGER test_audit_trigger
AFTER INSERT OR UPDATE OR DELETE ON test
FOR EACH ROW
Search WWH ::




Custom Search