Documentation
¶
Overview ¶
Package chunk reads a PostgreSQL table through SQL for the snapshot: it picks a strategy for the table (CTID ranges, a typed key domain, or key batches), splits it into chunks and scans them, as row maps or straight into Arrow. It also compiles the spec's row filter into the WHERE it scans with, and holds the row normalization the incremental path shares.
The replication stream, and everything that needs a logical decoding plugin, lives in the parent package.
Index ¶
- Variables
- func CompileFilter(f *spec.Filter) (sq.Sqlizer, error)
- func Normalize(v any) any
- func NormalizeRow(cols []string, vals []any) map[string]any
- func RawBytesType(dbType string) bool
- func ScanRow(rows *sql.Rows) ([]any, error)
- type Chunker
- func (c *Chunker) Bounds(ctx context.Context) ([][]any, error)
- func (c *Chunker) Partitions(ctx context.Context, n int) ([]source.Chunk, error)
- func (c *Chunker) Scan(ctx context.Context, ch source.Chunk, fn func(row map[string]any) error) error
- func (c *Chunker) ScanArrow(ctx context.Context, ch source.Chunk, enc *transport.RowEncoder, expected int) (int, error)
- func (c *Chunker) ScanArrowPages(ctx context.Context, ch source.Chunk, enc *transport.RowEncoder, maxBytes int, ...) (int, error)
- type Option
Constants ¶
This section is empty.
Variables ¶
var Builder = sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
Builder builds queries with Postgres $N placeholders.
Functions ¶
func CompileFilter ¶
CompileFilter compiles the structured filter into a squirrel Sqlizer for the snapshot chunk SELECT. Squirrel owns the SQL shape — identifier quoting aside, there is no hand-built SQL string, and it handles placeholder numbering and value binding. The structured spec.Filter is the single filter type; this is only its SQL rendering.
func Normalize ¶
Normalize maps driver values into the scalar subset shared with the stream decoder.
func NormalizeRow ¶
NormalizeRow maps one raw row to a column-keyed map through the shared scalar mapping.
func RawBytesType ¶
RawBytesType reports whether a PostgreSQL column type reaches Scan as bytes that Normalize turns into the same string: the text, JSON and binary types. Any other type goes through Normalize + AppendValue, exactly as the row path.
Types ¶
type Chunker ¶
type Chunker struct {
// contains filtered or unexported fields
}
Chunker splits a table into snapshot chunks. The strategy is CTID by default; a configured chunk column switches to a key-based strategy (batch-size for integer/float, next-query otherwise). A partitioned table (workers > 1) always uses a key-based strategy, because the chunk range must stay routable to the same worker as the live stream.
func New ¶
func New(ctx context.Context, db *sql.DB, source, pk string, chunkSize int, opts ...Option) (*Chunker, error)
New builds a chunker for one source table.
func (*Chunker) Bounds ¶
Bounds returns the ordered chunk boundary keys for the resolved strategy. The whole computation is retried on a transient error.
func (*Chunker) Partitions ¶
Partitions divides this table's single-column primary key into n contiguous, ordered ranges for worker partitioning — the same ranges govern the DBLog snapshot and the live-stream routing, so a key can never switch partition ownership between the two phases (no hash is involved, so the two phases cannot disagree).
It always returns exactly n disjoint ranges (an empty or sparse table is padded with degenerate or type-domain ranges), and marks this chunker key-partitioned: CTID ranges are physical and cannot be clipped to a PK range, so a partitioned table must chunk by key.
func (*Chunker) Scan ¶
func (c *Chunker) Scan(ctx context.Context, ch source.Chunk, fn func(row map[string]any) error) error
Scan executes the chunk SELECT and calls fn for every row, keyed by column name. Values decode through the same scalar mapping the pgoutput reader uses, so snapshot rows and stream rows land in Iceberg identically.
The whole scan is retried on a transient error, covering a failure during rows.Next/Scan and not just the initial QueryContext. Rows are buffered until the scan completes, so a retry never re-delivers rows to fn.
Within one scan, rows are normalized by a bounded pool of `workers` goroutines, but fn is always invoked from a single goroutine, so it need not be thread-safe. Row order is not preserved (snapshot rows are keyed inserts, order-independent).
func (*Chunker) ScanArrow ¶
func (c *Chunker) ScanArrow(ctx context.Context, ch source.Chunk, enc *transport.RowEncoder, expected int) (int, error)
ScanArrow reads one chunk straight into enc, one snapshot insert per row, and returns the row count; expected is the chunk's expected row count. An encoder the caller sized already (ReservePerRow) is used as is; otherwise it is sized from the chunk's first rows.
Scan hands every row over as a map of Go values, which the caller then encodes: the chunk was held as maps and again as Arrow, the most memory-expensive path in the Postgres snapshot (#590). Here a text, JSON or binary cell goes from the driver's buffer into the column's Arrow data buffer, and the buffers are sized once for the chunk. Any other cell is converted exactly as Scan converts it.
The read runs in one REPEATABLE READ, READ ONLY transaction, as Scan does, so the chunk sees a single consistent snapshot. Transaction and query setup are retried before the first row is appended; mid-scan failures are not retried, since retrying after rows have been appended would double-count them (they fall back to the coordinator's chunk redo on worker loss).
func (*Chunker) ScanArrowPages ¶
func (c *Chunker) ScanArrowPages(ctx context.Context, ch source.Chunk, enc *transport.RowEncoder, maxBytes int, emit func(rec arrow.RecordBatch, n int) error) (int, error)
ScanArrowPages reads one chunk straight into enc, cutting it into pages when the accumulated data bytes reach maxBytes. emit is called per page with the page's record (the caller owns it) and its row count; it may block (the worker's backpressure). The read runs in one REPEATABLE READ, READ ONLY transaction — the same as ScanArrow — so every page sees one consistent snapshot; the transaction is committed after the last page (#622).
type Option ¶
type Option func(*Chunker)
Option tunes a Chunker.
func WithChunkColumn ¶
WithChunkColumn selects the chunk column (#151). Empty keeps the CTID default.
func WithColumns ¶
WithColumns sets the column projection (#162). Empty means all columns.
func WithFilter ¶
WithFilter sets the compiled structured filter (#163). Nil means none.
func WithRetries ¶
WithRetries sets the transient-error retry budget for snapshot queries.
func WithTargetBytes ¶
WithTargetBytes sets the CTID strategy's target bytes per chunk (from sink.defaults.targetFileSize). Non-positive keeps the default.
func WithWorkers ¶
WithWorkers sets the concurrent normalization pool size. n < 1 is clamped to 1 (sequential).