Safe Haskell | None |
---|---|
Language | Haskell2010 |
TsWeb has a strong notion that postgres should be configured in a warm-standby
mode, with read-only queries being shipped to synchronous standbys whenever
possible. This module defines types for descriminating between read-only and
read-write connections, and also functions for making postgres connections,
pools, etc. The connect
function is working well enough for me, but will
undoubtedly need to be improved to support the full depth of postgresql
connection options.
Synopsis
- type SomeConn t = Tagged t Connection
- type ReadOnlyConn = SomeConn ReadOnly
- type ReadWriteConn = SomeConn ReadWrite
- type ReadOnlyPool = Pool ReadOnlyConn
- type ReadWritePool = Pool ReadWriteConn
- data ReadOnly
- data ReadWrite
- type HostName = String
- type DbName = String
- type SubPools = Int
- type KeepOpen = Int
- count :: ProjectibleWithPredicate AnyType Postgres (WithExprContext (BeamSqlBackendExpressionSyntax' Postgres)) t => Q Postgres db (QNested s0) t -> Q Postgres db s0 (QGenExpr QValueContext Postgres s0 Int)
- pool :: HostName -> DbName -> String -> SubPools -> NominalDiffTime -> KeepOpen -> IO (Pool (SomeConn t))
- withConnection :: Pool (SomeConn a) -> (SomeConn a -> IO b) -> IO b
- connect :: HostName -> DbName -> String -> IO (SomeConn t)
- withSavepoint :: SomeConn t -> IO a -> IO a
- withTransaction :: SomeConn t -> IO a -> IO a
- readOnly :: SomeConn t -> Pg a -> IO a
- readOnlyDebug :: SomeConn t -> Pg a -> IO a
- readWrite :: ReadWriteConn -> Pg a -> IO a
- readWriteDebug :: ReadWriteConn -> Pg a -> IO a
Documentation
type SomeConn t = Tagged t Connection Source #
Wrapper for some sort of Postgres connection; a raw `SomeConn t`
represents either a read-only or a read-write connection, but the concrete
ReadOnlyConn
and ReadWriteConn
are probably more useful in general.
type ReadOnlyConn = SomeConn ReadOnly Source #
Concrete read-only connection. Instantiate with connect
type ReadWriteConn = SomeConn ReadWrite Source #
Concrete read-write connection. Instantiate with connect
type ReadOnlyPool = Pool ReadOnlyConn Source #
Pool of read-only connections. Instantiate with pool
type ReadWritePool = Pool ReadWriteConn Source #
Pool of read-write connections. Instantiate with pool
count :: ProjectibleWithPredicate AnyType Postgres (WithExprContext (BeamSqlBackendExpressionSyntax' Postgres)) t => Q Postgres db (QNested s0) t -> Q Postgres db s0 (QGenExpr QValueContext Postgres s0 Int) Source #
An example of how Database.Beam
does counts; this really doesn't belong
here, and was just written for preliminary testing.
:: HostName | Postgres hostname/address |
-> DbName | Postgres database name |
-> String | User to connect as |
-> SubPools | Number of sub-pools to maintain (1 is fine) |
-> NominalDiffTime | How long to let an idle db connection linger |
-> KeepOpen | Max number of connections per stripe |
-> IO (Pool (SomeConn t)) |
Create a resource pool of either read-only or read-write postgres
connections. The docs for createPool
give the best description
for how this works.
withConnection :: Pool (SomeConn a) -> (SomeConn a -> IO b) -> IO b Source #
Run an action in a Pool
connection
readOnly :: SomeConn t -> Pg a -> IO a Source #
Run a read-only query; this will actually run any query at all, so higher-level logic should ensure that only read-only queries hit this function.
readOnlyDebug :: SomeConn t -> Pg a -> IO a Source #
Same as readOnly
, but prints any SQL that it runs.
readWrite :: ReadWriteConn -> Pg a -> IO a Source #
Run a query against a ReadWriteConn
readWriteDebug :: ReadWriteConn -> Pg a -> IO a Source #
Same as readWrite
, but printing all the SQL that gets executed.