Re: Batch insert in CTAS/MatView code

From: Andres Freund <andres(at)anarazel(dot)de>
To: Paul Guo <pguo(at)pivotal(dot)io>
Cc: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>, Taylor Vesely <tvesely(at)pivotal(dot)io>
Subject: Re: Batch insert in CTAS/MatView code
Date: 2019-09-27 21:49:51
Message-ID: 20190927214951.b6zwrxvodmqtonk4@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On 2019-09-09 18:31:54 +0800, Paul Guo wrote:
> diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
> index e9544822bf..8a844b3b5f 100644
> --- a/src/backend/access/heap/heapam.c
> +++ b/src/backend/access/heap/heapam.c
> @@ -2106,7 +2106,6 @@ heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples,
> CommandId cid, int options, BulkInsertState bistate)
> {
> TransactionId xid = GetCurrentTransactionId();
> - HeapTuple *heaptuples;
> int i;
> int ndone;
> PGAlignedBlock scratch;
> @@ -2115,6 +2114,10 @@ heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples,
> Size saveFreeSpace;
> bool need_tuple_data = RelationIsLogicallyLogged(relation);
> bool need_cids = RelationIsAccessibleInLogicalDecoding(relation);
> + /* Declare it as static to let this memory be not on stack. */
> + static HeapTuple heaptuples[MAX_MULTI_INSERT_TUPLES];
> +
> + Assert(ntuples <= MAX_MULTI_INSERT_TUPLES);
>
> /* currently not needed (thus unsupported) for heap_multi_insert() */
> AssertArg(!(options & HEAP_INSERT_NO_LOGICAL));
> @@ -2124,7 +2127,6 @@ heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples,
> HEAP_DEFAULT_FILLFACTOR);
>
> /* Toast and set header data in all the slots */
> - heaptuples = palloc(ntuples * sizeof(HeapTuple));
> for (i = 0; i < ntuples; i++)
> {
> HeapTuple tuple;

I don't think this is a good idea. We shouldn't unnecessarily allocate
8KB on the stack. Is there any actual evidence this is a performance
benefit? To me this just seems like it'll reduce the flexibility of the
API, without any benefit. I'll also note that you've apparently not
updated tableam.h to document this new restriction.

Greetings,

Andres Freund

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2019-09-27 21:52:17 Re: ECPG installcheck tests fail if PGDATABASE is set
Previous Message Andres Freund 2019-09-27 21:46:38 Re: Batch insert in CTAS/MatView code