Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ggNf3-0003Bi-Nn for pgsql-general@arkaria.postgresql.org; Mon, 07 Jan 2019 05:41:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ggNf2-0006nS-8p for pgsql-general@arkaria.postgresql.org; Mon, 07 Jan 2019 05:41:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ggNf1-0006nA-RM for pgsql-general@lists.postgresql.org; Mon, 07 Jan 2019 05:41:16 +0000 Received: from mail-lj1-x244.google.com ([2a00:1450:4864:20::244]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ggNey-0001U7-QN for pgsql-general@postgresql.org; Mon, 07 Jan 2019 05:41:14 +0000 Received: by mail-lj1-x244.google.com with SMTP id k15-v6so37061277ljc.8 for ; Sun, 06 Jan 2019 21:41:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=2kBQYln+7q3+Q6Dyrs2+/nSH80XhXG2zQorNbnzbsMM=; b=HJSrgFpGCqMM+6of+9YN8NLn5SA3ecwirNmTJGzVYrrt6+M2k+Qg34OnDN0fLj2ceA pj3pa2Il1ZoKSgIwt35BM04wNwm6tSUAHJ33NQxXaLJWQPNY+eVHrfxFOG39VKckrqbu fxyycFzSZM53SjojO7/8UiZnDU3S1ZH3RTDy0RQtvgAZOjWflIoWayqgk3li9+vZfixv UvFOuHSuR2Qe1BO8f5iZH6gvvV3LcWktX2EqBBlJG8YTgL5LNFxHHT5RNEWe4cNXhB7z 8p4AL+7rE3CcVEb3spgOs5ClyRMgk/LaVc9mXDrFi/wsDMaYaDtkBcgt9hm0KhCdspOO wu6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=2kBQYln+7q3+Q6Dyrs2+/nSH80XhXG2zQorNbnzbsMM=; b=SJq9rOJTpmbDh7QbdtXsuhvr2qquzUWKJ4/EUKl8l563EKHHjZ047kazQ4orho9kSu 0a7kTf0LHyA2Kr+OkE96HQW826bMKO7qWRHmsJZCg1HIzuNcJydoj00tFGH2uj5U1MWe PWlm2aZvH/OpqGmT4g5FGPvCd0OICb1OYlssBsw0Nouz9uPUrNORiVyESN/NZ+uHhSra w4GBmtqGogSxIs6ySSQ9WLf8IYxTpczb79OsiOXQpSkPQvwc3y2SsIQqvDL3wrsdDGus RJ81IUdkRAG2e81zokRDn3hGX+z4kJ6FOEsws+CzijSTYiHHIPDHC6HWXZCVZWK09dUe nMQQ== X-Gm-Message-State: AJcUukcM3zydFh4ReBloQWY9wMynaELILeQSL+418+5IEshSZJ4sLiKv NPKsQTiMuBQBo3ApuqFLODVl9G6i8l3Ln7utRuQ= X-Google-Smtp-Source: ALg8bN6JYsuMoiKLUn4IIPe/iG5m+YxUI/4eXMSUEFhcqzSAlWhFYUvcQGZ2Ja8J1bCOGh0V+rUdMcOhMUqQBhg6i00= X-Received: by 2002:a2e:9356:: with SMTP id m22-v6mr33153676ljh.135.1546839670174; Sun, 06 Jan 2019 21:41:10 -0800 (PST) MIME-Version: 1.0 References: <1iho1ehdf227kvm7p6vn11hg11jf19i8iv@4ax.com> In-Reply-To: From: Mitar Date: Sun, 6 Jan 2019 21:40:57 -0800 Message-ID: Subject: Re: Watching for view changes To: George Neuner Cc: pgsql-general@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi! I have a followup to this thread. George, thanks for all the help. I spend some time now investigating various approaches here and I am reporting here some findings, so that they might help others as well. First, I have tried the approach with MATERIALIZED VIEWs to hold the cached contents of the query. I attached triggers to all source tables which then notified (using LISTEN/NOTIFY) the client about changes. Client throttled those notifications and eventually triggered a REFRESH MATERIALIZED VIEW. Because I also attached triggers on the materialized view, I got notifications (using LISTEN/NOTIFY) of what rows have changed. Client might decided to fetch also rows themselves. For this to work well I made two patches. A patch to allow creation of TEMPORARY MATERIALIZED VIEWs [1] and a patch to allow attaching triggers on materialized views [2]. In the second patch I also changed the REFRESH MATERIALIZED VIEW CONCURRENTLY logic to issue not just REMOVEs and INSERTs for changed rows, but to compute which rows have changed and issue UPDATEs for them as well. This makes it easier for the client to know what changed. This worked well but it was slower than some other packages I have found on the web which were trying to provide a similar functionality. I made a benchmark to compare them [3] and found out that there was room for improvement. First, instead of sending updates of a MATERIALIZED VIEW using LISTEN/NOTIFY and then fetching rows, I instead used a trigger to copy changes to another temporary table, and then just used DELETE FROM temp_table RETURNING * to get all results from a table and returning it to the client, all inside same transaction, so data in that temporary table was never committed. This made things a bit better, mostly latency between a change and getting it to the client became more predictable. Before there were sometimes quite large spikes. Once I did that I realized that in fact MATERIALIZED VIEW is not really necessary. All I need is a place to cache previous results of the query, but I do not really care about updates to the MATERIALIZED VIEW. So I decided to do the following. If I already have a temporary table with previous results, when I want to refresh my query, I create a new temporary table using CREATE TABLE AS using the PREPAREd query, I compute diff between those two tables in the same way as REFRESH MATERIALIZED VIEW CONCURRENTLY does, in one query, and I return those results to the client. Then I just DROP TABLE old cache table, and rename new cache table to old cache name. So instead of computing a diff, updating materialized view, running triggers, and copying to the table, I just compute a diff and this is it. This works now very well. I made and published a package doings this [4]. Of course, the main problem is still that for every change in source table I have to eventually refresh the query. And without more logic this can really become problematic. Ideally, we could reuse some ideas/code from Incremental View Maintenance [5] to compute what changes to the query results should happen based on changes to source tables. Then we could just push those to the client (and update temporary table). We would then do full refresh only when things could not be inferred. Thanks again for everything. [1] https://commitfest.postgresql.org/21/1951/ [2] https://commitfest.postgresql.org/21/1948/ [3] https://github.com/mitar/node-pg-reactivity-benchmark [4] https://github.com/tozd/node-reactive-postgres [5] https://www.postgresql.org/message-id/flat/20181227215726.4d166b4874f8983a641123f5%40sraoss.co.jp Mitar -- http://mitar.tnode.com/ https://twitter.com/mitar_m