logical is tool for synchronizing from PostgreSQL to custom handler through replication slot
requires Go 1.13 or greater.
Postgresql 10.0+
- change
postgresql.conf
wal_level = 'logical'; # minimal, replica, or logical. postgres default replica, It determines how much information is written to the wal
max_replication_slots = 10; # max number of replication slots, The value should be greater than 1
- change
pg_hba.conf
# Add a new line below `replication`, $ is a variable
host $dbname $user $address md5 # example: `host webstore itmeng 192.168.0.1/24 md5`
go get github.com/atopx/logical
package main
import (
"context"
"fmt"
"github.com/yanmengfei/logical/client"
"github.com/yanmengfei/logical/logger"
"github.com/yanmengfei/logical/model"
"go.uber.org/zap"
)
type Consumer struct{}
func (h *Consumer) Deal(records []*model.Waldata) {
for _, record := range records {
fmt.Println(record)
// consumer data
}
}
func main() {
err := logger.Setup(zap.InfoLevel.String())
if err != nil {
panic(err)
}
c := client.New(&client.Config{
Host: "127.0.0.1",
Port: 5432,
User: "itmeng",
Password: "postgres_logical",
Database: "webstore",
Table: "book",
Slot: "book_cache_slot",
})
c.Register(new(Consumer))
logger.Info("start postgresql logical replication client")
if err = c.Start(context.Background()); err != nil {
logger.Panic(err.Error())
}
}