PostgreSQL的 initdb 源代码分析之十二

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:

继续分析

    /* Now create all the text config files */
    setup_config();

将其展开:

实质就是,确定各种参数,分别写入 postgresql.conf 、pg_hba.conf、pg_indent.conf 文件。 

复制代码
/*
 * set up all the config files
 */
static void
setup_config(void)
{
    char      **conflines;
    char        repltok[100];
    char        path[MAXPGPATH];

    fputs(_("creating configuration files ... "), stdout);
    fflush(stdout);

    /* postgresql.conf */

    conflines = readfile(conf_file);

    snprintf(repltok, sizeof(repltok), "max_connections = %d", n_connections);
    conflines = replace_token(conflines, "#max_connections = 100", repltok);

    if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
        snprintf(repltok, sizeof(repltok), "shared_buffers = %dMB",
                 (n_buffers * (BLCKSZ / 1024)) / 1024);
    else
        snprintf(repltok, sizeof(repltok), "shared_buffers = %dkB",
                 n_buffers * (BLCKSZ / 1024));
    conflines = replace_token(conflines, "#shared_buffers = 32MB", repltok);

#if DEF_PGPORT != 5432
    snprintf(repltok, sizeof(repltok), "#port = %d", DEF_PGPORT);
    conflines = replace_token(conflines, "#port = 5432", repltok);
#endif

    snprintf(repltok, sizeof(repltok), "lc_messages = '%s'",
             escape_quotes(lc_messages));
    conflines = replace_token(conflines, "#lc_messages = 'C'", repltok);

    snprintf(repltok, sizeof(repltok), "lc_monetary = '%s'",
             escape_quotes(lc_monetary));
    conflines = replace_token(conflines, "#lc_monetary = 'C'", repltok);

    snprintf(repltok, sizeof(repltok), "lc_numeric = '%s'",
             escape_quotes(lc_numeric));
    conflines = replace_token(conflines, "#lc_numeric = 'C'", repltok);

    snprintf(repltok, sizeof(repltok), "lc_time = '%s'",
             escape_quotes(lc_time));
    conflines = replace_token(conflines, "#lc_time = 'C'", repltok);

    switch (locale_date_order(lc_time))
    {
        case DATEORDER_YMD:
            strcpy(repltok, "datestyle = 'iso, ymd'");
            break;
        case DATEORDER_DMY:
            strcpy(repltok, "datestyle = 'iso, dmy'");
            break;
        case DATEORDER_MDY:
        default:
            strcpy(repltok, "datestyle = 'iso, mdy'");
            break;
    }
    conflines = replace_token(conflines, "#datestyle = 'iso, mdy'", repltok);

    snprintf(repltok, sizeof(repltok),
             "default_text_search_config = 'pg_catalog.%s'",
             escape_quotes(default_text_search_config));
    conflines = replace_token(conflines,
                         "#default_text_search_config = 'pg_catalog.simple'",
                              repltok);

    snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);

    writefile(path, conflines);

    chmod(path, S_IRUSR | S_IWUSR);

    free(conflines);


    /* pg_hba.conf */

    conflines = readfile(hba_file);

#ifndef HAVE_UNIX_SOCKETS
    conflines = filter_lines_with_token(conflines, "@remove-line-for-nolocal@");
#else
    conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
#endif

#ifdef HAVE_IPV6

    /*
     * Probe to see if there is really any platform support for IPv6, and
     * comment out the relevant pg_hba line if not.  This avoids runtime
     * warnings if getaddrinfo doesn't actually cope with IPv6.  Particularly
     * useful on Windows, where executables built on a machine with IPv6 may
     * have to run on a machine without.
     */
    {
        struct addrinfo *gai_result;
        struct addrinfo hints;
        int            err = 0;

#ifdef WIN32
        /* need to call WSAStartup before calling getaddrinfo */
        WSADATA        wsaData;

        err = WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif

        /* for best results, this code should match parse_hba() */
        hints.ai_flags = AI_NUMERICHOST;
        hints.ai_family = PF_UNSPEC;
        hints.ai_socktype = 0;
        hints.ai_protocol = 0;
        hints.ai_addrlen = 0;
        hints.ai_canonname = NULL;
        hints.ai_addr = NULL;
        hints.ai_next = NULL;

        if (err != 0 ||
            getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
            conflines = replace_token(conflines,
                               "host    all             all             ::1",
                             "#host    all             all             ::1");
    }
#else                            /* !HAVE_IPV6 */
    /* If we didn't compile IPV6 support at all, always comment it out */
    conflines = replace_token(conflines,
                              "host    all             all             ::1",
                              "#host    all             all             ::1");
#endif   /* HAVE_IPV6 */

    /* Replace default authentication methods */
    conflines = replace_token(conflines,
                              "@authmethod@",
                              authmethod);
    conflines = replace_token(conflines,
                              "@authmethodlocal@",
                              authmethodlocal);

    conflines = replace_token(conflines,
                              "@authcomment@",
                       strcmp(authmethod, "trust") ? "" : AUTHTRUST_WARNING);

    /* Replace username for replication */
    conflines = replace_token(conflines,
                              "@default_username@",
                              username);

    snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);

    writefile(path, conflines);
    chmod(path, S_IRUSR | S_IWUSR);

    free(conflines);

    /* pg_ident.conf */

    conflines = readfile(ident_file);

    snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);

    writefile(path, conflines);
    chmod(path, S_IRUSR | S_IWUSR);

    free(conflines);

    check_ok();
}
复制代码





相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
3月前
|
关系型数据库 MySQL Serverless
高顿教育:大数据抽数分析业务引入polardb mysql serverless
高顿教育通过使用polardb serverless形态进行数据汇总,然后统一进行数据同步到数仓,业务有明显高低峰期,灵活的弹性伸缩能力,大大降低了客户使用成本。
|
4月前
|
关系型数据库 BI 分布式数据库
PolarDB NL2BI解决方案,让你不懂SQL也能进行数据查询分析并生成BI报表
无需创建和开通资源,在预置环境中免费体验PolarDB MySQL及其NL2BI解决方案
PolarDB NL2BI解决方案,让你不懂SQL也能进行数据查询分析并生成BI报表
|
7月前
|
关系型数据库 物联网 PostgreSQL
沉浸式学习PostgreSQL|PolarDB 11: 物联网(IoT)、监控系统、应用日志、用户行为记录等场景 - 时序数据高吞吐存取分析
物联网场景, 通常有大量的传感器(例如水质监控、气象监测、新能源汽车上的大量传感器)不断探测最新数据并上报到数据库. 监控系统, 通常也会有采集程序不断的读取被监控指标(例如CPU、网络数据包转发、磁盘的IOPS和BW占用情况、内存的使用率等等), 同时将监控数据上报到数据库. 应用日志、用户行为日志, 也就有同样的特征, 不断产生并上报到数据库. 以上数据具有时序特征, 对数据库的关键能力要求如下: 数据高速写入 高速按时间区间读取和分析, 目的是发现异常, 分析规律. 尽量节省存储空间
600 1
|
1月前
|
存储 关系型数据库 MySQL
TiDB与MySQL、PostgreSQL等数据库的比较分析
【2月更文挑战第25天】本文将对TiDB、MySQL和PostgreSQL等数据库进行详细的比较分析,探讨它们各自的优势和劣势。TiDB作为一款分布式关系型数据库,在扩展性、并发性能等方面表现突出;MySQL以其易用性和成熟性受到广泛应用;PostgreSQL则在数据完整性、扩展性等方面具有优势。通过对比这些数据库的特点和适用场景,帮助企业更好地选择适合自己业务需求的数据库系统。
|
6月前
|
关系型数据库 定位技术 分布式数据库
沉浸式学习PostgreSQL|PolarDB 18: 通过GIS轨迹相似伴随|时态分析|轨迹驻点识别等技术对拐卖、诱骗场景进行侦查
本文主要教大家怎么用好数据库, 而不是怎么运维管理数据库、怎么开发数据库内核.
1066 1
|
2月前
|
关系型数据库 分布式数据库 PolarDB
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
85 3
|
2月前
|
关系型数据库 分布式数据库 PolarDB
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
76 1
|
2月前
|
关系型数据库 分布式数据库 PolarDB
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
电子书阅读分享《PolarDB开发者大会:PolarDB在线数据实时分析加速》
87 1
|
3月前
|
存储 关系型数据库 分布式数据库
阿里云PolarDB解决乐麦多源数据存储性能问题
乐麦通过使用PolarDB数据库,使整个系统之间的数据查询分析更加高效
390 3
|
4月前
|
SQL 关系型数据库 MySQL
PostgreSQL【异常 01】java.io.IOException:Tried to send an out-of-range integer as a 2-byte value 分析+解决
PostgreSQL【异常 01】java.io.IOException:Tried to send an out-of-range integer as a 2-byte value 分析+解决
81 1