Re: [GENERAL] Queries joining views - Mailing list pgsql-hackers

From Tom Lane
Subject Re: [GENERAL] Queries joining views
Date
Msg-id 10968.1156275984@sss.pgh.pa.us
Whole thread Raw
Responses Re: [GENERAL] Queries joining views  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: [GENERAL] Queries joining views  (Alvaro Herrera <alvherre@commandprompt.com>)
Re: [GENERAL] Queries joining views  (Alban Hertroys <alban@magproductions.nl>)
List pgsql-hackers
Alban Hertroys <alban@magproductions.nl> writes:
>> I'm confused too.  Would it be possible for you to send me a dump of
>> your database?

> Attached is a cleaned out database, the full schema is included, but
> only the relevant tables contain any data.

Thanks.  After digging through it a bit, I understand what's happening,
but I'm not seeing any simple fix.  The problem is basically that
you've got

create or replace view mm_product as
 SELECT ... FROM mm_product_table JOIN mm_object mmop USING (number);

and then the problem query has WHERE mm_product.number = insrel.snumber
which causes the planner to conclude that mm_product_table.number,
mm_object.number, and mm_insrel_table.snumber are all basically
interchangeable.  In particular it ends up performing the join between
mm_product_table.number and mm_object.number as though
mm_product_table.number were being joined to mm_insrel_table.snumber.
Which is fine, except that it's thinking that the statistics for
mm_object.number are applicable in this context, and they're completely
misleading.  After the join to mm_insrel_table, the statistics of the
variable are really like mm_insrel_table.number --- in particular the
fraction of the table that has to be visited is much larger than it
would've been for mm_object as a whole.

This is a problem we've understood in a generic form for awhile:
a join or selection might change the statistics of a variable,
and so the info stored in the catalogs ought to be modified somehow
to predict what will happen at upper join levels.  We've not seen
it in this particular form before, though.

I'm not sure if there's a whole lot you can do about it in the near term
other than refactor your schema to avoid having different tables joining
to different subranges of mm_object.number.  (You don't necessarily have
to get rid of mm_object --- just try assigning its keys from a serial,
or something, so that there's no correlation to the ranges of keys in
other tables.)

We might be able to do something about actually solving the statistical
problem in 8.3, but I fear it's too late to think about it for 8.2.

            regards, tom lane

pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: [PATCHES] COPY view
Next
From: Tom Lane
Date:
Subject: Re: [GENERAL] Queries joining views