DB 환경 차이로인한 쿼리 오류 트러블슈팅 - KimGyuBek/Threadly GitHub Wiki

문제 상황

증상

  • H2 환경에서는 정상 동작하던 커서 페이징 쿼리 가 테스트 DB를 운영 DB와 동일한 PostgreSQL로 바꾼 뒤 오류 발생.
  • JPA Repository의 native query에서 :cursorTimeStamp is null 조건을 사용했을때 H2에서는 문제없이 실행되었으나 PostgreSQL에서는 타입 캐스트 오류가 발생함

로그

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
Caused by: org.postgresql.util.PSQLException: ERROR: invalid input syntax for type timestamp: "is null"

원인 분석

where (
  p.created_at < :cursorTimeStamp
  or :cursorTimeStamp is null
)

H2와 달리 PostgreSQL에서는 null 비교 시 명확한 타입 정보가 필요하기 떄문으로 타입이 지정되지 않은 파라미터가 비교식에 포함될 경우 오류가 발생한다.


해결 과정

:cursorTimeStampTimestamp로 명시적 캐스팅하여 해결

cast (:cursorTimeStamp as timestamp) is null

결론

  • H2에서는 :cursorTimeStamp is null 조건이 정상적으로 동작했지만 PostgreSQL에서는 명시적인 타입 지정이 필요하여 쿼리가 실패하였다.
  • 테스트 DB와 운영 DB를 동일한 환경으로 유지해야 이러한 DB간의 문법 차이로 인한 문제를 사전에 방지할 수 있다.
⚠️ **GitHub.com Fallback** ⚠️