PostgreSQL的 initdb 源代码分析之七

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

    printf(_("The files belonging to this database system will be owned "
             "by user "%s".
"
             "This user must also own the server process.

"),
           effective_user);

    if (strcmp(lc_ctype, lc_collate) == 0 &&
        strcmp(lc_ctype, lc_time) == 0 &&
        strcmp(lc_ctype, lc_numeric) == 0 &&
        strcmp(lc_ctype, lc_monetary) == 0 &&
        strcmp(lc_ctype, lc_messages) == 0)
printf(_("The database cluster will be initialized with locale %s. "), lc_ctype); else { printf(_("The database cluster will be initialized with locales " " COLLATE: %s " " CTYPE: %s " " MESSAGES: %s " " MONETARY: %s " " NUMERIC: %s " " TIME: %s "), 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) == 0)
    {
        int            ctype_enc;

        ctype_enc = pg_get_encoding_from_locale(lc_ctype, true);

        if (ctype_enc == -1)
        {
            /* Couldn't recognize the locale's codeset */
            fprintf(stderr, _("%s: could not find suitable encoding for locale %s
"),
                    progname, lc_ctype);
            fprintf(stderr, _("Rerun %s with the -E option.
"), progname);
            fprintf(stderr, _("Try "%s --help" for more information.
"),
                    progname);
            exit(1);
        }
        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.
"
               "The default database encoding will be set to %s instead.
"),
                   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
"),
                    progname, lc_ctype, pg_encoding_to_char(ctype_enc));
            fprintf(stderr,
                  _("Encoding %s is not allowed as a server-side encoding.
"
                    "Rerun %s with a different locale selection.
"),
                    pg_encoding_to_char(ctype_enc), progname);
            exit(1);
#endif
        }
        else
        {
            encodingid = encodingid_to_string(ctype_enc);
            printf(_("The default database encoding has accordingly been set to %s.
"),
                   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 */