Hi there, hackers! How about trying out an idea to add an analog to save
memory in WAL files for deleting records, similar to multi-insert
optimization? This patch is trying to do just that.
Best Regards, Stepan Neretin!
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c
index 1161520f76..fb2d4563fd 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -3816,13 +3816,14 @@ ExecModifyTable(PlanState *pstate)
CmdType operation = node->operation;
ResultRelInfo *resultRelInfo;
PlanState *subplanstate;
- TupleTableSlot *slot;
+ TupleTableSlot *slot = NULL;
TupleTableSlot *oldSlot;
ItemPointerData tuple_ctid;
HeapTupleData oldtupdata;
HeapTuple oldtuple;
ItemPointer tupleid;
bool tuplock;
+ List *items_to_delete = NULL;
CHECK_FOR_INTERRUPTS();
@@ -4179,8 +4180,15 @@ ExecModifyTable(PlanState *pstate)
break;
case CMD_DELETE:
- slot = ExecDelete(&context, resultRelInfo, tupleid, oldtuple,
- true, false, node->canSetTag, NULL, NULL, NULL);
+ ItemPointer item_ptr = (ItemPointer) palloc0(sizeof(ItemPointerData));
+ item_ptr->ip_blkid = tupleid->ip_blkid;
+ item_ptr->ip_posid = tupleid->ip_posid;
+
+ if (!items_to_delete)
+ items_to_delete = list_make1(item_ptr);
+ else
+ items_to_delete = lappend(items_to_delete, item_ptr);
+
break;
case CMD_MERGE:
@@ -4197,10 +4205,28 @@ ExecModifyTable(PlanState *pstate)
* If we got a RETURNING result, return it to caller. We'll continue
* the work on next call.
*/
- if (slot)
+ if (slot && !(operation == CMD_DELETE))
return slot;
}
+ if (list_length(items_to_delete) > 0)
+ {
+ ListCell *cell;
+ ereport(WARNING, errmsg("NUM ITEMS TO DELETE = %d", list_length(items_to_delete)));
+ foreach(cell, items_to_delete)
+ {
+ ItemPointer item_ptr = (ItemPointer) lfirst(cell);
+ if (!slot)
+ slot = ExecDelete(&context, resultRelInfo, item_ptr, NULL,
+ true, false, node->canSetTag, NULL, NULL, NULL);
+ else
+ ExecDelete(&context, resultRelInfo, item_ptr, NULL,
+ true, false, node->canSetTag, NULL, NULL, NULL);
+ }
+
+ return slot;
+ }
+
/*
* Insert remaining tuples for batch insert.
*/