pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: W. Trevor King <wking@tremily.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Dynamically filtering a CTE?
Date: Fri, 20 Apr 2018 09:22:32 -0700
Message-ID: <20180420162232.GM27577@valgrind.us> (raw)
In-Reply-To: <CAKFQuwY36yat8Ycf_Ha6z25sZnNYMH9rHmTsG3QfkDZt1zX2XA@mail.gmail.com>
References: <20180420000055.GL27577@valgrind.us>
	<CAKFQuwY36yat8Ycf_Ha6z25sZnNYMH9rHmTsG3QfkDZt1zX2XA@mail.gmail.com>

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)] = 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-terminal recursion
    ', condition);
  END
  $$ LANGUAGE plpgsql STABLE;

which you can use like:

  SELECT * FROM ancestors('WHERE item.id = 62324721');

or (without filtering, for the full, slow CTE):

  SELECT * FROM ancestors('');

Thanks,
Trevor

-- 
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

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (5+ messages)  latest in thread

Message-ID: <20180420162232.GM27577@valgrind.us>
Permalink:  ../20180420162232.GM27577@valgrind.us/
Also on:    postgresql.org/message-id/20180420162232.GM27577@valgrind.us

 · 

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-general@postgresql.org
  Cc: wking@tremily.us, david.g.johnston@gmail.com
  Subject: Re: Dynamically filtering a CTE?
  In-Reply-To: <20180420162232.GM27577@valgrind.us>
  References: <20180420000055.GL27577@valgrind.us> <CAKFQuwY36yat8Ycf_Ha6z25sZnNYMH9rHmTsG3QfkDZt1zX2XA@mail.gmail.com> <20180420162232.GM27577@valgrind.us>

* 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