Re: Is it nonsense (read: stupid) to keep count of child entries via triggers and a custom table?

From: Craig Ringer <ringerc(at)ringerc(dot)id(dot)au>
To: Seref Arikan <serefarikan(at)kurumsalteknoloji(dot)com>
Cc: PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Is it nonsense (read: stupid) to keep count of child entries via triggers and a custom table?
Date: 2012-08-28 13:14:33
Message-ID: 503CC439.1060507@ringerc.id.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 08/28/2012 08:56 PM, Seref Arikan wrote:
> Can I simply adopt the naive approach of updating an EHR metadata table
> within a transaction in every partition addition/deletion operation?

Absolutely. That's a classic trade-off; pay the cost of maintaining a
materialized view at INSERT/UPDATE/DELETE time, in exchange for faster
access in frequent queries that're otherwise unacceptably expensive.

It *is* a trade-off, like any performance choice. Careful work is also
required to handle concurrency issues correctly.

I do the same thing in much smaller (tiny, even) databases where I have
expensive queries I want to respond before the user noticed they were
waiting. For example, in a parent->child relationship I sometimes
maintain a summary table with a 1:1 relationship with the parent that
summarizes the children.

It's usually a good idea to keep your summary tables clearly separate as
trigger-maintained materialized views, rather than updating "real"
entities with summary info too. You avoid churn on your "real" tables,
avoid some interesting lock ordering issues, etc.

Some explicit locking with `SELECT ... FOR UPDATE` can be important to
avoid unexpected concurrency issues.

--
Craig Ringer

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Seref Arikan 2012-08-28 13:18:54 Re: Is it nonsense (read: stupid) to keep count of child entries via triggers and a custom table?
Previous Message Craig Ringer 2012-08-28 13:04:58 Re: 9.2 and index only scans