9.3 Pre-proposal: Range Merge Join

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: 9.3 Pre-proposal: Range Merge Join
Date: 2012-04-16 05:40:50
Message-ID: 1334554850.10878.52.camel@jdavis
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I hope this is not an inappropriate time for 9.3 discussions. The flip
side of asking for submissions in the first couple commitfests means
that I need to submit proposals now.

What is a Range Join?

See attached SQL for example. The key difference is that the join
condition is not equality, but overlaps (&&).

Problem statement: slow. Nested loops are the only option, although they
can benefit from an inner GiST index if available. But if the join is
happening up in the plan tree somewhere, then it's impossible for any
index to be available.

Proposed solution: a modified merge join that can handle ranges.

1. Order the ranges on both sides by the lower bound, then upper bound.
Empty ranges can be excluded entirely.
2. Left := first range on left, Right := first range on right
3. If Left or Right is empty, terminate.
4. If lower(Left) > upper(Right), discard Right, goto 2
5. If lower(Right) > upper(Left), discard Left, goto 2
6. return (Left, Right) as joined tuple
7. Right := next range on right
8. goto 3

If we get step 4 or step 5 keeps getting triggered, and a btree index is
available (ordered by lower bound), we can re-probe to go to the correct
position, and consider that the new top range on that side. This is an
optimization for the case where there are large numbers of ranges with
no match on the other side.

Thanks to Nathan Boley for helping me devise this algorithm. However,
any bugs are mine alone ;)

Weaknesses: I haven't thought through the optimization, but I suspect it
will be hard to be very accurate in the costing. That might be OK,
because there aren't very many options anyway, but I'll need to think it
through.

Questions:

* Is this idea sane? -- that is, are ranges important enough that
people are willing to maintain a new operator?
* The more general problem might be "spatial joins" which can operate
in N dimensions, and I doubt this would work very well in that case.
Does someone know of a spatial join algorithm (without IP claims) that
would be as good as this one for ranges?
* Other thoughts?

Regards,
Jeff Davis

Attachment Content-Type Size
rangejoin.sql text/x-sql 957 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Darren Duncan 2012-04-16 06:18:24 Re: 9.3 Pre-proposal: Range Merge Join
Previous Message Etsuro Fujita 2012-04-16 05:36:04 Re: typo fix