Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6668585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:03:26+00:00 2026-05-26T03:03:26+00:00

I see from the Postgres 8.1 docs that EXPLAIN generated something like tabular data:

  • 0

I see from the Postgres 8.1 docs that EXPLAIN generated something like tabular data:

Prior to PostgreSQL 7.3, the plan was emitted in the form of a NOTICE
message. Now it appears as a query result (formatted like a table with
a single text column).

I’m working with 9.0, for which, the docs say, the output can be a variety of types, including TEXT and XML. What I’d really like to do is treat the output as a standard query result so that I could generate a simple report for a query or a set of queries, e.g.,

SELECT maxcost FROM (
    EXPLAIN VERBOSE 
    SELECT COUNT(*) 
      FROM Mytable
     WHERE value>17);

The above doesn’t work in any form that I’ve tried, and I made up the attribute maxcost to demonstrate how neat it would be to pull out specific bits of data (in this case, the maximum estimated cost of the query). Is there anything I can do that would get me part of the way there? I’d prefer to be able to work within a simple SQL console.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:03:27+00:00Added an answer on May 26, 2026 at 3:03 am

    No other answers so far, so here’s my own stab at it.

    It’s possible to read the results of explain into a variable within plpgsql, and since the output can be in XML one can wrap EXPLAIN in a stored function to yield the top-level costs using xpath:

    CREATE OR REPLACE FUNCTION estimate_cost(IN query text, 
    OUT startup numeric,
    OUT totalcost numeric, 
    OUT planrows numeric, 
    OUT planwidth numeric)
    AS
    $BODY$
    DECLARE
        query_explain  text;
        explanation       xml;
        nsarray text[][];
    BEGIN
    nsarray := ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']];
    query_explain :=e'EXPLAIN(FORMAT XML) ' || query;
    EXECUTE query_explain INTO explanation;
    startup := (xpath('/x:explain/x:Query/x:Plan/x:Startup-Cost/text()', explanation, nsarray))[1];
    totalcost := (xpath('/x:explain/x:Query/x:Plan/x:Total-Cost/text()', explanation, nsarray))[1];
    planrows := (xpath('/x:explain/x:Query/x:Plan/x:Plan-Rows/text()', explanation, nsarray))[1];
    planwidth := (xpath('/x:explain/x:Query/x:Plan/x:Plan-Width/text()', explanation, nsarray))[1];
    RETURN;
    END;
    $BODY$
    LANGUAGE plpgsql;
    

    Hence the example from the question becomes:

    SELECT totalcost
    FROM estimate_cost('SELECT COUNT(*) 
          FROM Mytable
         WHERE value>17');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I create a Popup balloon like you would see from Windows Messenger
I'm been reading up on Ajax and would like to see from the stackoverflow
I can see in the postgresql logs that certain simple queries (no joins and
I see from Virtual inheritance doesn't break static composition? that virtual inheritance adds sizeof(pointer)
I am trying to see from an SQL console what is inside an Oracle
I'm setting up a new project using CruiseControl.net 1.4. I see from ccnet contributions
I have some really funky code. As you can see from the code below
I see this from time to time and want to know what it is.
Using a WPF expander, I want the Header to change from See More to
see this demo from jquery ui you have to hold down the Ctrl key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.