Last time , we created a heirarchy of tables for logging, but we don't have anything they'd log, and we don't have a way to ensure the logging happens automagically. This time, we'll fix those problems. First, a thing to log: CREATE TABLE foo( id BIGSERIAL PRIMARY KEY, t TEXT ); Next, let's make an efficient trigger function. You may have seen one of these before, but it was probably row-based, so it couldn't be super efficient. This one is statement-based, and uses the new transition table feature in PostgreSQL 10. We only need to build one trigger function because our logging infrastructure is schema-agnostic. We could have separated it into three trigger functions, but that's just more code to maintain. It starts off as usual: CREATE OR REPLACE FUNCTION log() RETURNS TRIGGER LANGUAGE plpgsql AS $$ BEGIN IF TG_OP NOT IN ('INSERT', 'UPDATE', 'DELETE') THEN RAISE EXCEPTION 'This function
Popular posts from this blog
Have you ever found yourself in the middle of a long statement in psql and wanted to pull up your favorite editor? Now, you can, using the same shortcut of control-x control-e that you would in bash! Here's how: $EDITOR ~/.inputrc Now add the following lines: $if psql "\C-x\C-e": "\C-e\\e\C-m" $endif ...save the file, and you're good to go.
We started off by creating a logging infrastructure, then arranging for a single table to use it. Rather than repeat that work for each table, let's use a relatively although not completely new feature: `EVENT TRIGGER`. The idea here is that we fire a trigger on `CREATE TABLE` and see to it that the table is logged. We'll write the trigger first, even though in reality, we'd need to load the function it calls first. ```sql CREATE EVENT TRIGGER add_logger ON ddl_command_end WHEN tag IN ('create table') EXECUTE PROCEDURE add_logger(); COMMENT ON EVENT TRIGGER add_logger IS 'Ensure that each table which is not a log gets logged'; ``` The magic happens inside `add_logger()`, but it's magic we've already seen. First, we'll get the table's name and schema using `pg_event_trigger_ddl_commands()`, filtering out tables which are already log tables. The test here is crude and string-based, but we could easily go to schema-based