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 1f9YnV-00060p-Dm for pgsql-general@arkaria.postgresql.org; Fri, 20 Apr 2018 16:22:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f9YnU-0008D1-8E for pgsql-general@arkaria.postgresql.org; Fri, 20 Apr 2018 16:22:04 +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.89) (envelope-from ) id 1f9YnT-0008Cr-W5 for pgsql-general@lists.postgresql.org; Fri, 20 Apr 2018 16:22:04 +0000 Received: from resqmta-po-12v.sys.comcast.net ([2001:558:fe16:19:96:114:154:171]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9YnO-0004Us-Tv for pgsql-general@postgresql.org; Fri, 20 Apr 2018 16:22:03 +0000 Received: from resomta-po-18v.sys.comcast.net ([96.114.154.242]) by resqmta-po-12v.sys.comcast.net with ESMTP id 9Yn1fhKlL6DiT9YnIfhRJs; Fri, 20 Apr 2018 16:21:52 +0000 Received: from mail.tremily.us ([IPv6:2001:558:600a:ce:9448:e51d:eba9:621c]) by resomta-po-18v.sys.comcast.net with SMTP id 9YnGfBQ8Khqjv9YnHfbnce; Fri, 20 Apr 2018 16:21:51 +0000 Received: by mail.tremily.us (Postfix, from userid 1000) id 71CA115290DF; Fri, 20 Apr 2018 09:22:32 -0700 (PDT) Date: Fri, 20 Apr 2018 09:22:32 -0700 From: "W. Trevor King" To: "David G. Johnston" Cc: pgsql-general@postgresql.org Subject: Re: Dynamically filtering a CTE? Message-ID: <20180420162232.GM27577@valgrind.us> References: <20180420000055.GL27577@valgrind.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="z3ovhOgMYmj8MRdq" Content-Disposition: inline In-Reply-To: OpenPGP: id=39A2F3FA2AB17E5D8764F388FC29BDCDF15F5BE8; url=http://tremily.us/pubkey.txt User-Agent: Mutt/1.7.2 (2016-11-26) X-CMAE-Envelope: MS4wfDxSeO+rquG7KvYsbUIFBZ3cadv+uwkFPpORedZzZGpx97ZFhzJFkkaiuspsbfFboWY8vLaUG+4aSyRU2Ekup66PLxAZ9gJbcBVdEwinEigDCoIFK+jH Ej5aJeU/d0O5A6oGzuneMDch2+VWHDV9I3NztY4wLNUYLYH4xSq9syH6Dfy63K+6Ye6fSwzLpijk4nq5/tgYj+1w+Z92NhnFBcI= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --z3ovhOgMYmj8MRdq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Apr 19, 2018 at 05:28:00PM -0700, David G. Johnston wrote: > On Thursday, April 19, 2018, W. Trevor King wrote: > > Is there an idiomatic way to approach this problem? > > I would use pl/pgsql as the language and build a query using a > combination of text literals and the format() function - invoking > via pl/pgsql's EXECUTE command. That works. I've ended up with: CREATE OR REPLACE FUNCTION ancestors(condition text) RETURNS TABLE(id integer, ancestors integer[]) AS $$ BEGIN RETURN QUERY EXECUTE format(' WITH RECURSIVE _ancestors(id, ancestors) AS ( SELECT item.id AS id, ARRAY[item.ancestor_id] AS ancestors FROM items AS item %s UNION ALL SELECT descendant.id AS id, descendant.ancestors || ancestor.ancestor_id AS ancestors FROM _ancestors AS descendant JOIN items as ancestor ON descendant.ancestors[array_length(descendant.ancestors, 1)] = =3D ancestor.id ) SELECT id, ancestors[1:array_length(ancestors, 1) - 1] AS ancestors -- drop the = trailing NULL FROM _ancestors WHERE ancestors[array_length(ancestors, 1)] IS NULL -- remove non-termi= nal recursion ', condition); END $$ LANGUAGE plpgsql STABLE; which you can use like: SELECT * FROM ancestors('WHERE item.id =3D 62324721'); or (without filtering, for the full, slow CTE): SELECT * FROM ancestors(''); Thanks, Trevor --=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 --z3ovhOgMYmj8MRdq Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEpgNNa8H/zemHkt2gprXBz9Wld7YFAlraE8EACgkQprXBz9Wl d7Zofw//U/NrWKEMvCFOuIGlFU7GkyeHTQgU1HiM6VMU/tq095b2vnxfN4YEkTrK d5b5Rh2gne0+l/GLW9fMOzrQUxn7A3AMdYIqV7lDxc0yh2BdXdOJA32sg7PYnxcv QTJyuLc5KAG7QaQjtGacPOMXDomMSUCP9/B45Z36f8a6PejWRYzEwK0JLzkTF+Im MrzoxONMdEum+yGOZ7VVUwvsa2H5bAI9mnrky+w5dTZiJNFW09E7+ZzlmaFE7QAJ HBxYxYPCOLyJs1wzoygEiC7OPQiB82PGII5cEAgSJXgzQeE38n0/ytTfFVyy7C7S Z2Y+PzY6hVFk+e9cPfp+daUT0VAPuYQJrGlJo3b9n+6KKiXldZ4Vk+iVwHpqAvoR RR3QvmniLkdeaPJaONpDndXP65fzYWvyop2UtyGN9Hz60bEFHVa9mwbIuP9KfAvj qNyPugo4jRb5xR4MHi/sKsicux4/kgAjETAIq4WeWaAxbWnbfLY+n6cCgm0cKbhZ 7AEOUDvUTzUzroYlSE6mtU5NGj7NbBq/rZqn4oyQZjivBfm5nFK/vqiSsp6xezZb gBsomiz9n0UhoQmdp0gLu7eMwiPLF1tMF2GRf6GSsu9WXE6QnxEBSKY3bUMxyes4 DeJkvXm71sCHYr6mUZBXwnAPcIGXKelZCtK8uHTb5SE+LIEAdWU= =PJdI -----END PGP SIGNATURE----- --z3ovhOgMYmj8MRdq--