Fixed multiple issues with PostgreSQL 9.2+ queries.

The queries were still previously querying against the "query" column
for state, however this has all moved into the new "state" column.