Re: [PERFORM] Incr/Decr Integer

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: William Scott Jordan <wsjordan(at)brownpapertickets(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: [PERFORM] Incr/Decr Integer
Date: 2009-07-20 00:26:21
Message-ID: 20090720002621.GG4938@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-performance

William Scott Jordan wrote:
> Hi Andrew,
>
> That's a very good guess. We are in fact updating this table multiple
> times within the same triggered function, which is being called on an
> INSERT. Essentially, we're using this to keep a running total of the
> number of rows being held in another table.

This is the worst way to go about keeping running totals; it would be
far better to have a table holding a "last aggregated value" and deltas
from that; to figure out the current value of the counter, add the last
value plus/minus the deltas (I figure you'd normally have one +1 for
each insert and one -1 for each delete; update is an exercise to the
reader). You have another process that runs periodically and groups the
deltas to generate an up-to-date "last aggregated value", deleting the
deltas.

This way you should have little deadlock problems if any, because no
transaction needs to wait for another one to update the counter.

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Chris 2009-07-20 00:29:41 Re: PostgreSQL Databse Migration to the Latest Version and Help for Database Replication.
Previous Message Chris 2009-07-20 00:21:44 Re: Should I CLUSTER on PRIMARY KEY

Browse pgsql-performance by date

  From Date Subject
Next Message Robert James 2009-07-20 01:46:53 Fastest char datatype
Previous Message Robert James 2009-07-19 23:03:09 Can Postgres use an INDEX over an OR?