Re: Index/trigger implementation for accessing latest records - Mailing list pgsql-general

From Gavin Flower
Subject Re: Index/trigger implementation for accessing latest records
Date
Msg-id a167c537-52a7-63de-6a93-32bae794f8d7@archidevsys.co.nz
Whole thread Raw
In response to Index/trigger implementation for accessing latest records  (Alastair McKinley <a.mckinley@analyticsengines.com>)
List 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


pgsql-general by date:

Previous
From: Alastair McKinley
Date:
Subject: Index/trigger implementation for accessing latest records
Next
From: Alvaro Herrera
Date:
Subject: Re: Index/trigger implementation for accessing latest records