PostgreSQL – Error Returned by FOR UPDATE NOWAIT

lockingpostgresqlselect

The PostgreSQL 9.4 documentation states that adding the NOWAIT option to a SELECT FOR UPDATE means an error is generated when a row cannot be locked:

To prevent the operation from waiting for other transactions to commit, use the NOWAIT option. With NOWAIT, the statement reports an error, rather than waiting, if a selected row cannot be locked immediately.

Exactly what error would that be?

As this is an acceptable condition, I want my Java code to check for such an expected error and then work around it.

Best Answer

SQLState: 55P03

In Postgres 9.4.x I tested this by performing an unresolved SELECT FOR UPDATE in pgAdmin and then doing a SELECT FOR UPDATE NOWAIT in my Java app. I used the JDBC driver JDBC41 Postgresql Driver, Version 9.4-1201.

The result was the following PostgreSQL Error Code.

  • Message:
    ERROR: could not obtain lock on row in relation "my_table_"
  • Class:
    Class 55 — Object Not In Prerequisite State
  • SQLState:
    55P03
  • Condition Name:
    lock_not_available