Re: Finding latest record for a number of groups in an INSERT-only table

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Daniel Farina <daniel(at)heroku(dot)com>
Cc: David Johnston <polobo(at)yahoo(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Finding latest record for a number of groups in an INSERT-only table
Date: 2011-07-05 16:49:51
Message-ID: 9AC6F5A9-35DF-442D-87F0-D16AFE7F8148@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 5 Jul 2011, at 9:13, Daniel Farina wrote:

>> Setup a materialized view.
>
> This rather defeats the point AFAIK, because keeping the materialized
> view up to date (being more than thirty seconds out of date is not
> desirable) will be expensive. Maintaining the index on the (key,
> recency) pair is, in effect, a materialization of sorts already.

Except that you can't query an index directly and you can't create an index that only contains the latest rows without marking them as such, which is practically what a materialized view does.

Your concerns about the overhead of the materialized view seem rather pessimistic. The materialization adds some overhead to every insert/update/delete to test whether the current record in the materialized view should be replaced or kept, but since it's a much smaller table, that should be quite fast.

It's basically just a set of insert/update/delete triggers that fire once for every row, or once for every statement (that can be more efficient with insert-heavy tables, as long as you group your inserts into a single statement).

Whether you can handle the extra overhead depends on your insert-load.

You could also go with an intermediate solution, where you cache the latest record per key in the new inserts in a batch and send those to a different table once the batch finishes processing. The insert-load on that table will be much lower, so there will be more time to execute triggers of some kind to update the materialized view. Of course, you also introduce some lag there with respect to the visibility of those rows.

> In any case, as I was saying: there are terrible workarounds for this,
> but I think this is a rather common problem with INSERT-mostly
> relations that effectively want row-versioning of a sort, so I was
> hoping that lucid solutions to this issue have grown since 2008, when
> the thread I linked to transpired.

You really only want to know that a row is the latest in a group of similar rows. With MVCC, you can only really achieve that by updating the latest row and the last latest row to reflect their status (latest or not). That means you do two DELETE and INSERT operations every time you want to update what the latest row is.

That's hardly different from using triggers anyway.

The alternatives are the various SELECT queries that you've already seen.

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.

!DSPAM:737,4e1340c712095745717687!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message c k 2011-07-05 17:21:28 Python UCS4 error
Previous Message Alban Hertroys 2011-07-05 16:28:51 Re: Read MS-SQL data into Postgres via ODBC link?