While working on an example to test the PostgreSQL support for SQL:2008 result set limiting feature, I realized that this syntax doesn't work with a prepared statement:
SELECT pc.id AS pc_id, p.id AS p_id
FROM post_comment pc
INNER JOIN post p ON p.id = pc.post_id
ORDER BY pc.id
OFFSET ? ROWS
FETCH FIRST ? ROWS ONLY;
While for static statements using literals it's just fine, using a prepared statement throws:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "$2" Position: 140 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182) ~[postgresql-9.4-1202-jdbc41.jar:9.4] at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911) ~[postgresql-9.4-1202-jdbc41.jar:9.4] at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173) ~[postgresql-9.4-1202-jdbc41.jar:9.4] at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:615) ~[postgresql-9.4-1202-jdbc41.jar:9.4] at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:465) ~[postgresql-9.4-1202-jdbc41.jar:9.4] at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:458) ~[postgresql-9.4-1202-jdbc41.jar:9.4]
If the SQL query is changed to:
SELECT pc.id AS pc_id, p.id AS p_id
FROM post_comment pc
INNER JOIN post p ON p.id = pc.post_id
ORDER BY pc.id
OFFSET ? ROWS
FETCH FIRST (?) ROWS ONLY;
the parenthesis seem to do the trick and the bind parameter is taken into consideration.
Is this a bug or just an implementation detail?
The test is available on GitHub
?is a JSON operator and incorrectly treated by the JDBC driver: postgresql.nabble.com/… - a_horse_with_no_name