Proposal: Adding Parallel Query Support to ActiveRecord

I propose adding parallel query support to ActiveRecord for PostgreSQL, allowing for faster data retrieval by leveraging multiple processors. This feature aims to significantly improve performance for large and complex queries, making Rails applications more efficient and responsive.


In large applications with significant data volume, complex queries can become a bottleneck. These queries can take a substantial amount of time to execute, leading to slower response times and a suboptimal user experience. Traditional single-threaded query execution does not fully utilize modern multi-core processors, leaving performance improvements on the table.


By enabling parallel query execution in PostgreSQL, we can distribute the workload across multiple processors. This approach can drastically reduce query execution times for suitable queries. The proposed feature introduces a new parallel_query method in ActiveRecord, which sets the necessary PostgreSQL parameters to enable parallel execution.


The parallel_query method in ActiveRecord allows developers to specify the number of worker processes to be used for parallel execution. This method temporarily adjusts PostgreSQL settings to optimize parallel query performance and resets them after execution.

Example Usage: Before:

result = Product.where("price > ?", 100).to_a


result = Product.parallel_query do
  Product.where("price > ?", 100).to_a

Performance Improvements

Benchmark tests have shown a significant reduction in query execution time when using parallel query execution. For large datasets, performance improvements of up to more than 50% have been observed. These improvements can lead to faster page loads and a more responsive application, enhancing the overall user experience.

verified the performance for parllel vs without parallel.

checkout my PR for this: