pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Explain and filter over subplans
3+ messages / 2 participants
[nested] [flat]

* Explain and filter over subplans
@ 2024-01-18 16:52 Chantal Keller <chantal.keller@universite-paris-saclay.fr>
  2024-01-18 17:55 ` Re: Explain and filter over subplans Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Chantal Keller @ 2024-01-18 16:52 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi

I would like "explain" to output formulas for filtering over
subplans. Is it possible?

Here is a minimal example. Consider the queries:

create table t(a int);
explain (format xml, verbose true) select * from t where a >= all
(select * from t);

I put the result of the second query at the end of the e-mail.

The root node filters over "SubPlan 1", which is the materialization of
the inner "select * from t". But nothing indicates that this filtering
consists in checking that a is greater or equal than all the elements
returned by the subplan.

Is there a way to print it?

Many thanks
Chantal





                            QUERY PLAN
----------------------------------------------------------------
  <explain xmlns="http://www.postgresql.org/2009/explain";      +
    <Query>                                                     +
      <Plan>                                                    +
        <Node-Type>Seq Scan</Node-Type>                         +
        <Parallel-Aware>false</Parallel-Aware>                  +
        <Async-Capable>false</Async-Capable>                    +
        <Relation-Name>t</Relation-Name>                        +
        <Schema>public</Schema>                                 +
        <Alias>t</Alias>                                        +
        <Startup-Cost>0.00</Startup-Cost>                       +
        <Total-Cost>69688.75</Total-Cost>                       +
        <Plan-Rows>1275</Plan-Rows>                             +
        <Plan-Width>4</Plan-Width>                              +
        <Output>                                                +
          <Item>t.a</Item>                                      +
        </Output>                                               +
        <Filter>(SubPlan 1)</Filter>                            +
        <Plans>                                                 +
          <Plan>                                                +
            <Node-Type>Materialize</Node-Type>                  +
            <Parent-Relationship>SubPlan</Parent-Relationship>  +
            <Subplan-Name>SubPlan 1</Subplan-Name>              +
            <Parallel-Aware>false</Parallel-Aware>              +
            <Async-Capable>false</Async-Capable>                +
            <Startup-Cost>0.00</Startup-Cost>                   +
            <Total-Cost>48.25</Total-Cost>                      +
            <Plan-Rows>2550</Plan-Rows>                         +
            <Plan-Width>4</Plan-Width>                          +
            <Output>                                            +
              <Item>t_1.a</Item>                                +
            </Output>                                           +
            <Plans>                                             +
              <Plan>                                            +
                <Node-Type>Seq Scan</Node-Type>                 +
                <Parent-Relationship>Outer</Parent-Relationship>+
                <Parallel-Aware>false</Parallel-Aware>          +
                <Async-Capable>false</Async-Capable>            +
                <Relation-Name>t</Relation-Name>                +
                <Schema>public</Schema>                         +
                <Alias>t_1</Alias>                              +
                <Startup-Cost>0.00</Startup-Cost>               +
                <Total-Cost>35.50</Total-Cost>                  +
                <Plan-Rows>2550</Plan-Rows>                     +
                <Plan-Width>4</Plan-Width>                      +
                <Output>                                        +
                  <Item>t_1.a</Item>                            +
                </Output>                                       +
              </Plan>                                           +
            </Plans>                                            +
          </Plan>                                               +
        </Plans>                                                +
      </Plan>                                                   +
    </Query>                                                    +
  </explain>





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Explain and filter over subplans
  2024-01-18 16:52 Explain and filter over subplans Chantal Keller <chantal.keller@universite-paris-saclay.fr>
@ 2024-01-18 17:55 ` Tom Lane <tgl@sss.pgh.pa.us>
  2024-01-19 09:17   ` Re: Explain and filter over subplans Chantal Keller <chantal.keller@universite-paris-saclay.fr>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2024-01-18 17:55 UTC (permalink / raw)
  To: Chantal Keller <chantal.keller@universite-paris-saclay.fr>; +Cc: pgsql-general@lists.postgresql.org

Chantal Keller <chantal.keller@universite-paris-saclay.fr> writes:
> I would like "explain" to output formulas for filtering over
> subplans. Is it possible?

No, and that's been a to-do item for a long time.

Currently, EXPLAIN just ignores the "testexpr" field of SubPlan
nodes, which is what you are after.  We could print it, if we
could figure out an intelligible representation.  In the example
you give, the testexpr would probably render as "t.a >= $0"
where $0 represents the subplan's output column.

A very rough sketch, perhaps, is that instead of just
"(SubPlan 1)", we could print "(ALL t.a >= $0 FROM SubPlan 1)".
Some of the other SubLinkTypes might be harder to represent
in a way that makes sense to users.

I made a quick-hack patch to play with, if you're interested.

			regards, tom lane

Attachments:

wip-show-subplan-testexpr-in-explain.patch (text/x-diff, 1.8K) download diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 0b2a164057..5b81e2c4cf 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -8869,12 +8869,43 @@ get_rule_expr(Node *node, deparse_context *context, * We cannot see an already-planned subplan in rule deparsing, * only while EXPLAINing a query plan. We don't try to * reconstruct the original SQL, just reference the subplan - * that appears elsewhere in EXPLAIN's result. + * that appears elsewhere in EXPLAIN's result. It does seem + * useful to show the testexpr, however, and we also note + * whether the subplan will be hashed. */ + switch (subplan->subLinkType) + { + case EXISTS_SUBLINK: + appendStringInfoString(buf, "(EXISTS"); + break; + case ALL_SUBLINK: + appendStringInfoString(buf, "(ALL "); + break; + case ANY_SUBLINK: + appendStringInfoString(buf, "(ANY "); + break; + case ROWCOMPARE_SUBLINK: + appendStringInfoString(buf, "(ROWCOMPARE "); + break; + case EXPR_SUBLINK: + appendStringInfoString(buf, "(EXPR"); + break; + case MULTIEXPR_SUBLINK: + appendStringInfoString(buf, "(MULTIEXPR"); + break; + case ARRAY_SUBLINK: + appendStringInfoString(buf, "(ARRAY"); + break; + case CTE_SUBLINK: + /* This case is probably unreachable */ + appendStringInfoString(buf, "(CTE"); + break; + } + get_rule_expr(subplan->testexpr, context, showimplicit); if (subplan->useHashTable) - appendStringInfo(buf, "(hashed %s)", subplan->plan_name); + appendStringInfo(buf, " FROM hashed %s)", subplan->plan_name); else - appendStringInfo(buf, "(%s)", subplan->plan_name); + appendStringInfo(buf, " FROM %s)", subplan->plan_name); } break; =
^ permalink raw reply [nested|flat] 3+ messages in thread

* Re: Explain and filter over subplans
  2024-01-18 16:52 Explain and filter over subplans Chantal Keller <chantal.keller@universite-paris-saclay.fr>
  2024-01-18 17:55 ` Re: Explain and filter over subplans Tom Lane <tgl@sss.pgh.pa.us>
@ 2024-01-19 09:17   ` Chantal Keller <chantal.keller@universite-paris-saclay.fr>
  0 siblings, 0 replies; 3+ messages in thread

From: Chantal Keller @ 2024-01-19 09:17 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org

Thank you very much for your quick answer and patch!

I tested and this is exactly what I was looking for :-)

Best
Chantal




Le 18/01/2024 à 18:55, Tom Lane a écrit :
> Chantal Keller <chantal.keller@universite-paris-saclay.fr> writes:
>> I would like "explain" to output formulas for filtering over
>> subplans. Is it possible?
> 
> No, and that's been a to-do item for a long time.
> 
> Currently, EXPLAIN just ignores the "testexpr" field of SubPlan
> nodes, which is what you are after.  We could print it, if we
> could figure out an intelligible representation.  In the example
> you give, the testexpr would probably render as "t.a >= $0"
> where $0 represents the subplan's output column.
> 
> A very rough sketch, perhaps, is that instead of just
> "(SubPlan 1)", we could print "(ALL t.a >= $0 FROM SubPlan 1)".
> Some of the other SubLinkTypes might be harder to represent
> in a way that makes sense to users.
> 
> I made a quick-hack patch to play with, if you're interested.
> 
> 			regards, tom lane
> 





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2024-01-19 09:17 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-01-18 16:52 Explain and filter over subplans Chantal Keller <chantal.keller@universite-paris-saclay.fr>
2024-01-18 17:55 ` Tom Lane <tgl@sss.pgh.pa.us>
2024-01-19 09:17   ` Chantal Keller <chantal.keller@universite-paris-saclay.fr>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox