Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(pg): add whitespace between LIMIT and FOR UPDATE clause (#19762)

rebelice45ea3e6da475
diff --git a/backend/plugin/db/pg/query.go b/backend/plugin/db/pg/query.go
index bd58aef7f2..00002d21bd 100644
--- a/backend/plugin/db/pg/query.go
+++ b/backend/plugin/db/pg/query.go
@@ -238,28 +238,33 @@ func rewriteSelectLimit(sql string, sel *ast.SelectStmt, limitCount int) (string
// No LIMIT clause — find the right insertion point.
// PostgreSQL grammar order: ... ORDER BY ... LIMIT ... FOR UPDATE ...
// LIMIT goes BEFORE FOR UPDATE but AFTER everything else.
- insertPos := findLimitInsertPosition(sql, sel)
+ insertPos, beforeLocking := findLimitInsertPosition(sel)
+ if beforeLocking {
+ // Inserting at the start of FOR UPDATE/SHARE. The original whitespace
+ // before FOR becomes the separator before LIMIT; we add a trailing
+ // space to separate the limit value from FOR.
+ return sql[:insertPos] + fmt.Sprintf("LIMIT %d ", limitCount) + sql[insertPos:], nil
+ }
return sql[:insertPos] + fmt.Sprintf(" LIMIT %d", limitCount) + sql[insertPos:], nil
}
-// findLimitInsertPosition returns the byte offset where " LIMIT N" should be inserted.
-// It uses AST Loc positions exclusively — no string scanning for parentheses or keywords.
-func findLimitInsertPosition(_ string, sel *ast.SelectStmt) int {
+// findLimitInsertPosition returns the byte offset where " LIMIT N" should be inserted,
+// and whether the insertion is before a locking clause (FOR UPDATE/SHARE).
+func findLimitInsertPosition(sel *ast.SelectStmt) (int, bool) {
// LIMIT must appear before FOR UPDATE/SHARE.
- // Use the LockingClause's Loc.Start to insert just before it.
if sel.LockingClause != nil {
span := ast.ListSpan(sel.LockingClause)
if span.Start > 0 {
- return span.Start
+ return span.Start, true
}
}
// Otherwise insert at SelectStmt.Loc.End (after everything, including outer parens).
end := sel.Loc.End
if end <= 0 {
- return 0
+ return 0, false
}
- return end
+ return end, false
}
// extractIntFromNode extracts an integer value from a LIMIT/OFFSET node.
diff --git a/backend/plugin/db/pg/query_limit_test.go b/backend/plugin/db/pg/query_limit_test.go
index 06ea3c983d..df95c8f29c 100644
--- a/backend/plugin/db/pg/query_limit_test.go
+++ b/backend/plugin/db/pg/query_limit_test.go
@@ -183,6 +183,36 @@ func TestGetStatementWithResultLimitInline(t *testing.T) {
limit: 10,
wantErr: true,
},
+ {
+ name: "FOR UPDATE gets proper whitespace",
+ statement: "SELECT * FROM users WHERE id = 1 FOR UPDATE",
+ limit: 10,
+ want: "SELECT * FROM users WHERE id = 1 LIMIT 10 FOR UPDATE",
+ },
+ {
+ name: "FOR SHARE gets proper whitespace",
+ statement: "SELECT * FROM users FOR SHARE",
+ limit: 5,
+ want: "SELECT * FROM users LIMIT 5 FOR SHARE",
+ },
+ {
+ name: "SELECT ending with function call",
+ statement: "SELECT now()",
+ limit: 10,
+ want: "SELECT now() LIMIT 10",
+ },
+ {
+ name: "SELECT with subquery in WHERE",
+ statement: "SELECT * FROM users WHERE id IN (SELECT id FROM admins)",
+ limit: 10,
+ want: "SELECT * FROM users WHERE id IN (SELECT id FROM admins) LIMIT 10",
+ },
+ {
+ name: "Non-constant LIMIT expression falls back to error",
+ statement: "SELECT * FROM t LIMIT (1+2)",
+ limit: 5,
+ wantErr: true,
+ },
}
for _, tt := range tests {