Commit 75abb955 authored by Tom Lane's avatar Tom Lane

Throw suitable error for COPY TO STDOUT/FROM STDIN in a SQL function.

A client copy can't work inside a function because the FE/BE wire protocol
doesn't support nesting of a COPY operation within query results.  (Maybe
it could, but the protocol spec doesn't suggest that clients should support
this, and libpq for one certainly doesn't.)

In most PLs, this prohibition is enforced by spi.c, but SQL functions don't
use SPI.  A comparison of _SPI_execute_plan() and init_execution_state()
shows that rejecting client COPY is the only discrepancy in what they
allow, so there's no other similar bugs.

This is an astonishingly ancient oversight, so back-patch to all supported
branches.

Report: https://postgr.es/m/BY2PR05MB2309EABA3DEFA0143F50F0D593780@BY2PR05MB2309.namprd05.prod.outlook.com
parent f6d6d292
...@@ -500,7 +500,16 @@ init_execution_state(List *queryTree_list, ...@@ -500,7 +500,16 @@ init_execution_state(List *queryTree_list,
fcache->readonly_func ? CURSOR_OPT_PARALLEL_OK : 0, fcache->readonly_func ? CURSOR_OPT_PARALLEL_OK : 0,
NULL); NULL);
/* Precheck all commands for validity in a function */ /*
* Precheck all commands for validity in a function. This should
* generally match the restrictions spi.c applies.
*/
if (IsA(stmt, CopyStmt) &&
((CopyStmt *) stmt)->filename == NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot COPY to/from client in a SQL function")));
if (IsA(stmt, TransactionStmt)) if (IsA(stmt, TransactionStmt))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment