An insert that fires a trigger can produce more than one write: one to the application table and another to an audit table. Those writes do not always travel to storage in the same batch
yb_speculatively_execute_pl_statements lets YSQL continue through eligible procedural-language statements without immediately waiting for earlier buffered operations to finish. This can batch trigger writes alongside their parent writes and reduce storage flushes.
What does “speculative” mean?
Consider a trigger that inserts an audit row and then executes RETURN NEW. Without this optimization, a PL statement boundary can force pending writes to flush. With it enabled, YSQL can pass through eligible statements and keep batching.
The implementation checks whether proceeding is safe, including the handling of nontransactional side effects, dependencies, and errors. This is an execution optimization within the transaction; it does not change the requirement for the transaction to succeed before commit is acknowledged.
Check your release and permissions
Availability: Introduced in YugabyteDB v2024.2.3.1, released June 13, 2025, under #27464 in the release notes. The parameter defaults to off.
This demo was tested on YugabyteDB 2026.1.3.1-b0, where both the current setting and built-in default were off, and context was superuser. Check your server for availability and permissions:
SELECT version();
SELECT name, setting, boot_val, context, short_desc
FROM pg_settings
WHERE name = 'yb_speculatively_execute_pl_statements';
setting is the current value; boot_val is the built-in default. No row means this server does not expose the parameter. Changing it requires an account permitted to set it; access can vary by release and service.
Try it: one insert, two trigger calls
Use a disposable YSQL database. Save the following as yb-speculative-pl-trigger-demo.sql:
speculative-pl-trigger-demo.sql
\set ON_ERROR_STOP on
\pset pager off
-- Use a disposable YSQL database and an account allowed to set this parameter.
-- This script intentionally stops if its schema already exists.
SELECT version();
SELECT name, setting, boot_val, context
FROM pg_settings
WHERE name = 'yb_speculatively_execute_pl_statements';
SELECT current_setting('yb_speculatively_execute_pl_statements') AS original_pl_setting \gset
CREATE SCHEMA yb_pl_batch_demo;
CREATE TABLE yb_pl_batch_demo.events (id integer PRIMARY KEY, payload text);
CREATE TABLE yb_pl_batch_demo.audit (event_id integer);
CREATE FUNCTION yb_pl_batch_demo.audit_event()
RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO yb_pl_batch_demo.audit VALUES (NEW.id);
RETURN NEW;
END;
$$;
CREATE TRIGGER audit_event
BEFORE INSERT ON yb_pl_batch_demo.events
FOR EACH ROW EXECUTE FUNCTION yb_pl_batch_demo.audit_event();
SET yb_speculatively_execute_pl_statements = off;
\echo 'OFF: warm up the trigger'
BEGIN ISOLATION LEVEL READ COMMITTED;
INSERT INTO yb_pl_batch_demo.events VALUES (1, 'warmup'), (2, 'warmup');
COMMIT;
\echo 'OFF: record Storage Flush Requests from this plan'
BEGIN ISOLATION LEVEL READ COMMITTED;
EXPLAIN (ANALYZE, DIST, COSTS OFF)
INSERT INTO yb_pl_batch_demo.events VALUES (3, 'measured'), (4, 'measured');
COMMIT;
SET yb_speculatively_execute_pl_statements = on;
\echo 'ON: warm up the trigger'
BEGIN ISOLATION LEVEL READ COMMITTED;
INSERT INTO yb_pl_batch_demo.events VALUES (101, 'warmup'), (102, 'warmup');
COMMIT;
\echo 'ON: record Storage Flush Requests from this plan'
BEGIN ISOLATION LEVEL READ COMMITTED;
EXPLAIN (ANALYZE, DIST, COSTS OFF)
INSERT INTO yb_pl_batch_demo.events VALUES (103, 'measured'), (104, 'measured');
COMMIT;
\echo 'Expected: 8 events, 8 audit rows, and 0 missing audit rows'
SELECT
(SELECT count(*) FROM yb_pl_batch_demo.events) AS events,
(SELECT count(*) FROM yb_pl_batch_demo.audit) AS audit_rows,
(SELECT count(*) FROM yb_pl_batch_demo.events e
WHERE NOT EXISTS (SELECT 1 FROM yb_pl_batch_demo.audit a
WHERE a.event_id = e.id)) AS missing_audit_rows;
SET yb_speculatively_execute_pl_statements = :'original_pl_setting';
\echo 'Demo complete. The original session setting has been restored.'
-- Optional cleanup after inspecting the results:
-- DROP SCHEMA yb_pl_batch_demo CASCADE;
Then run it with ysqlsh:
ysqlsh -h YOUR_HOST -U yugabyte -d YOUR_DEMO_DATABASE \
-P pager=off -f yb-speculative-pl-trigger-demo.sql
It disables the pager, warms up each mode, and measures the same two-row insert with the setting off and on. Each run uses fresh keys.
EXPLAIN ANALYZE runs the insert and its triggers. The script creates a dedicated schema and stops if it already exists. It leaves the objects available for inspection.Read the evidence
The following results were captured on YugabyteDB 2026.1.3.1-b0, after warming up each mode. Both measured inserts created two application rows and two audit rows.
| Metric | Setting off | Setting on |
|---|---|---|
| Storage Write Requests | 4 | 4 |
| Storage Flush Requests | 4 | 1 |
| Trigger calls | 2 | 2 |
| Execution Time (one measurement) | 2.424 ms | 0.989 ms |
Compare the statement-level totals at the bottom of each plan. The off plan also reports three flushes under Values Scan; that is a plan-node measurement, not the total to compare or add to the bottom summary.
These excerpts show the recorded totals:
-- Setting off
Trigger audit_event: time=1.701 calls=2
Execution Time: 2.424 ms
Storage Write Requests: 4
Storage Flush Requests: 4
Storage Flush Execution Time: 1.867 ms
-- Setting on
Trigger audit_event: time=0.206 calls=2
Execution Time: 0.989 ms
Storage Write Requests: 4
Storage Flush Requests: 1
Storage Flush Execution Time: 0.657 ms
The final check, including both warm-ups, returned:
.events | audit_rows | missing_audit_rows
--------+------------+--------------------
8 | 8 | 0
A storage flush here sends buffered operations to the distributed storage layer. It is not a count of disk fsync calls. The timings are individual observations, not a throughput benchmark or a guaranteed speedup. Exact flush counts can also vary by release and execution path.
If your counts do not change, retain both plans and the server version. Additional flush boundaries or release-specific behavior can limit the benefit. A SQL-language default expression alone does not exercise this PL/pgSQL trigger example.
Final takeaway
Triggers can add storage round trips as well as logical work. yb_speculatively_execute_pl_statements gives YSQL more opportunity to batch eligible trigger writes with the insert that caused them.
Try the same off/on comparison with your own trigger workload, warming up both modes and comparing the statement-level totals. Validate latency, throughput, and application behavior on your exact release before changing production settings.
Have Fun!
Global commerce at full throttle (with YugabyteDB helping power the action)!
Explore Shopify’s BFCM dashboard →