Re: WIP patch: Improve relation size functions such as pg_relation_size() to avoid producing an error when called against a no longer visible relation

Lists: pgsql-hackers
From: Phil Sorber <phil(at)omniti(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: OmniTI DBA <dba(at)omniti(dot)com>
Subject: WIP patch: Improve relation size functions such as pg_relation_size() to avoid producing an error when called against a no longer visible relation
Date: 2011-12-17 20:52:29
Message-ID: CADAkt-h9YgxeckZi9TmaU_BOOhunP9QodkR0yTPwA7A=npSO+A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Attached is a patch that addresses the todo item "Improve relation
size functions such as pg_relation_size() to avoid producing an error
when called against a no longer visible relation."

http://archives.postgresql.org/pgsql-general/2010-10/msg00332.php

Instead of returning an error, they now return NULL if the OID is
found in pg_class when using SnapshotAny. I only applied it to 4
functions: select pg_relation_size, pg_total_relation_size,
pg_table_size and pg_indexes_size. These are the ones that were using
relation_open(). I changed them to using try_relation_open(). For
three of them I had to move the try_relation_open() call up one level
in the call stack and change the parameter types for some support
functions from Oid to Relation. They now also call a new function
relation_recently_dead() which is what checks for relation in
SnapshotAny. All regression tests passed.

Is SnapshotAny the snapshot I should be using? It seems to get the
correct results. I can drop a table and I get NULL. Then after a
vacuumdb it returns an error.

Attachment Content-Type Size
improve_relation_size_functions.patch text/x-patch 6.7 KB

From: Phil Sorber <phil(at)omniti(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: OmniTI DBA <dba(at)omniti(dot)com>
Subject: Re: WIP patch: Improve relation size functions such as pg_relation_size() to avoid producing an error when called against a no longer visible relation
Date: 2011-12-19 18:27:06
Message-ID: CADAkt-hka60SsdgmXAzPb0kFcoE4skvFginjXyKz_2iZ9viQ3Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Dec 17, 2011 at 3:52 PM, Phil Sorber <phil(at)omniti(dot)com> wrote:
> Attached is a patch that addresses the todo item "Improve relation
> size functions such as pg_relation_size() to avoid producing an error
> when called against a no longer visible relation."
>
> http://archives.postgresql.org/pgsql-general/2010-10/msg00332.php
>
> Instead of returning an error, they now return NULL if the OID is
> found in pg_class when using SnapshotAny. I only applied it to 4
> functions: select pg_relation_size, pg_total_relation_size,
> pg_table_size and pg_indexes_size. These are the ones that were using
> relation_open(). I changed them to using try_relation_open(). For
> three of them I had to move the try_relation_open() call up one level
> in the call stack and change the parameter types for some support
> functions from Oid to Relation. They now also call a new function
> relation_recently_dead() which is what checks for relation in
> SnapshotAny. All regression tests passed.
>
> Is SnapshotAny the snapshot I should be using? It seems to get the
> correct results. I can drop a table and I get NULL. Then after a
> vacuumdb it returns an error.

Something I'd like to point out myself is that I need to be using
ObjectIdAttributeNumber instead of Anum_pg_class_relfilenode. Probably
just luck that I got the intended results before. This will also
change the logic in a few other places.

Still not clear on the semantics of Snapshot{Any|Self|Now|Dirty}.


From: Phil Sorber <phil(at)omniti(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: OmniTI DBA <dba(at)omniti(dot)com>
Subject: Re: WIP patch: Improve relation size functions such as pg_relation_size() to avoid producing an error when called against a no longer visible relation
Date: 2011-12-20 02:31:10
Message-ID: CADAkt-g4cujTGmiz0wzS7DNJi1UasDNcD0ZFoz9xHyfbR34pMQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Dec 19, 2011 at 1:27 PM, Phil Sorber <phil(at)omniti(dot)com> wrote:
> On Sat, Dec 17, 2011 at 3:52 PM, Phil Sorber <phil(at)omniti(dot)com> wrote:
>> Attached is a patch that addresses the todo item "Improve relation
>> size functions such as pg_relation_size() to avoid producing an error
>> when called against a no longer visible relation."
>>
>> http://archives.postgresql.org/pgsql-general/2010-10/msg00332.php
>>
>> Instead of returning an error, they now return NULL if the OID is
>> found in pg_class when using SnapshotAny. I only applied it to 4
>> functions: select pg_relation_size, pg_total_relation_size,
>> pg_table_size and pg_indexes_size. These are the ones that were using
>> relation_open(). I changed them to using try_relation_open(). For
>> three of them I had to move the try_relation_open() call up one level
>> in the call stack and change the parameter types for some support
>> functions from Oid to Relation. They now also call a new function
>> relation_recently_dead() which is what checks for relation in
>> SnapshotAny. All regression tests passed.
>>
>> Is SnapshotAny the snapshot I should be using? It seems to get the
>> correct results. I can drop a table and I get NULL. Then after a
>> vacuumdb it returns an error.
>
> Something I'd like to point out myself is that I need to be using
> ObjectIdAttributeNumber instead of Anum_pg_class_relfilenode. Probably
> just luck that I got the intended results before. This will also
> change the logic in a few other places.
>
> Still not clear on the semantics of Snapshot{Any|Self|Now|Dirty}.

I've attached the updated version of my patch with the changes
mentioned in the previous email.

Attachment Content-Type Size
improve_relation_size_functions_v2.patch text/x-patch 6.9 KB

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Phil Sorber <phil(at)omniti(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org, OmniTI DBA <dba(at)omniti(dot)com>
Subject: Re: WIP patch: Improve relation size functions such as pg_relation_size() to avoid producing an error when called against a no longer visible relation
Date: 2011-12-22 18:20:50
Message-ID: CA+TgmobRunPJcPhF_gtXMxKMHGaUocwsXzW89_CVWKSjeVmh6A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, Dec 17, 2011 at 3:52 PM, Phil Sorber <phil(at)omniti(dot)com> wrote:
> Is SnapshotAny the snapshot I should be using? It seems to get the
> correct results. I can drop a table and I get NULL. Then after a
> vacuumdb it returns an error.

The suggestion on the original thread was to use SnapshotDirty or the
caller's MVCC snapshot. SnapshotAny doesn't seem like a good idea.
We don't want to include rows from, say, transactions that have rolled
back. And SnapshotAny rows can stick around for a long time - weeks,
months. If a table is only occasionally updated, autovacuum may not
process it for a long time.

On the other hand, I think using SnapshotDirty is going to work out so
well: isn't there a race condition? The caller's MVCC snapshot seems
better, but I still see pitfalls. Who is to say that the immediate
caller has the same snapshot as the scan? I'm thinking of cases
involving things like CTEs and nested function calls.

I'm wondering if we oughta just return NULL and be done with it. If
SELECT select 1241241241::regclass doesn't choke, I'm not sure select
pg_relation_size(1241241241) ought to either. It's not like the user
is going to see NULL and have no clue that the relation wasn't found.
At the very worst they might think it's empty.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Phil Sorber <phil(at)omniti(dot)com>, pgsql-hackers(at)postgresql(dot)org, OmniTI DBA <dba(at)omniti(dot)com>
Subject: Re: WIP patch: Improve relation size functions such as pg_relation_size() to avoid producing an error when called against a no longer visible relation
Date: 2011-12-22 18:33:53
Message-ID: 17463.1324578833@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> I'm wondering if we oughta just return NULL and be done with it.

+1. There are multiple precedents for that sort of response, which we
introduced exactly so that "SELECT some_function(oid) FROM some_catalog"
wouldn't fail just because one of the rows had gotten deleted by the
time the scan got to it. I don't think it's necessary for the
relation-size functions to be any smarter. Indeed, I'd assumed that's
all that Phil's patch did, since I'd not looked closer till just now.

regards, tom lane


From: Phil Sorber <phil(at)omniti(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org, OmniTI DBA <dba(at)omniti(dot)com>
Subject: Re: WIP patch: Improve relation size functions such as pg_relation_size() to avoid producing an error when called against a no longer visible relation
Date: 2011-12-22 19:02:28
Message-ID: CADAkt-gVTUC_xsZyOrL0W3vieESWid7dwuUoEr=xRrMBWoUkhA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Dec 22, 2011 at 1:33 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> I'm wondering if we oughta just return NULL and be done with it.
>
> +1.  There are multiple precedents for that sort of response, which we
> introduced exactly so that "SELECT some_function(oid) FROM some_catalog"
> wouldn't fail just because one of the rows had gotten deleted by the
> time the scan got to it.  I don't think it's necessary for the
> relation-size functions to be any smarter.  Indeed, I'd assumed that's
> all that Phil's patch did, since I'd not looked closer till just now.
>
>                        regards, tom lane

Here it is without the checking for recently dead. If it can't open
the relation it simply returns NULL.

Attachment Content-Type Size
improve_relation_size_functions_v3.patch text/x-patch 5.0 KB

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Phil Sorber <phil(at)omniti(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org, OmniTI DBA <dba(at)omniti(dot)com>
Subject: Re: WIP patch: Improve relation size functions such as pg_relation_size() to avoid producing an error when called against a no longer visible relation
Date: 2011-12-22 20:19:16
Message-ID: CA+TgmoYtHZLLB+yLzm3fnTv2Tvv4AB4+nySJ-3Lm2jW=vUb1qA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Dec 22, 2011 at 2:02 PM, Phil Sorber <phil(at)omniti(dot)com> wrote:
> On Thu, Dec 22, 2011 at 1:33 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>>> I'm wondering if we oughta just return NULL and be done with it.
>>
>> +1.  There are multiple precedents for that sort of response, which we
>> introduced exactly so that "SELECT some_function(oid) FROM some_catalog"
>> wouldn't fail just because one of the rows had gotten deleted by the
>> time the scan got to it.  I don't think it's necessary for the
>> relation-size functions to be any smarter.  Indeed, I'd assumed that's
>> all that Phil's patch did, since I'd not looked closer till just now.
>
> Here it is without the checking for recently dead. If it can't open
> the relation it simply returns NULL.

I think we probably ought to make pg_database_size() and
pg_tablespace_size() behave similarly.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


From: Phil Sorber <phil(at)omniti(dot)com>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org, OmniTI DBA <dba(at)omniti(dot)com>
Subject: Re: WIP patch: Improve relation size functions such as pg_relation_size() to avoid producing an error when called against a no longer visible relation
Date: 2011-12-23 00:01:21
Message-ID: CADAkt-gwO3YLu08SBzNeDBjAJcZUY_scCb7wXuRVuuQkRT_g1w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Dec 22, 2011 at 3:19 PM, Robert Haas <robertmhaas(at)gmail(dot)com> wrote:
> On Thu, Dec 22, 2011 at 2:02 PM, Phil Sorber <phil(at)omniti(dot)com> wrote:
>> On Thu, Dec 22, 2011 at 1:33 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>>> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>>>> I'm wondering if we oughta just return NULL and be done with it.
>>>
>>> +1.  There are multiple precedents for that sort of response, which we
>>> introduced exactly so that "SELECT some_function(oid) FROM some_catalog"
>>> wouldn't fail just because one of the rows had gotten deleted by the
>>> time the scan got to it.  I don't think it's necessary for the
>>> relation-size functions to be any smarter.  Indeed, I'd assumed that's
>>> all that Phil's patch did, since I'd not looked closer till just now.
>>
>> Here it is without the checking for recently dead. If it can't open
>> the relation it simply returns NULL.
>
> I think we probably ought to make pg_database_size() and
> pg_tablespace_size() behave similarly.
>
> --
> Robert Haas
> EnterpriseDB: http://www.enterprisedb.com
> The Enterprise PostgreSQL Company

Changes added.

Attachment Content-Type Size
improve_relation_size_functions_v4.patch text/x-patch 7.7 KB

From: Heikki Linnakangas <heikki(dot)linnakangas(at)enterprisedb(dot)com>
To: Phil Sorber <phil(at)omniti(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org, OmniTI DBA <dba(at)omniti(dot)com>
Subject: Re: WIP patch: Improve relation size functions such as pg_relation_size() to avoid producing an error when called against a no longer visible relation
Date: 2012-01-19 11:19:11
Message-ID: 4F17FC2F.2000405@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 23.12.2011 02:01, Phil Sorber wrote:
> On Thu, Dec 22, 2011 at 3:19 PM, Robert Haas<robertmhaas(at)gmail(dot)com> wrote:
>> On Thu, Dec 22, 2011 at 2:02 PM, Phil Sorber<phil(at)omniti(dot)com> wrote:
>>> On Thu, Dec 22, 2011 at 1:33 PM, Tom Lane<tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>>>> Robert Haas<robertmhaas(at)gmail(dot)com> writes:
>>>>> I'm wondering if we oughta just return NULL and be done with it.
>>>>
>>>> +1. There are multiple precedents for that sort of response, which we
>>>> introduced exactly so that "SELECT some_function(oid) FROM some_catalog"
>>>> wouldn't fail just because one of the rows had gotten deleted by the
>>>> time the scan got to it. I don't think it's necessary for the
>>>> relation-size functions to be any smarter. Indeed, I'd assumed that's
>>>> all that Phil's patch did, since I'd not looked closer till just now.
>>>
>>> Here it is without the checking for recently dead. If it can't open
>>> the relation it simply returns NULL.
>>
>> I think we probably ought to make pg_database_size() and
>> pg_tablespace_size() behave similarly.
>
> Changes added.

Looks good to me, committed. I added a sentence to the docs mentioning
the new behavior, and also a code comment to explain why returning NULL
is better than throwing an error.

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com