pg_ctl/miscinit: print "MyStartTime" as a long long instead of long to avoid 2038 problem.
Max Johnson <max.johnson@novatechautomation.com>
From: Max Johnson <max.johnson@novatechautomation.com>
To: "pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>
Date: 2024-09-24T19:33:24Z
Lists: pgsql-hackers
Attachments
- 0001-pg_ctl-and-miscinit-change-type-of-MyStartTime.patch (text/x-patch) patch 0001
Hello hackers,
I have found an instance of a time overflow with the start time that is written in "postmaster.pid". On a 32-bit Linux system, if the system date is past 01/19/2038, when you start Postgres with `pg_ctl start -D {datadir} ...`, the start time will have rolled back to approximately 1900. This is an instance of the "2038 problem". On my system, pg_ctl will not exit if the start time has overflowed.
This can be fixed by casting "MyStartTime" to a long long instead of just a long in "src/backend/utils/init/miscinit.c". Additionally, in "src/bin/pg_ctl/pg_ctl.c", when we read that value from the file, we should use "atoll()" instead of "atol()" to ensure we are reading it as a long long.
I have verified that this fixes the start time overflow on my 32-bit arm system. My glibc is compiled with 64-bit time_t.
Most systems running Postgres likely aren't 32-bit, but for embedded systems, this is important to ensure 2038 compatibility.
This is a fairly trivial patch, and I do not currently see any issues with using long long. I was told on IRC that a regression test is likely not necessary for this patch.
I look forward to hearing any feedback. This is my first open-source contribution!
Thank you,
Max Johnson
Embedded Linux Engineer I
NovaTech, LLC
13555 W. 107th Street | Lenexa, KS 66215
O: 913.451.1880
M: 913.742.4580
novatechautomation.com<http://www.novatechautomation.com/> | NovaTechLinkedIn<https://www.linkedin.com/company/565017>
NovaTech Automation is Net Zero committed. #KeepItCool<https://www.keepitcool.earth/>
Receipt of this email implies compliance with our terms and conditions<https://www.novatechautomation.com/email-terms-conditions>.
Commits
-
Add INT64_HEX_FORMAT and UINT64_HEX_FORMAT to c.h.
- efdc7d747539 18.0 landed
-
Fix Y2038 issues with MyStartTime.
- c91d0af0a2ad 12.21 landed
- b255493aefe9 13.17 landed
- a356d23fd3ad 17.1 landed
- 8aaf88b63de3 16.5 landed
- 5d6187d2a283 18.0 landed
- 5cea7168dfe6 14.14 landed
- 01731eeeaa96 15.9 landed