diff --git a/postgrest.cabal b/postgrest.cabal index cc35428cee..d1eb7f0c8b 100644 --- a/postgrest.cabal +++ b/postgrest.cabal @@ -105,6 +105,7 @@ library , network-uri >= 2.6.1 && < 2.8 , optparse-applicative >= 0.13 && < 0.18 , parsec >= 3.1.11 && < 3.2 + , postgresql-libpq , protolude >= 0.3.1 && < 0.4 , regex-tdfa >= 1.2.2 && < 1.4 , retry >= 0.7.4 && < 0.10 diff --git a/src/PostgREST/Query.hs b/src/PostgREST/Query.hs index a18bca604b..c8648588bb 100644 --- a/src/PostgREST/Query.hs +++ b/src/PostgREST/Query.hs @@ -24,6 +24,7 @@ import qualified Hasql.Decoders as HD import qualified Hasql.DynamicStatements.Snippet as SQL (Snippet) import qualified Hasql.DynamicStatements.Statement as SQL import qualified Hasql.Encoders as HE +import qualified Hasql.Session as Session import qualified Hasql.Statement as SQL import qualified Hasql.Transaction as SQL @@ -235,11 +236,19 @@ optionalRollback AppConfig{..} ApiRequest{iPreferences=Preferences{..}} = do shouldRollback = configDbTxAllowOverride && preferTransaction == Just Rollback +usePipeline :: Bool +usePipeline = True + +queuePipelineStatement :: params -> SQL.Statement params () -> SQL.Transaction () +queuePipelineStatement params stmt = + if usePipeline then SQL.inTransaction $ Session.queuePipelineStatement params stmt + else SQL.statement params stmt + -- | Runs local (transaction scoped) GUCs for every request. setPgLocals :: AppConfig -> KM.KeyMap JSON.Value -> Text -> ApiRequest -> PgVersion -> DbHandler () setPgLocals conf claims role req actualPgVersion = lift $ - SQL.statement mempty $ SQL.dynamicallyParameterized + queuePipelineStatement mempty $ SQL.dynamicallyParameterized ("select " <> intercalateSnippet ", " (searchPathSql : roleSql ++ claimsSql ++ [methodSql, pathSql] ++ headersSql ++ cookiesSql ++ appSettingsSql)) HD.noResult (configDbPreparedStatements conf) where @@ -270,7 +279,7 @@ setPgLocals conf claims role req actualPgVersion = lift $ -- | Runs the pre-request function. runPreReq :: AppConfig -> DbHandler () -runPreReq conf = lift $ traverse_ (SQL.statement mempty . stmt) (configDbPreRequest conf) +runPreReq conf = lift $ traverse_ (queuePipelineStatement mempty . stmt) (configDbPreRequest conf) where stmt req = SQL.Statement ("select " <> fromQi req <> "()")