Re: Setting Statistics on Functional Indexes - Mailing list pgsql-performance

From Tom Lane
Subject Re: Setting Statistics on Functional Indexes
Date
Msg-id 6668.1351105908@sss.pgh.pa.us
Whole thread Raw
In response to Setting Statistics on Functional Indexes  (Shaun Thomas <sthomas@optionshouse.com>)
Responses Re: Setting Statistics on Functional Indexes
List pgsql-performance
Shaun Thomas <sthomas@optionshouse.com> writes:
> 1. Is there any way to specifically set stats on a functional index?

Sure, the same way you would for a table.

regression=# create table foo (f1 int, f2 int);
CREATE TABLE
regression=# create index fooi on foo ((f1 + f2));
CREATE INDEX
regression=# \d fooi
      Index "public.fooi"
 Column |  Type   | Definition
--------+---------+------------
 expr   | integer | (f1 + f2)
btree, for table "public.foo"

regression=# alter index fooi alter column expr set statistics 5000;
ALTER INDEX

The weak spot in this, and the reason this isn't "officially" supported,
is that the column name for an index expression isn't set in stone.
But as long as you check what it's called you can set its target.

> 2. Why is the planner so ridiculously optimistic with functional
> indexes, even in the case of much higher selectivity as reported by
> pg_stats on the named columns?

It's not particularly (not that you've even defined what you think
"optimistic" is, much less mentioned what baseline you're comparing to).
I tried your example on HEAD and I got what seemed pretty decent
rowcount estimates ...

            regards, tom lane


pgsql-performance by date:

Previous
From: Böckler Andreas
Date:
Subject: Re: Query-Planer from 6seconds TO DAYS
Next
From: Shaun Thomas
Date:
Subject: Re: Setting Statistics on Functional Indexes