Skip to content

Commit

Permalink
Fix dsm segments leak in parallel scan code
Browse files Browse the repository at this point in the history
  • Loading branch information
pashkinelfe committed Mar 4, 2024
1 parent 0c1a98d commit ed7d195
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/tableam/handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ typedef struct ParallelOScanDescData
pg_atomic_uint64 downlinkIndex;
int workersReportedCount; /* number of workers that reported
* disk downlinks number */
int dsmSegAttached; /* number of workers currenly attached to dsm segment during disk scan stage */
bits8 flags;
int nworkers;
dsm_handle dsmHandle;
Expand Down
11 changes: 11 additions & 0 deletions src/btree/scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ switch_to_disk_scan(BTreeSeqScan *scan)
LWLockAcquire(&poscan->downlinksPublish, LW_EXCLUSIVE);
Assert(!poscan->dsmHandle);
scan->dsmSeg = dsm_create(MAXALIGN(poscan->downlinksCount * sizeof(scan->diskDownlinks[0])), 0);
poscan->dsmSegAttached = 0;
poscan->dsmHandle = dsm_segment_handle(scan->dsmSeg);
memcpy((Pointer) dsm_segment_address(scan->dsmSeg), scan->diskDownlinks,
scan->downlinksCount * sizeof(scan->diskDownlinks[0]));
Expand All @@ -448,6 +449,7 @@ switch_to_disk_scan(BTreeSeqScan *scan)
LWLockAcquire(&poscan->downlinksPublish, LW_EXCLUSIVE);
LWLockRelease(&poscan->downlinksPublish);

Assert(poscan->dsmSegAttached == 0);
qsort(dsm_segment_address(scan->dsmSeg), poscan->downlinksCount,
sizeof(scan->diskDownlinks[0]), cmp_downlinks);
}
Expand All @@ -466,13 +468,16 @@ switch_to_disk_scan(BTreeSeqScan *scan)
{
Assert(poscan->dsmHandle && !scan->dsmSeg);
scan->dsmSeg = dsm_attach(poscan->dsmHandle);
poscan->dsmSegAttached++;
}
if (scan->downlinksCount > 0)
{
index = pg_atomic_fetch_add_u64(&poscan->downlinkIndex, scan->downlinksCount);
memcpy((Pointer) dsm_segment_address(scan->dsmSeg) + index * sizeof(scan->diskDownlinks[0]),
scan->diskDownlinks, scan->downlinksCount * sizeof(scan->diskDownlinks[0]));
index += scan->downlinksCount;
dsm_detach(scan->dsmSeg);
poscan->dsmSegAttached--;
}
LWLockRelease(&poscan->downlinksPublish);

Expand Down Expand Up @@ -1620,7 +1625,10 @@ free_btree_seq_scan(BTreeSeqScan *scan)
END_CRIT_SECTION();

if (scan->dsmSeg)
{
Assert(scan->poscan->dsmSegAttached == 0); /* All workers should have already detached */
dsm_detach(scan->dsmSeg);
}
pfree(scan->diskDownlinks);
pfree(scan);
}
Expand Down Expand Up @@ -1648,7 +1656,10 @@ seq_scans_cleanup(void)
}
dlist_delete(&scan->listNode);
if (scan->dsmSeg)
{
Assert(scan->poscan->dsmSegAttached == 0);
dsm_detach(scan->dsmSeg);
}
pfree(scan);
}
dlist_init(&listOfScans);
Expand Down
1 change: 1 addition & 0 deletions src/orioledb.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ _PG_init(void)
int i;
int min_pool_size;


if (!process_shared_preload_libraries_in_progress)
return;

Expand Down

0 comments on commit ed7d195

Please sign in to comment.