Re: Which side of a Merge Join gets executed first? Do both sides always get executed?

From: Jerry Brenner <jbrenner(at)guidewire(dot)com>
To: Frédéric Yhuel <frederic(dot)yhuel(at)dalibo(dot)com>
Cc: pgsql-performance(at)lists(dot)postgresql(dot)org
Subject: Re: Which side of a Merge Join gets executed first? Do both sides always get executed?
Date: 2023-12-20 19:04:28
Message-ID: CACoKFYQ8UH+SNuo9JaAmXeg6Z6GJiqy0xEYGbEjqnDYf6N7NoQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Thanks. Does this make sense?

- There are 3 nodes under the Merge Join
- The first node is an InitPlan, due to the ANY(ARRAY()) - that gets
executed and finds 0 matching rows
- The second node is the outer node in the Merge Join and that is the
expensive node in our query plan
- The third node is the inner node in the Merge Join and that node
references the SubPlan generated by the first node. The IndexCond has*
"id = ANY($2) AND ..."* and the comparison with the result of the
SubPlan does not find a match, so that's where the short-circuiting happens.

Here are the relevant lines from the node (12) accessing the result of the
SubPlan:

"Plans": [
{
"Node Type": "Index Only Scan",
"Parent Relationship": "Outer",
"Parallel Aware": false,
"Scan Direction": "Forward",
"Index Name":
"policyperi_u_id_1mw8mh83lyyd9",
"Relation Name": "pc_policyperiod",
"Alias": "qroots0",
"Startup Cost": 0.69,
"Total Cost": 18.15,
"Plan Rows": 10,
"Plan Width": 8,
"Actual Startup Time": 0.045,
"Actual Total Time": 0.045,
"Actual Rows": 0,
"Actual Loops": 1,
"Index Cond": "(*(id = ANY ($2)) AND*
(retired = 0) AND (temporarybranch = false))",

Here's the screenshot again:

[image: image.png]

Thanks,
Jerry

On Wed, Dec 20, 2023 at 10:32 AM Frédéric Yhuel <frederic(dot)yhuel(at)dalibo(dot)com>
wrote:

>
>
> Le 20/12/2023 à 15:40, Jerry Brenner a écrit :
> > Whichever side gets executed first, is the execution of the side that
> > would be second get short circuited if 0 rows are returned by the first
> > side?
>
> Indeed, if 0 rows are returned from the outer relation, the scan of the
> inner relation is never executed.
>
> Best regards,
> Frédéric
>
>

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Frédéric Yhuel 2023-12-21 06:27:20 Re: Which side of a Merge Join gets executed first? Do both sides always get executed?
Previous Message Frédéric Yhuel 2023-12-20 18:32:47 Re: Which side of a Merge Join gets executed first? Do both sides always get executed?