Re: Lock for table renaming

Lists: pgsql-hackers
From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Lock for table renaming
Date: 2006-12-06 14:50:33
Message-ID: 200612061550.33887.peter_e@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Why does renaming a table take out an access exclusive lock on the target
table? Isn't this just an UPDATE on a few system catalog rows.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/


From: Markus Schiltknecht <markus(at)bluegap(dot)ch>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Lock for table renaming
Date: 2006-12-06 15:05:00
Message-ID: 4576DC1C.6040506@bluegap.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello Peter,

Peter Eisentraut wrote:
> Why does renaming a table take out an access exclusive lock on the target
> table? Isn't this just an UPDATE on a few system catalog rows.

I guess because system catalog updates are visible immediately? Try the
following:

markus=# CREATE TABLE test (a INT);
CREATE TABLE
markus=# BEGIN;
BEGIN
markus=# SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SET
markus=# INSERT INTO TEST (a) VALUES (1);
INSERT 0 1

Then switch to another terminal and rename the table by hand (to
circumvent the lock):

markus=# UPDATE pg_class SET relname='gone' WHERE relname = 'test';
UPDATE 1

Go back to the first transaction and try to read from the table again:

markus=# SELECT * FROM test;
ERROR: relation "test" does not exist

What works instead, is:

postgres=# SELECT * FROM gone;
a
---
1
(1 row)

AFAICT, that applies to both, READ COMMITTED as well as SERIALIZABLE.
Please correct me if I'm wrong here.

Regards

Markus


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Lock for table renaming
Date: 2006-12-06 15:19:37
Message-ID: 26025.1165418377@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> Why does renaming a table take out an access exclusive lock on the target
> table?

To prevent other transactions failing because the table "disappears"
underneath them.

regards, tom lane