Skip to content

Commit 022350b

Browse files
committed
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
1 parent 84c0e4b commit 022350b

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/backend/catalog/pg_shdepend.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -809,15 +809,19 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId)
809809
int slotCount;
810810
CatalogIndexState indstate;
811811
TupleTableSlot **slot;
812-
int nslots;
812+
int nslots,
813+
max_slots;
814+
bool slot_init = true;
813815

814816
sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
815817
sdepDesc = RelationGetDescr(sdepRel);
816818

817-
nslots = MAX_PGSHDEPEND_INSERT_BYTES / sizeof(FormData_pg_shdepend);
818-
slot = palloc(sizeof(TupleTableSlot *) * nslots);
819-
for (int i = 0; i < nslots; i++)
820-
slot[i] = MakeSingleTupleTableSlot(sdepDesc, &TTSOpsHeapTuple);
819+
/*
820+
* Allocate the slots to use, but delay initialization until we know that
821+
* they will be used.
822+
*/
823+
max_slots = MAX_PGSHDEPEND_INSERT_BYTES / sizeof(FormData_pg_shdepend);
824+
slot = palloc(sizeof(TupleTableSlot *) * max_slots);
821825

822826
indstate = CatalogOpenIndexes(sdepRel);
823827

@@ -842,6 +846,9 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId)
842846
{
843847
Form_pg_shdepend shdep;
844848

849+
if (slot_init)
850+
slot[slotCount] = MakeSingleTupleTableSlot(sdepDesc, &TTSOpsHeapTuple);
851+
845852
ExecClearTuple(slot[slotCount]);
846853

847854
shdep = (Form_pg_shdepend) GETSTRUCT(tup);
@@ -858,10 +865,11 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId)
858865
slotCount++;
859866

860867
/* If slots are full, insert a batch of tuples */
861-
if (slotCount == nslots)
868+
if (slotCount == max_slots)
862869
{
863870
CatalogTuplesMultiInsertWithInfo(sdepRel, slot, slotCount, indstate);
864871
slotCount = 0;
872+
slot_init = false;
865873
}
866874
}
867875

@@ -874,6 +882,8 @@ copyTemplateDependencies(Oid templateDbId, Oid newDbId)
874882
CatalogCloseIndexes(indstate);
875883
table_close(sdepRel, RowExclusiveLock);
876884

885+
/* Drop only the number of slots used */
886+
nslots = slot_init ? slotCount : max_slots;
877887
for (int i = 0; i < nslots; i++)
878888
ExecDropSingleTupleTableSlot(slot[i]);
879889
pfree(slot);

0 commit comments

Comments
 (0)