Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wka1H-000kt5-0P for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Jul 2026 04:17:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wka1F-00FovB-2u for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Jul 2026 04:17:49 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wka1F-00Fov2-1a for pgsql-hackers@lists.postgresql.org; Fri, 17 Jul 2026 04:17:49 +0000 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wka1C-00000000hhw-3VxO for pgsql-hackers@postgresql.org; Fri, 17 Jul 2026 04:17:49 +0000 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-382ef647e20so6873257a91.1 for ; Thu, 16 Jul 2026 21:17:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784261862; x=1784866662; darn=postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:content-type :message-id:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=GGOyH3MusxNQhqdpNwNqxXsasRRnKYuwMv8EOTQBl7g=; b=SrI4uxWttS/0YOx1VhjDry5Nnp3Ak872ZXiENzgiNcPl8Mo/96ucpl5PRk/vv2nKog Ejm3XmRpXA9HaS5YqT+JHRMpPWdQ40UWxkwcYbG4BXocOFgMkI1urwA7HhGdEWzS0cjt uTBD9DtfL2zwdWWWYiBJHf36obGk0bUYq52PtdkXj9bIo6ZjMU5UjGKkpmglZsTGwzW+ 4OldkN0doXMFHexLJWrtIkWie2JIp2IUfQDFdHi+1whzXhnIRkD2I5BwtgxwsT5mUXIG Kkmx6Qw+TQw/tVzb093jZd8k4w7gHFIVgR7U8c8C5B3P7hfpxQHaOpIDYjqkjTw4by5u +j7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784261862; x=1784866662; h=references:to:cc:in-reply-to:date:subject:mime-version:content-type :message-id:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=GGOyH3MusxNQhqdpNwNqxXsasRRnKYuwMv8EOTQBl7g=; b=OTPZzJDEz2hIBE5cR/v7hoqHTlCCCk0IBhy6OPb2Ntnpmx3FXNTqjDdxRj8G9+26vp x8z98Xy4aCQj0ziwT8HcasMZjvPXxYEIoWiBExYqcu6iM7JTqykXvwVw91nIpfQ5YLYD pj3IS/jN/Bs839NJnjJmpvtR/Y79DWze8g4uzhaYUZqYon/DvfbzXI8j9qs++Ji2yIsW ghRRJTBIxGgIzzd3CTXN/IBEVoa7fEtdnHZUtrYWZv9gxlcvO10d8imDEowbohNituYm 92ZITXJ5E92tgLEALEiavkKOw8dexKKolU5lKwZqq0w9bFnrvCVyeEkmFeVDDFTeh/OE 5v0w== X-Forwarded-Encrypted: i=1; AHgh+RrFQ7aIqh+ue2i/022YBjlSL0UDp9ejSu/Bni+yLYDO1xESdimDKemyB9oV0XhhrBTbWEyRFQrcFGabMTOq@postgresql.org X-Gm-Message-State: AOJu0YxKrBTLoCDW0Aw5mz81CJicNeKiN7vmtKTMBHbDv2KEyY0GEODO jBGYpbuSIWjkMk3ogpG3kNTI+9CVd9LD4cBd3DN27HYsmeswn5mgujjB X-Gm-Gg: AfdE7cnfwhdz1bgSMNtzw8YNr4W+R7lWBSzuQblyLI6Xnbkb89bieudBLMEhmrrPzaq Ok1j1yKo+Ha3g+8v/ipNVjEYm+Ts7i7OOW/POheWav0aAMNewLvIuu6A0H8vs0ovdVN9HCen9qV 90uxpaP7aaItgae77+OfLGO2oSGdWF2eGD2pNtxokCsmp6bY2Cle45/B8mtG1pxII7EYPeoT6i4 /CTdHQ/k7ZL3NFvOeMUrA6zYsjX/EOw2S/3NYs0qX+M4X9/k1TrG37ULpfvXlOZK4SX483X62Yg 19MAXEhR3S/h+GmudKEnh8Qstx0d9YV+rxsuD4wV0qEBPTpV+9C3xR72zJrw+vDZYgSOS6FZ/Vr Vxt0TtfdTCrVgKt/7etLvgENrdM29Iy8pLyQoocpU192I/DmuKL8hgYPR X-Received: by 2002:a17:90b:35cf:b0:387:e0db:3d8e with SMTP id 98e67ed59e1d1-38e4b5acbf8mr986651a91.41.1784261862465; Thu, 16 Jul 2026 21:17:42 -0700 (PDT) Received: from smtpclient.apple ([1.47.87.246]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3142a20b037sm1912446eec.27.2026.07.16.21.17.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 Jul 2026 21:17:41 -0700 (PDT) From: Denis Smirnov Message-Id: <80692DF4-04F0-4292-8AF0-E13D5E13CE74@gmail.com> Content-Type: multipart/mixed; boundary="Apple-Mail=_8357CA5E-6C9C-459D-B514-FCCFCC7B5160" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Re: Batching in executor Date: Fri, 17 Jul 2026 11:17:26 +0700 In-Reply-To: Cc: Junwang Zhao , cca5507 , Daniil Davydov <3danissimo@gmail.com>, PostgreSQL-development , Tomas Vondra To: Amit Langote References: <68f3771f-91f5-4cb7-b1de-74d9abbf0b96@vondra.me> <3D8D1BA5-B8A6-400A-B605-42EEE37890B4@gmail.com> <749D4E65-6E1F-4569-AA0D-5C354E138E57@gmail.com> X-Mailer: Apple Mail (2.3864.600.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_8357CA5E-6C9C-459D-B514-FCCFCC7B5160 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii Hi, Attached is v3 of the two-patch heap-local batching series. The series is still based on master at 1c4b1de8885. Changes since v2: - heap_page_batch_next() now verifies that the stored offsets, tuple count, current position, and buffer still belong to the current heap scan. This prevents a stale batch from being reused when a compatible buffer slot is used by another scan or survives a rescan. - Batch qual evaluation now enters the function-usage accounting code only when track_functions requires it. Per-call accounting is still preserved when function tracking is enabled. - Consecutive supported quals referencing the same attribute now reuse the already collected Datum values for rows remaining in the mask. - prepare_batch_qual() now has one outer loop over quals, while retaining the unmasked hot path for the first qual. These changes do not alter the external interfaces or the set of supported expressions. Best regards, Denis Smirnov --Apple-Mail=_8357CA5E-6C9C-459D-B514-FCCFCC7B5160 Content-Disposition: attachment; filename=v3-0001-Avoid-repeated-heapgettup_pagemode-calls-within-a.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v3-0001-Avoid-repeated-heapgettup_pagemode-calls-within-a.patch" Content-Transfer-Encoding: quoted-printable =46rom=20225082ac1cc273bae3cb7549e6b42c9ffac014a3=20Mon=20Sep=2017=20= 00:00:00=202001=0AMessage-ID:=20= <225082ac1cc273bae3cb7549e6b42c9ffac014a3.1784261314.git.darthunix@gmail.c= om>=0AFrom:=20Denis=20Smirnov=20=0ADate:=20Sun,=20= 12=20Jul=202026=2021:48:00=20+0800=0ASubject:=20[PATCH=20v3=201/2]=20= Avoid=20repeated=20heapgettup_pagemode()=20calls=20within=20a=0A=20heap=20= page=0A=0Aheap_getnextslot()=20currently=20calls=20heapgettup_pagemode()=20= for=20every=0Atuple,=20even=20though=20page=20mode=20has=20already=20= collected=20the=20offsets=20of=20all=0Avisible=20tuples=20on=20the=20= current=20page.=20This=20repeats=20tuple-at-a-time=20scan=0Abookkeeping=20= that=20can=20become=20noticeable=20for=20analytical=20workloads=0A= dominated=20by=20sequential=20heap=20scans.=0A=0AAdd=20HeapPageBatch=20= to=20BufferHeapTupleTableSlot.=20It=20references=20the=0Avisible=20tuple=20= offsets=20collected=20by=20the=20heap=20scan=20and=20records=20which=20= one=0Ais=20currently=20stored=20in=20the=20slot.=20After=20the=20first=20= tuple=20from=20a=20page,=0Aheap_getnextslot()=20can=20advance=20directly=20= through=20those=20offsets,=20update=0Ars_ctup=20and=20invalidate=20the=20= slot's=20tuple=20deformation=20state.=20The=20regular=0Aheap=20scan=20= path=20is=20used=20again=20when=20the=20page=20is=20exhausted.=0A=0AUse=20= this=20fast=20path=20only=20for=20page-mode=20scans=20without=20scan=20= keys.=20Scans=0Ausing=20non-MVCC=20snapshots=20or=20heap=20scan=20keys=20= retain=20the=20existing=20behavior.=0AClear=20the=20page=20batch=20= whenever=20the=20slot=20is=20cleared,=20materialized,=20or=0Aassigned=20= another=20tuple.=20The=20batch=20does=20not=20require=20additional=20= buffer=0Apins=20and=20is=20valid=20only=20while=20the=20scan=20and=20= slot=20remain=20on=20the=20same=0Abuffer.=0A=0AThis=20reduces=20= per-tuple=20overhead=20while=20preserving=20the=20existing=0A= tuple-at-a-time=20executor=20interface.=20It=20also=20provides=20initial=0A= infrastructure=20for=20passing=20page-sized=20groups=20of=20heap=20= tuples=20to=0Abatch-aware=20executor=20nodes=20in=20the=20future.=0A=0A= Discussion:=20= https://postgr.es/m/3D8D1BA5-B8A6-400A-B605-42EEE37890B4%40gmail.com=0A= ---=0A=20src/backend/access/heap/heapam.c=20=20|=2086=20= +++++++++++++++++++++++++++++++=0A=20src/backend/executor/execTuples.c=20= |=2012=20+++++=0A=20src/include/executor/tuptable.h=20=20=20|=20=208=20= +++=0A=20src/tools/pgindent/typedefs.list=20=20|=20=201=20+=0A=204=20= files=20changed,=20107=20insertions(+)=0A=0Adiff=20--git=20= a/src/backend/access/heap/heapam.c=20b/src/backend/access/heap/heapam.c=0A= index=209cdc221675b..b1a0f76c08c=20100644=0A---=20= a/src/backend/access/heap/heapam.c=0A+++=20= b/src/backend/access/heap/heapam.c=0A@@=20-44,6=20+44,7=20@@=0A=20= #include=20"catalog/pg_database_d.h"=0A=20#include=20"commands/vacuum.h"=0A= =20#include=20"executor/instrument_node.h"=0A+#include=20= "executor/tuptable.h"=0A=20#include=20"pgstat.h"=0A=20#include=20= "port/pg_bitutils.h"=0A=20#include=20"storage/lmgr.h"=0A@@=20-1470,12=20= +1471,87=20@@=20heap_getnext(TableScanDesc=20sscan,=20ScanDirection=20= direction)=0A=20=09return=20&scan->rs_ctup;=0A=20}=0A=20=0A+static=20= inline=20void=0A+AssertHeapPageBatchValid(HeapScanDesc=20scan,=20= TupleTableSlot=20*slot)=0A+{=0A+#ifdef=20USE_ASSERT_CHECKING=0A+=09= BufferHeapTupleTableSlot=20*bslot=20=3D=20(BufferHeapTupleTableSlot=20*)=20= slot;=0A+=09HeapPageBatch=20*batch=20=3D=20&bslot->batch;=0A+=0A+=09= Assert(!TTS_EMPTY(slot));=0A+=09Assert(batch->offsets=20!=3D=20NULL);=0A= +=09Assert(batch->offsets=20=3D=3D=20scan->rs_vistuples);=0A+=09= Assert(batch->ntuples=20=3D=3D=20scan->rs_ntuples);=0A+=09= Assert(batch->current=20=3D=3D=20scan->rs_cindex);=0A+=09= Assert(batch->current=20>=3D=200);=0A+=09Assert(batch->current=20<=20= batch->ntuples);=0A+=09Assert(batch->offsets[batch->current]=20=3D=3D=0A= +=09=09=20=20=20ItemPointerGetOffsetNumber(&slot->tts_tid));=0A+=09= Assert(bslot->buffer=20=3D=3D=20scan->rs_cbuf);=0A+=09= Assert(bslot->base.tuple=20=3D=3D=20&scan->rs_ctup);=0A+#endif=0A+}=0A+=0A= +static=20bool=0A+heap_page_batch_next(HeapScanDesc=20scan,=20= ScanDirection=20direction,=0A+=09=09=09=09=09=20TupleTableSlot=20*slot)=0A= +{=0A+=09BufferHeapTupleTableSlot=20*bslot=20=3D=20= (BufferHeapTupleTableSlot=20*)=20slot;=0A+=09HeapPageBatch=20*batch=20=3D=20= &bslot->batch;=0A+=09HeapTuple=09tuple=20=3D=20&scan->rs_ctup;=0A+=09= Page=09=09page;=0A+=09ItemId=09=09lpp;=0A+=09OffsetNumber=20lineoff;=0A+=09= int=09=09=09next;=0A+=0A+=09if=20(batch->offsets=20=3D=3D=20NULL)=0A+=09=09= return=20false;=0A+=0A+=09if=20(unlikely(batch->offsets=20!=3D=20= scan->rs_vistuples=20||=0A+=09=09=09=09=20batch->ntuples=20!=3D=20= scan->rs_ntuples=20||=0A+=09=09=09=09=20batch->current=20!=3D=20= scan->rs_cindex=20||=0A+=09=09=09=09=20bslot->buffer=20!=3D=20= scan->rs_cbuf))=0A+=09=09return=20false;=0A+=0A+=09Assert(direction=20=3D=3D= =20ForwardScanDirection=20||=0A+=09=09=20=20=20direction=20=3D=3D=20= BackwardScanDirection);=0A+=09AssertHeapPageBatchValid(scan,=20slot);=0A= +=0A+=09next=20=3D=20batch->current=20+=20direction;=0A+=09if=20(next=20= <=200=20||=20next=20>=3D=20batch->ntuples)=0A+=09=09return=20false;=0A+=0A= +=09page=20=3D=20BufferGetPage(scan->rs_cbuf);=0A+=09lineoff=20=3D=20= batch->offsets[next];=0A+=09lpp=20=3D=20PageGetItemId(page,=20lineoff);=0A= +=09Assert(ItemIdIsNormal(lpp));=0A+=0A+=09tuple->t_data=20=3D=20= (HeapTupleHeader)=20PageGetItem(page,=20lpp);=0A+=09tuple->t_len=20=3D=20= ItemIdGetLength(lpp);=0A+=09ItemPointerSetOffsetNumber(&tuple->t_self,=20= lineoff);=0A+=0A+=09scan->rs_cindex=20=3D=20next;=0A+=09batch->current=20= =3D=20next;=0A+=09bslot->base.tuple=20=3D=20tuple;=0A+=09bslot->base.off=20= =3D=200;=0A+=09slot->tts_nvalid=20=3D=200;=0A+=09slot->tts_tid=20=3D=20= tuple->t_self;=0A+=0A+=09return=20true;=0A+}=0A+=0A=20bool=0A=20= heap_getnextslot(TableScanDesc=20sscan,=20ScanDirection=20direction,=20= TupleTableSlot=20*slot)=0A=20{=0A+=09BufferHeapTupleTableSlot=20*bslot=20= =3D=20(BufferHeapTupleTableSlot=20*)=20slot;=0A=20=09HeapScanDesc=20scan=20= =3D=20(HeapScanDesc)=20sscan;=0A=20=0A=20=09/*=20Note:=20no=20locking=20= manipulations=20needed=20*/=0A+=09if=20(heap_page_batch_next(scan,=20= direction,=20slot))=0A+=09{=0A+=09=09= pgstat_count_heap_getnext(scan->rs_base.rs_rd);=0A+=09=09return=20true;=0A= +=09}=0A=20=0A=20=09if=20(sscan->rs_flags=20&=20SO_ALLOW_PAGEMODE)=0A=20=09= =09heapgettup_pagemode(scan,=20direction,=20sscan->rs_nkeys,=20= sscan->rs_key);=0A@@=20-1497,6=20+1573,16=20@@=20= heap_getnextslot(TableScanDesc=20sscan,=20ScanDirection=20direction,=20= TupleTableSlot=20*s=0A=20=0A=20=09= ExecStoreBufferHeapTuple(&scan->rs_ctup,=20slot,=0A=20=09=09=09=09=09=09=09= =20scan->rs_cbuf);=0A+=0A+=09if=20((sscan->rs_flags=20&=20= SO_ALLOW_PAGEMODE)=20&&=20sscan->rs_nkeys=20=3D=3D=200)=0A+=09{=0A+=09=09= bslot->batch.offsets=20=3D=20scan->rs_vistuples;=0A+=09=09= bslot->batch.ntuples=20=3D=20scan->rs_ntuples;=0A+=09=09= bslot->batch.current=20=3D=20scan->rs_cindex;=0A+=0A+=09=09= AssertHeapPageBatchValid(scan,=20slot);=0A+=09}=0A+=0A=20=09return=20= true;=0A=20}=0A=20=0Adiff=20--git=20a/src/backend/executor/execTuples.c=20= b/src/backend/executor/execTuples.c=0Aindex=2097ae019d10a..b4431734c3f=20= 100644=0A---=20a/src/backend/executor/execTuples.c=0A+++=20= b/src/backend/executor/execTuples.c=0A@@=20-707,6=20+707,14=20@@=20= tts_minimal_store_tuple(TupleTableSlot=20*slot,=20MinimalTuple=20mtup,=20= bool=20shouldFree=0A=20=20*=20TupleTableSlotOps=20implementation=20for=20= BufferHeapTupleTableSlot.=0A=20=20*/=0A=20=0A+static=20inline=20void=0A= +heap_page_batch_reset(HeapPageBatch=20*batch)=0A+{=0A+=09batch->offsets=20= =3D=20NULL;=0A+=09batch->ntuples=20=3D=200;=0A+=09batch->current=20=3D=20= 0;=0A+}=0A+=0A=20static=20void=0A=20tts_buffer_heap_init(TupleTableSlot=20= *slot)=0A=20{=0A@@=20-745,6=20+753,7=20@@=20= tts_buffer_heap_clear(TupleTableSlot=20*slot)=0A=20=09bslot->base.tuple=20= =3D=20NULL;=0A=20=09bslot->base.off=20=3D=200;=0A=20=09bslot->buffer=20=3D= =20InvalidBuffer;=0A+=09heap_page_batch_reset(&bslot->batch);=0A=20}=0A=20= =0A=20static=20void=0A@@=20-807,6=20+816,7=20@@=20= tts_buffer_heap_materialize(TupleTableSlot=20*slot)=0A=20=09= MemoryContext=20oldContext;=0A=20=0A=20=09Assert(!TTS_EMPTY(slot));=0A+=09= heap_page_batch_reset(&bslot->batch);=0A=20=0A=20=09/*=20If=20slot=20has=20= its=20tuple=20already=20materialized,=20nothing=20to=20do.=20*/=0A=20=09= if=20(TTS_SHOULDFREE(slot))=0A@@=20-946,6=20+956,8=20@@=20= tts_buffer_heap_store_tuple(TupleTableSlot=20*slot,=20HeapTuple=20tuple,=0A= =20{=0A=20=09BufferHeapTupleTableSlot=20*bslot=20=3D=20= (BufferHeapTupleTableSlot=20*)=20slot;=0A=20=0A+=09= heap_page_batch_reset(&bslot->batch);=0A+=0A=20=09if=20= (TTS_SHOULDFREE(slot))=0A=20=09{=0A=20=09=09/*=20materialized=20slot=20= shouldn't=20have=20a=20buffer=20to=20release=20*/=0Adiff=20--git=20= a/src/include/executor/tuptable.h=20b/src/include/executor/tuptable.h=0A= index=203db6c9c9bd0..4ec94d2b84b=20100644=0A---=20= a/src/include/executor/tuptable.h=0A+++=20= b/src/include/executor/tuptable.h=0A@@=20-282,6=20+282,13=20@@=20typedef=20= struct=20HeapTupleTableSlot=0A=20=09HeapTupleData=20tupdata;=09=09/*=20= optional=20workspace=20for=20storing=20tuple=20*/=0A=20}=20= HeapTupleTableSlot;=0A=20=0A+typedef=20struct=20HeapPageBatch=0A+{=0A+=09= const=20OffsetNumber=20*offsets;=09/*=20visible=20offsets=20on=20slot's=20= page,=20or=20NULL=20*/=0A+=09int=09=09=09ntuples;=09=09/*=20number=20of=20= offsets=20*/=0A+=09int=09=09=09current;=09=09/*=20index=20of=20slot's=20= tuple=20in=20offsets=20*/=0A+}=20HeapPageBatch;=0A+=0A=20/*=20heap=20= tuple=20residing=20in=20a=20buffer=20*/=0A=20typedef=20struct=20= BufferHeapTupleTableSlot=0A=20{=0A@@=20-296,6=20+303,7=20@@=20typedef=20= struct=20BufferHeapTupleTableSlot=0A=20=09=20*=20such=20a=20case,=20= since=20presumably=20base.tuple=20is=20pointing=20into=20the=20buffer.)=0A= =20=09=20*/=0A=20=09Buffer=09=09buffer;=09=09=09/*=20tuple's=20buffer,=20= or=20InvalidBuffer=20*/=0A+=09HeapPageBatch=20batch;=0A=20}=20= BufferHeapTupleTableSlot;=0A=20=0A=20typedef=20struct=20= MinimalTupleTableSlot=0Adiff=20--git=20= a/src/tools/pgindent/typedefs.list=20b/src/tools/pgindent/typedefs.list=0A= index=2056c1f997f88..e843e50ca5b=20100644=0A---=20= a/src/tools/pgindent/typedefs.list=0A+++=20= b/src/tools/pgindent/typedefs.list=0A@@=20-1266,6=20+1266,7=20@@=20= HeadlineParsedText=0A=20HeadlineWordEntry=0A=20HeapCheckContext=0A=20= HeapCheckReadStreamData=0A+HeapPageBatch=0A=20HeapPageFreeze=0A=20= HeapScanDesc=0A=20HeapScanDescData=0A--=20=0A2.54.0=0A=0A= --Apple-Mail=_8357CA5E-6C9C-459D-B514-FCCFCC7B5160 Content-Disposition: attachment; filename=v3-0002-Evaluate-SeqScan-quals-in-heap-page-batches.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v3-0002-Evaluate-SeqScan-quals-in-heap-page-batches.patch" Content-Transfer-Encoding: quoted-printable =46rom=2035bef6f9187a5d20688b94cc92dfbebab9e782cc=20Mon=20Sep=2017=20= 00:00:00=202001=0AMessage-ID:=20= <35bef6f9187a5d20688b94cc92dfbebab9e782cc.1784261314.git.darthunix@gmail.c= om>=0AIn-Reply-To:=20= <225082ac1cc273bae3cb7549e6b42c9ffac014a3.1784261314.git.darthunix@gmail.c= om>=0AReferences:=20= <225082ac1cc273bae3cb7549e6b42c9ffac014a3.1784261314.git.darthunix@gmail.c= om>=0AFrom:=20Denis=20Smirnov=20=0ADate:=20Tue,=20= 14=20Jul=202026=2015:39:41=20+0800=0ASubject:=20[PATCH=20v3=202/2]=20= Evaluate=20SeqScan=20quals=20in=20heap=20page=20batches=0A=0AHeap=20= scans=20can=20now=20expose=20the=20visible=20tuples=20remaining=20on=20= the=20current=0Apage=20through=20HeapPageBatch.=20=20Use=20this=20= information=20in=20SeqScan=20to=20evaluate=0Asuitable=20scan=20quals=20= for=20several=20tuples=20at=20once.=0A=0AFor=20up=20to=2064=20tuples,=20= first=20deform=20the=20referenced=20attribute=20into=20a=20Datum=0A= array,=20then=20invoke=20the=20existing=20operator=20function=20in=20a=20= tight=20loop.=20=20Store=0Athe=20results=20in=20a=2064-bit=20mask.=20=20= With=20multiple=20quals,=20successively=20narrow=0Athe=20mask=20and=20= avoid=20deforming=20attributes=20or=20calling=20operators=20for=20tuples=0A= already=20rejected=20by=20an=20earlier=20qual.=20=20This=20reduces=20= per-tuple=20executor=0Aoverhead=20without=20adding=20a=20new=20= expression=20or=20function=20API.=0A=0ABatch=20only=20an=20initial=20= sequence=20of=20quals=20in=20the=20order=20chosen=20by=20the=0Aplanner.=20= =20Do=20not=20reorder=20them,=20both=20to=20preserve=20the=20required=20= evaluation=0Aorder=20and=20to=20avoid=20losing=20any=20favorable=20tuple=20= deformation=20order=20already=0Apresent=20in=20the=20plan.=20=20Evaluate=20= the=20first=20unsupported=20qual=20and=20everything=0Afollowing=20it=20= one=20tuple=20at=20a=20time.=20=20When=20no=20HeapPageBatch=20is=20= available,=0Aevaluate=20the=20complete=20filter=20using=20the=20normal=20= scalar=20path.=0A=0ARecognize=20operators=20of=20the=20form=20Var=20op=20= Const,=20Const=20op=20Var,=20Var=20op=20Param,=0Aor=20Param=20op=20Var.=20= =20Require=20the=20operator=20function=20to=20return=20bool,=20not=0A= return=20a=20set,=20and=20be=20marked=20IMMUTABLE,=20STRICT,=20and=20= LEAKPROOF.=20=20Batch=0Aevaluation=20may=20call=20an=20operator=20for=20= tuples=20that=20are=20never=20requested,=20for=0Aexample=20because=20of=20= LIMIT=20or=20a=20later=20qual,=20so=20the=20operator=20must=20have=20no=0A= observable=20side=20effects=20and=20its=20catalog=20declarations=20must=20= be=20correct.=0APreserve=20collation,=20NULL=20behavior,=20parameter=20= evaluation,=20function=20usage=0Aaccounting,=20and=20both=20scan=20= directions.=0A=0AApply=20ordinary=20projection=20with=20ExecProject()=20= only=20after=20a=20tuple=20passes=0Aall=20quals.=20=20Projection=20= itself=20remains=20row-at-a-time=20and=20does=20not=20yet=0Abenefit=20= from=20page=20batching.=0A=0ADiscussion:=0A= https://postgr.es/m/3D8D1BA5-B8A6-400A-B605-42EEE37890B4%40gmail.com=0A= ---=0A=20src/backend/executor/nodeSeqscan.c=20=20=20=20=20=20|=20554=20= +++++++++++++++++++++++-=0A=20src/include/nodes/execnodes.h=20=20=20=20=20= =20=20=20=20=20=20|=20=20=201=20+=0A=20= src/test/regress/expected/plancache.out=20|=20129=20++++++=0A=20= src/test/regress/sql/plancache.sql=20=20=20=20=20=20|=20104=20+++++=0A=20= 4=20files=20changed,=20784=20insertions(+),=204=20deletions(-)=0A=0Adiff=20= --git=20a/src/backend/executor/nodeSeqscan.c=20= b/src/backend/executor/nodeSeqscan.c=0Aindex=20b8c528ca089..543a7a5c6a2=20= 100644=0A---=20a/src/backend/executor/nodeSeqscan.c=0A+++=20= b/src/backend/executor/nodeSeqscan.c=0A@@=20-27,16=20+27,409=20@@=0A=20=20= */=0A=20#include=20"postgres.h"=0A=20=0A+#include=20= "access/htup_details.h"=0A=20#include=20"access/relscan.h"=0A=20#include=20= "access/tableam.h"=0A+#include=20"catalog/pg_proc.h"=0A+#include=20= "catalog/pg_type.h"=0A=20#include=20"executor/execParallel.h"=0A=20= #include=20"executor/execScan.h"=0A=20#include=20"executor/executor.h"=0A= =20#include=20"executor/nodeSeqscan.h"=0A+#include=20"pgstat.h"=0A= +#include=20"utils/lsyscache.h"=0A=20#include=20"utils/rel.h"=0A=20=0A=20= static=20TupleTableSlot=20*SeqNext(SeqScanState=20*node);=0A=20=0A+/*=20= Private=20state=20for=20one=20scan=20qual=20evaluated=20in=20page=20= batches.=20*/=0A+struct=20SeqScanBatchQual=0A+{=0A+=09FmgrInfo=09func;=0A= +=09FunctionCallInfo=20fcinfo;=0A+=09ExprState=20=20*param_expr;=0A+=09= AttrNumber=09attnum;=0A+=09uint8=09=09var_argno;=0A+};=0A+=0A+/*=20= Private=20state=20for=20page-batch=20qual=20evaluation.=20*/=0A+struct=20= SeqScanBatchState=0A+{=0A+=09struct=20SeqScanBatchQual=20*quals;=0A+=09= ExprState=20=20*scalar_qual;=0A+=09uint64=09=09mask;=09=09=09/*=20bit=20= i=20is=20for=20batch=20index=20first=20+=20i=20*/=0A+=09BlockNumber=20= block;=0A+=09int=09=09=09first;=0A+=09int=09=09=09nrows;=0A+=09int=09=09=09= nquals;=0A+};=0A+=0A+static=20inline=20void=0A+reset_batch_qual(struct=20= SeqScanBatchState=20*state)=0A+{=0A+=09state->nrows=20=3D=200;=0A+}=0A+=0A= +static=20BufferHeapTupleTableSlot=20*=0A= +get_valid_batch_slot(TupleTableSlot=20*slot,=20BlockNumber=20*block)=0A= +{=0A+=09BufferHeapTupleTableSlot=20*bslot;=0A+=09HeapPageBatch=20= *batch;=0A+=0A+=09if=20(!TTS_IS_BUFFERTUPLE(slot)=20||=0A+=09=09= !ItemPointerIsValid(&slot->tts_tid))=0A+=09=09return=20NULL;=0A+=0A+=09= bslot=20=3D=20(BufferHeapTupleTableSlot=20*)=20slot;=0A+=09batch=20=3D=20= &bslot->batch;=0A+=09if=20(batch->offsets=20=3D=3D=20NULL=20||=0A+=09=09= !BufferIsValid(bslot->buffer)=20||=0A+=09=09batch->current=20<=200=20||=20= batch->current=20>=3D=20batch->ntuples)=0A+=09=09return=20NULL;=0A+=0A+=09= *block=20=3D=20BufferGetBlockNumber(bslot->buffer);=0A+=09if=20= (ItemPointerGetBlockNumber(&slot->tts_tid)=20!=3D=20*block=20||=0A+=09=09= ItemPointerGetOffsetNumber(&slot->tts_tid)=20!=3D=0A+=09=09= batch->offsets[batch->current])=0A+=09=09return=20NULL;=0A+=0A+=09return=20= bslot;=0A+}=0A+=0A+static=20pg_always_inline=20bool=0A= +reuse_batch_qual(struct=20SeqScanBatchState=20*state,=20TupleTableSlot=20= *slot,=0A+=09=09=09=09=20bool=20*result)=0A+{=0A+=09= BufferHeapTupleTableSlot=20*bslot;=0A+=09BlockNumber=20block;=0A+=09int=09= =09=09position;=0A+=0A+=09if=20(state->nrows=20<=3D=200=20||=20= state->nrows=20>=2064)=0A+=09=09goto=20invalid;=0A+=0A+=09bslot=20=3D=20= get_valid_batch_slot(slot,=20&block);=0A+=09if=20(bslot=20=3D=3D=20NULL)=0A= +=09=09goto=20invalid;=0A+=0A+=09position=20=3D=20bslot->batch.current=20= -=20state->first;=0A+=09if=20(block=20!=3D=20state->block=20||=0A+=09=09= position=20<=200=20||=20position=20>=3D=20state->nrows)=0A+=09=09goto=20= invalid;=0A+=0A+=09*result=20=3D=20(state->mask=20&=20(UINT64CONST(1)=20= <<=20position))=20!=3D=200;=0A+=09return=20true;=0A+=0A+invalid:=0A+=09= reset_batch_qual(state);=0A+=09return=20false;=0A+}=0A+=0A+static=20= pg_always_inline=20bool=0A+eval_scalar_qual(struct=20SeqScanBatchState=20= *state,=20TupleTableSlot=20*slot,=0A+=09=09=09=09=20ExprContext=20= *econtext)=0A+{=0A+=09econtext->ecxt_scantuple=20=3D=20slot;=0A+=09= return=20ExecQual(state->scalar_qual,=20econtext);=0A+}=0A+=0A+static=20= pg_always_inline=20Datum=0A+heap_page_batch_getattr(Page=20page,=20const=20= HeapPageBatch=20*batch,=20int=20index,=0A+=09=09=09=09=09=09TupleDesc=20= tupledesc,=20AttrNumber=20attnum,=20bool=20*isnull)=0A+{=0A+=09= HeapTupleData=20tuple;=0A+=09ItemId=09=09lpp;=0A+=0A+=09lpp=20=3D=20= PageGetItemId(page,=20batch->offsets[index]);=0A+=09= Assert(ItemIdIsNormal(lpp));=0A+=09tuple.t_data=20=3D=20= (HeapTupleHeader)=20PageGetItem(page,=20lpp);=0A+=09tuple.t_len=20=3D=20= ItemIdGetLength(lpp);=0A+=0A+=09return=20heap_getattr(&tuple,=20attnum,=20= tupledesc,=20isnull);=0A+}=0A+=0A+static=20pg_always_inline=20bool=0A= +eval_batch_qual(struct=20SeqScanBatchQual=20*qual,=20Datum=20value,=20= bool=20isnull)=0A+{=0A+=09FunctionCallInfo=20fcinfo=20=3D=20= qual->fcinfo;=0A+=09PgStat_FunctionCallUsage=20fcusage;=0A+=09Datum=09=09= result;=0A+=09bool=09=09track_function;=0A+=0A+=09= fcinfo->args[qual->var_argno].value=20=3D=20value;=0A+=09= fcinfo->args[qual->var_argno].isnull=20=3D=20isnull;=0A+=09if=20(isnull)=0A= +=09=09return=20false;=0A+=09Assert(!fcinfo->args[1=20-=20= qual->var_argno].isnull);=0A+=0A+=09track_function=20=3D=20= pgstat_track_functions=20>=20fcinfo->flinfo->fn_stats;=0A+=09if=20= (track_function)=0A+=09=09pgstat_init_function_usage(fcinfo,=20= &fcusage);=0A+=09fcinfo->isnull=20=3D=20false;=0A+=09result=20=3D=20= FunctionCallInvoke(fcinfo);=0A+=09if=20(track_function)=0A+=09=09= pgstat_end_function_usage(&fcusage,=20true);=0A+=0A+=09return=20= !fcinfo->isnull=20&&=20DatumGetBool(result);=0A+}=0A+=0A+static=20void=0A= +prepare_batch_qual(struct=20SeqScanBatchState=20*state,=20= TupleTableSlot=20*slot,=0A+=09=09=09=09=20=20=20ScanDirection=20= direction,=20ExprContext=20*econtext)=0A+{=0A+=09= BufferHeapTupleTableSlot=20*bslot;=0A+=09HeapPageBatch=20*batch;=0A+=09= Datum=09=09values[64];=0A+=09MemoryContext=20oldcontext;=0A+=09= BlockNumber=20block;=0A+=09Page=09=09page;=0A+=09uint64=09=09mask=20=3D=20= 0;=0A+=09int=09=09=09first;=0A+=09int=09=09=09nrows;=0A+=09int=09=09=09= position;=0A+=0A+=09Assert(ScanDirectionIsForward(direction)=20||=0A+=09=09= =20=20=20ScanDirectionIsBackward(direction));=0A+=09Assert(state->nquals=20= >=200);=0A+=0A+=09reset_batch_qual(state);=0A+=0A+=09bslot=20=3D=20= get_valid_batch_slot(slot,=20&block);=0A+=09if=20(bslot=20=3D=3D=20NULL)=0A= +=09=09return;=0A+=0A+=09batch=20=3D=20&bslot->batch;=0A+=09if=20= (ScanDirectionIsForward(direction))=0A+=09{=0A+=09=09first=20=3D=20= batch->current=20+=201;=0A+=09=09nrows=20=3D=20Min(64,=20batch->ntuples=20= -=20first);=0A+=09}=0A+=09else=0A+=09{=0A+=09=09nrows=20=3D=20Min(64,=20= batch->current);=0A+=09=09first=20=3D=20batch->current=20-=20nrows;=0A+=09= }=0A+=09if=20(nrows=20=3D=3D=200)=0A+=09=09return;=0A+=0A+=09oldcontext=20= =3D=20MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);=0A+=09page=20= =3D=20BufferGetPage(bslot->buffer);=0A+=0A+=09/*=20Evaluate=20in=20scan=20= direction,=20but=20index=20values=20and=20mask=20in=20batch=20order.=20= */=0A+=09for=20(int=20q=20=3D=200;=0A+=09=09=20q=20<=20state->nquals=20= &&=20(q=20=3D=3D=200=20||=20mask=20!=3D=200);=0A+=09=09=20q++)=0A+=09{=0A= +=09=09struct=20SeqScanBatchQual=20*qual=20=3D=20&state->quals[q];=0A+=09= =09FunctionCallInfo=20fcinfo=20=3D=20qual->fcinfo;=0A+=09=09= NullableDatum=20*arg=20=3D=20&fcinfo->args[1=20-=20qual->var_argno];=0A+=0A= +=09=09if=20(qual->param_expr=20!=3D=20NULL)=0A+=09=09=09arg->value=20=3D=20= ExecEvalExpr(qual->param_expr,=20econtext,=0A+=09=09=09=09=09=09=09=09=09= =20=20&arg->isnull);=0A+=09=09if=20(arg->isnull)=0A+=09=09=09mask=20=3D=20= 0;=0A+=09=09else=20if=20(q=20=3D=3D=200)=0A+=09=09{=0A+=09=09=09uint64=09= =09nulls=20=3D=200;=0A+=0A+=09=09=09/*=0A+=09=09=09=20*=20Every=20row=20= is=20a=20candidate=20for=20the=20first=20qual.=20Using=20the=20masked=0A= +=09=09=09=20*=20loops=20below=20would=20add=20per-row=20tests=20to=20= the=20common=20single-qual=0A+=09=09=09=20*=20case.=20Track=20nulls=20= separately,=20then=20build=20the=20result=20mask.=0A+=09=09=09=20*/=0A+=09= =09=09position=20=3D=20ScanDirectionIsForward(direction)=20?=200=20:=20= nrows=20-=201;=0A+=09=09=09for=20(int=20i=20=3D=200;=20i=20<=20nrows;=20= i++,=20position=20+=3D=20direction)=0A+=09=09=09{=0A+=09=09=09=09bool=09=09= isnull;=0A+=09=09=09=09uint64=09=09bit=20=3D=20UINT64CONST(1)=20<<=20= position;=0A+=0A+=09=09=09=09values[position]=20=3D=20= heap_page_batch_getattr(page,=20batch,=0A+=09=09=09=09=09=09=09=09=09=09=09= =09=09=09=20=20=20first=20+=20position,=0A+=09=09=09=09=09=09=09=09=09=09= =09=09=09=09=20=20=20slot->tts_tupleDescriptor,=0A+=09=09=09=09=09=09=09=09= =09=09=09=09=09=09=20=20=20qual->attnum,=20&isnull);=0A+=09=09=09=09if=20= (isnull)=0A+=09=09=09=09=09nulls=20|=3D=20bit;=0A+=09=09=09}=0A+=0A+=09=09= =09position=20=3D=20ScanDirectionIsForward(direction)=20?=200=20:=20= nrows=20-=201;=0A+=09=09=09for=20(int=20i=20=3D=200;=20i=20<=20nrows;=20= i++,=20position=20+=3D=20direction)=0A+=09=09=09{=0A+=09=09=09=09uint64=09= =09bit=20=3D=20UINT64CONST(1)=20<<=20position;=0A+=0A+=09=09=09=09if=20= (eval_batch_qual(qual,=20values[position],=0A+=09=09=09=09=09=09=09=09=09= (nulls=20&=20bit)=20!=3D=200))=0A+=09=09=09=09=09mask=20|=3D=20bit;=0A+=09= =09=09}=0A+=09=09}=0A+=09=09else=0A+=09=09{=0A+=09=09=09/*=0A+=09=09=09=20= *=20For=20later=20quals,=20mask=20contains=20only=20rows=20accepted=20by=20= all=0A+=09=09=09=20*=20preceding=20quals.=20Testing=20it=20in=20both=20= loops=20avoids=20fetching=0A+=09=09=09=20*=20attributes=20and=20invoking=20= operators=20for=20rows=20already=20rejected.=0A+=09=09=09=20*=0A+=09=09=09= =20*=20If=20the=20preceding=20qual=20uses=20the=20same=20attribute,=20= values=20for=20rows=0A+=09=09=09=20*=20still=20present=20in=20the=20mask=20= are=20already=20populated=20and=20non-NULL.=0A+=09=09=09=20*/=0A+=09=09=09= if=20(qual->attnum=20!=3D=20state->quals[q=20-=201].attnum)=0A+=09=09=09= {=0A+=09=09=09=09position=20=3D=20ScanDirectionIsForward(direction)=20?=20= 0=20:=20nrows=20-=201;=0A+=09=09=09=09for=20(int=20i=20=3D=200;=20i=20<=20= nrows;=20i++,=20position=20+=3D=20direction)=0A+=09=09=09=09{=0A+=09=09=09= =09=09bool=09=09isnull;=0A+=09=09=09=09=09uint64=09=09bit=20=3D=20= UINT64CONST(1)=20<<=20position;=0A+=0A+=09=09=09=09=09if=20((mask=20&=20= bit)=20=3D=3D=200)=0A+=09=09=09=09=09=09continue;=0A+=09=09=09=09=09= values[position]=20=3D=20heap_page_batch_getattr(page,=20batch,=0A+=09=09= =09=09=09=09=09=09=09=09=09=09=09=09=09=20=20=20first=20+=20position,=0A= +=09=09=09=09=09=09=09=09=09=09=09=09=09=09=09=20=20=20= slot->tts_tupleDescriptor,=0A+=09=09=09=09=09=09=09=09=09=09=09=09=09=09=09= =20=20=20qual->attnum,=20&isnull);=0A+=09=09=09=09=09if=20(isnull)=0A+=09= =09=09=09=09=09mask=20&=3D=20~bit;=0A+=09=09=09=09}=0A+=09=09=09}=0A+=0A= +=09=09=09position=20=3D=20ScanDirectionIsForward(direction)=20?=200=20:=20= nrows=20-=201;=0A+=09=09=09for=20(int=20i=20=3D=200;=20i=20<=20nrows=20= &&=20mask=20!=3D=200;=0A+=09=09=09=09=20i++,=20position=20+=3D=20= direction)=0A+=09=09=09{=0A+=09=09=09=09uint64=09=09bit=20=3D=20= UINT64CONST(1)=20<<=20position;=0A+=0A+=09=09=09=09if=20((mask=20&=20= bit)=20=3D=3D=200)=0A+=09=09=09=09=09continue;=0A+=09=09=09=09if=20= (!eval_batch_qual(qual,=20values[position],=20false))=0A+=09=09=09=09=09= mask=20&=3D=20~bit;=0A+=09=09=09}=0A+=09=09}=0A+=0A+=09=09if=20= (qual->param_expr=20!=3D=20NULL)=0A+=09=09{=0A+=09=09=09arg->value=20=3D=20= (Datum)=200;=0A+=09=09=09arg->isnull=20=3D=20true;=0A+=09=09}=0A+=09}=0A= +=09MemoryContextSwitchTo(oldcontext);=0A+=0A+=09state->mask=20=3D=20= mask;=0A+=09state->block=20=3D=20block;=0A+=09state->first=20=3D=20= first;=0A+=09state->nrows=20=3D=20nrows;=0A+}=0A+=0A+static=20bool=0A= +is_batch_qual_value(Node=20*node)=0A+{=0A+=09return=20IsA(node,=20= Const)=20||=0A+=09=09(IsA(node,=20Param)=20&&=0A+=09=09=20= (castNode(Param,=20node)->paramkind=20=3D=3D=20PARAM_EXTERN=20||=0A+=09=09= =20=20castNode(Param,=20node)->paramkind=20=3D=3D=20PARAM_EXEC));=0A+}=0A= +=0A+static=20OpExpr=20*=0A+match_batch_qual(Node=20*clause,=20uint8=20= *var_argno)=0A+{=0A+=09OpExpr=09=20=20=20*op;=0A+=09Node=09=20=20=20= *left;=0A+=09Node=09=20=20=20*right;=0A+=09Var=09=09=20=20=20*var;=0A+=0A= +=09if=20(!IsA(clause,=20OpExpr))=0A+=09=09return=20NULL;=0A+=09op=20=3D=20= (OpExpr=20*)=20clause;=0A+=09if=20(list_length(op->args)=20!=3D=202)=0A+=09= =09return=20NULL;=0A+=0A+=09left=20=3D=20linitial(op->args);=0A+=09right=20= =3D=20lsecond(op->args);=0A+=09if=20(IsA(left,=20Var)=20&&=20= is_batch_qual_value(right))=0A+=09=09*var_argno=20=3D=200;=0A+=09else=20= if=20(is_batch_qual_value(left)=20&&=20IsA(right,=20Var))=0A+=09=09= *var_argno=20=3D=201;=0A+=09else=0A+=09=09return=20NULL;=0A+=09var=20=3D=20= list_nth_node(Var,=20op->args,=20*var_argno);=0A+=0A+=09if=20= (var->varattno=20<=3D=200=20||=20var->varlevelsup=20!=3D=200=20||=0A+=09=09= var->varreturningtype=20!=3D=20VAR_RETURNING_DEFAULT)=0A+=09=09return=20= NULL;=0A+=09if=20(op->opresulttype=20!=3D=20BOOLOID=20||=0A+=09=09= op->opretset=20||=0A+=09=09func_volatile(op->opfuncid)=20!=3D=20= PROVOLATILE_IMMUTABLE=20||=0A+=09=09!func_strict(op->opfuncid)=20||=0A+=09= =09!get_func_leakproof(op->opfuncid))=0A+=09=09return=20NULL;=0A+=0A+=09= return=20op;=0A+}=0A+=0A+static=20void=0A+init_batch_qual(struct=20= SeqScanBatchQual=20*qual,=20OpExpr=20*op,=0A+=09=09=09=09PlanState=20= *parent)=0A+{=0A+=09uint8=09=09var_argno=20=3D=20IsA(linitial(op->args),=20= Var)=20?=200=20:=201;=0A+=09Expr=09=20=20=20*arg=20=3D=20(Expr=20*)=20= list_nth(op->args,=201=20-=20var_argno);=0A+=09Var=09=09=20=20=20*var=20= =3D=20list_nth_node(Var,=20op->args,=20var_argno);=0A+=0A+=09= fmgr_info(op->opfuncid,=20&qual->func);=0A+=09fmgr_info_set_expr((Node=20= *)=20op,=20&qual->func);=0A+=09qual->fcinfo=20=3D=20= palloc0(SizeForFunctionCallInfo(2));=0A+=09= InitFunctionCallInfoData(*qual->fcinfo,=20&qual->func,=202,=0A+=09=09=09=09= =09=09=09=20op->inputcollid,=20NULL,=20NULL);=0A+=09if=20(IsA(arg,=20= Const))=0A+=09{=0A+=09=09qual->fcinfo->args[1=20-=20var_argno].value=20=3D= =0A+=09=09=09castNode(Const,=20arg)->constvalue;=0A+=09=09= qual->fcinfo->args[1=20-=20var_argno].isnull=20=3D=0A+=09=09=09= castNode(Const,=20arg)->constisnull;=0A+=09}=0A+=09else=0A+=09{=0A+=09=09= Assert(IsA(arg,=20Param));=0A+=09=09qual->param_expr=20=3D=20= ExecInitExpr(arg,=20parent);=0A+=09}=0A+=09qual->attnum=20=3D=20= var->varattno;=0A+=09qual->var_argno=20=3D=20var_argno;=0A+}=0A+=0A= +static=20struct=20SeqScanBatchState=20*=0A+init_batch_state(SeqScan=20= *node,=20PlanState=20*parent)=0A+{=0A+=09struct=20SeqScanBatchState=20= *state;=0A+=09List=09=20=20=20*clauses=20=3D=20node->scan.plan.qual;=0A+=09= List=09=20=20=20*batch_clauses;=0A+=09List=09=20=20=20= *remaining_clauses;=0A+=09int=09=09=09nclauses=20=3D=20= list_length(clauses);=0A+=09int=09=09=09nquals=20=3D=200;=0A+=09int=09=09= =09i=20=3D=200;=0A+=0A+=09if=20(nclauses=20=3D=3D=200)=0A+=09=09return=20= NULL;=0A+=0A+=09foreach_ptr(Node,=20clause,=20clauses)=0A+=09{=0A+=09=09= uint8=09=09var_argno;=0A+=0A+=09=09if=20(match_batch_qual(clause,=20= &var_argno)=20=3D=3D=20NULL)=0A+=09=09=09break;=0A+=09=09nquals++;=0A+=09= }=0A+=09if=20(nquals=20=3D=3D=200)=0A+=09=09return=20NULL;=0A+=0A+=09= batch_clauses=20=3D=20list_copy_head(clauses,=20nquals);=0A+=09= remaining_clauses=20=3D=20list_copy_tail(clauses,=20nquals);=0A+=09state=20= =3D=20palloc0_object(struct=20SeqScanBatchState);=0A+=09state->nquals=20= =3D=20nquals;=0A+=09state->scalar_qual=20=3D=20= ExecInitQual(batch_clauses,=20parent);=0A+=09parent->qual=20=3D=20= ExecInitQual(remaining_clauses,=20parent);=0A+=09state->quals=20=3D=20= palloc0_array(struct=20SeqScanBatchQual,=20state->nquals);=0A+=09= foreach_ptr(OpExpr,=20op,=20batch_clauses)=0A+=09=09= init_batch_qual(&state->quals[i++],=20op,=20parent);=0A+=0A+=09return=20= state;=0A+}=0A+=0A=20/*=20= ----------------------------------------------------------------=0A=20=20= *=09=09=09=09=09=09Scan=20Support=0A=20=20*=20= ----------------------------------------------------------------=0A@@=20= -156,6=20+549,140=20@@=20ExecSeqScanWithQual(PlanState=20*pstate)=0A=20=09= =09=09=09=09=09=09NULL);=0A=20}=0A=20=0A+static=20pg_always_inline=20= TupleTableSlot=20*=0A+ExecSeqScanBatchLoop(PlanState=20*pstate,=20= ExprState=20*rest_qual,=0A+=09=09=09=09=09=20ProjectionInfo=20*projInfo,=20= bool=20use_batch)=0A+{=0A+=09SeqScanState=20*node=20=3D=20= castNode(SeqScanState,=20pstate);=0A+=09ExprContext=20*econtext=20=3D=20= pstate->ps_ExprContext;=0A+=09TupleTableSlot=20*result_slot;=0A+=09= TupleTableSlot=20*slot;=0A+=09ScanDirection=20direction;=0A+=09bool=09=09= prepare_batch;=0A+=09bool=09=09qual_result;=0A+=0A+=09= Assert(pstate->state->es_epq_active=20=3D=3D=20NULL);=0A+=09= Assert(projInfo=20=3D=3D=20pstate->ps_ProjInfo);=0A+=09= Assert(node->batch_state=20!=3D=20NULL);=0A+=09= pg_assume(node->batch_state->scalar_qual=20!=3D=20NULL);=0A+=0A+=09= direction=20=3D=20pstate->state->es_direction;=0A+=09if=20(!use_batch)=0A= +=09=09reset_batch_qual(node->batch_state);=0A+=0A+=09/*=0A+=09=20*=20= This=20is=20the=20no-EPQ=20path=20from=20ExecScanExtended(),=20kept=20= here=20so=20batch=0A+=09=20*=20qual=20evaluation=20can=20be=20added=20= below.=0A+=09=20*/=0A+=09ResetExprContext(econtext);=0A+=09for=20(;;)=0A= +=09{=0A+=09=09slot=20=3D=20ExecScanFetch(&node->ss,=20NULL,=0A+=09=09=09= =09=09=09=09=20(ExecScanAccessMtd)=20SeqNext,=0A+=09=09=09=09=09=09=09=20= (ExecScanRecheckMtd)=20SeqRecheck);=0A+=09=09if=20(TupIsNull(slot))=0A+=09= =09{=0A+=09=09=09reset_batch_qual(node->batch_state);=0A+=09=09=09if=20= (projInfo=20!=3D=20NULL)=0A+=09=09=09=09return=20= ExecClearTuple(projInfo->pi_state.resultslot);=0A+=09=09=09return=20= slot;=0A+=09=09}=0A+=0A+=09=09prepare_batch=20=3D=20false;=0A+=09=09if=20= (!use_batch)=0A+=09=09=09qual_result=20=3D=20= eval_scalar_qual(node->batch_state,=20slot,=20econtext);=0A+=09=09else=20= if=20(!reuse_batch_qual(node->batch_state,=20slot,=20&qual_result))=0A+=09= =09{=0A+=09=09=09qual_result=20=3D=20eval_scalar_qual(node->batch_state,=20= slot,=20econtext);=0A+=09=09=09prepare_batch=20=3D=20true;=0A+=09=09}=0A= +=0A+=09=09if=20(qual_result=20&&=20rest_qual=20!=3D=20NULL)=0A+=09=09{=0A= +=09=09=09econtext->ecxt_scantuple=20=3D=20slot;=0A+=09=09=09qual_result=20= =3D=20ExecQual(rest_qual,=20econtext);=0A+=09=09}=0A+=0A+=09=09= result_slot=20=3D=20slot;=0A+=09=09if=20(qual_result=20&&=20projInfo=20= !=3D=20NULL)=0A+=09=09{=0A+=09=09=09econtext->ecxt_scantuple=20=3D=20= slot;=0A+=09=09=09result_slot=20=3D=20ExecProject(projInfo);=0A+=09=09}=0A= +=0A+=09=09/*=20Finish=20the=20current=20tuple=20before=20evaluating=20= quals=20for=20later=20tuples.=20*/=0A+=09=09if=20(prepare_batch)=0A+=09=09= =09prepare_batch_qual(node->batch_state,=20slot,=20direction,=20= econtext);=0A+=0A+=09=09if=20(qual_result)=0A+=09=09=09return=20= result_slot;=0A+=0A+=09=09InstrCountFiltered1(node,=201);=0A+=09=09= ResetExprContext(econtext);=0A+=09}=0A+}=0A+=0A+static=20= pg_always_inline=20TupleTableSlot=20*=0A= +ExecSeqScanBatchDispatch(PlanState=20*pstate,=20ExprState=20*rest_qual,=0A= +=09=09=09=09=09=09=20ProjectionInfo=20*projInfo)=0A+{=0A+=09= ScanDirection=20direction=20=3D=20pstate->state->es_direction;=0A+=0A+=09= if=20(ScanDirectionIsForward(direction)=20||=0A+=09=09= ScanDirectionIsBackward(direction))=0A+=09=09return=20= ExecSeqScanBatchLoop(pstate,=20rest_qual,=20projInfo,=20true);=0A+=09= return=20ExecSeqScanBatchLoop(pstate,=20rest_qual,=20projInfo,=20false);=0A= +}=0A+=0A+/*=0A+=20*=20Variant=20of=20ExecSeqScan()=20when=20all=20scan=20= quals=20can=20be=20evaluated=20in=20batches.=0A+=20*/=0A+static=20= TupleTableSlot=20*=0A+ExecSeqScanWithBatchQual(PlanState=20*pstate)=0A+{=0A= +=09Assert(pstate->qual=20=3D=3D=20NULL);=0A+=0A+=09return=20= ExecSeqScanBatchDispatch(pstate,=20NULL,=20NULL);=0A+}=0A+=0A+/*=0A+=20*=20= Variant=20of=20ExecSeqScan()=20when=20batch=20quals=20are=20followed=20= by=20remaining=20quals.=0A+=20*/=0A+static=20TupleTableSlot=20*=0A= +ExecSeqScanWithBatchQualRest(PlanState=20*pstate)=0A+{=0A+=09= pg_assume(pstate->qual=20!=3D=20NULL);=0A+=0A+=09return=20= ExecSeqScanBatchDispatch(pstate,=20pstate->qual,=20NULL);=0A+}=0A+=0A+/*=0A= +=20*=20Variant=20of=20ExecSeqScan()=20when=20all=20scan=20quals=20can=20= be=20evaluated=20in=20batches=0A+=20*=20and=20projection=20is=20= required.=0A+=20*/=0A+static=20TupleTableSlot=20*=0A= +ExecSeqScanWithBatchQualProject(PlanState=20*pstate)=0A+{=0A+=09= Assert(pstate->qual=20=3D=3D=20NULL);=0A+=09= pg_assume(pstate->ps_ProjInfo=20!=3D=20NULL);=0A+=0A+=09return=20= ExecSeqScanBatchDispatch(pstate,=20NULL,=20pstate->ps_ProjInfo);=0A+}=0A= +=0A+/*=0A+=20*=20Variant=20of=20ExecSeqScan()=20when=20batch=20quals=20= are=20followed=20by=20remaining=20quals=0A+=20*=20and=20projection=20is=20= required.=0A+=20*/=0A+static=20TupleTableSlot=20*=0A= +ExecSeqScanWithBatchQualRestProject(PlanState=20*pstate)=0A+{=0A+=09= pg_assume(pstate->qual=20!=3D=20NULL);=0A+=09= pg_assume(pstate->ps_ProjInfo=20!=3D=20NULL);=0A+=0A+=09return=20= ExecSeqScanBatchDispatch(pstate,=20pstate->qual,=0A+=09=09=09=09=09=09=09= =09=09pstate->ps_ProjInfo);=0A+}=0A+=0A=20/*=0A=20=20*=20Variant=20of=20= ExecSeqScan()=20but=20when=20projection=20is=20required.=0A=20=20*/=0A@@=20= -265,16=20+792,32=20@@=20ExecInitSeqScan(SeqScan=20*node,=20EState=20= *estate,=20int=20eflags)=0A=20=09/*=0A=20=09=20*=20initialize=20child=20= expressions=0A=20=09=20*/=0A-=09scanstate->ss.ps.qual=20=3D=0A-=09=09= ExecInitQual(node->scan.plan.qual,=20(PlanState=20*)=20scanstate);=0A+=09= if=20(estate->es_epq_active=20=3D=3D=20NULL)=0A+=09=09= scanstate->batch_state=20=3D=20init_batch_state(node,=20= &scanstate->ss.ps);=0A+=09if=20(scanstate->batch_state=20=3D=3D=20NULL)=0A= +=09=09scanstate->ss.ps.qual=20=3D=0A+=09=09=09= ExecInitQual(node->scan.plan.qual,=20(PlanState=20*)=20scanstate);=0A=20=0A= =20=09/*=0A=20=09=20*=20When=20EvalPlanQual()=20is=20not=20in=20use,=20= assign=20ExecProcNode=20for=20this=20node=0A-=09=20*=20based=20on=20the=20= presence=20of=20qual=20and=20projection.=20Each=20ExecSeqScan*()=0A-=09=20= *=20variant=20is=20optimized=20for=20the=20specific=20combination=20of=20= these=20conditions.=0A+=09=20*=20based=20on=20the=20presence=20of=20= batch=20state,=20qual=20and=20projection.=20Each=0A+=09=20*=20= ExecSeqScan*()=20variant=20is=20optimized=20for=20the=20specific=20= combination=20of=0A+=09=20*=20these=20conditions.=0A=20=09=20*/=0A=20=09= if=20(scanstate->ss.ps.state->es_epq_active=20!=3D=20NULL)=0A=20=09=09= scanstate->ss.ps.ExecProcNode=20=3D=20ExecSeqScanEPQ;=0A+=09else=20if=20= (scanstate->batch_state=20!=3D=20NULL)=0A+=09{=0A+=09=09if=20= (scanstate->ss.ps.qual=20=3D=3D=20NULL=20&&=0A+=09=09=09= scanstate->ss.ps.ps_ProjInfo=20=3D=3D=20NULL)=0A+=09=09=09= scanstate->ss.ps.ExecProcNode=20=3D=20ExecSeqScanWithBatchQual;=0A+=09=09= else=20if=20(scanstate->ss.ps.qual=20=3D=3D=20NULL)=0A+=09=09=09= scanstate->ss.ps.ExecProcNode=20=3D=20ExecSeqScanWithBatchQualProject;=0A= +=09=09else=20if=20(scanstate->ss.ps.ps_ProjInfo=20=3D=3D=20NULL)=0A+=09=09= =09scanstate->ss.ps.ExecProcNode=20=3D=20ExecSeqScanWithBatchQualRest;=0A= +=09=09else=0A+=09=09=09scanstate->ss.ps.ExecProcNode=20=3D=20= ExecSeqScanWithBatchQualRestProject;=0A+=09}=0A=20=09else=20if=20= (scanstate->ss.ps.qual=20=3D=3D=20NULL)=0A=20=09{=0A=20=09=09if=20= (scanstate->ss.ps.ps_ProjInfo=20=3D=3D=20NULL)=0A@@=20-348,6=20+891,9=20= @@=20ExecReScanSeqScan(SeqScanState=20*node)=0A=20{=0A=20=09= TableScanDesc=20scan;=0A=20=0A+=09if=20(node->batch_state=20!=3D=20NULL)=0A= +=09=09reset_batch_qual(node->batch_state);=0A+=0A=20=09scan=20=3D=20= node->ss.ss_currentScanDesc;=0A=20=0A=20=09if=20(scan=20!=3D=20NULL)=0A= diff=20--git=20a/src/include/nodes/execnodes.h=20= b/src/include/nodes/execnodes.h=0Aindex=20e64fd8c7ea3..4c9b2e8986e=20= 100644=0A---=20a/src/include/nodes/execnodes.h=0A+++=20= b/src/include/nodes/execnodes.h=0A@@=20-1674,6=20+1674,7=20@@=20typedef=20= struct=20SeqScanState=0A=20=09ScanState=09ss;=09=09=09=09/*=20its=20= first=20field=20is=20NodeTag=20*/=0A=20=09Size=09=09pscan_len;=09=09/*=20= size=20of=20parallel=20heap=20scan=20descriptor=20*/=0A=20=09struct=20= SharedSeqScanInstrumentation=20*sinstrument;=0A+=09struct=20= SeqScanBatchState=20*batch_state;=0A=20}=20SeqScanState;=0A=20=0A=20/*=20= ----------------=0Adiff=20--git=20= a/src/test/regress/expected/plancache.out=20= b/src/test/regress/expected/plancache.out=0Aindex=20= d58534ca1cd..c8d471a2c2f=20100644=0A---=20= a/src/test/regress/expected/plancache.out=0A+++=20= b/src/test/regress/expected/plancache.out=0A@@=20-402,3=20+402,132=20@@=20= select=20name,=20generic_plans,=20custom_plans=20from=20= pg_prepared_statements=0A=20(1=20row)=0A=20=0A=20drop=20table=20= test_mode;=0A+--=20Set=20up=20a=20table=20spanning=20multiple=20heap=20= pages=20for=20batch=20qualification=20tests.=0A+create=20table=20= batch_param_test=20(a=20int,=20t=20text,=20padding=20text)=0A+=20=20with=20= (autovacuum_enabled=20=3D=20false);=0A+insert=20into=20batch_param_test=0A= +=20=20select=20i,=20'value-'=20||=20i,=20repeat('x',=20500)=0A+=20=20= from=20generate_series(1,=20200)=20i;=0A+--=20Test=20multiple=20batch=20= qualifications.=0A+select=20count(*)=20from=20batch_param_test=0A+=20=20= where=20a=20>=2050=20and=20a=20<=20151=20and=20t=20<>=20'value-100'=20= \gset=20batch_multi_const_=0A+\echo=20:batch_multi_const_count=0A+99=0A= +--=20Test=20batch=20qualifications=20followed=20by=20regular=20= qualification.=0A+explain=20(analyze,=20costs=20off,=20timing=20off,=20= summary=20off,=20buffers=20off)=0A+select=20*=20from=20batch_param_test=20= where=20a=20>=2050=20and=20length(t)=20=3D=208;=0A+=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20QUERY=20PLAN=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=0A= +----------------------------------------------------------=0A+=20Seq=20= Scan=20on=20batch_param_test=20(actual=20rows=3D49.00=20loops=3D1)=0A+=20= =20=20Filter:=20((a=20>=2050)=20AND=20(length(t)=20=3D=208))=0A+=20=20=20= Rows=20Removed=20by=20Filter:=20151=0A+(3=20rows)=0A+=0A+--=20Test=20= projection=20with=20batch=20qualification=20in=20both=20scan=20= directions.=0A+begin;=0A+declare=20batch_multi_scroll=20scroll=20cursor=20= for=0A+=20=20select=20a=20+=201=20as=20projected,=20upper(t)=20as=20= projected_text=0A+=20=20from=20batch_param_test=0A+=20=20where=20a=20>=20= 50=20and=20a=20<=2055=20and=20length(t)=20=3D=208;=0A+fetch=20forward=20= 1=20from=20batch_multi_scroll=20\gset=20batch_scroll_first_=0A+fetch=20= forward=201=20from=20batch_multi_scroll=20\gset=20batch_scroll_second_=0A= +fetch=20backward=201=20from=20batch_multi_scroll=20\gset=20= batch_scroll_backward_=0A+fetch=20forward=201=20from=20= batch_multi_scroll=20\gset=20batch_scroll_again_=0A+\echo=20= :batch_scroll_first_projected=20:batch_scroll_first_projected_text=0A+52=20= VALUE-51=0A+\echo=20:batch_scroll_second_projected=20= :batch_scroll_second_projected_text=0A+53=20VALUE-52=0A+\echo=20= :batch_scroll_backward_projected=20:batch_scroll_backward_projected_text=0A= +52=20VALUE-51=0A+\echo=20:batch_scroll_again_projected=20= :batch_scroll_again_projected_text=0A+53=20VALUE-52=0A+close=20= batch_multi_scroll;=0A+commit;=0A+--=20Check=20the=20projection=20result=20= slot=20at=20end=20of=20scan.=0A+select=20a=20+=201=20as=20projected=0A= +from=20batch_param_test=0A+where=20a=20>=20200=20and=20length(t)=20=3D=20= 8;=0A+=20projected=20=0A+-----------=0A+(0=20rows)=0A+=0A+--=20Test=20= batch=20qualification=20with=20external=20parameters=20in=20generic=20= plans.=0A+set=20plan_cache_mode=20=3D=20force_generic_plan;=0A+prepare=20= batch_param_int_rev(int)=20as=0A+=20=20select=20count(*)=20from=20= batch_param_test=20where=20$1=20>=20a;=0A+prepare=20= batch_param_multi(int,=20int,=20text)=20as=0A+=20=20select=20count(*)=20= from=20batch_param_test=0A+=20=20where=20a=20>=20$1=20and=20a=20<=20$2=20= and=20t=20<>=20$3;=0A+prepare=20batch_param_rest(int,=20text)=20as=0A+=20= =20select=20count(*)=20from=20batch_param_test=0A+=20=20where=20a=20>=20= $1=20and=20t=20<>=20$2=20and=20length(t)=20=3D=208;=0A+explain=20(costs=20= off)=20execute=20batch_param_rest(50,=20'value-75');=0A+=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20QUERY=20PLAN=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =0A+--------------------------------------------------------------=0A+=20= Aggregate=0A+=20=20=20->=20=20Seq=20Scan=20on=20batch_param_test=0A+=20=20= =20=20=20=20=20=20=20Filter:=20((a=20>=20$1)=20AND=20(t=20<>=20$2)=20AND=20= (length(t)=20=3D=208))=0A+(3=20rows)=0A+=0A+execute=20= batch_param_int_rev(101)=20\gset=20batch_int_rev_=0A+execute=20= batch_param_multi(50,=20151,=20'value-100')=20\gset=20batch_multi_first_=0A= +execute=20batch_param_multi(100,=20201,=20'value-150')=20\gset=20= batch_multi_second_=0A+execute=20batch_param_multi(null,=20151,=20= 'value-100')=20\gset=20batch_multi_null_first_=0A+execute=20= batch_param_multi(50,=20151,=20null)=20\gset=20batch_multi_null_last_=0A= +\echo=20:batch_int_rev_count=20:batch_multi_first_count=20= :batch_multi_second_count=20:batch_multi_null_first_count=20= :batch_multi_null_last_count=0A+100=2099=2099=200=200=0A+execute=20= batch_param_rest(50,=20'value-75')=20\gset=20batch_rest_first_=0A= +execute=20batch_param_rest(90,=20'value-95')=20\gset=20= batch_rest_second_=0A+\echo=20:batch_rest_first_count=20= :batch_rest_second_count=0A+48=208=0A+--=20Test=20PL/pgSQL=20parameter=20= compilation=20with=20batch=20qualification.=0A+create=20function=20= batch_param_plpgsql(p=20int,=20q=20text)=0A+returns=20table=20= (int_result=20bigint,=20text_result=20bigint)=0A+language=20plpgsql=20as=20= $$=0A+begin=0A+=20=20return=20query=0A+=20=20=20=20select=20(select=20= count(*)=20from=20batch_param_test=20b=20where=20b.a=20<=20p),=0A+=20=20=20= =20=20=20=20=20=20=20=20(select=20count(*)=20from=20batch_param_test=20b=20= where=20b.t=20=3D=20q);=0A+end=0A+$$;=0A+select=20*=20from=20= batch_param_plpgsql(1,=20'value-100')=20\gset=20batch_plpgsql_first_=0A= +select=20*=20from=20batch_param_plpgsql(101,=20'missing')=20\gset=20= batch_plpgsql_second_=0A+select=20*=20from=20batch_param_plpgsql(null,=20= null)=20\gset=20batch_plpgsql_null_=0A+\echo=20= :batch_plpgsql_first_int_result=20:batch_plpgsql_first_text_result=20= :batch_plpgsql_second_int_result=20:batch_plpgsql_second_text_result=20= :batch_plpgsql_null_int_result=20:batch_plpgsql_null_text_result=0A+0=20= 1=20100=200=200=200=0A+drop=20function=20batch_param_plpgsql(int,=20= text);=0A+--=20Test=20batch=20qualification=20with=20internal=20executor=20= parameters.=0A+create=20table=20batch_param_exec_test=20(id=20int,=20p=20= int,=20q=20text);=0A+insert=20into=20batch_param_exec_test=20values=0A+=20= =20(1,=201,=20'value-2'),=0A+=20=20(2,=20200,=20'missing'),=0A+=20=20(3,=20= null,=20null),=0A+=20=20(4,=20199,=20'value-200');=0A+--=20OFFSET=200=20= prevents=20conversion=20of=20EXISTS,=20keeping=20the=20correlated=20= condition=0A+--=20as=20a=20scan=20qualification.=0A+explain=20(costs=20= off)=0A+select=20exists=20(select=20from=20batch_param_test=20b=0A+=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20where=20o.p=20<=20b.a=20and=20b.t=20= <>=20o.q=20and=20length(b.t)=20=3D=208=0A+=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20offset=200)=0A+from=20batch_param_exec_test=20o;=0A+=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= QUERY=20PLAN=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=0A= +------------------------------------------------------------------=0A+=20= Seq=20Scan=20on=20batch_param_exec_test=20o=0A+=20=20=20SubPlan=20= exists_1=0A+=20=20=20=20=20->=20=20Seq=20Scan=20on=20batch_param_test=20= b=0A+=20=20=20=20=20=20=20=20=20=20=20Filter:=20((o.p=20<=20a)=20AND=20= (t=20<>=20o.q)=20AND=20(length(t)=20=3D=208))=0A+(4=20rows)=0A+=0A= +select=20array_agg(exists=20(select=20from=20batch_param_test=20b=0A+=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= where=20o.p=20<=20b.a=20and=20b.t=20<>=20o.q=20and=20length(b.t)=20=3D=20= 8=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20offset=200)=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= order=20by=20o.id)=20as=20combined_results=0A+from=20= batch_param_exec_test=20o;=0A+=20combined_results=20=0A= +------------------=0A+=20{t,f,f,f}=0A+(1=20row)=0A+=0A+drop=20table=20= batch_param_exec_test;=0A+reset=20plan_cache_mode;=0A+drop=20table=20= batch_param_test;=0Adiff=20--git=20a/src/test/regress/sql/plancache.sql=20= b/src/test/regress/sql/plancache.sql=0Aindex=20aed388d03a1..ecb35177ce9=20= 100644=0A---=20a/src/test/regress/sql/plancache.sql=0A+++=20= b/src/test/regress/sql/plancache.sql=0A@@=20-228,3=20+228,107=20@@=20= select=20name,=20generic_plans,=20custom_plans=20from=20= pg_prepared_statements=0A=20=20=20where=20=20name=20=3D=20= 'test_mode_pp';=0A=20=0A=20drop=20table=20test_mode;=0A+=0A+--=20Set=20= up=20a=20table=20spanning=20multiple=20heap=20pages=20for=20batch=20= qualification=20tests.=0A+create=20table=20batch_param_test=20(a=20int,=20= t=20text,=20padding=20text)=0A+=20=20with=20(autovacuum_enabled=20=3D=20= false);=0A+insert=20into=20batch_param_test=0A+=20=20select=20i,=20= 'value-'=20||=20i,=20repeat('x',=20500)=0A+=20=20from=20= generate_series(1,=20200)=20i;=0A+=0A+--=20Test=20multiple=20batch=20= qualifications.=0A+select=20count(*)=20from=20batch_param_test=0A+=20=20= where=20a=20>=2050=20and=20a=20<=20151=20and=20t=20<>=20'value-100'=20= \gset=20batch_multi_const_=0A+\echo=20:batch_multi_const_count=0A+=0A+--=20= Test=20batch=20qualifications=20followed=20by=20regular=20qualification.=0A= +explain=20(analyze,=20costs=20off,=20timing=20off,=20summary=20off,=20= buffers=20off)=0A+select=20*=20from=20batch_param_test=20where=20a=20>=20= 50=20and=20length(t)=20=3D=208;=0A+=0A+--=20Test=20projection=20with=20= batch=20qualification=20in=20both=20scan=20directions.=0A+begin;=0A= +declare=20batch_multi_scroll=20scroll=20cursor=20for=0A+=20=20select=20= a=20+=201=20as=20projected,=20upper(t)=20as=20projected_text=0A+=20=20= from=20batch_param_test=0A+=20=20where=20a=20>=2050=20and=20a=20<=2055=20= and=20length(t)=20=3D=208;=0A+fetch=20forward=201=20from=20= batch_multi_scroll=20\gset=20batch_scroll_first_=0A+fetch=20forward=201=20= from=20batch_multi_scroll=20\gset=20batch_scroll_second_=0A+fetch=20= backward=201=20from=20batch_multi_scroll=20\gset=20= batch_scroll_backward_=0A+fetch=20forward=201=20from=20= batch_multi_scroll=20\gset=20batch_scroll_again_=0A+\echo=20= :batch_scroll_first_projected=20:batch_scroll_first_projected_text=0A= +\echo=20:batch_scroll_second_projected=20= :batch_scroll_second_projected_text=0A+\echo=20= :batch_scroll_backward_projected=20:batch_scroll_backward_projected_text=0A= +\echo=20:batch_scroll_again_projected=20= :batch_scroll_again_projected_text=0A+close=20batch_multi_scroll;=0A= +commit;=0A+=0A+--=20Check=20the=20projection=20result=20slot=20at=20end=20= of=20scan.=0A+select=20a=20+=201=20as=20projected=0A+from=20= batch_param_test=0A+where=20a=20>=20200=20and=20length(t)=20=3D=208;=0A+=0A= +--=20Test=20batch=20qualification=20with=20external=20parameters=20in=20= generic=20plans.=0A+set=20plan_cache_mode=20=3D=20force_generic_plan;=0A= +prepare=20batch_param_int_rev(int)=20as=0A+=20=20select=20count(*)=20= from=20batch_param_test=20where=20$1=20>=20a;=0A+prepare=20= batch_param_multi(int,=20int,=20text)=20as=0A+=20=20select=20count(*)=20= from=20batch_param_test=0A+=20=20where=20a=20>=20$1=20and=20a=20<=20$2=20= and=20t=20<>=20$3;=0A+prepare=20batch_param_rest(int,=20text)=20as=0A+=20= =20select=20count(*)=20from=20batch_param_test=0A+=20=20where=20a=20>=20= $1=20and=20t=20<>=20$2=20and=20length(t)=20=3D=208;=0A+=0A+explain=20= (costs=20off)=20execute=20batch_param_rest(50,=20'value-75');=0A+execute=20= batch_param_int_rev(101)=20\gset=20batch_int_rev_=0A+execute=20= batch_param_multi(50,=20151,=20'value-100')=20\gset=20batch_multi_first_=0A= +execute=20batch_param_multi(100,=20201,=20'value-150')=20\gset=20= batch_multi_second_=0A+execute=20batch_param_multi(null,=20151,=20= 'value-100')=20\gset=20batch_multi_null_first_=0A+execute=20= batch_param_multi(50,=20151,=20null)=20\gset=20batch_multi_null_last_=0A= +\echo=20:batch_int_rev_count=20:batch_multi_first_count=20= :batch_multi_second_count=20:batch_multi_null_first_count=20= :batch_multi_null_last_count=0A+execute=20batch_param_rest(50,=20= 'value-75')=20\gset=20batch_rest_first_=0A+execute=20= batch_param_rest(90,=20'value-95')=20\gset=20batch_rest_second_=0A+\echo=20= :batch_rest_first_count=20:batch_rest_second_count=0A+=0A+--=20Test=20= PL/pgSQL=20parameter=20compilation=20with=20batch=20qualification.=0A= +create=20function=20batch_param_plpgsql(p=20int,=20q=20text)=0A+returns=20= table=20(int_result=20bigint,=20text_result=20bigint)=0A+language=20= plpgsql=20as=20$$=0A+begin=0A+=20=20return=20query=0A+=20=20=20=20select=20= (select=20count(*)=20from=20batch_param_test=20b=20where=20b.a=20<=20p),=0A= +=20=20=20=20=20=20=20=20=20=20=20(select=20count(*)=20from=20= batch_param_test=20b=20where=20b.t=20=3D=20q);=0A+end=0A+$$;=0A+=0A= +select=20*=20from=20batch_param_plpgsql(1,=20'value-100')=20\gset=20= batch_plpgsql_first_=0A+select=20*=20from=20batch_param_plpgsql(101,=20= 'missing')=20\gset=20batch_plpgsql_second_=0A+select=20*=20from=20= batch_param_plpgsql(null,=20null)=20\gset=20batch_plpgsql_null_=0A+\echo=20= :batch_plpgsql_first_int_result=20:batch_plpgsql_first_text_result=20= :batch_plpgsql_second_int_result=20:batch_plpgsql_second_text_result=20= :batch_plpgsql_null_int_result=20:batch_plpgsql_null_text_result=0A+=0A= +drop=20function=20batch_param_plpgsql(int,=20text);=0A+=0A+--=20Test=20= batch=20qualification=20with=20internal=20executor=20parameters.=0A= +create=20table=20batch_param_exec_test=20(id=20int,=20p=20int,=20q=20= text);=0A+insert=20into=20batch_param_exec_test=20values=0A+=20=20(1,=20= 1,=20'value-2'),=0A+=20=20(2,=20200,=20'missing'),=0A+=20=20(3,=20null,=20= null),=0A+=20=20(4,=20199,=20'value-200');=0A+=0A+--=20OFFSET=200=20= prevents=20conversion=20of=20EXISTS,=20keeping=20the=20correlated=20= condition=0A+--=20as=20a=20scan=20qualification.=0A+explain=20(costs=20= off)=0A+select=20exists=20(select=20from=20batch_param_test=20b=0A+=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20where=20o.p=20<=20b.a=20and=20b.t=20= <>=20o.q=20and=20length(b.t)=20=3D=208=0A+=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20offset=200)=0A+from=20batch_param_exec_test=20o;=0A+=0A= +select=20array_agg(exists=20(select=20from=20batch_param_test=20b=0A+=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= where=20o.p=20<=20b.a=20and=20b.t=20<>=20o.q=20and=20length(b.t)=20=3D=20= 8=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20offset=200)=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= order=20by=20o.id)=20as=20combined_results=0A+from=20= batch_param_exec_test=20o;=0A+=0A+drop=20table=20batch_param_exec_test;=0A= +reset=20plan_cache_mode;=0A+drop=20table=20batch_param_test;=0A--=20=0A= 2.54.0=0A=0A= --Apple-Mail=_8357CA5E-6C9C-459D-B514-FCCFCC7B5160--