Re: Improving worst-case merge join performance with often-null foreign key - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Improving worst-case merge join performance with often-null foreign key
Date
Msg-id 3677442.1682176872@sss.pgh.pa.us
Whole thread Raw
In response to Improving worst-case merge join performance with often-null foreign key  (Steinar Kaldager <steinar.kaldager@oda.com>)
Responses Re: Improving worst-case merge join performance with often-null foreign key
List pgsql-hackers
Steinar Kaldager <steinar.kaldager@oda.com> writes:
> First-time potential contributor here. We recently had an incident due
> to a sudden 1000x slowdown of a Postgres query (from ~10ms to ~10s)
> due to a join with a foreign key that was often null. We found that it
> was caused by a merge join with an index scan on one join path --
> whenever the non-null data happened to be such that the merge join
> couldn't be terminated early, the index would proceed to scan all of
> the null rows and filter each one out individually. Since this was an
> inner join, this was pointless; the nulls would never have matched the
> join clause anyway.

Hmm.  I don't entirely understand why the existing stop-at-nulls logic
in nodeMergejoin.c didn't fix this for you.  Maybe somebody has broken
that?  See the commentary for MJEvalOuterValues/MJEvalInnerValues.

Pushing down an IS NOT NULL restriction could possibly be of value
if the join is being done in the nulls-first direction, but that's
an extreme minority use-case.  I'm dubious that it'd be worth the
overhead in general.  It'd probably be more useful to make sure that
the planner's cost model is aware of this effect, so that it's prodded
to use nulls-last not nulls-first sort order when there are enough
nulls to make a difference.

            regards, tom lane



pgsql-hackers by date:

Previous
From: Gurjeet Singh
Date:
Subject: Re: [PATCH] Infinite loop while acquiring new TOAST Oid
Next
From: Tom Lane
Date:
Subject: Re: run pgindent on a regular basis / scripted manner