#19165·duckdb

COPY statement with `plan` function results in infinite loop if function is not changed by `plan` function.

Author: rustyconoverCreated Sep 26, 2025Updated Sep 30, 2026
Labelsunder reviewstale

What happens?

Hello DuckDB Team,

I'm implementing a custom COPY function, I am inspired by Max's BLOB copy function.

What's special about my CopyFunction is that it supplies a plan function but doesn't change the function that is called, it just changes the arguments to the function.

The problem is that when the plan function but does not change the function being called to one that does not have a plan function DuckDB enters an infinite loop.

When the plan function is called, that method needs to return a BoundStatement but to get a bound statement I need to use binder.Bind. But that call to the binder re-invokes the plan function and the loop never terminates.

Here is the call that is causing the loop:

cpp
BoundStatement Binder::BindCopyTo(CopyStatement &stmt, const CopyFunction &function, CopyToType copy_to_type) {
  if (function.plan) {
    // plan rewrite COPY TO
    return function.plan(*this, stmt);
  }

I'm not yet super familiar with the binder/planning interaction yet. I've worked around this problem is adding a new flag to the CopyStatement structure that indicates if that CopyStatement has already been planned. I am sure there may be a more elegant change and please excuse my naive fix here.

Could you please take a look at this and propose a better solution?

The registration of my copy function looks like this:

cpp
CopyFunction info("record_layout");

info.extension = "record_layout";
info.plan = CopyToRecordLayoutPlan;
...
loader.RegisterFunction(info);

Thanks for your help,

Rusty

To Reproduce

See issue above.

OS:

macOS

DuckDB Version:

1.4.0

DuckDB Client:

CLI

Hardware:

No response

Full Name:

Rusty Conover

Affiliation:

query.farm

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

I have tested with a stable release

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

No - Other reason (please specify in the issue body)

Did you include all code required to reproduce the issue?

  • Yes, I have

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

  • Yes, I have