Re: pg_relation_size locking - Mailing list pgsql-hackers

From Tom Lane
Subject Re: pg_relation_size locking
Date
Msg-id 3355.1134409907@sss.pgh.pa.us
Whole thread Raw
In response to Re: pg_relation_size locking  (Andreas Pflug <pgadmin@pse-consulting.de>)
Responses Re: pg_relation_size locking  (Andreas Pflug <pgadmin@pse-consulting.de>)
List pgsql-hackers
Andreas Pflug <pgadmin@pse-consulting.de> writes:
> Tom Lane wrote:
>> You have to have a lock to ensure that the table even exists, let alone
>> that you are looking at the right set of disk files.

> This would require a lock on pg_class, not table foo, no?

No, the convention is that you take a lock on the relation you're
interested in.  The fact that some of the information you care about is
in pg_class is incidental.  There is actually stuff going on behind
the scenes to make sure that you get up-to-date info when you do
LockRelation; looking at the pg_class row does *not* by itself guarantee
that.  That is, when you SearchSysCache you might get a row that was
good at the start of your transaction but no longer is; relation_open
with a lock guarantees that you get a relation descriptor that is
currently correct.

> Hm, I see the issue. Interesting enough, I *do* see the size growing. 
> OTOH, when running BEGIN;TRUNCATE against a test table and retrieving 
> pg_relation_size returns the previous relfilenode and size as expected.

That's a bit curious.  If they just did TRUNCATE then COPY, the commit
of the TRUNCATE should have released the lock.  If the TRUNCATE wasn't
committed yet, then how are you able to pick up the correct relfilenode
to look at?
        regards, tom lane


pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: Reducing relation locking overhead
Next
From: Stephan Szabo
Date:
Subject: Re: Foreign key trigger timing bug?