pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: jkoceniak@mediamath.com
To: pgsql-bugs@postgresql.org
Subject: BUG #13677: CPU 100% for WITH RECURSIVE CTE Queries
Date: Wed, 14 Oct 2015 02:24:54 +0000
Message-ID: <20151014022454.3021.54212@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      13677
Logged by:          Jamie Koceniak
Email address:      jkoceniak@mediamath.com
PostgreSQL version: 9.1.13
Operating system:   Debian GNU/Linux 7 (wheezy)
Description:        

Hi,

Are there any known bugs/issues with RECURSIVE CTE Queries? We have the
classic parent/child table (with only 82K total records). The hierarchy goes
as deep as 10 levels. We turned a WITH RECURSIVE query into a view (see
below). When I perform a simple count against this view, CPU goes to 100%
for the process. Also, as more and more concurrent queries are run against
this view, there is a huge decrease in performance, every process uses 100%
CPU. The query should run in about 120ms but ends up taking several
minutes.

Here is our view definition:
View definition:
 WITH RECURSIVE path AS (
                 SELECT a.id, a.name::text || ''::text AS path
                   FROM table1 a
                  WHERE a.parent_id IS NULL
        UNION ALL
                 SELECT a.id, (p.path || ' - '::text) || a.name::text AS
path
                   FROM table1 a, path p
                  WHERE p.id = a.parent_id
        )
 SELECT a.id, p.path AS full_path, ( SELECT count(*) AS count
           FROM table1 child
          WHERE child.parent_id = a.id) AS child_count
   FROM table1 a, path p
  WHERE a.id = p.id
  ORDER BY a.id;

How do we optimize a query like this? Is there a way to rewrite this query
without using recursive cte?

Thanks!



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (2+ messages)  latest in thread

Message-ID: <20151014022454.3021.54212@wrigleys.postgresql.org>
Permalink:  ../20151014022454.3021.54212@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20151014022454.3021.54212@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: jkoceniak@mediamath.com
  Subject: Re: BUG #13677: CPU 100% for WITH RECURSIVE CTE Queries
  In-Reply-To: <20151014022454.3021.54212@wrigleys.postgresql.org>
  References: <20151014022454.3021.54212@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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