Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mwNhk-0007ZE-Hv for pgsql-general@arkaria.postgresql.org; Sun, 12 Dec 2021 12:11:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mwNhj-0008JT-31 for pgsql-general@arkaria.postgresql.org; Sun, 12 Dec 2021 12:11:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mwNhi-0008JK-P1 for pgsql-general@lists.postgresql.org; Sun, 12 Dec 2021 12:11:46 +0000 Received: from mail.gavagai.nl ([5.135.160.52]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mwNhe-0000Kc-LE for pgsql-general@lists.postgresql.org; Sun, 12 Dec 2021 12:11:45 +0000 Received: from tipi (ns3002999.ip-151-80-42.eu [151.80.42.145]) by mail.gavagai.nl (MAIL-O-TRON) with ESMTPSA id 33AF9EED588 for ; Sun, 12 Dec 2021 13:11:39 +0100 (CET) Date: Sun, 12 Dec 2021 13:11:36 +0100 From: Wicher Cc: Subject: Re: Advice on using materialized views Message-ID: <20211212131136.476d4de2@tipi> In-Reply-To: <1639065987898@dmwebmail.dmwebmail.chezphil.org> References: <20211207103251.59354436@tipi> <1639065987898@dmwebmail.dmwebmail.chezphil.org> X-Mailer: Claws Mail 3.18.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 09 Dec 2021 16:06:27 +0000 "Phil Endecott" wrote: > Thanks both for your replies. > > Wicher wrote: > > On Mon, 06 Dec 2021 18:48:47 +0000 > > "Phil Endecott" wrote: > >> and > >> I need to modify the definition of a view that filters the "new" > >> values from the raw table each time the materialised view is > >> refreshed. > > > > You won't necessarily need to rewrite the "recent data" view definitions, I think. What is > > deemed "recent" depends on what's in the materialized views (it'd be anything newer than > > whatever is in there). The good news is that you can simply query for that :-) > > So trivially, in your "the data that is more recent than the stuff from the materialized > > views" non-materialized view you'd use a definition like > > SELECT .... WHERE sometimestamp > (select max(sometimestamp) from the_materialized_view) > > or something along those lines. > > > I guess I was hoping that someone would suggest a more "magic" > way to do this sort of thing. Actually I'm a bit surprised that > materialised views don't event have a way to either > > - Refresh a materialised view whenever a source table is modified; > > - Refresh a materialised view whenever it is read, if a source table > has changed since it was last refreshed. > > Beyond that, I could imagine smart updates where e.g. if you > modify source table rows with primary key K, then you only need > to refresh materialised view rows derived from K. > > I think this could all be done on top of triggers. I wonder, do any > other databases do things like this automagically? > Not too long ago I asking the list something similar but came up short: https://www.postgresql.org/message-id/20211129102315.058595fe@tipi Here's my own take on that problem area, tangent to your question. This project aims to do (or make it easier to do) the things you mention: https://git.sr.ht/~nullenenenen/DBSamizdat It supports your first use case out of the box, and may make your second use case easier to accommodate, give it a go :-) There's a sweet spot for materialized views. But at some point (volume/computational load/freshness requirements) it becomes necessary to use tables instead so that you can indeed implement efficient partial recalculation. As far as I know. I too am curious about other approaches.