diff --git a/src/storage/postgres_optimizer.cpp b/src/storage/postgres_optimizer.cpp index 393eb7e2..4d9b3aa5 100644 --- a/src/storage/postgres_optimizer.cpp +++ b/src/storage/postgres_optimizer.cpp @@ -54,6 +54,11 @@ static void OptimizePostgresScanLimitPushdown(unique_ptr &op) { } auto &bind_data = get.bind_data->Cast(); + if (bind_data.max_threads != 1 || !bind_data.can_use_main_thread) { + // cannot push down limit/offset if we are not using the main thread + OptimizePostgresScanLimitPushdown(op->children[0]); + return; + } string generated_limit_clause = ""; if (limit.limit_val.Type() != LimitNodeType::UNSET) { diff --git a/test/sql/storage/limit.test b/test/sql/storage/limit.test index 16113432..15ca5bff 100644 --- a/test/sql/storage/limit.test +++ b/test/sql/storage/limit.test @@ -39,3 +39,35 @@ query II EXPLAIN FROM s.large_tbl LIMIT 5; ---- logical_opt :.*LIMIT.* + + +statement ok +set pg_pages_per_task=1 + +query I +FROM s.large_tbl LIMIT 5 +---- +0 +1 +2 +3 +4 + +query I +FROM s.large_tbl LIMIT 5 OFFSET 5 +---- +5 +6 +7 +8 +9 + +statement ok +set explain_output='optimized_only' + +# limit is still in plan as we were not able to push down due to parallel execution + +query II +EXPLAIN FROM s.large_tbl LIMIT 5; +---- +logical_opt :.*LIMIT.*