Copyright | (c) James King 2020 2021 |
---|---|
License | BSD3 |
Maintainer | [email protected] |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
Database.PostgreSQL.Replicant
Description
Connect to a PostgreSQL server as a logical replication client and receive changes.
The basic API is this:
withLogicalStream defaultSettings $ change -> do
print change
catch
err -> do
show err
This is a low-level library meant to give the primitives necessary to library authors to add streaming replication support. The API here to rather simplistic but should be hooked up to something like conduit to provide better ergonomics.
Synopsis
- data Change = Change {
- changeNextLSN :: LSN
- changeDeltas :: [WalLogData]
- data Column = Column {
- columnName :: !Text
- columnType :: !Text
- columnValue :: !WalValue
- data Delete = Delete {
- deleteSchema :: !Text
- deleteTable :: !Text
- deleteColumns :: ![Column]
- data Insert = Insert {
- insertSchema :: !String
- insertTable :: !String
- insertColumns :: ![Column]
- data Message = Message {
- messageTransactional :: !Bool
- messagePrefix :: !Text
- messageContent :: !Text
- data PgSettings = PgSettings {
- pgUser :: String
- pgPassword :: Maybe String
- pgDbName :: String
- pgHost :: String
- pgPort :: String
- pgSlotName :: String
- pgUpdateDelay :: String
- data ReplicantConnection
- data Update = Update {
- updateSchema :: !Text
- updateTable :: !Text
- updateColumns :: ![Column]
- data WalLogData
- connect :: PgSettings -> IO ReplicantConnection
- getConnection :: ReplicantConnection -> Connection
- unsafeCreateConnection :: Connection -> ReplicantConnection
- withLogicalStream :: PgSettings -> (Change -> IO LSN) -> IO ()
Types
Constructors
Change | |
Fields
|
Instances
Eq Change Source # | |
Show Change Source # | |
Generic Change Source # | |
ToJSON Change Source # | |
Defined in Database.PostgreSQL.Replicant.Message | |
FromJSON Change Source # | |
type Rep Change Source # | |
Defined in Database.PostgreSQL.Replicant.Message type Rep Change = D1 ('MetaData "Change" "Database.PostgreSQL.Replicant.Message" "postgresql-replicant-0.2.0.1-LfvKLvORtF58sQk3dBptIQ" 'False) (C1 ('MetaCons "Change" 'PrefixI 'True) (S1 ('MetaSel ('Just "changeNextLSN") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 LSN) :*: S1 ('MetaSel ('Just "changeDeltas") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 [WalLogData]))) |
Represents a single table column. We only support the wal2json
logical decoder plugin and make no attempt to parse anything but
JSON-like primitives.
Constructors
Column | |
Fields
|
Represents a single delete query in the logical replication format
Constructors
Delete | |
Fields
|
Represents a single insert query in the logical replication format.
Constructors
Insert | |
Fields
|
Occasionally the server may also send these for informational purposes and can be ignored. May be used internally.
Constructors
Message | |
Fields
|
data PgSettings Source #
Constructors
PgSettings | |
Fields
|
Instances
Eq PgSettings Source # | |
Defined in Database.PostgreSQL.Replicant.Settings | |
Show PgSettings Source # | |
Defined in Database.PostgreSQL.Replicant.Settings Methods showsPrec :: Int -> PgSettings -> ShowS # show :: PgSettings -> String # showList :: [PgSettings] -> ShowS # |
data ReplicantConnection Source #
Instances
Eq ReplicantConnection Source # | |
Defined in Database.PostgreSQL.Replicant.Connection Methods (==) :: ReplicantConnection -> ReplicantConnection -> Bool # (/=) :: ReplicantConnection -> ReplicantConnection -> Bool # |
Represents a single update query in the logical replication format.
Constructors
Update | |
Fields
|
data WalLogData Source #
Instances
Connection Handling
connect :: PgSettings -> IO ReplicantConnection Source #
Connect to the PostgreSQL server in replication mode
unsafeCreateConnection :: Connection -> ReplicantConnection Source #
Unsafe function for wrapping regular libpq Connection. This is unsafe because the Connection needs to be set up to send replication commands. Improperly constructed connections can lead to runtime exceptions.
Functions
withLogicalStream :: PgSettings -> (Change -> IO LSN) -> IO () Source #
Connect to a PostgreSQL database as a user with the replication attribute and start receiving changes using the logical replication protocol. Logical replication happens at the query level so the changes you get represent the set of queries in a transaction: insert, update, and delete.
This function will create the replication slot, if it doesn't exist, or reconnect to it otherwise and restart the stream from where the replication slot left off.
This function can throw exceptions in IO
and shut-down the
socket in case of any error.