EXPLAIN omits schema?

Started by Dave Pagealmost 19 years ago39 messageshackers
Jump to latest
#1Dave Page
dpage@pgadmin.org

I was just looking at implementing some query tuning/debugging features
in pgAdmin, and was looking to use EXPLAIN output to get a list of the
base tables involved in the users' query. Unfortunately though it
doesn't include the schema name in the output which means I have no way
of telling for sure which table is being referred to (even in a single
query, consider SELECT * FROM s1.foo, s2.foo).

Looking to fix this, a comment in src/backend/commands/explain.c
indicates that this is intentional:

/* We only show the rel name, not schema name */
relname = get_rel_name(rte->relid);

Anyone know why? This seems like a bug to me given the ambiguity of
possible output.

Regards, Dave.

#2Stephen Frost
sfrost@snowman.net
In reply to: Dave Page (#1)
Re: EXPLAIN omits schema?

* Dave Page (dpage@postgresql.org) wrote:

/* We only show the rel name, not schema name */
relname = get_rel_name(rte->relid);

Anyone know why? This seems like a bug to me given the ambiguity of
possible output.

I'd assume it's to keep the explain output smaller with the
expectation/assumption that in general you're going to know. A possible
work-around would be to just always provide table aliases for your
queries, as those are shown in the explain.

In terms of behaviour changes, I think it'd be nice to show the schema
name when necessary but otherwise don't, ala how '\d <view>' works.

Another option might be to omit the schema when an alias is provided, or
maybe even omit the entire table name in favor of the alias.

Just my 2c.

Thanks,

Stephen

#3Dave Page
dpage@pgadmin.org
In reply to: Stephen Frost (#2)
Re: EXPLAIN omits schema?

Stephen Frost wrote:

* Dave Page (dpage@postgresql.org) wrote:

/* We only show the rel name, not schema name */
relname = get_rel_name(rte->relid);

Anyone know why? This seems like a bug to me given the ambiguity of
possible output.

I'd assume it's to keep the explain output smaller with the
expectation/assumption that in general you're going to know. A possible
work-around would be to just always provide table aliases for your
queries, as those are shown in the explain.

I have no control over the queries themselves.

In terms of behaviour changes, I think it'd be nice to show the schema
name when necessary but otherwise don't, ala how '\d <view>' works.

In my case that would be awkward as pgAdmin would then need to try to
work out what the actual table was based on the search path used for the
users query.

Another option might be to omit the schema when an alias is provided, or
maybe even omit the entire table name in favor of the alias.

That would make it very painful as I'd need to parse the query client
side to resolve the table names. Yeuch.

Just adding the schema name seems the most sensible and usable option -
not to mention the easiest!

Regards, Dave

#4Stephen Frost
sfrost@snowman.net
In reply to: Dave Page (#3)
Re: EXPLAIN omits schema?

* Dave Page (dpage@postgresql.org) wrote:

Stephen Frost wrote:

In terms of behaviour changes, I think it'd be nice to show the schema
name when necessary but otherwise don't, ala how '\d <view>' works.

In my case that would be awkward as pgAdmin would then need to try to
work out what the actual table was based on the search path used for the
users query.

Actually, as mentioned in another thread, a function to take a table
name and a search_path and return the 'fully qualified' table name would
make that much easier, and would be useful in other situations.

Another option might be to omit the schema when an alias is provided, or
maybe even omit the entire table name in favor of the alias.

That would make it very painful as I'd need to parse the query client
side to resolve the table names. Yeuch.

Indeed, if you're not constructing the queries that would make things
somewhat difficult. Then again, parsing the explain output seems like
it's going to be rather difficult itself anyway.

Just adding the schema name seems the most sensible and usable option -
not to mention the easiest!

While completely ignoring the current behaviour and likely the reason
it's done the way it is now... explain output was, and still is
primairly, for humans to read.

Thanks,

Stephen

#5Dave Page
dpage@pgadmin.org
In reply to: Stephen Frost (#4)
Re: EXPLAIN omits schema?

Stephen Frost wrote:

Indeed, if you're not constructing the queries that would make things
somewhat difficult. Then again, parsing the explain output seems like
it's going to be rather difficult itself anyway.

Well, we do that anyway - and just grabbing the base table names isn't
too hard.

Just adding the schema name seems the most sensible and usable option -
not to mention the easiest!

While completely ignoring the current behaviour and likely the reason
it's done the way it is now... explain output was, and still is
primairly, for humans to read.

Humans deserve schemas as well!! :-). As for the likely reason for the
current behaviour, well, I'd rather have precise,
non-potentially-ambiguous info than save a few characters.

Regards, Dave

#6Lukas Kahwe Smith
smith@pooteeweet.org
In reply to: Stephen Frost (#2)
Re: EXPLAIN omits schema?

Stephen Frost wrote:

* Dave Page (dpage@postgresql.org) wrote:

/* We only show the rel name, not schema name */
relname = get_rel_name(rte->relid);

Anyone know why? This seems like a bug to me given the ambiguity of
possible output.

I'd assume it's to keep the explain output smaller with the
expectation/assumption that in general you're going to know. A possible
work-around would be to just always provide table aliases for your
queries, as those are shown in the explain.

I am hoping that once we have WITH RECURSIVE, we could optionally
provide a normalized dump into a table of the EXPLAIN output, that could
then be easily "connected" the the old output using WITH RECURSIVE.

regards,
Lukas

#7Magnus Hagander
magnus@hagander.net
In reply to: Dave Page (#5)
Re: EXPLAIN omits schema?

On Wed, Jun 13, 2007 at 01:20:25PM +0100, Dave Page wrote:

Just adding the schema name seems the most sensible and usable option -
not to mention the easiest!

While completely ignoring the current behaviour and likely the reason
it's done the way it is now... explain output was, and still is
primairly, for humans to read.

Humans deserve schemas as well!! :-). As for the likely reason for the
current behaviour, well, I'd rather have precise,
non-potentially-ambiguous info than save a few characters.

Just to open a whole new can of worms ;-)

I read an article a couple of days ago about the "machine readable showplan
output" in SQL Server 2005 (basically, it's EXPLAIN output but in XML
format). It does make a lot of sense if yourp rimary interface is !=
commandline (psql), such as pgadmin or phppgadmin. The idea being that you
can stick in *all* the details you want, since you can't possibly clutter
up the display. And you stick them in a well-defined XML format (or another
format if you happen to hate XML) where the client-side program can easily
parse out whatever it needs. It's also future-proof - if you add a new
field somewhere, the client program parser won't break.

Something worth doing? Not to replace the current explain output, but as a
second option (EXPLAIN XML whatever)?

//Magnus

#8Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Magnus Hagander (#7)
Re: EXPLAIN omits schema?

Magnus Hagander wrote:

Just to open a whole new can of worms ;-)

I read an article a couple of days ago about the "machine readable showplan
output" in SQL Server 2005 (basically, it's EXPLAIN output but in XML
format). It does make a lot of sense if yourp rimary interface is !=
commandline (psql), such as pgadmin or phppgadmin. The idea being that you
can stick in *all* the details you want, since you can't possibly clutter
up the display. And you stick them in a well-defined XML format (or another
format if you happen to hate XML) where the client-side program can easily
parse out whatever it needs. It's also future-proof - if you add a new
field somewhere, the client program parser won't break.

Something worth doing? Not to replace the current explain output, but as a
second option (EXPLAIN XML whatever)?

FYI a patch was posted for this some time ago, because a friend of mine
wanted to help a student to write an EXPLAIN parsing tool.

--
Alvaro Herrera http://www.amazon.com/gp/registry/CTMLCN8V17R4
"Having your biases confirmed independently is how scientific progress is
made, and hence made our great society what it is today" (Mary Gardiner)

#9Magnus Hagander
magnus@hagander.net
In reply to: Alvaro Herrera (#8)
Re: EXPLAIN omits schema?

On Wed, Jun 13, 2007 at 08:47:30AM -0400, Alvaro Herrera wrote:

Magnus Hagander wrote:

Just to open a whole new can of worms ;-)

I read an article a couple of days ago about the "machine readable showplan
output" in SQL Server 2005 (basically, it's EXPLAIN output but in XML
format). It does make a lot of sense if yourp rimary interface is !=
commandline (psql), such as pgadmin or phppgadmin. The idea being that you
can stick in *all* the details you want, since you can't possibly clutter
up the display. And you stick them in a well-defined XML format (or another
format if you happen to hate XML) where the client-side program can easily
parse out whatever it needs. It's also future-proof - if you add a new
field somewhere, the client program parser won't break.

Something worth doing? Not to replace the current explain output, but as a
second option (EXPLAIN XML whatever)?

FYI a patch was posted for this some time ago, because a friend of mine
wanted to help a student to write an EXPLAIN parsing tool.

Didn't see that one. Explain in XML format? Got an URL for it, I can't seem
to find it on -patches.

//Magnus

#10Pavel Stehule
pavel.stehule@gmail.com
In reply to: Magnus Hagander (#7)
Re: EXPLAIN omits schema?

Something worth doing? Not to replace the current explain output, but as a
second option (EXPLAIN XML whatever)?

//Magnus

It's good idea. Similar situation is in stack trace output.

Pavel

#11Heikki Linnakangas
heikki.linnakangas@enterprisedb.com
In reply to: Magnus Hagander (#7)
Re: EXPLAIN omits schema?

Magnus Hagander wrote:

On Wed, Jun 13, 2007 at 01:20:25PM +0100, Dave Page wrote:

Just adding the schema name seems the most sensible and usable option -
not to mention the easiest!

While completely ignoring the current behaviour and likely the reason
it's done the way it is now... explain output was, and still is
primairly, for humans to read.

Humans deserve schemas as well!! :-). As for the likely reason for the
current behaviour, well, I'd rather have precise,
non-potentially-ambiguous info than save a few characters.

Just to open a whole new can of worms ;-)

I read an article a couple of days ago about the "machine readable showplan
output" in SQL Server 2005 (basically, it's EXPLAIN output but in XML
format). It does make a lot of sense if yourp rimary interface is !=
commandline (psql), such as pgadmin or phppgadmin. The idea being that you
can stick in *all* the details you want, since you can't possibly clutter
up the display. And you stick them in a well-defined XML format (or another
format if you happen to hate XML) where the client-side program can easily
parse out whatever it needs. It's also future-proof - if you add a new
field somewhere, the client program parser won't break.

Something worth doing? Not to replace the current explain output, but as a
second option (EXPLAIN XML whatever)?

I agree it would be nice to have machine readable explain output.

DB2 has the concept of "explain tables". Explain output is written to
tables, which tools query and pretty print the output. I like that idea
in principle. PostgreSQL is a relational database, so having the explain
output in relations make sense. No need for XML or any other extra
libraries, in either the server or client. Having the data in relational
format allows you to query them. For example, show me all sequential
scans, or all nodes where the estimated number of rows is off by a
certain factor.

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com

#12Stephen Frost
sfrost@snowman.net
In reply to: Heikki Linnakangas (#11)
Re: EXPLAIN omits schema?

* Heikki Linnakangas (heikki@enterprisedb.com) wrote:

Something worth doing? Not to replace the current explain output, but as a
second option (EXPLAIN XML whatever)?

I agree it would be nice to have machine readable explain output.

Seconded here, I'd much rather see this as a seperate option rather than
cluttering up regular 'explain' output for the humans. I do think we
should provide the schema name when it's not clear from the search_path
tho, since that helps the humans too. :)

DB2 has the concept of "explain tables". Explain output is written to
tables, which tools query and pretty print the output. I like that idea
in principle. PostgreSQL is a relational database, so having the explain
output in relations make sense. No need for XML or any other extra
libraries, in either the server or client. Having the data in relational
format allows you to query them. For example, show me all sequential
scans, or all nodes where the estimated number of rows is off by a
certain factor.

I like this approach, the only downside is someone/something needs to manage
those tables, unless you can say where the tables are to put the explain
output into or similar? Also, with tables, if someone really wants XML
the tables can be extracted as XML.

Thanks,

Stephen

#13Magnus Hagander
magnus@hagander.net
In reply to: Heikki Linnakangas (#11)
Re: EXPLAIN omits schema?

On Wed, Jun 13, 2007 at 02:02:24PM +0100, Heikki Linnakangas wrote:

Magnus Hagander wrote:

On Wed, Jun 13, 2007 at 01:20:25PM +0100, Dave Page wrote:

Just adding the schema name seems the most sensible and usable option -
not to mention the easiest!

While completely ignoring the current behaviour and likely the reason
it's done the way it is now... explain output was, and still is
primairly, for humans to read.

Humans deserve schemas as well!! :-). As for the likely reason for the
current behaviour, well, I'd rather have precise,
non-potentially-ambiguous info than save a few characters.

Just to open a whole new can of worms ;-)

I read an article a couple of days ago about the "machine readable showplan
output" in SQL Server 2005 (basically, it's EXPLAIN output but in XML
format). It does make a lot of sense if yourp rimary interface is !=
commandline (psql), such as pgadmin or phppgadmin. The idea being that you
can stick in *all* the details you want, since you can't possibly clutter
up the display. And you stick them in a well-defined XML format (or another
format if you happen to hate XML) where the client-side program can easily
parse out whatever it needs. It's also future-proof - if you add a new
field somewhere, the client program parser won't break.

Something worth doing? Not to replace the current explain output, but as a
second option (EXPLAIN XML whatever)?

I agree it would be nice to have machine readable explain output.

DB2 has the concept of "explain tables". Explain output is written to
tables, which tools query and pretty print the output. I like that idea
in principle. PostgreSQL is a relational database, so having the explain
output in relations make sense. No need for XML or any other extra
libraries, in either the server or client. Having the data in relational
format allows you to query them. For example, show me all sequential
scans, or all nodes where the estimated number of rows is off by a
certain factor.

Assuming you can actually *represent* the whole plan as tables, that would
of course work fine. But I assume you mean "virtual tables"? So I do
EXPLAIN whatever, and get back one or more resultssets with the data? Or do
they write it to *actual* tables in the database?

Machine-readable is of course the main point - the exact format is more of
an implementation detail.

//Magnus

#14Heikki Linnakangas
heikki.linnakangas@enterprisedb.com
In reply to: Magnus Hagander (#13)
Re: EXPLAIN omits schema?

Magnus Hagander wrote:

On Wed, Jun 13, 2007 at 02:02:24PM +0100, Heikki Linnakangas wrote:

DB2 has the concept of "explain tables". Explain output is written to
tables, which tools query and pretty print the output. I like that idea
in principle. PostgreSQL is a relational database, so having the explain
output in relations make sense. No need for XML or any other extra
libraries, in either the server or client. Having the data in relational
format allows you to query them. For example, show me all sequential
scans, or all nodes where the estimated number of rows is off by a
certain factor.

Assuming you can actually *represent* the whole plan as tables, that would
of course work fine.

Sure you can. It's just a question of how complex the schema is :).

But I assume you mean "virtual tables"? So I do
EXPLAIN whatever, and get back one or more resultssets with the data? Or do
they write it to *actual* tables in the database?

I'm not sure. DB2 had real tables, but I found that a bit clumsy. It was
nice because your old explain results were accumulated, but it was also
not nice because of that same thing.

One idea would be temporary tables.

Machine-readable is of course the main point - the exact format is more of
an implementation detail.

Agreed.

A potential problem is that as we add new node types etc., we need to
extend the schema (whether it's a real relational schema or XML), and
clients need to understand it. But I guess we already have the same
problem with clients that parse the current explain output.

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com

#15Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Magnus Hagander (#9)
Re: EXPLAIN omits schema?

Magnus Hagander wrote:

On Wed, Jun 13, 2007 at 08:47:30AM -0400, Alvaro Herrera wrote:

Magnus Hagander wrote:

Just to open a whole new can of worms ;-)

I read an article a couple of days ago about the "machine readable showplan
output" in SQL Server 2005 (basically, it's EXPLAIN output but in XML
format). It does make a lot of sense if yourp rimary interface is !=
commandline (psql), such as pgadmin or phppgadmin. The idea being that you
can stick in *all* the details you want, since you can't possibly clutter
up the display. And you stick them in a well-defined XML format (or another
format if you happen to hate XML) where the client-side program can easily
parse out whatever it needs. It's also future-proof - if you add a new
field somewhere, the client program parser won't break.

Something worth doing? Not to replace the current explain output, but as a
second option (EXPLAIN XML whatever)?

FYI a patch was posted for this some time ago, because a friend of mine
wanted to help a student to write an EXPLAIN parsing tool.

Didn't see that one. Explain in XML format? Got an URL for it, I can't seem
to find it on -patches.

I can't find the patch itself ... maybe he didn't ever post it. He last
talked about it here:
http://archives.postgresql.org/pgsql-hackers/2006-04/msg00455.php

BTW can I bug you to add the Message-Ids in the messages as displayed in
our archives?

--
Alvaro Herrera http://www.PlanetPostgreSQL.org/
"El d�a que dejes de cambiar dejar�s de vivir"

#16Aidan Van Dyk
aidan@highrise.ca
In reply to: Alvaro Herrera (#15)
Re: EXPLAIN omits schema?

* Alvaro Herrera <alvherre@commandprompt.com> [070613 09:58]:

BTW can I bug you to add the Message-Ids in the messages as displayed in
our archives?

Yes! Yes! Yes! Pretty please!

--
Aidan Van Dyk Create like a god,
aidan@highrise.ca command like a king,
http://www.highrise.ca/ work like a slave.

#17Tom Lane
tgl@sss.pgh.pa.us
In reply to: Dave Page (#1)
Re: EXPLAIN omits schema?

Dave Page <dpage@postgresql.org> writes:

Looking to fix this, a comment in src/backend/commands/explain.c
indicates that this is intentional:

Quite.

Anyone know why?

As already noted, it'd usually be clutter in lines that are too long
already. Also, conditionally adding a schema name isn't very good
because it makes life even more complicated for programs that are
parsing EXPLAIN output (yes, there are some).

I agree with the idea of having an option to get EXPLAIN's output in
an entirely different, more machine-readable format. Not wedded to
XML, but I fear that a pure relational structure might be too strict ---
there's a lot of variability in the entries already. XML also could
deal naturally with nesting, whereas we'd have to jump through hoops
to represent the plan tree structure in relational form.

regards, tom lane

#18Magnus Hagander
magnus@hagander.net
In reply to: Alvaro Herrera (#15)
Re: EXPLAIN omits schema?

On Wed, Jun 13, 2007 at 09:55:19AM -0400, Alvaro Herrera wrote:

Magnus Hagander wrote:

On Wed, Jun 13, 2007 at 08:47:30AM -0400, Alvaro Herrera wrote:

Magnus Hagander wrote:

Just to open a whole new can of worms ;-)

I read an article a couple of days ago about the "machine readable showplan
output" in SQL Server 2005 (basically, it's EXPLAIN output but in XML
format). It does make a lot of sense if yourp rimary interface is !=
commandline (psql), such as pgadmin or phppgadmin. The idea being that you
can stick in *all* the details you want, since you can't possibly clutter
up the display. And you stick them in a well-defined XML format (or another
format if you happen to hate XML) where the client-side program can easily
parse out whatever it needs. It's also future-proof - if you add a new
field somewhere, the client program parser won't break.

Something worth doing? Not to replace the current explain output, but as a
second option (EXPLAIN XML whatever)?

FYI a patch was posted for this some time ago, because a friend of mine
wanted to help a student to write an EXPLAIN parsing tool.

Didn't see that one. Explain in XML format? Got an URL for it, I can't seem
to find it on -patches.

I can't find the patch itself ... maybe he didn't ever post it. He last
talked about it here:
http://archives.postgresql.org/pgsql-hackers/2006-04/msg00455.php

BTW can I bug you to add the Message-Ids in the messages as displayed in
our archives?

No. Because I don't know how to do that :-) And what work is done to th
archives should be done to redo the whole thing and not bandaid what we
have now.

That said, you can get the message-id if you do a view-source. It's in a
comment at the beginning of the page.

//Magnus

#19Joshua Tolley
eggyknap@gmail.com
In reply to: Heikki Linnakangas (#14)
Re: EXPLAIN omits schema?

On 6/13/07, Heikki Linnakangas <heikki@enterprisedb.com> wrote:

Magnus Hagander wrote:

On Wed, Jun 13, 2007 at 02:02:24PM +0100, Heikki Linnakangas wrote:

DB2 has the concept of "explain tables". Explain output is written to
tables, which tools query and pretty print the output. I like that idea
in principle. PostgreSQL is a relational database, so having the explain
output in relations make sense. No need for XML or any other extra
libraries, in either the server or client. Having the data in relational
format allows you to query them. For example, show me all sequential
scans, or all nodes where the estimated number of rows is off by a
certain factor.

Assuming you can actually *represent* the whole plan as tables, that would
of course work fine.

Sure you can. It's just a question of how complex the schema is :).

But I assume you mean "virtual tables"? So I do
EXPLAIN whatever, and get back one or more resultssets with the data? Or do
they write it to *actual* tables in the database?

I'm not sure. DB2 had real tables, but I found that a bit clumsy. It was
nice because your old explain results were accumulated, but it was also
not nice because of that same thing.

One idea would be temporary tables.

Machine-readable is of course the main point - the exact format is more of
an implementation detail.

Agreed.

A potential problem is that as we add new node types etc., we need to
extend the schema (whether it's a real relational schema or XML), and
clients need to understand it. But I guess we already have the same
problem with clients that parse the current explain output.

Oracle forces you (AFAIK) to create a set of tables to store explain
plan output, so when you EXPLAIN, it populates those tables, and then
you have to query to get it out. This is nice for admin tools that
have to parse the explain output, though it's obviously a pain for
explain-ing inside a command-line. An XML explain would be neat.

On a different sideline based on the original note of this thread,
much as EXPLAIN doesn't include the schema, \d doesn't include the
schema to describe INHERIT relationships in 8.2.4. If you have two
tables called PARENT, in two different schemas, and a child that
inherits from one of them, \d won't tell you which of the two it
inherits from.

- Josh

#20Dave Page
dpage@pgadmin.org
In reply to: Magnus Hagander (#7)
Re: EXPLAIN omits schema?

Magnus Hagander wrote:

Something worth doing? Not to replace the current explain output, but as a
second option (EXPLAIN XML whatever)?

Yeah, thats been mentioned before. I was looking to bring it up for 8.4.

/D

#21Tom Lane
tgl@sss.pgh.pa.us
In reply to: Joshua Tolley (#19)
#22Bruce Momjian
bruce@momjian.us
In reply to: Magnus Hagander (#13)
#23Dave Page
dpage@pgadmin.org
In reply to: Tom Lane (#17)
#24Andrew Dunstan
andrew@dunslane.net
In reply to: Tom Lane (#17)
#25Aidan Van Dyk
aidan@highrise.ca
In reply to: Magnus Hagander (#18)
#26Joshua D. Drake
jd@commandprompt.com
In reply to: Tom Lane (#17)
#27Joshua D. Drake
jd@commandprompt.com
In reply to: Andrew Dunstan (#24)
#28Lukas Kahwe Smith
smith@pooteeweet.org
In reply to: Tom Lane (#17)
#29Bruce Momjian
bruce@momjian.us
In reply to: Tom Lane (#17)
#30Bruce Momjian
bruce@momjian.us
In reply to: Joshua D. Drake (#27)
#31Greg Sabino Mullane
greg@turnstep.com
In reply to: Magnus Hagander (#7)
#32Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#29)
#33Joshua D. Drake
jd@commandprompt.com
In reply to: Bruce Momjian (#30)
#34Florian Pflug
fgp@phlo.org
In reply to: Bruce Momjian (#30)
#35Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#30)
#36Nikolay Samokhvalov
samokhvalov@gmail.com
In reply to: Bruce Momjian (#30)
#37Bruce Momjian
bruce@momjian.us
In reply to: Tom Lane (#32)
#38Simon Riggs
simon@2ndQuadrant.com
In reply to: Tom Lane (#32)
#39Stefan Kaltenbrunner
stefan@kaltenbrunner.cc
In reply to: Tom Lane (#35)