Re: min()/max() with BRIN indexes

From: Wayne <lists-pgsql(at)useunix(dot)net>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Re: min()/max() with BRIN indexes
Date: 2020-02-29 21:40:58
Message-ID: 20200229214058.GE20190@ln-1.useunix.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, Feb 29, 2020 at 02:37:15PM -0500, Tom Lane wrote:
> Wayne <lists-pgsql(at)useunix(dot)net> writes:
> > I have rather large tables that use a time stamp as an index. New entries
> > are continuously added to the table with the current time. If I convert
> > from BTREE to BRIN indexes and select records with specific date ranges
> > the BRIN is used and performance is acceptable. However I often want to
> > get the latest time stamp using the max() function. I didn't expect that
> > this would result in a sequential scan of the table and skip the BRIN
> > index.
>
> > Is this expected behavior?
>
> Yeah. In principle a BRIN index could be used to accelerate finding min
> or max, but there's no actual support for that at the moment ... and in
> any case, it'd still be substantially slower than the equivalent with
> a btree index, which can locate the extremal values immediately.
>
> For this particular case, you might be able to fake it with something like
>
> select max(ts) from mytab where ts > 'some cutoff'
>
> if you can estimate some not-too-far-before-current-time cutoff
> that you are sure you'll find some records after.
>
> regards, tom lane
>

Thanks Tom,

I kind of "discovered" the 'some cutoff' trick prior to my posting but
neglected to mention it as I couldn't figure out why it worked but
max(ts) by itself wouldn't.

Agreed, it would be substantially slower than a btree index but much
faster than a seq scan of the table. In this use case they are monthly
tables typically >= 130gig. The btree index is typically >20 gig while
the corresponding brin is ~ 2meg. For all other use cases on these
tables the brin index is a great space vs performance compromise.

For now I can get by with the 'some cutoff' estimate but I hope adding
min()/max() to brin indexes on the wish list.

Thanks again,
Wayne

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Karen Goh 2020-03-01 10:17:48 What is the right syntax for retrieving the last_insert_id() in Postgresql ?
Previous Message Tom Lane 2020-02-29 19:37:15 Re: min()/max() with BRIN indexes