Heuristic:2ndQuadrant Pglogical Batch Insert Tuning
| Knowledge Sources | |
|---|---|
| Domains | Replication, Optimization, Database |
| Last Updated | 2026-10-13 03:51 GMT |
Overview
`pglogical.batch_inserts` (default: `true`) enables PostgreSQL internal batch insert mode for higher throughput, but only works when `pglogical.conflict_resolution` is set to `error` and no conflicting triggers exist.
Description
pglogical can use PostgreSQL's internal batch insert mechanism (the same mode used by `COPY`) to apply `INSERT` operations in bulk rather than one row at a time. This significantly improves throughput for `INSERT`-heavy replication workloads. The batch insert mode accumulates tuples and flushes them in batches when a minimum tuple threshold (`MIN_MULTI_INSERT_TUPLES`) is reached. However, batch mode has strict requirements:
- `pglogical.conflict_resolution` must be set to `error` (conflicts are not detected during batch mode).
- The table must not have `INSTEAD OF INSERT` or `BEFORE INSERT` triggers.
- The table must not have columns with volatile default expressions.
- Batch mode only applies to consecutive `INSERT`s into the same relation; a different relation or a non-`INSERT` operation flushes the batch.
Usage
Keep `pglogical.batch_inserts = true` (the default) for high-throughput `INSERT` replication where conflict resolution is set to `error`. Disable it (`false`) if you need conflict detection with `apply_remote` or other non-`error` resolvers, or if you encounter issues with trigger-heavy tables. The setting is a `PGC_POSTMASTER` parameter, requiring a server restart to change.
The Insight (Rule of Thumb)
- Action: Set `pglogical.batch_inserts` in `postgresql.conf` (requires restart, `PGC_POSTMASTER`).
- Value: `true` (default) for throughput; `false` for compatibility with non-`error` conflict resolvers.
- Trade-off: Batch mode can dramatically improve `INSERT` throughput but disables conflict detection during batched operations.
- Compatibility: Only works with `pglogical.conflict_resolution = 'error'`. Incompatible with `INSTEAD OF INSERT` / `BEFORE INSERT` triggers and volatile default expressions.
Reasoning
PostgreSQL's internal batch insert mode bypasses per-row trigger firing and conflict checking, allowing the executor to insert multiple rows in a single heap insertion. This is the same mechanism `COPY` uses. pglogical leverages this by accumulating consecutive `INSERT` operations for the same relation. Once a minimum number of tuples (`MIN_MULTI_INSERT_TUPLES`) is reached, it switches to multi-insert mode. The restriction to `error` conflict resolution exists because batch mode does not check for existing tuples before inserting, so conflicts cannot be detected.
Code Evidence
Batch insert GUC definition — from `pglogical.c:866-873`:
DefineCustomBoolVariable("pglogical.batch_inserts",
"Batch inserts if possible",
NULL,
&pglogical_batch_inserts,
true,
PGC_POSTMASTER,
0,
NULL, NULL, NULL);
Batch insert logic in apply — from `pglogical_apply.c:570-585`:
else if (pglogical_batch_inserts &&
RelationGetRelid(rel->rel) != QueueRelid &&
apply_api.can_multi_insert &&
apply_api.can_multi_insert(rel))
{
if (rel != last_insert_rel)
{
last_insert_rel = rel;
last_insert_rel_cnt = 0;
}
else if (last_insert_rel_cnt++ >= MIN_MULTI_INSERT_TUPLES)
{
use_multi_insert = true;
last_insert_rel_cnt = 0;
}
}
Batch mode requires `error` conflict resolution — from README:
Also the batch mode will only work when `pglogical.conflict_resolution` is
set to `error`.