Reduce reaper memory usage
@@ -228,6 +228,10 @@ void
ContinuousQueryReaperMain(void)
{
HASHCTL hctl;
+ MemoryContext cxt = AllocSetContextCreate(TopMemoryContext, "Reaper Context",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
MemSet(&hctl, 0, sizeof(hctl));
hctl.hcxt = CurrentMemoryContext;
@@ -254,21 +258,21 @@ ContinuousQueryReaperMain(void)
{
int deleted = 0;
bool error = false;
-
- StartTransactionCommand();
- SetCurrentStatementStartTimestamp();
+ MemoryContext old;
+ Relation rel;
PG_TRY();
{
- /*
- * We need to acquire a pipeline_query lock first to ensure proper lock ordering
- * of all remaining lock acquisitions to avoid deadlocks.
- */
- Relation rel = heap_open(PipelineQueryRelationOid, RowExclusiveLock);
+ StartTransactionCommand();
+ MemoryContextReset(cxt);
+ old = MemoryContextSwitchTo(cxt);
+ rel = heap_open(PipelineQueryRelationOid, RowExclusiveLock);
ttl_rels = get_ttl_rels(&min_sleep);
-
heap_close(rel, NoLock);
+ MemoryContextSwitchTo(old);
+
+ CommitTransactionCommand();
foreach(lc, ttl_rels)
{
@@ -277,6 +281,11 @@ ContinuousQueryReaperMain(void)
RangeVar *matrel;
Oid relid;
+ CHECK_FOR_INTERRUPTS();
+
+ if (get_sigterm_flag())
+ break;
+
Assert(list_length(rels) == 3);
relid = intVal(linitial(rels));
@@ -288,9 +297,22 @@ ContinuousQueryReaperMain(void)
*/
if (should_expire(relid))
{
+ StartTransactionCommand();
+ SetCurrentStatementStartTimestamp();
+
+ /*
+ * We need to acquire a pipeline_query lock first to ensure proper lock ordering
+ * of all remaining lock acquisitions to avoid deadlocks.
+ */
+ rel = heap_open(PipelineQueryRelationOid, RowExclusiveLock);
+
deleted = DeleteTTLExpiredRows(cv, matrel);
total_deleted += deleted;
set_last_expiration(relid, deleted);
+
+ heap_close(rel, NoLock);
+
+ CommitTransactionCommand();
}
}
}
@@ -305,12 +327,9 @@ ContinuousQueryReaperMain(void)
PopActiveSnapshot();
AbortCurrentTransaction();
- StartTransactionCommand();
}
PG_END_TRY();
- CommitTransactionCommand();
-
/*
* If nothing was deleted on this run, we're done for now
*/
