duckdb / duckdb/pg_duckdb

DuckdbExplainOneQueryHook is not called for "explain execute" queries

Open
#751 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
3.2k
Forks
204
PR merge metrics
No merged PRs in 30d

Description

### What happens?

With `duckdb.force_execution` set to true, this sequence of queries crashes server:
```sql
CREATE TABLE INT8_TBL(q1 int8, q2 int8);
explain (memory, analyze, format json) select * from int8_tbl i8;
prepare int8_query as select * from int8_tbl i8;
explain (memory) execute int8_query;
```

This happens because first query sets `duckdb_explain_format` to `duckdb::ExplainFormat::JSON` in DuckdbExplainOneQueryHook, meanwhile `EXPLAIN EXECUTE` queries are considered Utility statements, so `ExplainOneQuery_hook` is not called and `duckdb_explain_format` is still set to `duckdb::ExplainFormat::JSON`, which it should not be.

For instance, this sequence of queries works just well:
```sql
explain (memory, analyze, format json) select * from int8_tbl i8;
prepare int8_query as select * from int8_tbl i8;
explain (memory, format json) execute int8_query;
```

I suggest moving code that sets value of `duckdb_explain_format` from DuckdbExplainOneQueryHook to Duckdb_ExplainCustomScan_Cpp, it seems its the only place where that value is actually needed.
With that change, server no longer crashes.

### To Reproduce

1. Create fresh DB with pg_duckdb and duckdb.force_execution set to true
2. Run this sequence of queries:
```sql
CREATE TABLE INT8_TBL(q1 int8, q2 int8);
explain (memory, analyze, format json) select * from int8_tbl i8;
prepare int8_query as select * from int8_tbl i8;
explain (memory) execute int8_query;
```
3. Get a server crash

### OS:

Linux

### pg_duckdb Version (if built from source use commit hash):

ea75539aa6bf4520fce537cffbe75fb12e4bcc65

### Postgres Version (if built from source use commit hash):

17

### Hardware:

_No response_

### Full Name:

Jora Babayan

### Affiliation:

Postgres Professional

### What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.

I have tested with a source build

### Did you include all relevant data sets for reproducing the issue?

Not applicable - the reproduction does not require a data set

### Did you include all code required to reproduce the issue?

- [x] Yes, I have

### Did you include all relevant configuration (e.g., CPU architecture, Linux distribution) to reproduce the issue?

- [x] Yes, I have

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.