Re: Small TRUNCATE glitch

Lists: pgsql-hackers
From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Subject: Small TRUNCATE glitch
Date: 2008-04-03 15:58:11
Message-ID: 1007.1207238291@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Just noticed that TRUNCATE fails to clear the stats collector's counts
for the table. I am not sure if it should reset the event counts or
not (any thoughts?) but surely it is wrong to not zero the live/dead
tuple counts.

regards, tom lane


From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: Small TRUNCATE glitch
Date: 2008-04-03 17:02:34
Message-ID: 20080403170234.GC22851@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Apr 03, 2008 at 11:58:11AM -0400, Tom Lane wrote:
> Just noticed that TRUNCATE fails to clear the stats collector's counts
> for the table. I am not sure if it should reset the event counts or
> not (any thoughts?) but surely it is wrong to not zero the live/dead
> tuple counts.

Wern't there complaints from people regularly truncating and
refilling tables getting bad plans because they lost the statistics?

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: Small TRUNCATE glitch
Date: 2008-04-03 17:07:44
Message-ID: 4121.1207242464@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Martijn van Oosterhout <kleptog(at)svana(dot)org> writes:
> On Thu, Apr 03, 2008 at 11:58:11AM -0400, Tom Lane wrote:
>> Just noticed that TRUNCATE fails to clear the stats collector's counts
>> for the table. I am not sure if it should reset the event counts or
>> not (any thoughts?) but surely it is wrong to not zero the live/dead
>> tuple counts.

> Wern't there complaints from people regularly truncating and
> refilling tables getting bad plans because they lost the statistics?

Not related --- the planner doesn't look at pgstats data.

regards, tom lane


From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: Small TRUNCATE glitch
Date: 2008-04-03 22:13:53
Message-ID: 20080403221353.GP2528@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom Lane wrote:
> Just noticed that TRUNCATE fails to clear the stats collector's counts
> for the table. I am not sure if it should reset the event counts or
> not (any thoughts?) but surely it is wrong to not zero the live/dead
> tuple counts.

Agreed, the live/dead counters should be reset. Regarding event counts,
my take is that we should have a separate statement count for truncate
(obviously not a tuple count), and the others should be left alone.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: Small TRUNCATE glitch
Date: 2008-04-03 22:23:39
Message-ID: 25100.1207261419@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> Tom Lane wrote:
>> Just noticed that TRUNCATE fails to clear the stats collector's counts
>> for the table. I am not sure if it should reset the event counts or
>> not (any thoughts?) but surely it is wrong to not zero the live/dead
>> tuple counts.

> Agreed, the live/dead counters should be reset. Regarding event counts,
> my take is that we should have a separate statement count for truncate
> (obviously not a tuple count), and the others should be left alone.

I thought some more about how to do it, and stumbled over how to cope
with TRUNCATE being rolled back. That nixed my first idea of just
having TRUNCATE send a zero-the-counters-now message.

regards, tom lane


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Small TRUNCATE glitch
Date: 2008-05-10 00:33:41
Message-ID: 200805100033.m4A0Xf318007@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Added to TODO:

o Clear table counters on TRUNCATE

http://archives.postgresql.org/pgsql-hackers/2008-04/msg00169.php

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

Tom Lane wrote:
> Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> > Tom Lane wrote:
> >> Just noticed that TRUNCATE fails to clear the stats collector's counts
> >> for the table. I am not sure if it should reset the event counts or
> >> not (any thoughts?) but surely it is wrong to not zero the live/dead
> >> tuple counts.
>
> > Agreed, the live/dead counters should be reset. Regarding event counts,
> > my take is that we should have a separate statement count for truncate
> > (obviously not a tuple count), and the others should be left alone.
>
> I thought some more about how to do it, and stumbled over how to cope
> with TRUNCATE being rolled back. That nixed my first idea of just
> having TRUNCATE send a zero-the-counters-now message.
>
> regards, tom lane
>
> --
> 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. +


From: Alex Shulgin <ash(at)commandprompt(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Alvaro Herrera <alvherre(at)commandprompt(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Small TRUNCATE glitch
Date: 2014-12-09 14:17:56
Message-ID: 87mw6w29gr.fsf@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Bruce Momjian <bruce(at)momjian(dot)us> writes:
>
> Added to TODO:
>
> o Clear table counters on TRUNCATE
>
> http://archives.postgresql.org/pgsql-hackers/2008-04/msg00169.php

Hello,

Attached is a WIP patch for this TODO.

Attachment Content-Type Size
0001-WIP-track-TRUNCATEs-in-pgstat-transaction-stats.patch text/x-diff 9.6 KB
unknown_filename text/plain 1.8 KB

From: Alex Shulgin <ash(at)commandprompt(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Alvaro Herrera <alvherre(at)commandprompt(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Small TRUNCATE glitch
Date: 2014-12-09 21:32:10
Message-ID: 87iohk1pd1.fsf@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


Alex Shulgin <ash(at)commandprompt(dot)com> writes:
>
> Bruce Momjian <bruce(at)momjian(dot)us> writes:
>>
>> Added to TODO:
>>
>> o Clear table counters on TRUNCATE
>>
>> http://archives.postgresql.org/pgsql-hackers/2008-04/msg00169.php
>
> Hello,
>
> Attached is a WIP patch for this TODO.

This part went as an attachment, which wasn't my intent:
========================================================

It does the trick by tracking if a TRUNCATE command was issued under a
(sub)transaction and uses this knowledge to reset the live/dead tuple
counters later if the transaction was committed. Testing in simple
cases shows that this clears the counters correctly, including use of
savepoints.

The 2PC part requires extending bool flag to fit the trunc flag, is this
approach sane? Given that 2PC transaction should survive server
restart, it's reasonable to expect it to also survive the upgrade, so I
see no clean way of adding another bool field to the
TwoPhasePgStatRecord struct (unless some would like to add checks on
record length, etc.).

I'm going to add some regression tests, but not sure what would be the
best location for this. The truncate.sql seems like natural choice, but
stats are not updating realtime, so I'd need to borrow some tricks from
stats.sql or better put the new tests in the stats.sql itself?

--
Alex


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Alex Shulgin <ash(at)commandprompt(dot)com>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Small TRUNCATE glitch
Date: 2014-12-10 01:04:05
Message-ID: 20141210010405.GU1768@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Alex Shulgin wrote:

> The 2PC part requires extending bool flag to fit the trunc flag, is this
> approach sane? Given that 2PC transaction should survive server
> restart, it's reasonable to expect it to also survive the upgrade, so I
> see no clean way of adding another bool field to the
> TwoPhasePgStatRecord struct (unless some would like to add checks on
> record length, etc.).

I don't think we need to have 2PC files survive a pg_upgrade. It seems
perfectly okay to remove them from the new cluster at some appropriate
time, *if* they are copied from the old cluster at all (I don't think
they should be.)

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


From: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, Alex Shulgin <ash(at)commandprompt(dot)com>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Small TRUNCATE glitch
Date: 2014-12-10 08:32:42
Message-ID: 5488052A.7090104@vmware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 12/10/2014 03:04 AM, Alvaro Herrera wrote:
> Alex Shulgin wrote:
>
>> The 2PC part requires extending bool flag to fit the trunc flag, is this
>> approach sane? Given that 2PC transaction should survive server
>> restart, it's reasonable to expect it to also survive the upgrade, so I
>> see no clean way of adding another bool field to the
>> TwoPhasePgStatRecord struct (unless some would like to add checks on
>> record length, etc.).
>
> I don't think we need to have 2PC files survive a pg_upgrade. It seems
> perfectly okay to remove them from the new cluster at some appropriate
> time, *if* they are copied from the old cluster at all (I don't think
> they should be.)

I think pg_upgrade should check if there are any prepared transactions
pending, and refuse to upgrade if there are. It could be made to work,
but it's really not worth the trouble. If there are any pending prepared
transactions in the system when you run pg_upgrade, it's more likely to
be a mistake or oversight in the first place, than on purpose.

- Heikki


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
Cc: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, Alex Shulgin <ash(at)commandprompt(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Small TRUNCATE glitch
Date: 2014-12-10 09:58:21
Message-ID: 20141210095821.GC13011@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Dec 10, 2014 at 10:32:42AM +0200, Heikki Linnakangas wrote:
> >I don't think we need to have 2PC files survive a pg_upgrade. It seems
> >perfectly okay to remove them from the new cluster at some appropriate
> >time, *if* they are copied from the old cluster at all (I don't think
> >they should be.)
>
> I think pg_upgrade should check if there are any prepared
> transactions pending, and refuse to upgrade if there are. It could
> be made to work, but it's really not worth the trouble. If there are
> any pending prepared transactions in the system when you run
> pg_upgrade, it's more likely to be a mistake or oversight in the
> first place, than on purpose.

pg_upgrade already checks for prepared transactions and errors out if
they exist; see check_for_prepared_transactions().

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

+ Everyone has their own god. +


From: Alex Shulgin <ash(at)commandprompt(dot)com>
To: Bruce Momjian <bruce(at)momjian(dot)us>
Cc: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Small TRUNCATE glitch
Date: 2014-12-10 14:07:26
Message-ID: 877fxz1tup.fsf@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Bruce Momjian <bruce(at)momjian(dot)us> writes:

> On Wed, Dec 10, 2014 at 10:32:42AM +0200, Heikki Linnakangas wrote:
>> >I don't think we need to have 2PC files survive a pg_upgrade. It seems
>> >perfectly okay to remove them from the new cluster at some appropriate
>> >time, *if* they are copied from the old cluster at all (I don't think
>> >they should be.)
>>
>> I think pg_upgrade should check if there are any prepared
>> transactions pending, and refuse to upgrade if there are. It could
>> be made to work, but it's really not worth the trouble. If there are
>> any pending prepared transactions in the system when you run
>> pg_upgrade, it's more likely to be a mistake or oversight in the
>> first place, than on purpose.
>
> pg_upgrade already checks for prepared transactions and errors out if
> they exist; see check_for_prepared_transactions().

Alright, that's good to know. So I'm just adding a new bool field to
the 2PC pgstat record instead of the bit magic.

Attached is v0.2, now with a regression test included.

--
Alex

Attachment Content-Type Size
truncate-and-pgstat-v0.2.patch text/x-diff 15.0 KB