Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time zone offset being subtracted from utc timestamp when inserting into column while using prepared statements #1094

Open
skunkworker opened this issue Oct 8, 2021 · 3 comments

Comments

@skunkworker
Copy link
Contributor

skunkworker commented Oct 8, 2021

rails 6.1.4.1
activerecord-jdbcpostgresql-adapter (61.1-java)
activerecord-jdbc-adapter (61.1-java)
jdbc-postgres (42.2.14)

jruby 9.2.19.0 on openjdk8

application.rb

config.time_zone = "UTC"
Table "public.foobars"
 Column            |            Type             | Collation | Nullable |                     Default
--------------------------------------------------------+-----------------------------+-----------+----------+--------------------------------------------------
 id                | integer                     |           | not null | nextval('foobars_id_seq'::regclass)
 created_at        | timestamp without time zone |           |          |
 updated_at        | timestamp without time zone |           |          | 

When prepared statements are turned on and a record is inserted into the database, the active record debug log shows the correct UTC timestamp with an offset of +00:00.

[DEBUG] [2021-10-08 13:13:20 -0600] Foobar Create (31.2ms)  INSERT INTO "foobars" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", Fri, 08 Oct 2021 19:13:20.573182000 UTC +00:00], ["updated_at", Fri, 08 Oct 2021 19:13:20.573363000 UTC +00:00], ]

However the inserted record takes my current offset (MDT -6:00) and applies it to the record, saving the incorrect time.

app_development=# select * from foobars;
-[ RECORD 1 ]------------------------------------------+-----------------------------------------

created_at                                             | 2021-10-08 13:13:14.411114
updated_at                                             | 2021-10-08 13:13:14.411256

Turning off prepared statements solves this problem.

@skunkworker skunkworker changed the title Time zone offset being subtracted from timestamp when inserting into timestamp without time zone field while using prepared statements true Time zone offset being subtracted from utc timestamp when inserting into column while using prepared statements Oct 8, 2021
@skunkworker
Copy link
Contributor Author

Looking into this more, the current timezone in my postgres instance is America/Denver. But once again, MRI does not have this issue using the pg adapter but JRuby does.

psql (13.4)
Type "help" for help.

testdb=# show timezone;
    TimeZone
----------------
 America/Denver
(1 row)

@skunkworker
Copy link
Contributor Author

skunkworker commented Oct 11, 2021

I suspect this isn't a commonly run into issue, because most if not all production databases would be running on a host in UTC time and postgres set to UTC.

@akimd
Copy link

akimd commented Jun 29, 2022

Maybe not common, but does happen. We have been hit. It results in a big mess :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants