Documentation
¶
Index ¶
- func CreatePublication(ctx context.Context, conn *pgconn.PgConn, name string, tables []pgx.Identifier) error
- func CreateSlot(ctx context.Context, conn *pgconn.PgConn, name string) error
- func DropPublication(ctx context.Context, conn *pgconn.PgConn, name string) error
- func DropSlot(ctx context.Context, conn *pgconn.PgConn, name string) error
- type BeginOperation
- type Broker
- type Collector
- type CommitOperation
- type Connector
- type DeleteOperation
- type Handler
- type InsertOperation
- type Row
- type TruncateOperation
- type UpdateOperation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreatePublication ¶
func CreatePublication(ctx context.Context, conn *pgconn.PgConn, name string, tables []pgx.Identifier) error
CreatePublication creates a PUBLICATION for the given tables. If tables is empty, the publication is created FOR ALL TABLES. Use a regular (not replication-mode) connection.
func CreateSlot ¶
CreateSlot creates a logical replication slot with the pgoutput plugin. It is safe to call only against a connection opened in replication mode.
func DropPublication ¶
DropPublication drops a publication if it exists.
Types ¶
type BeginOperation ¶
BeginOperation marks the start of a transaction.
type Broker ¶
type Broker struct {
// Conn is an open replication connection (opened with
// "?replication=database" in the connection string).
Conn *pgconn.PgConn
// Handler receives decoded operations.
Handler Handler
// Slot is the logical replication slot name (see CreateSlot).
Slot string
// Publication is the publication name (see CreatePublication).
Publication string
}
Broker runs the replication loop. It starts streaming from Slot using Publication, decodes WAL payloads, and dispatches them to Handler.
The slot's confirmed flush LSN advances only after Handler.HandleCommit returns nil, so a failed transaction replays on restart.
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector decodes pgoutput WAL payloads into handler calls.
type CommitOperation ¶
type CommitOperation struct {
CommitLSN pglogrepl.LSN
TransactionEndLSN pglogrepl.LSN
CommitTime time.Time
}
CommitOperation marks the end of a transaction. The broker advances the replication slot to TransactionEndLSN after the handler returns nil.
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector wraps a replication connection: starts replication for a slot, sends periodic standby status updates, and receives backend messages.
func (*Connector) Ack ¶
Ack advances the position reported back to the primary. The next Status call will send it as the confirmed flush LSN.
func (*Connector) Receive ¶
Receive reads the next backend message, respecting the keepalive deadline.
type DeleteOperation ¶
type DeleteOperation struct {
TableName pgx.Identifier
OldRow pgx.CollectableRow
}
DeleteOperation represents a single row delete. OldRow contains the key columns (or all columns under REPLICA IDENTITY FULL).
type Handler ¶
type Handler interface {
HandleBegin(*BeginOperation) error
HandleCommit(*CommitOperation) error
HandleInsert(*InsertOperation) error
HandleUpdate(*UpdateOperation) error
HandleDelete(*DeleteOperation) error
HandleTruncate(*TruncateOperation) error
}
Handler receives decoded replication events. Every method is called in WAL order on a single goroutine. Returning a non-nil error aborts the broker loop; the slot's confirmed LSN is only advanced after HandleCommit returns nil, so a failed event replays on restart.
type InsertOperation ¶
type InsertOperation struct {
TableName pgx.Identifier
NewRow pgx.CollectableRow
}
InsertOperation represents a single row insert.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row adapts a pgoutput TupleData to pgx.CollectableRow, so handlers can decode replicated values with pgx.RowTo / pgx.RowToStructByName.
func (*Row) FieldDescriptions ¶
func (r *Row) FieldDescriptions() []pgconn.FieldDescription
FieldDescriptions implements pgx.CollectableRow.
type TruncateOperation ¶
type TruncateOperation struct {
TableNames []pgx.Identifier
Cascade bool
RestartIdentity bool
}
TruncateOperation represents a TRUNCATE affecting one or more tables.
type UpdateOperation ¶
type UpdateOperation struct {
TableName pgx.Identifier
NewRow pgx.CollectableRow
OldRow pgx.CollectableRow
}
UpdateOperation represents a single row update. OldRow is nil unless the table has REPLICA IDENTITY FULL or an explicit replica identity index.