March 28th, 2008 by depesz | Tags: , , , , , , | 4 comments »
Did it help? If yes - maybe you can help me? Donate BTC to 19zPa5diT2LZqGtTi8f8bfApLn8rw9zBHx

another new, cool feature commited by tom lane: “Support statement-level ON TRUNCATE triggers."

original patch was submitted by simon riggs, and tom commited it today/yesterday (depending on time zone).

commit log can be found in pgsql-committers archives.

patch is quite large, it modified 23 files.

functionality seems to be pretty obvious: you can create triggers called when “truncate" will happen.

two benefits that i can think of right now are:

  • slony will be able to work despite doing truncate on database (slony is just an example, it is equally important to any other replication system based on triggers)
  • auditing software (think table_log will be able to log data about truncation

as for example usage, let's write a trigger that will backup truncated tables (just in case we'd need to get the data back).

sample data structure:

CREATE TABLE users (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL DEFAULT '' UNIQUE,
password TEXT
);

now trigger:

CREATE OR REPLACE FUNCTION trg_truncate() RETURNS TRIGGER AS
$BODY$
DECLARE
new_table_name TEXT;
BEGIN
new_table_name := 'backup_' || TG_TABLE_NAME || '_' || extract(epoch FROM timeofday()::timestamp);
execute 'CREATE TABLE ' || quote_ident(new_table_name) || ' AS SELECT * FROM '|| quote_ident(TG_TABLE_NAME);
RETURN NULL;
END;
$BODY$
LANGUAGE 'plpgsql';
CREATE TRIGGER trg_truncate BEFORE TRUNCATE ON users FOR EACH STATEMENT EXECUTE PROCEDURE trg_truncate();

now, let's test the thing:

# insert into users (username) values ('test'),('depesz'),('xxx');
INSERT 0 3

data inserted, which can be checked:

# select * from users;
id | username | password
----+----------+----------
1 | test | [null]
2 | depesz | [null]
3 | xxx | [null]
(3 rows)

quick glance at list of tables:

# \dt
List of relations
Schema | Name | Type | Owner
--------+-------+-------+--------
public | users | table | depesz
(1 row)

nothing surprising. only 1 table, our testbed :)

now, for the real work:

# truncate users;
TRUNCATE TABLE

data should be lost. is it?

# select * from users;
id | username | password
----+----------+----------
(0 rows)

what about table list?

# \dt
List of relations
Schema | Name | Type | Owner
--------+-------------------------------+-------+--------
public | backup_users_1206737561.90902 | table | depesz
public | users | table | depesz
(2 rows)

ok, we have new table. and it's content?

# select * from "backup_users_1206737561.90902";
id | username | password
----+----------+----------
1 | test | [null]
2 | depesz | [null]
3 | xxx | [null]
(3 rows)

everything worked as planned. another great feature in upcoming release.

  1. 4 comments

  2. # Andreas 'akretschmer' Kretschmer
    Mar 29, 2008

    Hey, you can’t await 8.4, right?

    Btw.: Thank you for the nice articles.

  3. Mar 29, 2008

    @Andreas ‘akretschmer’ Kretschmer:
    i’ve been using cvs-head builds since 7.something (on my development machine, not on production servers), so it’s not very strange for me to do cvs up; make every 2-3 days.

    i just decided, that since i have pg-related blog, i will write about new features in 8.4, so others will get some idea about what to expect from new version.

    usually when new version reaches beta and/or prerelease stage, a lot of people write about what’s new. but they usually cover only the “big” changes. i’ll try to write about every new feature, so when it will be finally released, and somebody will be asked “whether we should upgrade to 8.4″, he will be able to simply point asker to http://www.depesz.com/index.php/tag/84/ to get a quick overview of what’s new :)

  4. # MaxTheITpro
    Apr 6, 2008

    I can’t wait to take Postgresql 8.3.x for a spin. There’s a lot of buzz around the Net about this version that promises something for everyone.

  5. Apr 6, 2008

    @MaxTheITpro:
    so why dont you? i mean – 8.3 is released. and there is even 8.3.1 already there.

    besides – take a note that i’m commenting what’s *not* in 8.3 – these posts are for 8.4 features.

Leave a comment