Skip to content

Commit 3a13f12

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 e77f605 commit 3a13f12

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/backend/parser/analyze.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,17 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
347347
* We have three cases to deal with: DEFAULT VALUES (selectStmt == NULL),
348348
* VALUES list, or general SELECT input. We special-case VALUES, both for
349349
* efficiency and so we can handle DEFAULT specifications.
350+
*
351+
* The grammar allows attaching ORDER BY, LIMIT, FOR UPDATE, or WITH to a
352+
* VALUES clause. If we have any of those, treat it as a general SELECT;
353+
* so it will work, but you can't use DEFAULT items together with those.
350354
*/
351-
isGeneralSelect = (selectStmt && selectStmt->valuesLists == NIL);
355+
isGeneralSelect = (selectStmt && (selectStmt->valuesLists == NIL ||
356+
selectStmt->sortClause != NIL ||
357+
selectStmt->limitOffset != NULL ||
358+
selectStmt->limitCount != NULL ||
359+
selectStmt->lockingClause != NIL ||
360+
selectStmt->withClause != NULL));
352361

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

0 commit comments

Comments
 (0)