Skip to content

Instantly share code, notes, and snippets.

@benjie
Created November 24, 2016 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjie/9d4f8df41ebf7a0fb78788c00c75561a to your computer and use it in GitHub Desktop.
Save benjie/9d4f8df41ebf7a0fb78788c00c75561a to your computer and use it in GitHub Desktop.
begin;
create table a(foo varchar not null primary key);
create table b(foo varchar not null primary key, constraint qux foreign key (foo) references a deferrable initially deferred);
create function bar() returns trigger as $$
begin
if TG_OP = 'INSERT' then
insert into b(foo) values(new.foo);
elsif TG_OP = 'DELETE' then
delete from b where foo = old.foo;
end if;
return new;
end;
$$ language plpgsql;
create trigger bar after insert or delete on a for each row execute procedure bar();
commit;
insert into a values('baz');
begin;
delete from a where foo = 'baz';
drop trigger bar on a;
drop function bar();
alter table b drop constraint qux;
commit;
@benjie
Copy link
Author

benjie commented Nov 24, 2016

$ dropdb pg_test; createdb pg_test && psql -v ON_ERROR_STOP=1 pg_test < testcase.sql
SET
Time: 0.227 ms
BEGIN
Time: 0.064 ms
CREATE TABLE
Time: 4.057 ms
CREATE TABLE
Time: 3.511 ms
CREATE FUNCTION
Time: 2.160 ms
CREATE TRIGGER
Time: 0.342 ms
COMMIT
Time: 3.321 ms
INSERT 0 1
Time: 1.941 ms
BEGIN
Time: 0.077 ms
DELETE 1
Time: 0.929 ms
DROP TRIGGER
Time: 0.584 ms
DROP FUNCTION
Time: 0.173 ms
ALTER TABLE
Time: 0.508 ms
ERROR:  XX000: relation 5022917 has no triggers
LOCATION:  afterTriggerInvokeEvents, trigger.c:3828
Time: 0.178 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment