pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter@eisentraut.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: thread-safety: gmtime_r(), localtime_r()
Date: Fri, 23 Aug 2024 05:47:25 +0000
Message-ID: <E1shN8v-0013F0-5i@gemulon.postgresql.org> (raw)

thread-safety: gmtime_r(), localtime_r()

Use gmtime_r() and localtime_r() instead of gmtime() and localtime(),
for thread-safety.

There are a few affected calls in libpq and ecpg's libpgtypes, which
are probably effectively bugs, because those libraries already claim
to be thread-safe.

There is one affected call in the backend.  Most of the backend
otherwise uses the custom functions pg_gmtime() and pg_localtime(),
which are implemented differently.

While we're here, change the call in the backend to gmtime*() instead
of localtime*(), since for that use time zone behavior is irrelevant,
and this side-steps any questions about when time zones are
initialized by localtime_r() vs localtime().

Portability: gmtime_r() and localtime_r() are in POSIX but are not
available on Windows.  Windows has functions gmtime_s() and
localtime_s() that can fulfill the same purpose, so we add some small
wrappers around them.  (Note that these *_s() functions are also
different from the *_s() functions in the bounds-checking extension of
C11.  We are not using those here.)

On MinGW, you can get the POSIX-style *_r() functions by defining
_POSIX_C_SOURCE appropriately before including <time.h>.  This leads
to a conflict at least in plpython because apparently _POSIX_C_SOURCE
gets defined in some header there, and then our replacement
definitions conflict with the system definitions.  To avoid that sort
of thing, we now always define _POSIX_C_SOURCE on MinGW and use the
POSIX-style functions here.

Reviewed-by: Stepan Neretin <sncfmgg@gmail.com>
Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>
Reviewed-by: Thomas Munro <thomas.munro@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/eba1dc75-298e-4c46-8869-48ba8aad7d70@eisentraut.org

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a2bbc58f743489784de797d81be37ea309cb0773

Modified Files
--------------
meson.build                                |  4 ++++
src/backend/utils/adt/pg_locale.c          |  3 ++-
src/include/port/win32_port.h              | 13 +++++++++++++
src/interfaces/ecpg/pgtypeslib/dt_common.c | 11 +++++++----
src/interfaces/ecpg/pgtypeslib/timestamp.c |  3 ++-
src/interfaces/libpq/fe-trace.c            |  3 ++-
src/template/win32                         |  3 +++
7 files changed, 33 insertions(+), 7 deletions(-)



Message-ID: <E1shN8v-0013F0-5i@gemulon.postgresql.org>
Permalink:  ../E1shN8v-0013F0-5i@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1shN8v-0013F0-5i@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: peter@eisentraut.org, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: thread-safety: gmtime_r(), localtime_r()
  In-Reply-To: <E1shN8v-0013F0-5i@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox