Documentation
¶
Overview ¶
Package postgres is the built-in PostgreSQL dialect for the msgin sql adapter engine (github.com/kartaladev/msgin/adapter/database/sql). It owns the exact PostgreSQL SQL for the lease/claim source, the lock/FOR UPDATE strategy, the outbound INSERT, and the idempotent-consumer dedup inbox.
It is a leaf-test dialect module (ADR 0011): it requires the engine ONLY — no SQL driver, no testcontainers, no testify — so a production consumer that imports it pays only the engine's (stdlib + clockwork) dependency cost. The caller injects the PostgreSQL database/sql driver (e.g. jackc/pgx/v5/stdlib) and provisions the *sql.DB; msgin does not import a driver.
The public entry points are:
- LeaseDialect() — the msginsql.LeaseDialect passed to NewPollingSource/NewOutboundAdapter (the single stateless value also satisfies msginsql.LockDialect for WithStrategy(StrategyLockForUpdate)).
- InboxDialect() — the msginsql.InboxDialect passed to NewInboxDeduper.
- DDL(table) / InboxDDL(table) — the reference CREATE TABLE statements for callers to fold into their own migration tool (msgin never runs DDL on the production path; ADR 0010 D2).
Behavior is identical to the pre-split built-in: this package MOVED verbatim out of the engine (Plan 006 Task 4); only its module boundary changed.
Example (MemoryGroupStore) ¶
Example_memoryGroupStore wires an Aggregator over the in-memory msgin.MessageGroupStore (adapter/memory) and runs it end to end — the only Output-checked example in this file, so it needs no live database or container. See Example_sqlGroupStore below: swapping the store for a durable msginsql.GroupStore is the ONLY change required to make this same wiring durable and safe across multiple processes.
package main
import (
"context"
"fmt"
msgin "github.com/kartaladev/msgin"
"github.com/kartaladev/msgin/adapter/memory"
"github.com/kartaladev/msgin/channel"
"github.com/kartaladev/msgin/routing"
)
// groupStoreLineItem is one line of an order, correlated by orderID toward a
// released order total — mirrors msgin's own ExampleAggregator payload.
type groupStoreLineItem struct {
sku string
price int
}
// wireGroupAggregator builds an Aggregator over store and out the SAME way
// regardless of which msgin.MessageGroupStore implementation store is: this
// wiring is the whole point of the MessageGroupStore SPI (ADR 0020/0021) — an
// Aggregator built over memory.NewGroupStore() (in-process only) and one
// built over msginsql.NewGroupStore(...) (durable, multi-process) are
// interchangeable from here down.
func wireGroupAggregator(store msgin.MessageGroupStore, out msgin.MessageChannel) (*routing.Aggregator, error) {
return routing.NewAggregator[groupStoreLineItem, int](store,
func(_ context.Context, group []msgin.Message[groupStoreLineItem]) (msgin.Message[int], error) {
total := 0
for _, m := range group {
total += m.Payload().price
}
return msgin.New(total), nil
},
routing.WithOutputChannel(out),
routing.WithCompletionSize(3),
)
}
// runGroupAggregatorDemo drives 3 correlated line-items through agg and
// prints the released order total. It also starts agg.Run in the
// background: a durable store's crash recovery rides ENTIRELY on the
// reaper Run drives (see msginsql.GroupStore's "go agg.Run(ctx) is
// REQUIRED" doc) — a caller who never calls Run silently never
// crash-recovers a group stuck between ClaimGroup and SettleGroup. Showing
// it here, even though the in-memory store below never crashes
// mid-process, keeps this wiring identical to the durable one in
// Example_sqlGroupStore.
func runGroupAggregatorDemo(store msgin.MessageGroupStore) {
out := channel.NewDirectChannel()
if _, err := out.Subscribe(msgin.HandlerFunc(func(_ context.Context, m msgin.Message[any]) error {
fmt.Printf("order total: %v\n", m.Payload())
return nil
})); err != nil {
panic(err)
}
agg, err := wireGroupAggregator(store, out)
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go agg.Run(ctx)
items := []groupStoreLineItem{
{sku: "widget", price: 10},
{sku: "gadget", price: 25},
{sku: "gizmo", price: 15},
}
for i, it := range items {
msg := msgin.New[any](it, msgin.WithID(fmt.Sprintf("line-%d", i)), msgin.WithHeaders(map[string]any{
msgin.HeaderCorrelationID: "order-1",
}))
if err := agg.Handle(context.Background(), msg); err != nil {
panic(err)
}
}
}
func main() {
store, err := memory.NewGroupStore()
if err != nil {
panic(err)
}
runGroupAggregatorDemo(store)
}
Output: order total: 50
Example (SqlGroupStore) ¶
Example_sqlGroupStore is a compile-only reference for the durable, multi-process equivalent of Example_memoryGroupStore — it has no "Output:" comment, so `go test` compiles it (proving the exact call shape below is correct and stays correct) but never executes it, which is why db is left nil rather than opened against a real PostgreSQL instance: a caller adopting this wires db to an already-open, schema-provisioned *sql.DB (postgres.GroupDDL, or GroupStore.EnsureSchema for dev/test) of their own.
The ONLY change from Example_memoryGroupStore is the store constructor: msginsql.NewGroupStore(db, table, postgres.GroupDialect()) in place of memory.NewGroupStore(). Everything below wireGroupAggregator — the Aggregator wiring, the release strategy, agg.Handle — is identical either way. What is NOT optional for the durable store is go agg.Run(ctx) (already present in runGroupAggregatorDemo, shared by both examples): msginsql.GroupStore.RecoverInterval() returns its lease TTL (not 0), so a group crashed between ClaimGroup and SettleGroup is only ever recovered by the reaper Run drives — omit Run and that crash recovery silently never happens.
package main
import (
"context"
stdsql "database/sql"
"fmt"
msgin "github.com/kartaladev/msgin"
msginsql "github.com/kartaladev/msgin/adapter/database/sql"
"github.com/kartaladev/msgin/adapter/database/sql/postgres"
"github.com/kartaladev/msgin/channel"
"github.com/kartaladev/msgin/routing"
)
// groupStoreLineItem is one line of an order, correlated by orderID toward a
// released order total — mirrors msgin's own ExampleAggregator payload.
type groupStoreLineItem struct {
sku string
price int
}
// wireGroupAggregator builds an Aggregator over store and out the SAME way
// regardless of which msgin.MessageGroupStore implementation store is: this
// wiring is the whole point of the MessageGroupStore SPI (ADR 0020/0021) — an
// Aggregator built over memory.NewGroupStore() (in-process only) and one
// built over msginsql.NewGroupStore(...) (durable, multi-process) are
// interchangeable from here down.
func wireGroupAggregator(store msgin.MessageGroupStore, out msgin.MessageChannel) (*routing.Aggregator, error) {
return routing.NewAggregator[groupStoreLineItem, int](store,
func(_ context.Context, group []msgin.Message[groupStoreLineItem]) (msgin.Message[int], error) {
total := 0
for _, m := range group {
total += m.Payload().price
}
return msgin.New(total), nil
},
routing.WithOutputChannel(out),
routing.WithCompletionSize(3),
)
}
// runGroupAggregatorDemo drives 3 correlated line-items through agg and
// prints the released order total. It also starts agg.Run in the
// background: a durable store's crash recovery rides ENTIRELY on the
// reaper Run drives (see msginsql.GroupStore's "go agg.Run(ctx) is
// REQUIRED" doc) — a caller who never calls Run silently never
// crash-recovers a group stuck between ClaimGroup and SettleGroup. Showing
// it here, even though the in-memory store below never crashes
// mid-process, keeps this wiring identical to the durable one in
// Example_sqlGroupStore.
func runGroupAggregatorDemo(store msgin.MessageGroupStore) {
out := channel.NewDirectChannel()
if _, err := out.Subscribe(msgin.HandlerFunc(func(_ context.Context, m msgin.Message[any]) error {
fmt.Printf("order total: %v\n", m.Payload())
return nil
})); err != nil {
panic(err)
}
agg, err := wireGroupAggregator(store, out)
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go agg.Run(ctx)
items := []groupStoreLineItem{
{sku: "widget", price: 10},
{sku: "gadget", price: 25},
{sku: "gizmo", price: 15},
}
for i, it := range items {
msg := msgin.New[any](it, msgin.WithID(fmt.Sprintf("line-%d", i)), msgin.WithHeaders(map[string]any{
msgin.HeaderCorrelationID: "order-1",
}))
if err := agg.Handle(context.Background(), msg); err != nil {
panic(err)
}
}
}
func main() {
var db *stdsql.DB // caller-provided, already open and schema-provisioned
store, err := msginsql.NewGroupStore(db, "msgin_group", postgres.GroupDialect())
if err != nil {
panic(err)
}
runGroupAggregatorDemo(store)
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DDL ¶
DDL returns the reference CREATE TABLE (+ claim index) statement for the lease/claim schema on PostgreSQL, for callers to fold into their own migration tool. It validates table (ErrInvalidTableName on a bad identifier) before building the statement; msgin never runs this on the production path (ADR 0010 D2 — the caller provisions the schema).
func GroupDDL ¶
GroupDDL returns the reference CREATE TABLE statements (group-lease table + member table + supporting indexes) for the durable Aggregator group store on PostgreSQL, for callers to fold into their own migration tool. It validates table (ErrInvalidTableName on a bad identifier) before building — the sole entry point, applying the same identifier-injection discipline as DDL: there is no string-returning DDL method on the GroupDialect interface to bypass validation (ADR 0021 §3). msgin never runs this on the production path (the caller provisions the schema).
func GroupDialect ¶
func GroupDialect() msginsql.GroupDialect
GroupDialect returns the built-in PostgreSQL GroupDialect. Pass it as the required dialect argument to msginsql.NewGroupStore. It owns the exact PostgreSQL group-aggregation SQL (group-row FOR UPDATE locking, epoch-fenced claim with RETURNING, claimed-set re-absorption) for the durable, multi- process Aggregator store (ADR 0021).
func InboxDDL ¶
InboxDDL returns the reference CREATE TABLE (+ retention index) statement for the idempotent-consumer dedup inbox on PostgreSQL, for callers to fold into their own migration tool (ADR 0010 D10). It validates table (ErrInvalidTableName on a bad identifier) as the sole entry point, applying the same identifier-injection discipline as DDL (reference finding I1): there is no string-returning DDL method on the InboxDialect interface to bypass validation.
func InboxDialect ¶
func InboxDialect() msginsql.InboxDialect
InboxDialect returns the built-in PostgreSQL InboxDialect — the same stateless value as LeaseDialect(), narrowed to the dedup-inbox SPI (ADR 0010 D10). Pass it as the required dialect argument to NewInboxDeduper.
func LeaseDialect ¶
func LeaseDialect() msginsql.LeaseDialect
LeaseDialect returns the built-in PostgreSQL LeaseDialect (lease/claim strategy). It owns the exact PostgreSQL SQL for every operation; pass it as the required dialect argument to NewPollingSource/NewOutboundAdapter. The single stateless value also satisfies msginsql.LockDialect (for WithStrategy(StrategyLockForUpdate)) and msginsql.InboxDialect.
Types ¶
This section is empty.