Skip to content

Commit

Permalink
Always enter parallel mode when scanning a table in parallel (#604)
Browse files Browse the repository at this point in the history
Previously we were only calling `EnterParallelMode()` if we were not
already in parallel mode. And then when we were done with that the scan
for which we entered parallel mode, we would call `ExitParallelMode()`.
This had the problem that if we were doing two scans, we could call
ExitParallelMode() before both scans were done:

1. Scan A starts: EnterParallelMode
2. Scan B starts: do nothing
3. Scan A exists: ExitParallelMode
4. Scan B is now running in parallel even though we exited parallel
mode.

This was causing an assertion in the Postgres code to fail.

This change fixes that by always calling `EnterParallelMode()` when we
do a parallel scan and always calling `ExitParallelMode()` when we are
finished with a scan. This works because Postgres internally keeps a
counter of the amount of times we entered parallel mode. So now in the
above example we would do:

1. Scan A starts: EnterParallelMode(), counter 1, enter parallel mode
1. Scan B starts: EnterParallelMode(), counter 2, do nothing
3. Scan A exists: ExitParallelMode(), counter 1, do nothing
3. Scan B exists: ExitParallelMode(), counter 0, exit parallel mode


Fixes #592
  • Loading branch information
JelteF authored Feb 14, 2025
1 parent a6e4e7a commit a344d04
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/scan/postgres_table_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ PostgresTableReader::PostgresTableReader(const char *table_scan_query, bool coun
RESUME_CANCEL_INTERRUPTS();
}

if (!IsInParallelMode()) {
EnterParallelMode();
entered_parallel_mode = true;
}
EnterParallelMode();
entered_parallel_mode = true;

ParallelContext *pcxt;
parallel_executor_info = PostgresFunctionGuard(ExecInitParallelPlan, table_scan_planstate,
Expand Down

0 comments on commit a344d04

Please sign in to comment.