Re: Index/trigger implementation for accessing latest records

From: Gavin Flower <GavinFlower(at)archidevsys(dot)co(dot)nz>
To: Alastair McKinley <a(dot)mckinley(at)analyticsengines(dot)com>, "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Index/trigger implementation for accessing latest records
Date: 2018-05-02 10:36:12
Message-ID: a167c537-52a7-63de-6a93-32bae794f8d7@archidevsys.co.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Alastair,

See embedded comments.

On 02/05/18 21:51, Alastair McKinley wrote:
>
> Hi,
>
>
> I have a table that stores a location identifier per person which will
> be appended to many times.
>
> However, for many queries in this system we only need to know the most
> recent location per person, which is limited to about 1000 records.
>
>
> Is the following trigger/index strategy a reasonable and safe approach
> to fast access to the latest location records per person?
>
>
> 1. A boolean column (latest_record default true) to identify the
> latest record per person
> 2. A before insert trigger that updates all other records for that
> person to latest_record = false
> 3. A partial index on the latest_record column where latest_record is
> true
>
Suggest simplest and fastest is to use timestamptz, a timestamp with
time zone (copes with changes of daylight saving and different
timezones. The you have no need of triggers.

Then all you need to do, is search for the person-id with the maximum
value of the timestampz!

[...]

Cheers,
Gavin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2018-05-02 20:32:36 Re: Index/trigger implementation for accessing latest records
Previous Message Alastair McKinley 2018-05-02 09:51:49 Index/trigger implementation for accessing latest records