Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9JVS-0006zP-6f for pgsql-general@arkaria.postgresql.org; Fri, 20 Apr 2018 00:02:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f9JVQ-0006hV-Oi for pgsql-general@arkaria.postgresql.org; Fri, 20 Apr 2018 00:02:24 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9JVQ-0006hK-D2 for pgsql-general@lists.postgresql.org; Fri, 20 Apr 2018 00:02:24 +0000 Received: from resqmta-po-12v.sys.comcast.net ([2001:558:fe16:19:96:114:154:171]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9JVK-0002WG-UM for pgsql-general@postgresql.org; Fri, 20 Apr 2018 00:02:22 +0000 Received: from resomta-po-11v.sys.comcast.net ([96.114.154.235]) by resqmta-po-12v.sys.comcast.net with ESMTP id 9JUwfgSJ26DiT9JVJfg0KD; Fri, 20 Apr 2018 00:02:17 +0000 Received: from mail.tremily.us ([67.168.0.137]) by resomta-po-11v.sys.comcast.net with SMTP id 9JTLfPJvS2Wc69JTMfPzMZ; Fri, 20 Apr 2018 00:00:16 +0000 Received: by mail.tremily.us (Postfix, from userid 1000) id D78E41527B78; Thu, 19 Apr 2018 17:00:55 -0700 (PDT) Date: Thu, 19 Apr 2018 17:00:55 -0700 From: "W. Trevor King" To: pgsql-general@postgresql.org Subject: Dynamically filtering a CTE? Message-ID: <20180420000055.GL27577@valgrind.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="75WsOQSofUOhcSOp" Content-Disposition: inline OpenPGP: id=39A2F3FA2AB17E5D8764F388FC29BDCDF15F5BE8; url=http://tremily.us/pubkey.txt User-Agent: Mutt/1.7.2 (2016-11-26) X-CMAE-Envelope: MS4wfHZekba7620J6pzi5L4oreKNWl+kXAYBapFP7NjRtRJbmU+JpNdHG2GfkSep+stbm0VzhjwLxy8yxCCh7YVtr6uQruBXX5pELr2bYlz7hUW/yaPKoZxj O01xUbHA/8h1iVDbCQhfBBLC/K+GjbMA/dJKdbdjymtl2B6MXhh9vMHny42/dS4jh7/Nx8WbV5o+HQ== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --75WsOQSofUOhcSOp Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I have a slow =E2=80=98WITH RECURSIVE=E2=80=99 CTE like: CREATE VIEW ancestors AS WITH RECURSIVE _ancestors(descendant, ancestors) AS ( SELECT item.id AS id, ARRAY[item.ancestor_id] AS ancestors FROM items AS item UNION ALL SELECT child.id AS id child.ancestors || ancestor.ancestor_id AS ancestors FROM _ancestors AS child JOIN items as ancestor ON child.ancestors[array_length(child.ancestors, 1)] =3D ancestor.id ) SELECT * FROM _ancestors WHERE child.ancestors[array_length(child.ancestors, 1)] IS NULL; I'll usually only need a few rows, so I'm being bitten by PostgreSQL's CTE optimization fence [1]. I'm looking to optimize it by limiting the dynamically limiting the number of rows in the initial query, as if it had been: WITH RECURSIVE _ancestors(id, ancestors) AS ( SELECT item.id AS id, ARRAY[item.ancestor_id] AS ancestors FROM items AS item WHERE {your condition here} UNION ALL =E2=80=A6 ) My initial thought was to create a function which accepted a WITH clause as an argument. Something like: CREATE OR REPLACE FUNCTION ancestors(condition) RETURNS TABLE(id integer, ancestors integer[]) AS $$ WITH RECURSIVE _ancestors(id, ancestors) AS ( SELECT item.id AS id, ARRAY[item.ancestor_id] AS ancestors FROM items AS item WHERE condition UNION ALL =E2=80=A6 ) =E2=80=A6 $$ LANGUAGE SQL; or with =E2=80=98WHERE condition(item)=E2=80=99. But I couldn't find a way= to define an argument that was a where condition [2] or a record=E2=86=92boolean function [3]. I could probably use PREPARE/EXECUTE [4] to dynamically construct the WHERE statement, but that looks like it may have its own optimization issues and there's no way to stash it for use in subsequent sessions. Perhaps a function to run the PREPARE? Is there an idiomatic way to approach this problem? Thanks, Trevor [1]: https://www.postgresql.org/message-id/201209191305.44674.db@kavod.com [2]: https://www.postgresql.org/docs/10/static/sql-select.html#SQL-WHERE [3]: https://www.postgresql.org/docs/10/static/sql-createfunction.html [4]: https://www.postgresql.org/docs/10/static/sql-prepare.html --=20 This email may be signed or encrypted with GnuPG (http://www.gnupg.org). For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy --75WsOQSofUOhcSOp Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEpgNNa8H/zemHkt2gprXBz9Wld7YFAlrZLZ8ACgkQprXBz9Wl d7Y6yA//RZx9slVPERDWr820HvIlaKKlaI/RQxm1zEYe5HpgHkSRh98ejTvCwTSp 4mAy+V8wTuE2XgzqhvYGCfJF9NF6PlB2uTP44hOKyIGCz82zQWDPPxAx+HR/PAjr cqllwcaVt6K9aQzGJRU8Wq8IxU7hOE36z/0J9p2y/o11dktJmQMjJtJATudbW0P0 fMDNUafzEfWFb51wZ9ip4Y7bOxEZLdC6p9TeUaPc9jg/IfoNv0ITZX7i59fzwvp/ 7RJTcrXE5mWqBRHKOXj87OyXuZeNWOIhnPVCOgCLmp5YEMn33Trru9ehC6QgpdsA xWuc2ajYAnVQQMsCN5qr6VmioCWYxtj32PyVsEUQb5bzAS9flxdzttrqHLTK/N8u p/uwm0uYay/fSaPE0bfqyDq8aRmvs+dj7oqS1Mlbbsgi6nsxVwJHiRytst5c7Yma Zjia8okwNPC7ZRz8bGb1081tyy6T0wGusizH3Ld/QS1f6HceRS0vQGlhmKTTpEpP raiOmndEk82r1R5Xa1B4V4W8IahKW3aL4+ObOThvz8fmUIH7GJmpP227AOUyN92L ry2pvyP4UlNYKpQHA6j9EW5ESmYdp4lqnGfl1YQxy49JWo8hBX4NWMI3V4hqqe8b sI/MlP4fTg/5Rq2bPPp0GCrssiHG+uEAtAYmTNerlKU3cEgHPuk= =mOyY -----END PGP SIGNATURE----- --75WsOQSofUOhcSOp--