Re: help with design of the 'COUNT(*) in separate table schema

From: Peter Geoghegan <peter(dot)geoghegan86(at)gmail(dot)com>
To: Dennis Gearon <gearond(at)sbcglobal(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: help with design of the 'COUNT(*) in separate table schema
Date: 2010-10-20 23:31:18
Message-ID: AANLkTi=57n4GL4Btkr7JnyCDLnG62K1Ye5uBc9SMBsiE@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 20 October 2010 23:52, Dennis Gearon <gearond(at)sbcglobal(dot)net> wrote:
> Regarding the previously discussed triggers on tables to keep track of count:
> http://www.varlena.com/GeneralBits/120.php
> http://www.varlena.com/GeneralBits/49.php
> <from article>
> CREATE OR REPLACE FUNCTION count_rows()
> RETURNS TRIGGER AS
> '
>   BEGIN
>      IF TG_OP = ''INSERT'' THEN
>         UPDATE rowcount
>            SET total_rows = total_rows + 1
>            WHERE table_name = TG_RELNAME;
>      ELSIF TG_OP = ''DELETE'' THEN
>         UPDATE rowcount
>            SET total_rows = total_rows - 1
>            WHERE table_name = TG_RELNAME;
>      END IF;
>      RETURN NULL;
>   END;
> ' LANGUAGE plpgsql;
> </from article>
>
> Wouldn't something like this need row-locking (SELECT for UPDATE) in order to serialize the execution of all triggers?
>

The update will acquire a row level lock on rowcount for the
TG_RELNAME tuple without you doing anything else.

--
Regards,
Peter Geoghegan

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rich Shepard 2010-10-20 23:35:47 Re: Cannot Start Postgres After System Boot
Previous Message Bruce Momjian 2010-10-20 23:26:39 Re: queriing the version of libpq