Implementation:2ndQuadrant Pglogical Apply Heap
| Knowledge Sources | |
|---|---|
| Domains | Database, Replication |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
Heap API-based apply backend that applies replicated row changes via direct tuple manipulation using PostgreSQL's internal heap access methods, with conflict detection, trigger firing, and multi-insert batching support.
Description
The heap apply backend is the default apply mode for pglogical. It provides functions for INSERT, UPDATE, and DELETE that operate directly on the heap using PostgreSQL's internal executor infrastructure:
- pglogical_apply_heap_insert(): Opens the relation, checks for conflicts via index scan, forms the remote tuple, fires BEFORE INSERT triggers, performs heap_insert, fires AFTER INSERT triggers, and updates indexes. Supports multi-insert batching.
- pglogical_apply_heap_update(): Finds the local tuple via replica identity index, resolves conflicts, fires BEFORE UPDATE triggers, performs heap_update, fires AFTER UPDATE triggers, and updates indexes.
- pglogical_apply_heap_delete(): Finds the local tuple via replica identity index, resolves conflicts, fires BEFORE DELETE triggers, performs heap_delete, and fires AFTER DELETE triggers.
Multi-insert batching (pglogical_apply_heap_can_mi, pglogical_apply_heap_mi_add_tuple, pglogical_apply_heap_mi_finish) accumulates consecutive INSERTs to the same table and flushes them via heap_multi_insert after MIN_MULTI_INSERT_TUPLES (5) tuples, reducing per-tuple overhead.
Usage
This is the default apply backend, automatically selected when pglogical.use_spi = false. Users configure it via GUCs but do not call these functions directly.
Code Reference
Source Location
- Repository: pglogical
- File: pglogical_apply_heap.c
- Lines: L356-567 (insert), L567-761 (update), L761-825 (delete); L121 (commit), L116 (begin); multi-insert: can_mi, mi_add_tuple, mi_finish
Signature
void pglogical_apply_heap_insert(PGLogicalRelation *rel, PGLogicalTupleData *newtup);
void pglogical_apply_heap_update(PGLogicalRelation *rel, PGLogicalTupleData *oldtup, PGLogicalTupleData *newtup);
void pglogical_apply_heap_delete(PGLogicalRelation *rel, PGLogicalTupleData *oldtup);
void pglogical_apply_heap_begin(void);
void pglogical_apply_heap_commit(void);
bool pglogical_apply_heap_can_mi(void);
void pglogical_apply_heap_mi_add_tuple(PGLogicalRelation *rel, PGLogicalTupleData *newtup);
void pglogical_apply_heap_mi_finish(void);
Import
/* Internal C functions, registered in the apply_api dispatch table.
Not user-callable. Selected when pglogical.use_spi = false (default). */
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| rel | PGLogicalRelation* | Yes Target relation with attribute mapping | |
| newtup | PGLogicalTupleData* | Yes New tuple values (INSERT, UPDATE) | |
| oldtup | PGLogicalTupleData* | Yes Old tuple key (UPDATE, DELETE) |
Outputs
| Name | Type | Description |
|---|---|---|
| applied rows | database rows | Tuple inserted/updated/deleted on subscriber |
| triggers fired | trigger events | BEFORE/AFTER row triggers (replica triggers only) |
| indexes updated | index entries | Indexes updated for each change |
| conflict resolution | decision | Conflict detected and resolved (if applicable) |
Usage Examples
Configuring Heap Apply Mode
-- In postgresql.conf (defaults shown):
pglogical.use_spi = off -- Use heap API (default)
pglogical.batch_inserts = on -- Enable multi-insert batching
pglogical.conflict_resolution = 'error' -- Conflict strategy (heap supports all)
Multi-Insert Batching Flow (Internal)
// Abstract multi-insert logic (NOT real implementation)
void apply_heap_insert(rel, newtup) {
// Check if we can batch
if (use_multi_insert && can_mi()) {
// Accumulate tuple
mi_add_tuple(rel, newtup);
if (++insert_count >= MIN_MULTI_INSERT_TUPLES) {
mi_finish(); // Flush via heap_multi_insert
}
} else {
// Single insert with conflict detection
conflict_idx = pglogical_tuple_find_conflict(...);
if (conflict_idx) try_resolve_conflict(...);
heap_insert(...);
fire_triggers(...);
}
}
Related Pages
Implements Principle
- Principle:2ndQuadrant_Pglogical_Change_Application
- Principle:2ndQuadrant_Pglogical_Conflict_Resolution
Uses Heuristic
- Heuristic:2ndQuadrant_Pglogical_Batch_Insert_Tuning
- Heuristic:2ndQuadrant_Pglogical_Conflict_Resolution_Selection