Skip to content

Commit 1c461a8

Browse files
committed
Refactor: make default_locale internal to pg_locale.c.
Discussion: https://postgr.es/m/2228884bb1f1a02614b39f71a90c94d2cc8a3a2f.camel@j-davis.com Reviewed-by: Peter Eisentraut, Andreas Karlsson
1 parent 005c6b8 commit 1c461a8

File tree

3 files changed

+72
-43
lines changed

3 files changed

+72
-43
lines changed

src/backend/utils/adt/pg_locale.c

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
#include "access/htup_details.h"
5858
#include "catalog/pg_collation.h"
59+
#include "catalog/pg_database.h"
5960
#include "common/hashfn.h"
6061
#include "mb/pg_wchar.h"
6162
#include "miscadmin.h"
@@ -116,6 +117,8 @@ char *localized_full_months[12 + 1];
116117
/* is the databases's LC_CTYPE the C locale? */
117118
bool database_ctype_is_c = false;
118119

120+
static struct pg_locale_struct default_locale;
121+
119122
/* indicates whether locale information cache is valid */
120123
static bool CurrentLocaleConvValid = false;
121124
static bool CurrentLCTimeValid = false;
@@ -1458,8 +1461,6 @@ lc_ctype_is_c(Oid collation)
14581461
return (lookup_collation_cache(collation, true))->ctype_is_c;
14591462
}
14601463

1461-
struct pg_locale_struct default_locale;
1462-
14631464
void
14641465
make_icu_collator(const char *iculocstr,
14651466
const char *icurules,
@@ -1554,7 +1555,69 @@ pg_locale_deterministic(pg_locale_t locale)
15541555
}
15551556

15561557
/*
1557-
* Create a locale_t from a collation OID. Results are cached for the
1558+
* Initialize default_locale with database locale settings.
1559+
*/
1560+
void
1561+
init_database_collation(void)
1562+
{
1563+
HeapTuple tup;
1564+
Form_pg_database dbform;
1565+
Datum datum;
1566+
bool isnull;
1567+
1568+
/* Fetch our pg_database row normally, via syscache */
1569+
tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
1570+
if (!HeapTupleIsValid(tup))
1571+
elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
1572+
dbform = (Form_pg_database) GETSTRUCT(tup);
1573+
1574+
if (dbform->datlocprovider == COLLPROVIDER_BUILTIN)
1575+
{
1576+
char *datlocale;
1577+
1578+
datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale);
1579+
datlocale = TextDatumGetCString(datum);
1580+
1581+
builtin_validate_locale(dbform->encoding, datlocale);
1582+
1583+
default_locale.info.builtin.locale = MemoryContextStrdup(
1584+
TopMemoryContext, datlocale);
1585+
}
1586+
else if (dbform->datlocprovider == COLLPROVIDER_ICU)
1587+
{
1588+
char *datlocale;
1589+
char *icurules;
1590+
1591+
datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale);
1592+
datlocale = TextDatumGetCString(datum);
1593+
1594+
datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull);
1595+
if (!isnull)
1596+
icurules = TextDatumGetCString(datum);
1597+
else
1598+
icurules = NULL;
1599+
1600+
make_icu_collator(datlocale, icurules, &default_locale);
1601+
}
1602+
else
1603+
{
1604+
Assert(dbform->datlocprovider == COLLPROVIDER_LIBC);
1605+
}
1606+
1607+
default_locale.provider = dbform->datlocprovider;
1608+
1609+
/*
1610+
* Default locale is currently always deterministic. Nondeterministic
1611+
* locales currently don't support pattern matching, which would break a
1612+
* lot of things if applied globally.
1613+
*/
1614+
default_locale.deterministic = true;
1615+
1616+
ReleaseSysCache(tup);
1617+
}
1618+
1619+
/*
1620+
* Create a pg_locale_t from a collation OID. Results are cached for the
15581621
* lifetime of the backend. Thus, do not free the result with freelocale().
15591622
*
15601623
* As a special optimization, the default/database collation returns 0.

src/backend/utils/init/postinit.c

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect
318318
bool isnull;
319319
char *collate;
320320
char *ctype;
321-
char *datlocale;
322321

323322
/* Fetch our pg_database row normally, via syscache */
324323
tup = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
@@ -423,42 +422,7 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect
423422
strcmp(ctype, "POSIX") == 0)
424423
database_ctype_is_c = true;
425424

426-
if (dbform->datlocprovider == COLLPROVIDER_BUILTIN)
427-
{
428-
datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale);
429-
datlocale = TextDatumGetCString(datum);
430-
431-
builtin_validate_locale(dbform->encoding, datlocale);
432-
433-
default_locale.info.builtin.locale = MemoryContextStrdup(
434-
TopMemoryContext, datlocale);
435-
}
436-
else if (dbform->datlocprovider == COLLPROVIDER_ICU)
437-
{
438-
char *icurules;
439-
440-
datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale);
441-
datlocale = TextDatumGetCString(datum);
442-
443-
datum = SysCacheGetAttr(DATABASEOID, tup, Anum_pg_database_daticurules, &isnull);
444-
if (!isnull)
445-
icurules = TextDatumGetCString(datum);
446-
else
447-
icurules = NULL;
448-
449-
make_icu_collator(datlocale, icurules, &default_locale);
450-
}
451-
else
452-
datlocale = NULL;
453-
454-
default_locale.provider = dbform->datlocprovider;
455-
456-
/*
457-
* Default locale is currently always deterministic. Nondeterministic
458-
* locales currently don't support pattern matching, which would break a
459-
* lot of things if applied globally.
460-
*/
461-
default_locale.deterministic = true;
425+
init_database_collation();
462426

463427
/*
464428
* Check collation version. See similar code in
@@ -478,7 +442,10 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect
478442
if (dbform->datlocprovider == COLLPROVIDER_LIBC)
479443
locale = collate;
480444
else
481-
locale = datlocale;
445+
{
446+
datum = SysCacheGetAttrNotNull(DATABASEOID, tup, Anum_pg_database_datlocale);
447+
locale = TextDatumGetCString(datum);
448+
}
482449

483450
actual_versionstr = get_collation_actual_version(dbform->datlocprovider, locale);
484451
if (!actual_versionstr)

src/include/utils/pg_locale.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,12 @@ struct pg_locale_struct
9393

9494
typedef struct pg_locale_struct *pg_locale_t;
9595

96-
extern PGDLLIMPORT struct pg_locale_struct default_locale;
97-
9896
extern void make_icu_collator(const char *iculocstr,
9997
const char *icurules,
10098
struct pg_locale_struct *resultp);
10199

102100
extern bool pg_locale_deterministic(pg_locale_t locale);
101+
extern void init_database_collation(void);
103102
extern pg_locale_t pg_newlocale_from_collation(Oid collid);
104103

105104
extern char *get_collation_actual_version(char collprovider, const char *collcollate);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy