Commit 022350b8 authored by Michael Paquier's avatar Michael Paquier

Minimize slot creation for multi-inserts of pg_shdepend

When doing multiple insertions in pg_shdepend for the copy of
dependencies from a template database in CREATE DATABASE, the same
number of slots would have been created and used all the time.  As the
number of items to insert is not known in advance, this makes most of
the slots created for nothing.  This improves the slot handling so as
slot creation only happens when needed, minimizing the overhead of the
operation.

Author: Michael Paquier
Reviewed-by: Daniel Gustafsson
Discussion: https://postgr.es/m/20200731024148.GB3317@paquier.xyz
parent 84c0e4b9
...@@ -809,15 +809,19 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId) ...@@ -809,15 +809,19 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId)
int slotCount; int slotCount;
CatalogIndexState indstate; CatalogIndexState indstate;
TupleTableSlot **slot; TupleTableSlot **slot;
int nslots; int nslots,
max_slots;
bool slot_init = true;
sdepRel = table_open(SharedDependRelationId, RowExclusiveLock); sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
sdepDesc = RelationGetDescr(sdepRel); sdepDesc = RelationGetDescr(sdepRel);
nslots = MAX_PGSHDEPEND_INSERT_BYTES / sizeof(FormData_pg_shdepend); /*
slot = palloc(sizeof(TupleTableSlot *) * nslots); * Allocate the slots to use, but delay initialization until we know that
for (int i = 0; i < nslots; i++) * they will be used.
slot[i] = MakeSingleTupleTableSlot(sdepDesc, &TTSOpsHeapTuple); */
max_slots = MAX_PGSHDEPEND_INSERT_BYTES / sizeof(FormData_pg_shdepend);
slot = palloc(sizeof(TupleTableSlot *) * max_slots);
indstate = CatalogOpenIndexes(sdepRel); indstate = CatalogOpenIndexes(sdepRel);
...@@ -842,6 +846,9 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId) ...@@ -842,6 +846,9 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId)
{ {
Form_pg_shdepend shdep; Form_pg_shdepend shdep;
if (slot_init)
slot[slotCount] = MakeSingleTupleTableSlot(sdepDesc, &TTSOpsHeapTuple);
ExecClearTuple(slot[slotCount]); ExecClearTuple(slot[slotCount]);
shdep = (Form_pg_shdepend) GETSTRUCT(tup); shdep = (Form_pg_shdepend) GETSTRUCT(tup);
...@@ -858,10 +865,11 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId) ...@@ -858,10 +865,11 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId)
slotCount++; slotCount++;
/* If slots are full, insert a batch of tuples */ /* If slots are full, insert a batch of tuples */
if (slotCount == nslots) if (slotCount == max_slots)
{ {
CatalogTuplesMultiInsertWithInfo(sdepRel, slot, slotCount, indstate); CatalogTuplesMultiInsertWithInfo(sdepRel, slot, slotCount, indstate);
slotCount = 0; slotCount = 0;
slot_init = false;
} }
} }
...@@ -874,6 +882,8 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId) ...@@ -874,6 +882,8 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId)
CatalogCloseIndexes(indstate); CatalogCloseIndexes(indstate);
table_close(sdepRel, RowExclusiveLock); table_close(sdepRel, RowExclusiveLock);
/* Drop only the number of slots used */
nslots = slot_init ? slotCount : max_slots;
for (int i = 0; i < nslots; i++) for (int i = 0; i < nslots; i++)
ExecDropSingleTupleTableSlot(slot[i]); ExecDropSingleTupleTableSlot(slot[i]);
pfree(slot); pfree(slot);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment