Explain and filter over subplans

Started by Chantal Kellerover 2 years ago3 messagesgeneral
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrytests failedCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t137723
psql -h localhost -U postgres

Built from patchset v2 (message #2), July 27, 2026 at 11:23 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t137723_2 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t137723_2 && git checkout t137723_2

Patchset v2 (message #2) is on t137723_2

Jump to latest
#1Chantal Keller
chantal.keller@universite-paris-saclay.fr

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&quot;&gt; +
<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>

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Chantal Keller (#1)
Re: Explain and filter over subplans

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:

t137723_2
wip-show-subplan-testexpr-in-explain.patchtext/x-diff; charset=us-ascii; name=wip-show-subplan-testexpr-in-explain.patchDownload+34-3
#3Chantal Keller
chantal.keller@universite-paris-saclay.fr
In reply to: Tom Lane (#2)
Re: Explain and filter over subplans

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 :

Show quoted text

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