postgresql-replicant-0.2.0.1: PostgreSQL logical streaming replication library
Copyright(c) James King 2020 2021
LicenseBSD3
Maintainerjames@agentultra.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

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

Types

data Change Source #

Constructors

Change 

Fields

Instances

Instances details
Eq Change Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

Methods

(==) :: Change -> Change -> Bool #

(/=) :: Change -> Change -> Bool #

Show Change Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

Generic Change Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

Associated Types

type Rep Change :: Type -> Type #

Methods

from :: Change -> Rep Change x #

to :: Rep Change x -> Change #

ToJSON Change Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

FromJSON Change Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

type Rep Change Source # 
Instance details

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])))

data Column Source #

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 

Instances

Instances details
Eq Column Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

Methods

(==) :: Column -> Column -> Bool #

(/=) :: Column -> Column -> Bool #

Show Column Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

data Delete Source #

Represents a single delete query in the logical replication format

Constructors

Delete 

data Insert Source #

Represents a single insert query in the logical replication format.

Constructors

Insert 

data Message Source #

Occasionally the server may also send these for informational purposes and can be ignored. May be used internally.

data PgSettings Source #

Constructors

PgSettings 

Fields

Instances

Instances details
Eq PgSettings Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Settings

Show PgSettings Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Settings

data Update Source #

Represents a single update query in the logical replication format.

Constructors

Update 

data WalLogData Source #

Instances

Instances details
Eq WalLogData Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

Show WalLogData Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

Generic WalLogData Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

Associated Types

type Rep WalLogData :: Type -> Type #

ToJSON WalLogData Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

FromJSON WalLogData Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

type Rep WalLogData Source # 
Instance details

Defined in Database.PostgreSQL.Replicant.Message

type Rep WalLogData = D1 ('MetaData "WalLogData" "Database.PostgreSQL.Replicant.Message" "postgresql-replicant-0.2.0.1-LfvKLvORtF58sQk3dBptIQ" 'False) ((C1 ('MetaCons "WInsert" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Insert)) :+: C1 ('MetaCons "WUpdate" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Update))) :+: (C1 ('MetaCons "WDelete" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Delete)) :+: C1 ('MetaCons "WMessage" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Message))))

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.