0

Hi I have one "mysql" query that looks like this:

... WHERE (maintenanceLockTimestamp + "+ delay+" SECOND) < CURRENT_TIMESTAMP ) ) "

The thing is that, we need to move from MYSQL to PostgreSQL, but when executing the same query, I get:

Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "SECONDS"

I checked here: https://www.postgresqltutorial.com/postgresql-date-functions/

and it looks like the SECOND function is not present in PostgreSQL, do you know any alternative?

thanks

1 Answer 1

2

In Postgres, you can use:

where maintenanceLockTimestamp + :delay * interval '1 second' < current_timestamp

Note the use of a parameter to avoid munging the query string with literal values.

In both databases, this is better written with the date/time arithmetic on the current_timestamp:

where maintenanceLockTimestamp < current_timestamp - ? second  -- MySQL
where maintenanceLockTimestamp < current_timestamp - :delay * interval '1 second'  -- Postgres

This allows the optimizer to use an index on maintenanceLockTimestamp for performance.

Sign up to request clarification or add additional context in comments.

3 Comments

what an amazing answer.... incredible. I fixed that issue, now I have another one related to the naming strategy, since my table is named in the entity with @Table(name=NodeInfo) but in postgres I have node_info. But the 'seconds'problem is working. Thanks.
Actually I'm getting this: Thanks for your answer, I still get this: PSQLException: ERROR: operator does not exist: character varying * interval Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
@Manuelarte . . . You need to pass the parameter in as a number or convert to a number in Postgres -- say :delay::int * interval '1 second'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.