From: Denis Smirnov <darthunix@gmail.com>
To: Amit Langote <amitlangote09@gmail.com>
Cc: Junwang Zhao <zhjwpku@gmail.com>
Cc: cca5507 <cca5507@qq.com>
Cc: Daniil Davydov <3danissimo@gmail.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Cc: Tomas Vondra <tomas@vondra.me>
Subject: Re: Batching in executor
Date: Wed, 15 Jul 2026 15:12:43 +0800
Message-ID: <A800AC8E-6D9F-4F04-9ACB-D73A2CF87D58@gmail.com> (raw)
In-Reply-To: <749D4E65-6E1F-4569-AA0D-5C354E138E57@gmail.com>
References: <CA+HiwqFfAY_ZFqN8wcAEMw71T9hM_kA8UtyHaZZEZtuT3UyogA@mail.gmail.com>
<68f3771f-91f5-4cb7-b1de-74d9abbf0b96@vondra.me>
<CA+HiwqGqeS_94wxiYa8VpymiR_OtFPKDSpX+Me=MYWO45f5yig@mail.gmail.com>
<dbce8def-e817-4e70-a9cb-e62574724d95@vondra.me>
<CA+HiwqGM0ZTeVHicSkGnCp-2U-jvU-KBQCkPJ0N7nAj_c2LjZg@mail.gmail.com>
<CA+HiwqHyE7-oOvtZ+OC-4N7DvKSr8Jbu75erMLQ7O4d6gfkBhg@mail.gmail.com>
<CA+HiwqEPMwhg6pUE4XML2rG4fRqKMpeGTtMwRPGes90f9iOqtg@mail.gmail.com>
<CA+HiwqEZja5rJ78p3FBDZNvynWsHwanxyt6h0YaK_r84NemXng@mail.gmail.com>
<tencent_B45268659AF0400F68EC1643950ABB6A1B09@qq.com>
<CAJDiXggP41+-HrRzT+BmtgmS8wkoZM7b4skkQA5NAe+NFEMPSQ@mail.gmail.com>
<CA+HiwqGufj46_4kT6z5nQrCOxp3rcMDR8fn_t33n2pDKokNRQg@mail.gmail.com>
<CA+HiwqH-2GmTKLW9kHdnqV4KdFiPfuAdVK2TgqOM2JaaeUYXnw@mail.gmail.com>
<CAEG8a3JKNRGwBb-C_YV6Px4aFHwd_G=AsB5AbfyU4-8YZT54AQ@mail.gmail.com>
<tencent_164C2A5EDF8189DD70287661406D688A8506@qq.com>
<CAEG8a3K22s9LmDZa486suFu3AV=yRuTf09j+Ww+djG63y=R8hg@mail.gmail.com>
<CA+HiwqGkway9dcpTVmRs0Q=ZsQQ8Nx0MRjy9jfDmR8QFLnd-Mg@mail.gmail.com>
<CA+HiwqHLjBegqeUw-babABj_icAiBTq5gh48-D3BEodVZkm8Ug@mail.gmail.com>
<CA+HiwqEnHfjCVEo9PcTCNTCQVDBSRVeu=rPLqAc1tTafu7zowg@mail.gmail.com>
<CA+HiwqHv1CggSbpn=n+GU=Cp1AqDD25=hzx_PTYyqjMyZO2+mA@mail.gmail.com>
<3D8D1BA5-B8A6-400A-B605-42EEE37890B4@gmail.com>
<749D4E65-6E1F-4569-AA0D-5C354E138E57@gmail.com>
Hi,
I continued the heap-only experiment I described earlier in this
thread and attached v2 as a two-patch series. This is an alternative
implementation of that experiment; it does not replace or supersede
Amit's v8 series.
I think a batched executor does not necessarily require a new API for
passing batches between plan nodes. Not every node will benefit from
batching; the most promising nodes are those that process a large stream
of tuples. A sequential scan is a natural first candidate. Since
heapam is currently the only table AM implementation shipped in core,
these patches keep the experiment local to heap scans.
The first patch uses information that heap page mode already collects.
After heapgettup_pagemode() finds the visible offsets on a page, the
buffer heap slot records that page-local batch. heap_getnextslot() can
then advance through the remaining offsets without another call to
heapgettup_pagemode(). The scan still returns one ordinary slot at a
time to its parent, so this changes neither the table AM API nor the
interface between plan nodes.
The second patch uses the same page batch to evaluate a supported
initial prefix of SeqScan quals over as many as 64 tuples. It gathers
Datums in a tight loop and then calls the existing operator functions in
another tight loop. A 64-bit mask records passing rows. With multiple
supported quals, each qual narrows the mask, and later quals are called
only for rows that passed the earlier ones.
This is not SIMD. The gain comes from better locality and from doing
less executor work between adjacent operator calls. An important
consequence is that operators need no new batch or SIMD-specific API;
the code calls their existing scalar functions and also supports user
types and cross-type operators.
The batch path currently accepts binary OpExpr clauses of these forms:
Var op Const/Param
Const/Param op Var
Here Param means PARAM_EXTERN or PARAM_EXEC.
The operator must return bool, must not return a set, and its function
must be marked IMMUTABLE, STRICT, and LEAKPROOF. These restrictions are
important because a batch may evaluate up to 63 later rows before the
parent asks for them. Such functions must have no side effects, and
their catalog declarations must be correct. Other clauses use the
existing row-at-a-time path.
The patches do not reorder quals. They batch only the supported initial
prefix in the order chosen by the planner. The first unsupported qual
and everything after it remain row-at-a-time. This also retains the
planner's potentially useful ordering for tuple deformation and
short-circuit evaluation.
Projection still uses the normal ExecProject() path, and it runs only
for rows that pass all quals. Thus projection gets no batching benefit
in this version. EPQ and cases without a suitable heap page batch also
fall back to the existing scalar behavior.
The patches are:
0001 Avoid repeated heapgettup_pagemode() calls within a heap page
0002 Evaluate SeqScan quals in heap page batches
I also reran the benchmark against the same current master and against
Amit's v8 applied to that master. The base was master at 1c4b1de8885;
v8 applied cleanly. pass-all is a > 0, and pass-none is a < 0. Each
cell below is the change from unpatched master for 5M / 10M rows;
negative values mean faster.
all-visible:
query this series Amit v8
count(*) -6.5% / -7.0% -9.0% / -10.4%
count(*) WHERE pass-all -17.6% / -17.7% -5.3% / -4.3%
count(*) WHERE pass-none -21.6% / -22.0% -8.7% / -9.4%
not-all-visible:
query this series Amit v8
count(*) -5.2% / -3.8% -9.2% / -6.8%
count(*) WHERE pass-all -17.3% / -16.4% -4.3% / -3.4%
count(*) WHERE pass-none -24.8% / -20.4% -12.3% / -7.4%
In this run, v8 was somewhat faster for count(*) without a qual. For
the two qual cases, local batch evaluation made this series about
13-14% faster than v8.
The tables contained a 100-byte text value and about 43 tuples per heap
page. They were prewarmed and fit in shared_buffers. Parallel query,
JIT, synchronized scans, and autovacuum were disabled. The
not-all-visible case used frozen heap tuples with the visibility map
truncated.
I used an -O2 build on an Apple M5 Pro. For each query I ran three
warmups followed by 31 measured EXPLAIN ANALYZE executions and used the
median. I repeated each variant twice in symmetric order and averaged
the two medians to reduce frequency drift.
The full regression suite passes for the two-patch series.
I would appreciate feedback on both the heap-local batch representation
and the idea of keeping batch qual evaluation inside SeqScan while
preserving the ordinary slot interface above it.
Best regards,
Denis Smirnov
Attachments:download all (2 files, .tar.gz)v2-0001-Avoid-repeated-heapgettup_pagemode-calls-within-a.patch (application/octet-stream, 7.8K) download
From 4e0cffbfd728213677fa9f15800f4ac6a4607503 Mon Sep 17 00:00:00 2001
Message-ID: <4e0cffbfd728213677fa9f15800f4ac6a4607503.1784099400.git.darthunix@gmail.com>
From: Denis Smirnov <darthunix@gmail.com>
Date: Sun, 12 Jul 2026 21:48:00 +0800
Subject: [PATCH v2 1/2] Avoid repeated heapgettup_pagemode() calls within a
heap page
heap_getnextslot() currently calls heapgettup_pagemode() for every
tuple, even though page mode has already collected the offsets of all
visible tuples on the current page. This repeats tuple-at-a-time scan
bookkeeping that can become noticeable for analytical workloads
dominated by sequential heap scans.
Add HeapPageBatch to BufferHeapTupleTableSlot. It references the
visible tuple offsets collected by the heap scan and records which one
is currently stored in the slot. After the first tuple from a page,
heap_getnextslot() can advance directly through those offsets, update
rs_ctup and invalidate the slot's tuple deformation state. The regular
heap scan path is used again when the page is exhausted.
Use this fast path only for page-mode scans without scan keys. Scans
using non-MVCC snapshots or heap scan keys retain the existing behavior.
Clear the page batch whenever the slot is cleared, materialized, or
assigned another tuple. The batch does not require additional buffer
pins and is valid only while the scan and slot remain on the same
buffer.
This reduces per-tuple overhead while preserving the existing
tuple-at-a-time executor interface. It also provides initial
infrastructure for passing page-sized groups of heap tuples to
batch-aware executor nodes in the future.
Discussion: https://postgr.es/m/3D8D1BA5-B8A6-400A-B605-42EEE37890B4%40gmail.com
---
src/backend/access/heap/heapam.c | 84 +++++++++++++++++++++++++++++++
src/backend/executor/execTuples.c | 12 +++++
src/include/executor/tuptable.h | 8 +++
src/tools/pgindent/typedefs.list | 1 +
4 files changed, 105 insertions(+)
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.cindex 9cdc221675b..e0a620cd172 100644--- a/src/backend/access/heap/heapam.c+++ b/src/backend/access/heap/heapam.c@@ -44,6 +44,7 @@
#include "catalog/pg_database_d.h"
#include "commands/vacuum.h"
#include "executor/instrument_node.h"
+#include "executor/tuptable.h"
#include "pgstat.h"
#include "port/pg_bitutils.h"
#include "storage/lmgr.h"
@@ -1470,12 +1471,85 @@ heap_getnext(TableScanDesc sscan, ScanDirection direction)
return &scan->rs_ctup;
}
+static inline void+AssertHeapPageBatchValid(HeapScanDesc scan, TupleTableSlot *slot)+{+#ifdef USE_ASSERT_CHECKING+ BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;+ HeapPageBatch *batch = &bslot->batch;++ Assert(!TTS_EMPTY(slot));+ Assert(batch->offsets != NULL);+ Assert(batch->offsets == scan->rs_vistuples);+ Assert(batch->ntuples == scan->rs_ntuples);+ Assert(batch->current == scan->rs_cindex);+ Assert(batch->current >= 0);+ Assert(batch->current < batch->ntuples);+ Assert(batch->offsets[batch->current] ==+ ItemPointerGetOffsetNumber(&slot->tts_tid));+ Assert(bslot->buffer == scan->rs_cbuf);+ Assert(bslot->base.tuple == &scan->rs_ctup);+#endif+}++static bool+heap_page_batch_next(HeapScanDesc scan, ScanDirection direction,+ TupleTableSlot *slot)+{+ BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;+ HeapPageBatch *batch = &bslot->batch;+ HeapTuple tuple = &scan->rs_ctup;+ Page page;+ ItemId lpp;+ OffsetNumber lineoff;+ int next;++ if (batch->offsets == NULL)+ return false;++ if (unlikely(batch->current != scan->rs_cindex ||+ bslot->buffer != scan->rs_cbuf))+ return false;++ Assert(direction == ForwardScanDirection ||+ direction == BackwardScanDirection);+ AssertHeapPageBatchValid(scan, slot);++ next = batch->current + direction;+ if (next < 0 || next >= batch->ntuples)+ return false;++ page = BufferGetPage(scan->rs_cbuf);+ lineoff = batch->offsets[next];+ lpp = PageGetItemId(page, lineoff);+ Assert(ItemIdIsNormal(lpp));++ tuple->t_data = (HeapTupleHeader) PageGetItem(page, lpp);+ tuple->t_len = ItemIdGetLength(lpp);+ ItemPointerSetOffsetNumber(&tuple->t_self, lineoff);++ scan->rs_cindex = next;+ batch->current = next;+ bslot->base.tuple = tuple;+ bslot->base.off = 0;+ slot->tts_nvalid = 0;+ slot->tts_tid = tuple->t_self;++ return true;+}+
bool
heap_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
{
+ BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
HeapScanDesc scan = (HeapScanDesc) sscan;
/* Note: no locking manipulations needed */
+ if (heap_page_batch_next(scan, direction, slot))+ {+ pgstat_count_heap_getnext(scan->rs_base.rs_rd);+ return true;+ }
if (sscan->rs_flags & SO_ALLOW_PAGEMODE)
heapgettup_pagemode(scan, direction, sscan->rs_nkeys, sscan->rs_key);
@@ -1497,6 +1571,16 @@ heap_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *s
ExecStoreBufferHeapTuple(&scan->rs_ctup, slot,
scan->rs_cbuf);
++ if ((sscan->rs_flags & SO_ALLOW_PAGEMODE) && sscan->rs_nkeys == 0)+ {+ bslot->batch.offsets = scan->rs_vistuples;+ bslot->batch.ntuples = scan->rs_ntuples;+ bslot->batch.current = scan->rs_cindex;++ AssertHeapPageBatchValid(scan, slot);+ }+
return true;
}
diff --git a/src/backend/executor/execTuples.c b/src/backend/executor/execTuples.cindex 97ae019d10a..b4431734c3f 100644--- a/src/backend/executor/execTuples.c+++ b/src/backend/executor/execTuples.c@@ -707,6 +707,14 @@ tts_minimal_store_tuple(TupleTableSlot *slot, MinimalTuple mtup, bool shouldFree
* TupleTableSlotOps implementation for BufferHeapTupleTableSlot.
*/
+static inline void+heap_page_batch_reset(HeapPageBatch *batch)+{+ batch->offsets = NULL;+ batch->ntuples = 0;+ batch->current = 0;+}+
static void
tts_buffer_heap_init(TupleTableSlot *slot)
{
@@ -745,6 +753,7 @@ tts_buffer_heap_clear(TupleTableSlot *slot)
bslot->base.tuple = NULL;
bslot->base.off = 0;
bslot->buffer = InvalidBuffer;
+ heap_page_batch_reset(&bslot->batch);
}
static void
@@ -807,6 +816,7 @@ tts_buffer_heap_materialize(TupleTableSlot *slot)
MemoryContext oldContext;
Assert(!TTS_EMPTY(slot));
+ heap_page_batch_reset(&bslot->batch);
/* If slot has its tuple already materialized, nothing to do. */
if (TTS_SHOULDFREE(slot))
@@ -946,6 +956,8 @@ tts_buffer_heap_store_tuple(TupleTableSlot *slot, HeapTuple tuple,
{
BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
+ heap_page_batch_reset(&bslot->batch);+
if (TTS_SHOULDFREE(slot))
{
/* materialized slot shouldn't have a buffer to release */
diff --git a/src/include/executor/tuptable.h b/src/include/executor/tuptable.hindex 3db6c9c9bd0..4ec94d2b84b 100644--- a/src/include/executor/tuptable.h+++ b/src/include/executor/tuptable.h@@ -282,6 +282,13 @@ typedef struct HeapTupleTableSlot
HeapTupleData tupdata; /* optional workspace for storing tuple */
} HeapTupleTableSlot;
+typedef struct HeapPageBatch+{+ const OffsetNumber *offsets; /* visible offsets on slot's page, or NULL */+ int ntuples; /* number of offsets */+ int current; /* index of slot's tuple in offsets */+} HeapPageBatch;+
/* heap tuple residing in a buffer */
typedef struct BufferHeapTupleTableSlot
{
@@ -296,6 +303,7 @@ typedef struct BufferHeapTupleTableSlot
* such a case, since presumably base.tuple is pointing into the buffer.)
*/
Buffer buffer; /* tuple's buffer, or InvalidBuffer */
+ HeapPageBatch batch;
} BufferHeapTupleTableSlot;
typedef struct MinimalTupleTableSlot
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.listindex 56c1f997f88..e843e50ca5b 100644--- a/src/tools/pgindent/typedefs.list+++ b/src/tools/pgindent/typedefs.list@@ -1266,6 +1266,7 @@ HeadlineParsedText
HeadlineWordEntry
HeapCheckContext
HeapCheckReadStreamData
+HeapPageBatch
HeapPageFreeze
HeapScanDesc
HeapScanDescData
--
2.54.0
=
v2-0002-Evaluate-SeqScan-quals-in-heap-page-batches.patch (application/octet-stream, 30.2K) download
From 4700a99409674ef50f87f3e3e415c271cebdf537 Mon Sep 17 00:00:00 2001
Message-ID: <4700a99409674ef50f87f3e3e415c271cebdf537.1784099400.git.darthunix@gmail.com>
In-Reply-To: <4e0cffbfd728213677fa9f15800f4ac6a4607503.1784099400.git.darthunix@gmail.com>
References: <4e0cffbfd728213677fa9f15800f4ac6a4607503.1784099400.git.darthunix@gmail.com>
From: Denis Smirnov <darthunix@gmail.com>
Date: Tue, 14 Jul 2026 15:39:41 +0800
Subject: [PATCH v2 2/2] Evaluate SeqScan quals in heap page batches
Heap scans can now expose the visible tuples remaining on the current
page through HeapPageBatch. Use this information in SeqScan to evaluate
suitable scan quals for several tuples at once.
For up to 64 tuples, first deform the referenced attribute into a Datum
array, then invoke the existing operator function in a tight loop. Store
the results in a 64-bit mask. With multiple quals, successively narrow
the mask and avoid deforming attributes or calling operators for tuples
already rejected by an earlier qual. This reduces per-tuple executor
overhead without adding a new expression or function API.
Batch only an initial sequence of quals in the order chosen by the
planner. Do not reorder them, both to preserve the required evaluation
order and to avoid losing any favorable tuple deformation order already
present in the plan. Evaluate the first unsupported qual and everything
following it one tuple at a time. When no HeapPageBatch is available,
evaluate the complete filter using the normal scalar path.
Recognize operators of the form Var op Const, Const op Var, Var op Param,
or Param op Var. Require the operator function to return bool, not
return a set, and be marked IMMUTABLE, STRICT, and LEAKPROOF. Batch
evaluation may call an operator for tuples that are never requested, for
example because of LIMIT or a later qual, so the operator must have no
observable side effects and its catalog declarations must be correct.
Preserve collation, NULL behavior, parameter evaluation, function usage
accounting, and both scan directions.
Apply ordinary projection with ExecProject() only after a tuple passes
all quals. Projection itself remains row-at-a-time and does not yet
benefit from page batching.
Discussion:
https://postgr.es/m/3D8D1BA5-B8A6-400A-B605-42EEE37890B4%40gmail.com
---
src/backend/executor/nodeSeqscan.c | 562 +++++++++++++++++++++++-
src/include/nodes/execnodes.h | 1 +
src/test/regress/expected/plancache.out | 129 ++++++
src/test/regress/sql/plancache.sql | 104 +++++
4 files changed, 792 insertions(+), 4 deletions(-)
diff --git a/src/backend/executor/nodeSeqscan.c b/src/backend/executor/nodeSeqscan.cindex b8c528ca089..0c0a67803ec 100644--- a/src/backend/executor/nodeSeqscan.c+++ b/src/backend/executor/nodeSeqscan.c@@ -27,16 +27,417 @@
*/
#include "postgres.h"
+#include "access/htup_details.h"
#include "access/relscan.h"
#include "access/tableam.h"
+#include "catalog/pg_proc.h"+#include "catalog/pg_type.h"
#include "executor/execParallel.h"
#include "executor/execScan.h"
#include "executor/executor.h"
#include "executor/nodeSeqscan.h"
+#include "pgstat.h"+#include "utils/lsyscache.h"
#include "utils/rel.h"
static TupleTableSlot *SeqNext(SeqScanState *node);
+/* Private state for one scan qual evaluated in page batches. */+struct SeqScanBatchQual+{+ FmgrInfo func;+ FunctionCallInfo fcinfo;+ ExprState *param_expr;+ AttrNumber attnum;+ uint8 var_argno;+};++/* Private state for page-batch qual evaluation. */+struct SeqScanBatchState+{+ struct SeqScanBatchQual *quals;+ ExprState *scalar_qual;+ uint64 mask; /* bit i is for batch index first + i */+ BlockNumber block;+ int first;+ int nrows;+ int nquals;+};++static inline void+reset_batch_qual(struct SeqScanBatchState *state)+{+ state->nrows = 0;+}++static BufferHeapTupleTableSlot *+get_valid_batch_slot(TupleTableSlot *slot, BlockNumber *block)+{+ BufferHeapTupleTableSlot *bslot;+ HeapPageBatch *batch;++ if (!TTS_IS_BUFFERTUPLE(slot) ||+ !ItemPointerIsValid(&slot->tts_tid))+ return NULL;++ bslot = (BufferHeapTupleTableSlot *) slot;+ batch = &bslot->batch;+ if (batch->offsets == NULL ||+ !BufferIsValid(bslot->buffer) ||+ batch->current < 0 || batch->current >= batch->ntuples)+ return NULL;++ *block = BufferGetBlockNumber(bslot->buffer);+ if (ItemPointerGetBlockNumber(&slot->tts_tid) != *block ||+ ItemPointerGetOffsetNumber(&slot->tts_tid) !=+ batch->offsets[batch->current])+ return NULL;++ return bslot;+}++static pg_always_inline bool+reuse_batch_qual(struct SeqScanBatchState *state, TupleTableSlot *slot,+ bool *result)+{+ BufferHeapTupleTableSlot *bslot;+ BlockNumber block;+ int position;++ if (state->nrows <= 0 || state->nrows > 64)+ goto invalid;++ bslot = get_valid_batch_slot(slot, &block);+ if (bslot == NULL)+ goto invalid;++ position = bslot->batch.current - state->first;+ if (block != state->block ||+ position < 0 || position >= state->nrows)+ goto invalid;++ *result = (state->mask & (UINT64CONST(1) << position)) != 0;+ return true;++invalid:+ reset_batch_qual(state);+ return false;+}++static pg_always_inline bool+eval_scalar_qual(struct SeqScanBatchState *state, TupleTableSlot *slot,+ ExprContext *econtext)+{+ econtext->ecxt_scantuple = slot;+ return ExecQual(state->scalar_qual, econtext);+}++static pg_always_inline Datum+heap_page_batch_getattr(Page page, const HeapPageBatch *batch, int index,+ TupleDesc tupledesc, AttrNumber attnum, bool *isnull)+{+ HeapTupleData tuple;+ ItemId lpp;++ lpp = PageGetItemId(page, batch->offsets[index]);+ Assert(ItemIdIsNormal(lpp));+ tuple.t_data = (HeapTupleHeader) PageGetItem(page, lpp);+ tuple.t_len = ItemIdGetLength(lpp);++ return heap_getattr(&tuple, attnum, tupledesc, isnull);+}++static pg_always_inline bool+eval_batch_qual(struct SeqScanBatchQual *qual, Datum value, bool isnull)+{+ FunctionCallInfo fcinfo = qual->fcinfo;+ PgStat_FunctionCallUsage fcusage;+ Datum result;++ fcinfo->args[qual->var_argno].value = value;+ fcinfo->args[qual->var_argno].isnull = isnull;+ if (isnull)+ return false;+ Assert(!fcinfo->args[1 - qual->var_argno].isnull);++ pgstat_init_function_usage(fcinfo, &fcusage);+ fcinfo->isnull = false;+ result = FunctionCallInvoke(fcinfo);+ pgstat_end_function_usage(&fcusage, true);++ return !fcinfo->isnull && DatumGetBool(result);+}++static void+prepare_batch_qual(struct SeqScanBatchState *state, TupleTableSlot *slot,+ ScanDirection direction, ExprContext *econtext)+{+ BufferHeapTupleTableSlot *bslot;+ HeapPageBatch *batch;+ Datum values[64];+ MemoryContext oldcontext;+ BlockNumber block;+ Page page;+ uint64 mask = 0;+ int first;+ int nrows;+ int position;++ Assert(ScanDirectionIsForward(direction) ||+ ScanDirectionIsBackward(direction));+ Assert(state->nquals > 0);++ reset_batch_qual(state);++ bslot = get_valid_batch_slot(slot, &block);+ if (bslot == NULL)+ return;++ batch = &bslot->batch;+ if (ScanDirectionIsForward(direction))+ {+ first = batch->current + 1;+ nrows = Min(64, batch->ntuples - first);+ }+ else+ {+ nrows = Min(64, batch->current);+ first = batch->current - nrows;+ }+ if (nrows == 0)+ return;++ oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);+ page = BufferGetPage(bslot->buffer);+ /*+ * Every row is a candidate for the first qual. Using the masked loops+ * below would require an all-ones input mask, whose per-row tests add+ * measurable overhead to the common single-qual case. Track nulls+ * separately while gathering values, then build the result mask while+ * evaluating them.+ *+ * Evaluate in scan direction, but index values and mask in batch order.+ */+ {+ struct SeqScanBatchQual *qual = &state->quals[0];+ FunctionCallInfo fcinfo = qual->fcinfo;+ NullableDatum *arg = &fcinfo->args[1 - qual->var_argno];+ uint64 nulls = 0;++ if (qual->param_expr != NULL)+ arg->value = ExecEvalExpr(qual->param_expr, econtext,+ &arg->isnull);+ if (!arg->isnull)+ {+ position = ScanDirectionIsForward(direction) ? 0 : nrows - 1;+ for (int i = 0; i < nrows; i++, position += direction)+ {+ bool isnull;+ uint64 bit = UINT64CONST(1) << position;++ values[position] = heap_page_batch_getattr(page, batch,+ first + position,+ slot->tts_tupleDescriptor,+ qual->attnum, &isnull);+ if (isnull)+ nulls |= bit;+ }++ position = ScanDirectionIsForward(direction) ? 0 : nrows - 1;+ for (int i = 0; i < nrows; i++, position += direction)+ {+ uint64 bit = UINT64CONST(1) << position;++ if (eval_batch_qual(qual, values[position],+ (nulls & bit) != 0))+ mask |= bit;+ }+ }++ if (qual->param_expr != NULL)+ {+ arg->value = (Datum) 0;+ arg->isnull = true;+ }+ }++ /*+ * For later quals, mask contains only rows accepted by all preceding+ * quals. Testing it in both loops avoids fetching attributes and invoking+ * operators for rows already rejected.+ */+ for (int q = 1; q < state->nquals && mask != 0; q++)+ {+ struct SeqScanBatchQual *qual = &state->quals[q];+ FunctionCallInfo fcinfo = qual->fcinfo;+ NullableDatum *arg = &fcinfo->args[1 - qual->var_argno];++ if (qual->param_expr != NULL)+ arg->value = ExecEvalExpr(qual->param_expr, econtext,+ &arg->isnull);+ if (arg->isnull)+ {+ if (qual->param_expr != NULL)+ arg->value = (Datum) 0;+ mask = 0;+ break;+ }++ position = ScanDirectionIsForward(direction) ? 0 : nrows - 1;+ for (int i = 0; i < nrows; i++, position += direction)+ {+ bool isnull;+ uint64 bit = UINT64CONST(1) << position;++ if ((mask & bit) == 0)+ continue;+ values[position] = heap_page_batch_getattr(page, batch,+ first + position,+ slot->tts_tupleDescriptor,+ qual->attnum, &isnull);+ if (isnull)+ mask &= ~bit;+ }++ position = ScanDirectionIsForward(direction) ? 0 : nrows - 1;+ for (int i = 0; i < nrows && mask != 0;+ i++, position += direction)+ {+ uint64 bit = UINT64CONST(1) << position;++ if ((mask & bit) == 0)+ continue;+ if (!eval_batch_qual(qual, values[position], false))+ mask &= ~bit;+ }++ if (qual->param_expr != NULL)+ {+ arg->value = (Datum) 0;+ arg->isnull = true;+ }+ }+ MemoryContextSwitchTo(oldcontext);++ state->mask = mask;+ state->block = block;+ state->first = first;+ state->nrows = nrows;+}++static bool+is_batch_qual_value(Node *node)+{+ return IsA(node, Const) ||+ (IsA(node, Param) &&+ (castNode(Param, node)->paramkind == PARAM_EXTERN ||+ castNode(Param, node)->paramkind == PARAM_EXEC));+}++static OpExpr *+match_batch_qual(Node *clause, uint8 *var_argno)+{+ OpExpr *op;+ Node *left;+ Node *right;+ Var *var;++ if (!IsA(clause, OpExpr))+ return NULL;+ op = (OpExpr *) clause;+ if (list_length(op->args) != 2)+ return NULL;++ left = linitial(op->args);+ right = lsecond(op->args);+ if (IsA(left, Var) && is_batch_qual_value(right))+ *var_argno = 0;+ else if (is_batch_qual_value(left) && IsA(right, Var))+ *var_argno = 1;+ else+ return NULL;+ var = list_nth_node(Var, op->args, *var_argno);++ if (var->varattno <= 0 || var->varlevelsup != 0 ||+ var->varreturningtype != VAR_RETURNING_DEFAULT)+ return NULL;+ if (op->opresulttype != BOOLOID ||+ op->opretset ||+ func_volatile(op->opfuncid) != PROVOLATILE_IMMUTABLE ||+ !func_strict(op->opfuncid) ||+ !get_func_leakproof(op->opfuncid))+ return NULL;++ return op;+}++static void+init_batch_qual(struct SeqScanBatchQual *qual, OpExpr *op,+ PlanState *parent)+{+ uint8 var_argno = IsA(linitial(op->args), Var) ? 0 : 1;+ Expr *arg = (Expr *) list_nth(op->args, 1 - var_argno);+ Var *var = list_nth_node(Var, op->args, var_argno);++ fmgr_info(op->opfuncid, &qual->func);+ fmgr_info_set_expr((Node *) op, &qual->func);+ qual->fcinfo = palloc0(SizeForFunctionCallInfo(2));+ InitFunctionCallInfoData(*qual->fcinfo, &qual->func, 2,+ op->inputcollid, NULL, NULL);+ if (IsA(arg, Const))+ {+ qual->fcinfo->args[1 - var_argno].value =+ castNode(Const, arg)->constvalue;+ qual->fcinfo->args[1 - var_argno].isnull =+ castNode(Const, arg)->constisnull;+ }+ else+ {+ Assert(IsA(arg, Param));+ qual->param_expr = ExecInitExpr(arg, parent);+ }+ qual->attnum = var->varattno;+ qual->var_argno = var_argno;+}++static struct SeqScanBatchState *+init_batch_state(SeqScan *node, PlanState *parent)+{+ struct SeqScanBatchState *state;+ List *clauses = node->scan.plan.qual;+ List *batch_clauses;+ List *remaining_clauses;+ int nclauses = list_length(clauses);+ int nquals = 0;+ int i = 0;++ if (nclauses == 0)+ return NULL;++ foreach_ptr(Node, clause, clauses)+ {+ uint8 var_argno;++ if (match_batch_qual(clause, &var_argno) == NULL)+ break;+ nquals++;+ }+ if (nquals == 0)+ return NULL;++ batch_clauses = list_copy_head(clauses, nquals);+ remaining_clauses = list_copy_tail(clauses, nquals);+ state = palloc0_object(struct SeqScanBatchState);+ state->nquals = nquals;+ state->scalar_qual = ExecInitQual(batch_clauses, parent);+ parent->qual = ExecInitQual(remaining_clauses, parent);+ state->quals = palloc0_array(struct SeqScanBatchQual, state->nquals);+ foreach_ptr(OpExpr, op, batch_clauses)+ init_batch_qual(&state->quals[i++], op, parent);++ return state;+}+
/* ----------------------------------------------------------------
* Scan Support
* ----------------------------------------------------------------
@@ -156,6 +557,140 @@ ExecSeqScanWithQual(PlanState *pstate)
NULL);
}
+static pg_always_inline TupleTableSlot *+ExecSeqScanBatchLoop(PlanState *pstate, ExprState *rest_qual,+ ProjectionInfo *projInfo, bool use_batch)+{+ SeqScanState *node = castNode(SeqScanState, pstate);+ ExprContext *econtext = pstate->ps_ExprContext;+ TupleTableSlot *result_slot;+ TupleTableSlot *slot;+ ScanDirection direction;+ bool prepare_batch;+ bool qual_result;++ Assert(pstate->state->es_epq_active == NULL);+ Assert(projInfo == pstate->ps_ProjInfo);+ Assert(node->batch_state != NULL);+ pg_assume(node->batch_state->scalar_qual != NULL);++ direction = pstate->state->es_direction;+ if (!use_batch)+ reset_batch_qual(node->batch_state);++ /*+ * This is the no-EPQ path from ExecScanExtended(), kept here so batch qual+ * evaluation can be added below.+ */+ ResetExprContext(econtext);+ for (;;)+ {+ slot = ExecScanFetch(&node->ss, NULL,+ (ExecScanAccessMtd) SeqNext,+ (ExecScanRecheckMtd) SeqRecheck);+ if (TupIsNull(slot))+ {+ reset_batch_qual(node->batch_state);+ if (projInfo != NULL)+ return ExecClearTuple(projInfo->pi_state.resultslot);+ return slot;+ }++ prepare_batch = false;+ if (!use_batch)+ qual_result = eval_scalar_qual(node->batch_state, slot, econtext);+ else if (!reuse_batch_qual(node->batch_state, slot, &qual_result))+ {+ qual_result = eval_scalar_qual(node->batch_state, slot, econtext);+ prepare_batch = true;+ }++ if (qual_result && rest_qual != NULL)+ {+ econtext->ecxt_scantuple = slot;+ qual_result = ExecQual(rest_qual, econtext);+ }++ result_slot = slot;+ if (qual_result && projInfo != NULL)+ {+ econtext->ecxt_scantuple = slot;+ result_slot = ExecProject(projInfo);+ }++ /* Finish the current tuple before evaluating quals for later tuples. */+ if (prepare_batch)+ prepare_batch_qual(node->batch_state, slot, direction, econtext);++ if (qual_result)+ return result_slot;++ InstrCountFiltered1(node, 1);+ ResetExprContext(econtext);+ }+}++static pg_always_inline TupleTableSlot *+ExecSeqScanBatchDispatch(PlanState *pstate, ExprState *rest_qual,+ ProjectionInfo *projInfo)+{+ ScanDirection direction = pstate->state->es_direction;++ if (ScanDirectionIsForward(direction) ||+ ScanDirectionIsBackward(direction))+ return ExecSeqScanBatchLoop(pstate, rest_qual, projInfo, true);+ return ExecSeqScanBatchLoop(pstate, rest_qual, projInfo, false);+}++/*+ * Variant of ExecSeqScan() when all scan quals can be evaluated in batches.+ */+static TupleTableSlot *+ExecSeqScanWithBatchQual(PlanState *pstate)+{+ Assert(pstate->qual == NULL);++ return ExecSeqScanBatchDispatch(pstate, NULL, NULL);+}++/*+ * Variant of ExecSeqScan() when batch quals are followed by remaining quals.+ */+static TupleTableSlot *+ExecSeqScanWithBatchQualRest(PlanState *pstate)+{+ pg_assume(pstate->qual != NULL);++ return ExecSeqScanBatchDispatch(pstate, pstate->qual, NULL);+}++/*+ * Variant of ExecSeqScan() when all scan quals can be evaluated in batches+ * and projection is required.+ */+static TupleTableSlot *+ExecSeqScanWithBatchQualProject(PlanState *pstate)+{+ Assert(pstate->qual == NULL);+ pg_assume(pstate->ps_ProjInfo != NULL);++ return ExecSeqScanBatchDispatch(pstate, NULL, pstate->ps_ProjInfo);+}++/*+ * Variant of ExecSeqScan() when batch quals are followed by remaining quals+ * and projection is required.+ */+static TupleTableSlot *+ExecSeqScanWithBatchQualRestProject(PlanState *pstate)+{+ pg_assume(pstate->qual != NULL);+ pg_assume(pstate->ps_ProjInfo != NULL);++ return ExecSeqScanBatchDispatch(pstate, pstate->qual,+ pstate->ps_ProjInfo);+}+
/*
* Variant of ExecSeqScan() but when projection is required.
*/
@@ -265,16 +800,32 @@ ExecInitSeqScan(SeqScan *node, EState *estate, int eflags)
/*
* initialize child expressions
*/
- scanstate->ss.ps.qual =- ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);+ if (estate->es_epq_active == NULL)+ scanstate->batch_state = init_batch_state(node, &scanstate->ss.ps);+ if (scanstate->batch_state == NULL)+ scanstate->ss.ps.qual =+ ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
/*
* When EvalPlanQual() is not in use, assign ExecProcNode for this node
- * based on the presence of qual and projection. Each ExecSeqScan*()- * variant is optimized for the specific combination of these conditions.+ * based on the presence of batch state, qual and projection. Each+ * ExecSeqScan*() variant is optimized for the specific combination of+ * these conditions.
*/
if (scanstate->ss.ps.state->es_epq_active != NULL)
scanstate->ss.ps.ExecProcNode = ExecSeqScanEPQ;
+ else if (scanstate->batch_state != NULL)+ {+ if (scanstate->ss.ps.qual == NULL &&+ scanstate->ss.ps.ps_ProjInfo == NULL)+ scanstate->ss.ps.ExecProcNode = ExecSeqScanWithBatchQual;+ else if (scanstate->ss.ps.qual == NULL)+ scanstate->ss.ps.ExecProcNode = ExecSeqScanWithBatchQualProject;+ else if (scanstate->ss.ps.ps_ProjInfo == NULL)+ scanstate->ss.ps.ExecProcNode = ExecSeqScanWithBatchQualRest;+ else+ scanstate->ss.ps.ExecProcNode = ExecSeqScanWithBatchQualRestProject;+ }
else if (scanstate->ss.ps.qual == NULL)
{
if (scanstate->ss.ps.ps_ProjInfo == NULL)
@@ -348,6 +899,9 @@ ExecReScanSeqScan(SeqScanState *node)
{
TableScanDesc scan;
+ if (node->batch_state != NULL)+ reset_batch_qual(node->batch_state);+
scan = node->ss.ss_currentScanDesc;
if (scan != NULL)
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.hindex e64fd8c7ea3..4c9b2e8986e 100644--- a/src/include/nodes/execnodes.h+++ b/src/include/nodes/execnodes.h@@ -1674,6 +1674,7 @@ typedef struct SeqScanState
ScanState ss; /* its first field is NodeTag */
Size pscan_len; /* size of parallel heap scan descriptor */
struct SharedSeqScanInstrumentation *sinstrument;
+ struct SeqScanBatchState *batch_state;
} SeqScanState;
/* ----------------
diff --git a/src/test/regress/expected/plancache.out b/src/test/regress/expected/plancache.outindex d58534ca1cd..c8d471a2c2f 100644--- a/src/test/regress/expected/plancache.out+++ b/src/test/regress/expected/plancache.out@@ -402,3 +402,132 @@ select name, generic_plans, custom_plans from pg_prepared_statements
(1 row)
drop table test_mode;
+-- Set up a table spanning multiple heap pages for batch qualification tests.+create table batch_param_test (a int, t text, padding text)+ with (autovacuum_enabled = false);+insert into batch_param_test+ select i, 'value-' || i, repeat('x', 500)+ from generate_series(1, 200) i;+-- Test multiple batch qualifications.+select count(*) from batch_param_test+ where a > 50 and a < 151 and t <> 'value-100' \gset batch_multi_const_+\echo :batch_multi_const_count+99+-- Test batch qualifications followed by regular qualification.+explain (analyze, costs off, timing off, summary off, buffers off)+select * from batch_param_test where a > 50 and length(t) = 8;+ QUERY PLAN +----------------------------------------------------------+ Seq Scan on batch_param_test (actual rows=49.00 loops=1)+ Filter: ((a > 50) AND (length(t) = 8))+ Rows Removed by Filter: 151+(3 rows)++-- Test projection with batch qualification in both scan directions.+begin;+declare batch_multi_scroll scroll cursor for+ select a + 1 as projected, upper(t) as projected_text+ from batch_param_test+ where a > 50 and a < 55 and length(t) = 8;+fetch forward 1 from batch_multi_scroll \gset batch_scroll_first_+fetch forward 1 from batch_multi_scroll \gset batch_scroll_second_+fetch backward 1 from batch_multi_scroll \gset batch_scroll_backward_+fetch forward 1 from batch_multi_scroll \gset batch_scroll_again_+\echo :batch_scroll_first_projected :batch_scroll_first_projected_text+52 VALUE-51+\echo :batch_scroll_second_projected :batch_scroll_second_projected_text+53 VALUE-52+\echo :batch_scroll_backward_projected :batch_scroll_backward_projected_text+52 VALUE-51+\echo :batch_scroll_again_projected :batch_scroll_again_projected_text+53 VALUE-52+close batch_multi_scroll;+commit;+-- Check the projection result slot at end of scan.+select a + 1 as projected+from batch_param_test+where a > 200 and length(t) = 8;+ projected +-----------+(0 rows)++-- Test batch qualification with external parameters in generic plans.+set plan_cache_mode = force_generic_plan;+prepare batch_param_int_rev(int) as+ select count(*) from batch_param_test where $1 > a;+prepare batch_param_multi(int, int, text) as+ select count(*) from batch_param_test+ where a > $1 and a < $2 and t <> $3;+prepare batch_param_rest(int, text) as+ select count(*) from batch_param_test+ where a > $1 and t <> $2 and length(t) = 8;+explain (costs off) execute batch_param_rest(50, 'value-75');+ QUERY PLAN +--------------------------------------------------------------+ Aggregate+ -> Seq Scan on batch_param_test+ Filter: ((a > $1) AND (t <> $2) AND (length(t) = 8))+(3 rows)++execute batch_param_int_rev(101) \gset batch_int_rev_+execute batch_param_multi(50, 151, 'value-100') \gset batch_multi_first_+execute batch_param_multi(100, 201, 'value-150') \gset batch_multi_second_+execute batch_param_multi(null, 151, 'value-100') \gset batch_multi_null_first_+execute batch_param_multi(50, 151, null) \gset batch_multi_null_last_+\echo :batch_int_rev_count :batch_multi_first_count :batch_multi_second_count :batch_multi_null_first_count :batch_multi_null_last_count+100 99 99 0 0+execute batch_param_rest(50, 'value-75') \gset batch_rest_first_+execute batch_param_rest(90, 'value-95') \gset batch_rest_second_+\echo :batch_rest_first_count :batch_rest_second_count+48 8+-- Test PL/pgSQL parameter compilation with batch qualification.+create function batch_param_plpgsql(p int, q text)+returns table (int_result bigint, text_result bigint)+language plpgsql as $$+begin+ return query+ select (select count(*) from batch_param_test b where b.a < p),+ (select count(*) from batch_param_test b where b.t = q);+end+$$;+select * from batch_param_plpgsql(1, 'value-100') \gset batch_plpgsql_first_+select * from batch_param_plpgsql(101, 'missing') \gset batch_plpgsql_second_+select * from batch_param_plpgsql(null, null) \gset batch_plpgsql_null_+\echo :batch_plpgsql_first_int_result :batch_plpgsql_first_text_result :batch_plpgsql_second_int_result :batch_plpgsql_second_text_result :batch_plpgsql_null_int_result :batch_plpgsql_null_text_result+0 1 100 0 0 0+drop function batch_param_plpgsql(int, text);+-- Test batch qualification with internal executor parameters.+create table batch_param_exec_test (id int, p int, q text);+insert into batch_param_exec_test values+ (1, 1, 'value-2'),+ (2, 200, 'missing'),+ (3, null, null),+ (4, 199, 'value-200');+-- OFFSET 0 prevents conversion of EXISTS, keeping the correlated condition+-- as a scan qualification.+explain (costs off)+select exists (select from batch_param_test b+ where o.p < b.a and b.t <> o.q and length(b.t) = 8+ offset 0)+from batch_param_exec_test o;+ QUERY PLAN +------------------------------------------------------------------+ Seq Scan on batch_param_exec_test o+ SubPlan exists_1+ -> Seq Scan on batch_param_test b+ Filter: ((o.p < a) AND (t <> o.q) AND (length(t) = 8))+(4 rows)++select array_agg(exists (select from batch_param_test b+ where o.p < b.a and b.t <> o.q and length(b.t) = 8+ offset 0)+ order by o.id) as combined_results+from batch_param_exec_test o;+ combined_results +------------------+ {t,f,f,f}+(1 row)++drop table batch_param_exec_test;+reset plan_cache_mode;+drop table batch_param_test;diff --git a/src/test/regress/sql/plancache.sql b/src/test/regress/sql/plancache.sqlindex aed388d03a1..ecb35177ce9 100644--- a/src/test/regress/sql/plancache.sql+++ b/src/test/regress/sql/plancache.sql@@ -228,3 +228,107 @@ select name, generic_plans, custom_plans from pg_prepared_statements
where name = 'test_mode_pp';
drop table test_mode;
++-- Set up a table spanning multiple heap pages for batch qualification tests.+create table batch_param_test (a int, t text, padding text)+ with (autovacuum_enabled = false);+insert into batch_param_test+ select i, 'value-' || i, repeat('x', 500)+ from generate_series(1, 200) i;++-- Test multiple batch qualifications.+select count(*) from batch_param_test+ where a > 50 and a < 151 and t <> 'value-100' \gset batch_multi_const_+\echo :batch_multi_const_count++-- Test batch qualifications followed by regular qualification.+explain (analyze, costs off, timing off, summary off, buffers off)+select * from batch_param_test where a > 50 and length(t) = 8;++-- Test projection with batch qualification in both scan directions.+begin;+declare batch_multi_scroll scroll cursor for+ select a + 1 as projected, upper(t) as projected_text+ from batch_param_test+ where a > 50 and a < 55 and length(t) = 8;+fetch forward 1 from batch_multi_scroll \gset batch_scroll_first_+fetch forward 1 from batch_multi_scroll \gset batch_scroll_second_+fetch backward 1 from batch_multi_scroll \gset batch_scroll_backward_+fetch forward 1 from batch_multi_scroll \gset batch_scroll_again_+\echo :batch_scroll_first_projected :batch_scroll_first_projected_text+\echo :batch_scroll_second_projected :batch_scroll_second_projected_text+\echo :batch_scroll_backward_projected :batch_scroll_backward_projected_text+\echo :batch_scroll_again_projected :batch_scroll_again_projected_text+close batch_multi_scroll;+commit;++-- Check the projection result slot at end of scan.+select a + 1 as projected+from batch_param_test+where a > 200 and length(t) = 8;++-- Test batch qualification with external parameters in generic plans.+set plan_cache_mode = force_generic_plan;+prepare batch_param_int_rev(int) as+ select count(*) from batch_param_test where $1 > a;+prepare batch_param_multi(int, int, text) as+ select count(*) from batch_param_test+ where a > $1 and a < $2 and t <> $3;+prepare batch_param_rest(int, text) as+ select count(*) from batch_param_test+ where a > $1 and t <> $2 and length(t) = 8;++explain (costs off) execute batch_param_rest(50, 'value-75');+execute batch_param_int_rev(101) \gset batch_int_rev_+execute batch_param_multi(50, 151, 'value-100') \gset batch_multi_first_+execute batch_param_multi(100, 201, 'value-150') \gset batch_multi_second_+execute batch_param_multi(null, 151, 'value-100') \gset batch_multi_null_first_+execute batch_param_multi(50, 151, null) \gset batch_multi_null_last_+\echo :batch_int_rev_count :batch_multi_first_count :batch_multi_second_count :batch_multi_null_first_count :batch_multi_null_last_count+execute batch_param_rest(50, 'value-75') \gset batch_rest_first_+execute batch_param_rest(90, 'value-95') \gset batch_rest_second_+\echo :batch_rest_first_count :batch_rest_second_count++-- Test PL/pgSQL parameter compilation with batch qualification.+create function batch_param_plpgsql(p int, q text)+returns table (int_result bigint, text_result bigint)+language plpgsql as $$+begin+ return query+ select (select count(*) from batch_param_test b where b.a < p),+ (select count(*) from batch_param_test b where b.t = q);+end+$$;++select * from batch_param_plpgsql(1, 'value-100') \gset batch_plpgsql_first_+select * from batch_param_plpgsql(101, 'missing') \gset batch_plpgsql_second_+select * from batch_param_plpgsql(null, null) \gset batch_plpgsql_null_+\echo :batch_plpgsql_first_int_result :batch_plpgsql_first_text_result :batch_plpgsql_second_int_result :batch_plpgsql_second_text_result :batch_plpgsql_null_int_result :batch_plpgsql_null_text_result++drop function batch_param_plpgsql(int, text);++-- Test batch qualification with internal executor parameters.+create table batch_param_exec_test (id int, p int, q text);+insert into batch_param_exec_test values+ (1, 1, 'value-2'),+ (2, 200, 'missing'),+ (3, null, null),+ (4, 199, 'value-200');++-- OFFSET 0 prevents conversion of EXISTS, keeping the correlated condition+-- as a scan qualification.+explain (costs off)+select exists (select from batch_param_test b+ where o.p < b.a and b.t <> o.q and length(b.t) = 8+ offset 0)+from batch_param_exec_test o;++select array_agg(exists (select from batch_param_test b+ where o.p < b.a and b.t <> o.q and length(b.t) = 8+ offset 0)+ order by o.id) as combined_results+from batch_param_exec_test o;++drop table batch_param_exec_test;+reset plan_cache_mode;+drop table batch_param_test;--
2.54.0
=
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: darthunix@gmail.com, amitlangote09@gmail.com, zhjwpku@gmail.com, cca5507@qq.com, 3danissimo@gmail.com, tomas@vondra.me
Subject: Re: Batching in executor
In-Reply-To: <A800AC8E-6D9F-4F04-9ACB-D73A2CF87D58@gmail.com>
References: <CA+HiwqFfAY_ZFqN8wcAEMw71T9hM_kA8UtyHaZZEZtuT3UyogA@mail.gmail.com> <tencent_B45268659AF0400F68EC1643950ABB6A1B09@qq.com> <CAJDiXggP41+-HrRzT+BmtgmS8wkoZM7b4skkQA5NAe+NFEMPSQ@mail.gmail.com> <CA+HiwqGufj46_4kT6z5nQrCOxp3rcMDR8fn_t33n2pDKokNRQg@mail.gmail.com> <CA+HiwqH-2GmTKLW9kHdnqV4KdFiPfuAdVK2TgqOM2JaaeUYXnw@mail.gmail.com> <CAEG8a3JKNRGwBb-C_YV6Px4aFHwd_G=AsB5AbfyU4-8YZT54AQ@mail.gmail.com> <tencent_164C2A5EDF8189DD70287661406D688A8506@qq.com> <CAEG8a3K22s9LmDZa486suFu3AV=yRuTf09j+Ww+djG63y=R8hg@mail.gmail.com> <CA+HiwqGkway9dcpTVmRs0Q=ZsQQ8Nx0MRjy9jfDmR8QFLnd-Mg@mail.gmail.com> <CA+HiwqHLjBegqeUw-babABj_icAiBTq5gh48-D3BEodVZkm8Ug@mail.gmail.com> <CA+HiwqEnHfjCVEo9PcTCNTCQVDBSRVeu=rPLqAc1tTafu7zowg@mail.gmail.com> <CA+HiwqHv1CggSbpn=n+GU=Cp1AqDD25=hzx_PTYyqjMyZO2+mA@mail.gmail.com> <3D8D1BA5-B8A6-400A-B605-42EEE37890B4@gmail.com> <749D4E65-6E1F-4569-AA0D-5C354E138E57@gmail.com> <A800AC8E-6D9F-4F04-9ACB-D73A2CF87D58@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox