Skip site navigation (1) Skip section navigation (2)

Re: Is drop/restore trigger transactional?

From: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Craig James <cjames(at)emolecules(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Is drop/restore trigger transactional?
Date: 2012-08-07 20:45:18
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-performance
On Tue, Aug 7, 2012 at 1:15 PM, Merlin Moncure <mmoncure(at)gmail(dot)com> wrote:
> On Tue, Aug 7, 2012 at 1:48 PM, Craig James <cjames(at)emolecules(dot)com> wrote:
>> I found this discussion from 2005 that says you can drop and restore a
>> trigger inside a transaction, but that doing so locks the whole table:
>>> From: Jeff Davis
>>> It got me curious enough that I tested it, and apparently droping a
>>> trigger locks the table. Any actions on that table must wait until the
>>> transaction that drops the trigger finishes.
>>> So, technically my system works, but requires a rather nasty lock while
>>> the transaction (the one that doesn't want the trigger to execute)
>>> finishes.
>> I have a process that copies customer data from one database to
>> another, and we know that the trigger has already done its work.  The
>> trigger is thus redundant, but it slows the copy WAY down, so I wanted
>> to drop/restore it inside a transaction.
>> Is it still true that drop-trigger inside a transaction will lock the
>> whole table?  We're using 8.4.
> absolutely -- the database needs to guard against other writers to the
> table doing inserts in the meantime.

But why must it?  Why can't other writers simply obey the trigger,
since its removal has not yet been committed?  You could have the
anomaly that a longer-running later-committing transaction used the
old trigger while a shorter-running earlier-committing transaction
used the new one (which isn't really an anomaly if the old and new are
identical), but is that even barred if neither of them is in
serializable mode?  And since triggers can do pretty much anything
they want internally, there isn't much of a transactional guarantee
with them anyway.

> there's no concept in SQL of
> 'enforce this trigger for all writers, except for me' nor should there
> be.

Why shouldn't there be, other than the bother of implementing and
documenting it?  Sometimes theory needs to compromise with reality.
When we don't provide slightly dangerous ways to make those
compromises, people are forced to use very dangerous ways instead.

> one possible workaround is to hack your trigger function so that it
> doesn't operate for particular roles.  so your trigger might be:
> IF current_user = 'bulk_writer' THEN
>   return new;
> <expensive stuff>

I don't know Craig's case, but often the most expensive of the
"expensive stuff" is the bare fact of firing a trigger in the first



In response to


pgsql-performance by date

Next:From: Merlin MoncureDate: 2012-08-07 21:21:36
Subject: Re: Is drop/restore trigger transactional?
Previous:From: Scott MarloweDate: 2012-08-07 20:39:58
Subject: Re: Is drop/restore trigger transactional?

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group