Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZmCqr-0004PC-BF for pgsql-bugs@arkaria.postgresql.org; Wed, 14 Oct 2015 03:35:41 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZmCqq-0003Af-Sp for pgsql-bugs@arkaria.postgresql.org; Wed, 14 Oct 2015 03:35:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZmBkb-0006GD-3z for pgsql-bugs@postgresql.org; Wed, 14 Oct 2015 02:25:09 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1ZmBkW-0007Op-Ng for pgsql-bugs@postgresql.org; Wed, 14 Oct 2015 02:25:07 +0000 MIME-Version: 1.0 Subject: BUG #13677: CPU 100% for WITH RECURSIVE CTE Queries To: pgsql-bugs@postgresql.org From: jkoceniak@mediamath.com Date: Wed, 14 Oct 2015 02:24:54 +0000 Message-ID: <20151014022454.3021.54212@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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