Skip to content

Commit

Permalink
use pipeline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
robx committed Mar 16, 2023
1 parent 0fd9df7 commit e8d50d3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions postgrest.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions src/PostgREST/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 <> "()")
Expand Down

0 comments on commit e8d50d3

Please sign in to comment.