Implementation:2ndQuadrant Pglogical Apply SPI
| Knowledge Sources | |
|---|---|
| Domains | Database, Replication |
| Last Updated | 2026-10-13 00:00 GMT |
Overview
SPI-based apply backend that applies replicated row changes by generating and executing SQL INSERT/UPDATE/DELETE statements via PostgreSQL's Server Programming Interface, providing a simpler but slower alternative to the heap API without conflict resolution support.
Description
The SPI apply backend is the alternative apply mode, activated when pglogical.use_spi = true. It generates SQL statements for each row change and executes them via SPI_execute_with_args():
- pglogical_apply_spi_insert(): Builds an INSERT statement with column names and parameter placeholders, then executes via SPI.
- pglogical_apply_spi_update(): Builds an UPDATE statement matching the replica identity key, then executes via SPI.
- pglogical_apply_spi_delete(): Builds a DELETE statement matching the replica identity key, then executes via SPI.
SPI mode is simpler and more compatible (uses the standard SQL executor path), but has two key limitations: it is slower (SQL parsing overhead per change), and it does not support conflict resolution (requires pglogical.conflict_resolution = error). If a conflict occurs, the SPI statement fails and the apply worker errors.
Usage
Select this mode by setting pglogical.use_spi = true in postgresql.conf. This is an internal backend; users configure it via GUCs but do not call these functions directly.
Code Reference
Source Location
- Repository: pglogical
- File: pglogical_apply_spi.c
- Lines: L108-174 (insert), L174-253 (update), L253-330 (delete); L86 (begin), L97 (commit)
Signature
void pglogical_apply_spi_insert(PGLogicalRelation *rel, PGLogicalTupleData *newtup);
void pglogical_apply_spi_update(PGLogicalRelation *rel, PGLogicalTupleData *oldtup, PGLogicalTupleData *newtup);
void pglogical_apply_spi_delete(PGLogicalRelation *rel, PGLogicalTupleData *oldtup);
void pglogical_apply_spi_begin(void);
void pglogical_apply_spi_commit(void);
Import
/* Internal C functions, registered in the apply_api dispatch table.
Selected when pglogical.use_spi = true. */
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 | SQL INSERT/UPDATE/DELETE executed on subscriber |
| SQL statement | string | Generated SQL (visible in debug logs) |
Usage Examples
Enabling SPI Mode
-- In postgresql.conf:
pglogical.use_spi = on
pglogical.conflict_resolution = error -- Required with SPI mode
Generated SQL (Internal)
-- INSERT generated by pglogical_apply_spi_insert:
INSERT INTO public.users (id, name, email) VALUES ($1, $2, $3)
-- UPDATE generated by pglogical_apply_spi_update:
UPDATE public.users SET name = $1, email = $2 WHERE id = $3
-- DELETE generated by pglogical_apply_spi_delete:
DELETE FROM public.users WHERE id = $1
Related Pages
Implements Principle
Uses Heuristic
- Heuristic:2ndQuadrant_Pglogical_Batch_Insert_Tuning
- Heuristic:2ndQuadrant_Pglogical_Conflict_Resolution_Selection