Skip to content

Error 500 on /metrics when Postgres datestyle is not MDY

After upgrading from GitLab Omnibus 15.0.5-ce to 15.1.6-ce (from gitlab-exporter 1.11.0 to 1.18.2), I get some 500 errors on gitlab-exporter's /metrics:

# curl http://x.x.x.x:9168/metrics
PG::DatetimeFieldOverflow: ERROR:  date/time field value out of range: "01-01-1970"
LINE 2: AND (COALESCE(migration_pre_import_started_at, '01-01-1970')...
                                                       ^
HINT:  Perhaps you need a different "datestyle" setting.

	/opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/gitlab-exporter-11.18.2/lib/gitlab_exporter/database/row_count.rb:310:in `exec'
	/opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/gitlab-exporter-11.18.2/lib/gitlab_exporter/database/row_count.rb:310:in `block in execute'
	/opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/gitlab-exporter-11.18.2/lib/gitlab_exporter/database/base.rb:59:in `block in with_connection_pool'
[...]

The issue here comes from a few MM-DD-YYYY date strings in some SQL queries; casting to date/timestamp will only work if Postgres' datestyle is MDY, but I'm using YMD as a default:

gitlab-db=> show datestyle ;
 DateStyle 
-----------
 ISO, YMD
(1 row)

I suggest using TO_TIMESTAMP(0) instead of '01-01-1970' for epoch, and will propose a merge-request.