From 2c81857d41825f1c0e430b1977000438c5d056f7 Mon Sep 17 00:00:00 2001 From: Shane O'Brien Date: Mon, 16 Oct 2023 08:45:04 +0100 Subject: [PATCH] Switch to `WITH _ AS MATERIALIZED (_)` for `materialize` --- src/Rel8/Query/Materialize.hs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Rel8/Query/Materialize.hs b/src/Rel8/Query/Materialize.hs index 618d2d2a..38b1ac33 100644 --- a/src/Rel8/Query/Materialize.hs +++ b/src/Rel8/Query/Materialize.hs @@ -2,7 +2,8 @@ module Rel8.Query.Materialize ( materialize - ) where + ) +where -- base import Prelude @@ -26,19 +27,16 @@ import Rel8.Table.Opaleye ( unpackspec ) -- you're doing this can sometimes help to nudge it in a particular direction. -- -- 'materialize' is currently implemented in terms of Postgres' --- [@WITH](https://www.postgresql.org/docs/current/queries-with.html) syntax. --- Note that on newer versions of PostgreSQL starting with version 12, @WITH@ --- doesn't always automatically materialize if the results of the query aren't --- used more than once. We reserve the right to change the implementation of --- 'materialize' to use the newer @WITH foo AS MATERIALIZED bar@ syntax --- introduced in PostgreSQL 12 in the future. Currently Rel8 does not use --- @AS MATERIALIZED@ to support earlier PostgreSQL versions. +-- [@WITH](https://www.postgresql.org/docs/current/queries-with.html) syntax, +-- specifically the @WITH _ AS MATERIALIZED (_)@ form introduced in PostgreSQL +-- 12. This means that 'materialize' can only be used with PostgreSQL 12 or +-- newer. materialize :: (Table Expr a, Table Expr b) => Query a -> (Query a -> Query b) -> Query b materialize query f = (>>= rebind "with") . fromOpaleye $ - withExplicit unpackspec + withMaterializedExplicit unpackspec (toOpaleye query') (toOpaleye . f . fromOpaleye) where - query' = query >>= rebind "materialize" + query' = query