Re: Change lock requirements for adding a trigger

Lists: pgsql-hackers
From: Decibel! <decibel(at)decibel(dot)org>
To: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Change lock requirements for adding a trigger
Date: 2008-05-30 00:18:31
Message-ID: 1E5ACBA6-0F44-4162-B8DA-34532AF7898D@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Is there a reason that we can't add a trigger to a table while a
select is running? This is a serious pain when trying to setup
londiste or slony.
--
Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
Give your computer some brain candy! www.distributed.net Team #1828


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Decibel! <decibel(at)decibel(dot)org>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Change lock requirements for adding a trigger
Date: 2008-05-30 14:51:34
Message-ID: 1212159094.4120.114.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Thu, 2008-05-29 at 19:18 -0500, Decibel! wrote:

> Is there a reason that we can't add a trigger to a table while a
> select is running? This is a serious pain when trying to setup
> londiste or slony.

This is constrained by locking.

There are a subset of DDL commands that might be able to be performed
with just an ExclusiveLock or ShareLock rather than an
AccessExclusiveLock. Nobody has studied which sub-statements this might
apply to, but its do-able since CREATE INDEX already does this.

--
Simon Riggs www.2ndQuadrant.com
PostgreSQL Training, Services and Support


From: Decibel! <decibel(at)decibel(dot)org>
To: Simon Riggs <simon(at)2ndquadrant(dot)com>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Change lock requirements for adding a trigger
Date: 2008-06-03 21:48:08
Message-ID: F38A0C4E-C087-4F42-B5AA-9C7A6E9026B0@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On May 30, 2008, at 9:51 AM, Simon Riggs wrote:
> On Thu, 2008-05-29 at 19:18 -0500, Decibel! wrote:
>> Is there a reason that we can't add a trigger to a table while a
>> select is running? This is a serious pain when trying to setup
>> londiste or slony.
>
> This is constrained by locking.
>
> There are a subset of DDL commands that might be able to be performed
> with just an ExclusiveLock or ShareLock rather than an
> AccessExclusiveLock. Nobody has studied which sub-statements this
> might
> apply to, but its do-able since CREATE INDEX already does this.

Is there a good way to determine this other than depending on
knowledge of the source code?
--
Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
Give your computer some brain candy! www.distributed.net Team #1828


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Decibel! <decibel(at)decibel(dot)org>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Change lock requirements for adding a trigger
Date: 2008-06-03 22:04:13
Message-ID: 1212530653.4148.31.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Tue, 2008-06-03 at 16:48 -0500, Decibel! wrote:
> On May 30, 2008, at 9:51 AM, Simon Riggs wrote:
> > On Thu, 2008-05-29 at 19:18 -0500, Decibel! wrote:
> >> Is there a reason that we can't add a trigger to a table while a
> >> select is running? This is a serious pain when trying to setup
> >> londiste or slony.
> >
> > This is constrained by locking.
> >
> > There are a subset of DDL commands that might be able to be performed
> > with just an ExclusiveLock or ShareLock rather than an
> > AccessExclusiveLock. Nobody has studied which sub-statements this
> > might
> > apply to, but its do-able since CREATE INDEX already does this.
>
> Is there a good way to determine this other than depending on
> knowledge of the source code?

The source doesn't know yet. So just analysis and thinking.

The mechanism to hold less than an AccessExclusiveLock it doesn't exist
yet, but it never will unless we have a list of the things that might be
performed correctly with a lower level of lock.

--
Simon Riggs www.2ndQuadrant.com
PostgreSQL Training, Services and Support


From: Decibel! <decibel(at)decibel(dot)org>
To: Simon Riggs <simon(at)2ndquadrant(dot)com>
Cc: PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Change lock requirements for adding a trigger
Date: 2008-06-04 13:31:46
Message-ID: 85B9C2D2-76F8-467F-958B-A1B8324A74FA@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Jun 3, 2008, at 5:04 PM, Simon Riggs wrote:
> On Tue, 2008-06-03 at 16:48 -0500, Decibel! wrote:
>> On May 30, 2008, at 9:51 AM, Simon Riggs wrote:
>>> On Thu, 2008-05-29 at 19:18 -0500, Decibel! wrote:
>>>> Is there a reason that we can't add a trigger to a table while a
>>>> select is running? This is a serious pain when trying to setup
>>>> londiste or slony.
>>>
>>> This is constrained by locking.
>>>
>>> There are a subset of DDL commands that might be able to be
>>> performed
>>> with just an ExclusiveLock or ShareLock rather than an
>>> AccessExclusiveLock. Nobody has studied which sub-statements this
>>> might
>>> apply to, but its do-able since CREATE INDEX already does this.
>>
>> Is there a good way to determine this other than depending on
>> knowledge of the source code?
>
> The source doesn't know yet. So just analysis and thinking.
>
> The mechanism to hold less than an AccessExclusiveLock it doesn't
> exist
> yet, but it never will unless we have a list of the things that
> might be
> performed correctly with a lower level of lock.

Ok, I'll take a stab at such a list. Can anyone think of any reasons
why CREATE TRIGGER couldn't get by with ShareLock?
--
Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
Give your computer some brain candy! www.distributed.net Team #1828


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Decibel! <decibel(at)decibel(dot)org>
Cc: Simon Riggs <simon(at)2ndquadrant(dot)com>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Change lock requirements for adding a trigger
Date: 2008-06-04 14:59:25
Message-ID: 24709.1212591565@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Decibel! <decibel(at)decibel(dot)org> writes:
> Ok, I'll take a stab at such a list. Can anyone think of any reasons
> why CREATE TRIGGER couldn't get by with ShareLock?

pg_class.reltriggers.

regards, tom lane


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Decibel! <decibel(at)decibel(dot)org>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Change lock requirements for adding a trigger
Date: 2008-06-04 20:28:08
Message-ID: 1212611288.4148.207.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Wed, 2008-06-04 at 10:59 -0400, Tom Lane wrote:
> Decibel! <decibel(at)decibel(dot)org> writes:
> > Ok, I'll take a stab at such a list. Can anyone think of any reasons
> > why CREATE TRIGGER couldn't get by with ShareLock?
>
> pg_class.reltriggers.

ISTM that we do this in many ways on pg_class, if we believe the docs.

We have

* relhasindex (bool) set by CREATE INDEX but not unset by DROP INDEX

* relhasrules (bool)

* reltriggers (int2) set by CREATE and DROP, since its an integer

Seems we should have one consistent way of adding associated objects.

If CREATE INDEX can take a Share lock and can update pg_class, why would
it not be theoretically possible for CREATE TRIGGER?

--
Simon Riggs www.2ndQuadrant.com
PostgreSQL Training, Services and Support


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Simon Riggs <simon(at)2ndquadrant(dot)com>
Cc: Decibel! <decibel(at)decibel(dot)org>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Change lock requirements for adding a trigger
Date: 2008-06-04 20:33:20
Message-ID: 29221.1212611600@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Simon Riggs <simon(at)2ndquadrant(dot)com> writes:
> We have
> * relhasindex (bool) set by CREATE INDEX but not unset by DROP INDEX
> * relhasrules (bool)
> * reltriggers (int2) set by CREATE and DROP, since its an integer

Right.

> If CREATE INDEX can take a Share lock and can update pg_class, why would
> it not be theoretically possible for CREATE TRIGGER?

It's (probably) theoretically possible, if we replace reltriggers with a
bool that acts more like relhasindex, ie it's a hint to go look in
pg_triggers. My point was just that you can't arbitrarily decide that
some operation needs only a given strength of lock if you are not up to
speed on these sorts of details.

regards, tom lane


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Decibel! <decibel(at)decibel(dot)org>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Change lock requirements for adding a trigger
Date: 2008-06-04 21:09:55
Message-ID: 1212613795.4148.209.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Wed, 2008-06-04 at 16:33 -0400, Tom Lane wrote:
> Simon Riggs <simon(at)2ndquadrant(dot)com> writes:
> > We have
> > * relhasindex (bool) set by CREATE INDEX but not unset by DROP INDEX
> > * relhasrules (bool)
> > * reltriggers (int2) set by CREATE and DROP, since its an integer
>
> Right.
>
> > If CREATE INDEX can take a Share lock and can update pg_class, why would
> > it not be theoretically possible for CREATE TRIGGER?
>
> It's (probably) theoretically possible, if we replace reltriggers with a
> bool that acts more like relhasindex, ie it's a hint to go look in
> pg_triggers. My point was just that you can't arbitrarily decide that
> some operation needs only a given strength of lock if you are not up to
> speed on these sorts of details.

Understood. Wouldn't have looked there without your hint.

--
Simon Riggs www.2ndQuadrant.com
PostgreSQL Training, Services and Support


From: Simon Riggs <simon(at)2ndquadrant(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Decibel! <decibel(at)decibel(dot)org>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Change lock requirements for adding a trigger
Date: 2008-06-13 20:06:50
Message-ID: 1213387611.25121.250.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On Wed, 2008-06-04 at 16:33 -0400, Tom Lane wrote:
> Simon Riggs <simon(at)2ndquadrant(dot)com> writes:
> > We have
> > * relhasindex (bool) set by CREATE INDEX but not unset by DROP INDEX
> > * relhasrules (bool)
> > * reltriggers (int2) set by CREATE and DROP, since its an integer
>
> Right.
>
> > If CREATE INDEX can take a Share lock and can update pg_class, why would
> > it not be theoretically possible for CREATE TRIGGER?
>
> It's (probably) theoretically possible, if we replace reltriggers with a
> bool that acts more like relhasindex, ie it's a hint to go look in
> pg_triggers.

Looking at this area of locking, I've noticed that the locks held by
CREATE TRIGGER are more of a problem than might be apparent.

* Locks held by CREATE TRIGGER are an issue for trigger-based
replication systems, where triggers are frequently added and removed to
various tables.

* ALTER TABLE .. ADD FOREIGN KEY holds an AccessExclusiveveLock on
*both* referencing and referenced tables. It does this because we must
add triggers to both tables. So reducing the lock strength required by
CREATE TRIGGER would also allow a reduction in lock strength for adding
FKs.

So useful steps will be to

* refactor pg_class code so that CREATE TRIGGER uses an identical
approach to CREATE INDEX

* reduce lock strength for CREATE TRIGGER and ALTER TABLE ... ADD
FOREIGN KEY so that it takes a ShareLock during
ATAddForeignKeyConstraint()

* look at how we can reduce lock strength for other ALTER TABLE
subcommands. Not sure how yet.

--
Simon Riggs www.2ndQuadrant.com
PostgreSQL Training, Services and Support


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: info(at)2ndquadrant(dot)com
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Decibel!" <decibel(at)decibel(dot)org>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Change lock requirements for adding a trigger
Date: 2008-07-17 01:34:29
Message-ID: 200807170134.m6H1YTr20106@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Added to TODO:

* Reduce locking requirements for creating a trigger

http://archives.postgresql.org/pgsql-hackers/2008-06/msg00635.php

---------------------------------------------------------------------------

Simon Riggs wrote:
>
> On Wed, 2008-06-04 at 16:33 -0400, Tom Lane wrote:
> > Simon Riggs <simon(at)2ndquadrant(dot)com> writes:
> > > We have
> > > * relhasindex (bool) set by CREATE INDEX but not unset by DROP INDEX
> > > * relhasrules (bool)
> > > * reltriggers (int2) set by CREATE and DROP, since its an integer
> >
> > Right.
> >
> > > If CREATE INDEX can take a Share lock and can update pg_class, why would
> > > it not be theoretically possible for CREATE TRIGGER?
> >
> > It's (probably) theoretically possible, if we replace reltriggers with a
> > bool that acts more like relhasindex, ie it's a hint to go look in
> > pg_triggers.
>
> Looking at this area of locking, I've noticed that the locks held by
> CREATE TRIGGER are more of a problem than might be apparent.
>
> * Locks held by CREATE TRIGGER are an issue for trigger-based
> replication systems, where triggers are frequently added and removed to
> various tables.
>
> * ALTER TABLE .. ADD FOREIGN KEY holds an AccessExclusiveveLock on
> *both* referencing and referenced tables. It does this because we must
> add triggers to both tables. So reducing the lock strength required by
> CREATE TRIGGER would also allow a reduction in lock strength for adding
> FKs.
>
> So useful steps will be to
>
> * refactor pg_class code so that CREATE TRIGGER uses an identical
> approach to CREATE INDEX
>
> * reduce lock strength for CREATE TRIGGER and ALTER TABLE ... ADD
> FOREIGN KEY so that it takes a ShareLock during
> ATAddForeignKeyConstraint()
>
> * look at how we can reduce lock strength for other ALTER TABLE
> subcommands. Not sure how yet.
>
> --
> Simon Riggs www.2ndQuadrant.com
> PostgreSQL Training, Services and Support
>
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +