COPY: validate option presence rather than option values

Поиск
Список
Период
Сортировка
Искать

COPY: validate option presence rather than option values

От:
Chao Li <li.evan.chao@gmail.com>
Дата:
Вложения:
Hi,

While testing COPY TO (FORMAT json), I noticed that the doc says FORCE_ARRAY is only allowed with FORMAT json:
```
   
    FORCE_ARRAY
    
     
      Force output of square brackets as array decorations at the beginning
      and end of output, and commas between the rows. It is allowed only in
      COPY TO, and only when using
      json format. The default is
      false.
     
    
   
```

However, this succeeds:
```
evantest=# copy t1 to stdout (format csv, force_array false);
1
```

So this is clearly validating the parsed value of force_array, rather than whether the option was specified at all.

Then I noticed a related issue with ESCAPE. That option is only allowed in CSV mode, but if I do:
```
evantest=# copy t1 to stdout (format json, escape);
ERROR:  escape requires a parameter
```

The error complains about the syntax of ESCAPE, rather than the fact that ESCAPE is not allowed for "FORMAT json".

That behavior feels misleading, because once the user fixes the syntax problem, they just hit the real failure next:
```
evantest=# copy t1 to stdout (format json, escape 1);
ERROR:  COPY ESCAPE requires CSV mode
```

When ESCAPE is specified for a non-CSV format, I think we should fail immediately, instead of parsing the option argument first.

After that, I did a thorough pass over the other COPY options and found a few more cases with the same problem. So this patch tightens option validation by checking option presence where appropriate, rather than the parsed value.

The fix is straightforward, see the attached patch for details.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Re: COPY: validate option presence rather than option values

От:
Masahiko Sawada <sawada.mshk@gmail.com>
Дата:
On Tue, May 5, 2026 at 9:26 PM Chao Li  wrote:
>
> Hi,
>
> While testing COPY TO (FORMAT json), I noticed that the doc says FORCE_ARRAY is only allowed with FORMAT json:
> ```
>    
>     FORCE_ARRAY
>     
>      
>       Force output of square brackets as array decorations at the beginning
>       and end of output, and commas between the rows. It is allowed only in
>       COPY TO, and only when using
>       json format. The default is
>       false.
>      
>     
>    
> ```
>
> However, this succeeds:
> ```
> evantest=# copy t1 to stdout (format csv, force_array false);
> 1
> ```
>
> So this is clearly validating the parsed value of force_array, rather than whether the option was specified at all.

Hmm, I'm not sure this is actually a problem in practice. It seems
straightforward to me that the documentation stating 'it is allowed
only when...' can be interpreted as 'it is allowed to be enabled only
when...'. My concern is that the proposed patch could make the command
unnecessarily less flexible. This is especially true in cases where
users programmatically generate a template COPY query and pass
explicit boolean values (true/false) to the options. Also, other
commands like VACUUM handle their boolean options in the exact same
manner:

=# vacuum (parallel 1, full) pg_class;
ERROR:  VACUUM FULL cannot be performed in parallel
=# vacuum (parallel 1, full off) pg_class;
VACUUM

Regards,

-- 
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com


FAQ