Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w7o4B-005i62-0i for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Apr 2026 05:24:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w7o49-00EzCU-1C for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Apr 2026 05:24:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w7o48-00EzCK-35 for pgsql-hackers@lists.postgresql.org; Wed, 01 Apr 2026 05:24:33 +0000 Received: from mail-yw1-x1129.google.com ([2607:f8b0:4864:20::1129]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w7o47-000000025QF-0PqG for pgsql-hackers@postgresql.org; Wed, 01 Apr 2026 05:24:32 +0000 Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-79f8d6bc4e1so4464397b3.2 for ; Tue, 31 Mar 2026 22:24:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775021070; x=1775625870; darn=postgresql.org; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=7mCdZXeoqPB6VBPdDWQzJxv3UnHl42dQMUrYtrlM/2w=; b=nx6dfm6OFuq9oEpqJMqNODqVcA4qZOywSivwi6f5RK1lmmaU8WNEKF7CSisasz27y/ W2bTGRPLIVgTEpH3rFgmBzsYPAHlMwHLxd5rl2jFw+oWDJhst7gzskAfVLzHwyImlfIZ dX2CTtgtmSBMHKpjQWtJELl3Ik/Q7FhZ9YlQHxwXwBejAjuPgjGjgaWOuKH2jCQ/1CSX Trg7jkOVHQJE0mNbjP2AZEhmfwv9K/HbBKEbIsQs2GM8gjndKAZ+8djv/9itEt+p9ekk XiezQUgZ9erMmMwx3yL6a4s6zqjVIWCCL1U+aq1KA4Geqr2sV5LIY2rdFVHUNlcrdsy9 UogQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775021070; x=1775625870; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=7mCdZXeoqPB6VBPdDWQzJxv3UnHl42dQMUrYtrlM/2w=; b=FapTUhgQl22/B1oAYyupBRLtIXSraMPrPJeMIrcNE66c9+TzWATLMBPfvvCtYZh+yQ WwKrzg6pAeRV29O7cWTU5UJPB848Bb9i4BQwv12EVgTR/ZyFv72l5+2Tr7QIDxIEodOa 4Dgh2FrD8IdMHkvRnEsEdwgYRIRnVnV+2a3A4nTzBEuCONdw5Eksq9BYYazkd7PXGjk9 tA1PQWdysAPGpOoJ6CMhjD7W0VeUGMIdDpb2pqQ5JoQs/j7TZLYwTaTp1zCdJBCkJoHC 28QWJ4L5Yjr6Jo7U9Og+j5GRk552LdfwcD6fm1oJU5fS6iOtbfqW3gYTjFojTMA60pdk 12dw== X-Gm-Message-State: AOJu0YweHF69sm0K3CbNX86XtQ2wb/BH5NR+BLnOt9HA3GAjPaRVX3sj 0w7dTHUF7sr8d3DSIJidvkxw7Q8TG/VJZdPWc7hRVqc5cIdelMfvtLWDd393leX+ X-Gm-Gg: ATEYQzwyKw+yDTTIuSN00FVXJYRbYTnsAsX5BqOsm/W/4imLeHrTzAjPUxxCsJKngdj 9okCPnTPPInk4RxwXY3yzYIUyt0/6A4BY8ARmyGIB+XXpaiJTe5FO3sJuTVHI51dPo/hg3Jy6Ot P8qIlvtO3U6OogFpuwwIJhOMcMTk/xwV+rw3rfbz9AY9MJS4PUk0KoXPH4PpyW9AdJgn0+TT1Wv LRxEMNvq9mBp1Kow99NBknNnZxWIJaqlPDdyUW7ccqFjR732lA4wKSQtYAkkG0Kxsc0oyacyIAS KXqqGXokEkhUCqDFt7P46I61cjoXuNOfd2o8OJSElbE16xhjMSHOEpHh02wYsx8YHcEpo6vrSLk Hqx2kXE3bEe08k1mF5YKkC7scZRLSLy+LwBG+88D4AL79SpagPDpN10bLgvdVX0f9rPNW1CN2UY D9Mt1Xla6k1Syg2EJ2OVYGKJOfBPtweJiZmLtDeB6MNeSZHP/GYqIzJLWjgIP5tENvo2xQf+ghh 6PC+MiucutkaAtpvVVe3d9q4Llzi6ry0kNs X-Received: by 2002:a05:690c:9e:b0:799:3a3:2f25 with SMTP id 00721157ae682-7a21262ea6dmr24199677b3.38.1775021069828; Tue, 31 Mar 2026 22:24:29 -0700 (PDT) Received: from ?IPV6:2600:1700:8952:80:9943:bea8:6231:ddbe? ([2600:1700:8952:80:9943:bea8:6231:ddbe]) by smtp.gmail.com with ESMTPSA id 00721157ae682-79cba079784sm58179287b3.34.2026.03.31.22.24.28 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 31 Mar 2026 22:24:28 -0700 (PDT) Content-Type: multipart/mixed; boundary="------------k7VmMCaDUKf8Vx2x6OT7mmhy" Message-ID: <70926239-1062-41c6-a831-f29b215d57bb@gmail.com> Date: Wed, 1 Apr 2026 00:24:06 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: pgsql-hackers From: Bryan Green Subject: [PATCH] BUG: Stale LC_MESSAGES translations after SET on windows. List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------k7VmMCaDUKf8Vx2x6OT7mmhy Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Greetings hackers, The libintl library provides a libintl_setlocale function that we need to use to correctly invalidate the libintl translation cache. We are not doing this as the following shows. On Windows, changing lc_messages at runtime via SET returns stale translations from the previous locale. For example: SET lc_messages = 'de_DE'; SELECT 1/0; -- FEHLER: Division durch Null (correct, German) SET lc_messages = 'en_US'; SELECT 1/0; -- FEHLER: Division durch Null (wrong, still German) libintl caches translated messages in a binary tree keyed by (msgid, domain, category). The key struct includes a counter and a pointer to the translation text and translation length in the mmap'd .mo file for that locale. Background: Assume you have ("PL/pgSQL function %s line %d at %s", "plpgsql-19",1729) for (msgid, domain, category) respectively. Gettext uses strcmp for msgid and domain, and integer subtraction for category as it's comparison function. The search through the tree will short circuit at a node as soon as one of the three-- compared left to right as listed in the tuple-- is not a match. If the comparison is negative then it goes left in the tree, positive it goes right. If all three components of the key match then it checks that the counter matches _nl_msg_cat_cntr. If the counter matches then it is a cache hit. If the counter does not match then this translation was stored in the cache before the current locale was set with libintl_setlocale-- in other words it is stale. In this case the cache is invalidated and it is a cache miss because the locale has changed. Notice locale is not directly involved in the key structure for windows. So, if you don't increment the counter by calling libintl_setlocale and the two locales have the same msgid's for a given domain you will get the problem shown at the beginning of this email. The fix adds an explicit call to libintl_setlocale(LC_MESSAGES, ...) in pg_perm_setlocale() after IsoLocaleName() has produced the POSIX name. Instead of an explicit call, we could define _INTL_REDIRECT_MACROS before including libintl.h in c.h. This would make all setlocale() calls go through libintl_setlocale on MSVC. I chose to be explicit to limit the blast radius. The attached patch limits the change to a single spot and resolves the identified bug. Patch attached. -- Bryan Green EDB: https://www.enterprisedb.com --------------k7VmMCaDUKf8Vx2x6OT7mmhy Content-Type: text/plain; charset=UTF-8; name="v1-0001-Fix-stale-LC_MESSAGES-translations.patch" Content-Disposition: attachment; filename="v1-0001-Fix-stale-LC_MESSAGES-translations.patch" Content-Transfer-Encoding: base64 RnJvbSBiZTUzYTZlNjBjOWExZDQ2ZTM3OTdiZmZjNjA0ODE2MzI0ZmRmNjE2IE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBCcnlhbiBHcmVlbiA8ZGJye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