PostgreSQL的 initdb 源代码分析之七

时间:2021-01-07 15:03:05

继续分析:由于我使用initdb的时候,没有指定 locale,所以会使用OS的缺省locale,这里是 en_US.UTF-8

    printf(_("The files belonging to this database system will be owned "
"by user \"%s\".\n"
"This user must also own the server process.\n\n"),
effective_user); if (strcmp(lc_ctype, lc_collate) == &&
strcmp(lc_ctype, lc_time) == &&
strcmp(lc_ctype, lc_numeric) == &&
strcmp(lc_ctype, lc_monetary) == &&
strcmp(lc_ctype, lc_messages) == )
printf(_("The database cluster will be initialized with locale %s.\n"), lc_ctype); else
{ printf(_("The database cluster will be initialized with locales\n"
" COLLATE: %s\n"
" CTYPE: %s\n"
" MESSAGES: %s\n"
" MONETARY: %s\n"
" NUMERIC: %s\n"
" TIME: %s\n"),
lc_collate,
lc_ctype,
lc_messages,
lc_monetary,
lc_numeric,
lc_time);
}

接下来,再看Encoding:

我这里计算得到 ctype_enc 的值是 6,

pg_encoding_to_char(ctype_enc) 得到 UTF8。

所以输出: The default database encoding has accordingly been set to UTF8。

    if (strlen(encoding) == )
{
int ctype_enc; ctype_enc = pg_get_encoding_from_locale(lc_ctype, true); if (ctype_enc == -)
{
/* Couldn't recognize the locale's codeset */
fprintf(stderr, _("%s: could not find suitable encoding for locale %s\n"),
progname, lc_ctype);
fprintf(stderr, _("Rerun %s with the -E option.\n"), progname);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
progname);
exit();
}
else if (!pg_valid_server_encoding_id(ctype_enc))
{
/*
* We recognized it, but it's not a legal server encoding. On
* Windows, UTF-8 works with any locale, so we can fall back to
* UTF-8.
*/
#ifdef WIN32
printf(_("Encoding %s implied by locale is not allowed as a server-side encoding.\n"
"The default database encoding will be set to %s instead.\n"),
pg_encoding_to_char(ctype_enc),
pg_encoding_to_char(PG_UTF8));
ctype_enc = PG_UTF8;
encodingid = encodingid_to_string(ctype_enc);
#else
fprintf(stderr,
_("%s: locale %s requires unsupported encoding %s\n"),
progname, lc_ctype, pg_encoding_to_char(ctype_enc));
fprintf(stderr,
_("Encoding %s is not allowed as a server-side encoding.\n"
"Rerun %s with a different locale selection.\n"),
pg_encoding_to_char(ctype_enc), progname);
exit();
#endif
}
else
{
encodingid = encodingid_to_string(ctype_enc);
printf(_("The default database encoding has accordingly been set to %s.\n"),
pg_encoding_to_char(ctype_enc));
}
}
else
encodingid = get_encoding_id(encoding);

user_enc = atoi(encodingid);

    if (!check_locale_encoding(lc_ctype, user_enc) ||  
      !check_locale_encoding(lc_collate, user_enc))
        exit(1); /* check_locale_encoding printed the error */