Skip to content

Commit 392c66f

Browse files
committed
Behave correctly if INSERT ... VALUES is decorated with additional clauses.
In versions 8.2 and up, the grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to VALUES, and hence to INSERT ... VALUES. But the special-case code for VALUES in transformInsertStmt() wasn't expecting any of those, and just ignored them, leading to unexpected results. Rather than complicate the special-case path, just ensure that the presence of any of those clauses makes us treat the query as if it had a general SELECT. Per report from Hitoshi Harada.
1 parent e32229a commit 392c66f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/backend/parser/analyze.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,16 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
353353
* We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
354354
* VALUES list, or general SELECT input. We special-case VALUES, both for
355355
* efficiency and so we can handle DEFAULT specifications.
356+
*
357+
* The grammar allows attaching ORDER BY, LIMIT, or FOR UPDATE to a
358+
* VALUES clause. If we have any of those, treat it as a general SELECT;
359+
* so it will work, but you can't use DEFAULT items together with those.
356360
*/
357-
isGeneralSelect = (selectStmt && selectStmt->valuesLists == NIL);
361+
isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
362+
selectStmt->sortClause != NIL ||
363+
selectStmt->limitOffset != NULL ||
364+
selectStmt->limitCount != NULL ||
365+
selectStmt->lockingClause != NIL));
358366

359367
/*
360368
* If a non-nil rangetable/namespace was passed in, and we are doing

0 commit comments

Comments
 (0)