tty tty - 1 year ago 218
Java Question

Postgres JDBC driver: PSQLException: syntax error at or near RETURNING

For some reason the JDBC PostgresSQL driver is adding: RETURNING * to the end of select statements.
Does anyone have any idea why?


protected static final String AUTH_QUERY = "SELECT \"SECRET\" FROM \"user\" WHERE \"NAME\" = :name";

String password ="name", username).executeScalar(String.class);


org.postgresql.util.PSQLException: ERROR: syntax error at or near "RETURNING"
Position: 47
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(
at org.postgresql.core.v3.QueryExecutorImpl.processResults(
at org.postgresql.core.v3.QueryExecutorImpl.execute(
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(
at org.sql2o.Query.executeScalar(
at org.sql2o.Query.executeScalar(
at org.sql2o.Query.executeScalar(

Data source (JNDI):

<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">

<New id="mydb" class="">
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClass">org.postgresql.Driver</Set>
<Set name="jdbcUrl">jdbc:postgresql://localhost:5432/mydb</Set>
<Set name="user">user</Set>
<Set name="password">pass</Set>

PostgreSQL JDBC driver version


Packet capture

No. Time Source Destination Protocol Length Info
12 0.175636000 PGSQL 182 >P/B/D/E/S

Frame 12: 182 bytes on wire (1456 bits), 182 bytes captured (1456 bits) on interface 0
Type: Parse
Length: 69
Parameters: 1
Type OID: 1043

Answer Source

This looks like a problem with sql2o. The comments on the bug report say:

Whe using PostgreSQL, all SELECT statements will fail with message: org.postgresql.util.PSQLException: ERROR: syntax error at or near "RETURNING"

Seems to be related to this issue

This has been fixed with version 1.1.2.

The fix requires the QuirkMode enum flag to be set to PostgreSQL when creating a new instance of sql2o. It changes default behaviour of queries to NOT fetch generated keys by default. When it is needed to fetch generated keys, the returnGeneratedKeys parameter in the generateQuery method should be set.

Since Sql2o 1.6.0, include the sql2o-postgres dependency and use new PostgresQuirks() instead of QuirksMode.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download