Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fw9mK-00027O-Qh for pgsql-general@arkaria.postgresql.org; Sat, 01 Sep 2018 17:33:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fw9mI-00060E-8F for pgsql-general@arkaria.postgresql.org; Sat, 01 Sep 2018 17:33:42 +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 1fw9mH-000607-QW for pgsql-general@lists.postgresql.org; Sat, 01 Sep 2018 17:33:42 +0000 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fw9mE-0005fQ-Jh for pgsql-general@lists.postgresql.org; Sat, 01 Sep 2018 17:33:40 +0000 Received: by mail-pf1-x42d.google.com with SMTP id b11-v6so6915609pfo.3 for ; Sat, 01 Sep 2018 10:33:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:message-id:references:to; bh=P+DOZuhp3SRz+n5khHN+FKEH5i0QlSTLSQ1pk145NbA=; b=qcZrtaYfmSoGMsef5sl6PvK5vtrTwKZMPKgc50FPe6pbG1RvcgvN7Sv5g1u8tj5kiR xNzKvXWlr9LvDAdoD0SLxwW6021UJB/zdf2wnK/nxsbIlbEBeRGB1M3K7f2B0QhRkJr9 Qr0OYwnJ7zrsUMHQG9ruZVytJxRqxx4jgLmJWvRpLrtwzqrNwEjpg/1qORQGk4GJgGx2 e0tx95DUwKeaTfH3iyAHjbwS+kjBO2JFZGEBP0S1rrYNpJ7gFrG922M4lh9TaEJVRjul dmEFiJ68I8QS2SVPK9BlL/MMAE8zPjLVHlOgOBiBtpSwAnWisyJ5ICZZ7xGqyx6VIfll 1MZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date :message-id:references:to; bh=P+DOZuhp3SRz+n5khHN+FKEH5i0QlSTLSQ1pk145NbA=; b=cshMVY1XUhcCsnYygkHJCdnCuTpE8AnVjS7ahjqghD21eqHAvDS7+CLNnTHD5lexhW LrWeQghstf9uS1YYQF2Ld9pLj5rZDWPsi8rkSSYpSUZ6ZXk3SozqWdrqHQ/lsSHC6DWG 2AaUkuGpx156TNjsiAlmLthe0X2IYtzmczFOUoBxhoucgj8jWuJ/MeHZ7u2AK9W6EggZ Q8w2t/4NjRLYgNTbnl+Kc6L76WMD974w5KF8UcSV0Ds+pDkwhQ353uJG2GBkDsB6p4Si +Tyjgdg73nZ2zphwGDm7YPFV/ytg6ncuyUHEd1PdjnjwJ8+C7cKFl/InFNKseTOsKaf3 4y9w== X-Gm-Message-State: APzg51CI0LSCCa4zorwh7z288THFcwz3b8M9iilOT77K3T4n/w7Tg5Gb wg2AUDPztBAhc+kh/qsxReg= X-Google-Smtp-Source: ANB0VdZLm+m1253gU3LGM95A+G36Jtpvz/JStOqcGoUlpj1ls59WmtDtv2hikbtJ7Wkj4Mkn0ov7ew== X-Received: by 2002:a63:a919:: with SMTP id u25-v6mr19763564pge.211.1535823216899; Sat, 01 Sep 2018 10:33:36 -0700 (PDT) Received: from ?IPv6:2600:8801:8800:1a::d? ([2600:8801:8800:1a::d]) by smtp.gmail.com with ESMTPSA id j16-v6sm28291315pfk.125.2018.09.01.10.33.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 01 Sep 2018 10:33:36 -0700 (PDT) Content-Type: multipart/alternative; boundary="Apple-Mail=_94C1D810-9FB5-4B66-949C-BE24BF63C759" Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.1\)) Subject: Re: Rules and decision logic triggered by / acting on JSONB nodes in Postgres From: Guyren Howe X-Mailbutler-Tracking-Uuid: 9EA63BA3-9333-44A0-B286-61DA9EA07511 In-Reply-To: <20108283-DE54-4754-8371-B34547BE0739@tallyfy.com> X-Mailbutler-Link-Tracking-Uuid: Date: Sat, 1 Sep 2018 10:33:16 -0700 Message-Id: <7F877E97-57CA-46AF-914F-30EE48481B30@gmail.com> References: <20108283-DE54-4754-8371-B34547BE0739@tallyfy.com> To: Amit Kothari , pgsql-general@lists.postgresql.org X-Mailer: Apple Mail (2.3445.9.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_94C1D810-9FB5-4B66-949C-BE24BF63C759 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 On Sep 1, 2018, at 7:14 , Amit Kothari wrote: >=20 > Hi all, >=20 > I=E2=80=99m sorry if this question is off-topic for postgres-core, but = didn=E2=80=99t know where else to ask. >=20 > We=E2=80=99ve built a workflow and BPM (business process management) = tool with pg as the database, and we=E2=80=99d like to explore = conditional logic within steps in a workflow being triggers as functions = when a certain JSONB node changes. >=20 > An example -> If (form field contains =E2=80=9Cbob=E2=80=9D) then = (change state of another object) >=20 > In effect, since all our objects are JSONB - we wanted to =E2=80=9Cliste= n=E2=80=9D to a change in one JSONB node in order to make a = corresponding change in another JSONB node.=20 >=20 > Can this be either native in postgres, or even a pg function?=20 >=20 > In general - how do people approach rules and decision logic in = Postgres at a very large scale e.g. say 100,000 rules that must be = evaluated on a commit?=20 >=20 > If it makes more sense - please see the example screenshot on our = features page at https://tallyfy.com/features - which visualizes = conditional branching. That *looks* like each team gets their own workflows, so you could = restrict your rules search to the rules for the team that created the = change. Anything you could do to restrict the number of rules would = help, of course. One option would be to express rules as an (antecedent, consequent) = pair, where the antecedent is the argument to json_extract_path and the = consequent is the rest of the arguments to a function that carries out = the change =E2=80=94 something along those lines, in any event. Iterating through a small set of such rules would be tolerable. If you = really do have 100,000 rules that have to be applied to each change, my = first thought is to use a SAT solver. But even there, you would have to = compute the results of all your antecedents, so it would depend how many = of those you have. You might use a map-reduce farm, but all the machines are going to be = hitting your database pretty hard. You might be able to reduce your 100,000 antecedents to something a bit = more tractable by finding the overlaps between them and so only = computing the shared parts of antecedents once. You might want to use LISTEN-NOTIFY so an external logic engine =E2=80=94 = perhaps using map-reduce, perhaps you just have a set of logic engines = that each takes some part of that 100,000 rules and just brute forces = them =E2=80=94 gets to know about the changes. I=E2=80=99d need to know some more details about what you=E2=80=99re up = to, to give you a more specific answer, but that=E2=80=99s what occurs = to me given what you=E2=80=99ve told us. --Apple-Mail=_94C1D810-9FB5-4B66-949C-BE24BF63C759 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 On = Sep 1, 2018, at 7:14 , Amit Kothari <amit@tallyfy.com> = wrote:

Hi = all,

I=E2=80=99m sorry if this question is = off-topic for postgres-core, but didn=E2=80=99t know where else to = ask.

We=E2=80=99ve built a workflow and BPM = (business process management) tool with pg as the database, and we=E2=80=99= d like to explore conditional logic within steps in a workflow being = triggers as functions when a certain JSONB node changes.
An example -> If (form field contains =E2=80=9Cbob=E2=80=9D= ) then (change state of another object)

In = effect, since all our objects are JSONB - we wanted to =E2=80=9Clisten=E2=80= =9D to a change in one JSONB node in order to make a corresponding = change in another JSONB node.

Can this be = either native in postgres, or even a pg function?

In general - how do people approach rules and decision logic = in Postgres at a very large scale e.g. say 100,000 rules that must be = evaluated on a commit?

If it makes more = sense - please see the example screenshot on our features page at https://tallyfy.com/features - which visualizes = conditional branching.

That *looks* like each team gets their own = workflows, so you could restrict your rules search to the rules for the = team that created the change. Anything you could do to restrict the = number of rules would help, of course.

One option would be to express rules as an = (antecedent, consequent) pair, where the antecedent is the argument to = json_extract_path and the consequent is the rest of the arguments to a = function that carries out the change =E2=80=94 something along those = lines, in any event.

Iterating = through a small set of such rules would be tolerable. If you really do = have 100,000 rules that have to be applied to each change, my first = thought is to use a SAT solver. But even there, you would have to = compute the results of all your antecedents, so it would depend how many = of those you have.

You might use a = map-reduce farm, but all the machines are going to be hitting your = database pretty hard.

You might be = able to reduce your 100,000 antecedents to something a bit more = tractable by finding the overlaps between them and so only computing the = shared parts of antecedents once.

You = might want to use LISTEN-NOTIFY so an external logic engine =E2=80=94 = perhaps using map-reduce, perhaps you just have a set of logic engines = that each takes some part of that 100,000 rules and just brute forces = them =E2=80=94 gets to know about the changes.

I=E2=80=99d need to know some more details about = what you=E2=80=99re up to, to give you a more specific answer, but = that=E2=80=99s what occurs to me given what you=E2=80=99ve told = us.

3D""= --Apple-Mail=_94C1D810-9FB5-4B66-949C-BE24BF63C759--