Bootstrap

Centos7.6部署postgresql15主从

安装pg15(master和standby)

Centos7.6安装postgresql15

  • 注意:只要完成初始化数据库之前的步骤

主数据库配置(master)

初始化数据库

${PGHOME}/bin/initdb -D ${PGDATA} --encoding=UTF8 --lc-collate=en_US.UTF-8 --lc-ctype=en_US.UTF-8 #初始化数据库

创建归档日志目录

mkdir -p $PGDATA/pg_archive

设置数据库访问权限

vi $PGDATA/pg_hba.conf #添加修改
#添加

host    replication     repl            10.0.1.203/24           scram-sha-256
host    all             all             0.0.0.0/0               scram-sha-256

修改数据库配置文件

# basic
listen_addresses = '*'                # 监听所有ip
port = 5432                           # 端口
max_connections = 1000                # 最大连接数
superuser_reserved_connections = 10   # 给超级用户预留的连接数
shared_buffers = 1GB                  # 共享内存,一般设置为内存的1/4
work_mem = 16MB                       # 设置在写入临时磁盘文件之前查询操作(例如排序或哈希表)可使用的最大内存容量
maintenance_work_mem = 256MB          # 在维护性操作(例如VACUUM、CREATE INDEX和ALTER TABLE ADD FOREIGN KEY)中使用的 最大的内存量
timezone = 'Asia/Shanghai'            # 系统时区
hot_standby = on                      # 打开热备

# optimizer
default_statistics_target = 500       # 默认100,ANALYZE在pg_statistic中存储的信息量,增大该值,会增加ANALYZE的时间,但会让解释计划更精准

# wal
max_wal_size = 1GB                    # 建议与shared_buffers保持一致
min_wal_size = 80MB                   # 建议max_wal_size/12.5
wal_log_hints = on                    # 控制WAL日志记录的方式,建议打开
wal_level = replica                   # wal日志写入级别,要使用流复制,必须使用replica或更高级别
wal_sender_timeout = 60s              # 设置WAL发送者在发送WAL数据时等待主服务器响应的超时时间

# archive
archive_mode = on                     # 
archive_command = 'gzip < %p > /data/pgsql/data/pg_archive/%f.gz'

# log 近7天轮询
log_destination = 'csvlog'            # 日志格式
logging_collector = on                # 日志收集器
log_directory = 'pg_log'              # 日志目录 $PGDATA/pg_log
log_filename = 'postgresql.%a'        # 7天日志轮询
log_file_mode = 0600                  # 日志文件的权限
log_rotation_size = 0                 # 日志的最大尺寸,设置为零时将禁用基于大小创建新的日志文件
log_truncate_on_rotation = on         # 这个参数将导致PostgreSQL截断(覆盖而不是追加)任何已有的同名日志文件
log_min_duration_statement = 0        # 如果语句运行至少指定的时间量,将导致记录每一个这种完成的语句的持续时间
log_duration = on                     # 每一个完成的语句的持续时间被记录
log_lock_waits = on                   # 控制当一个会话为获得一个锁等到超过deadlock_timeout时,是否要产生一个日志消息
log_statement = 'mod'                 # 控制哪些 SQL 语句被记录。有效值是 none (off)、ddl、mod和 all(所有语句)。ddl记录所有数据定义语句,例如CREATE、ALTER和 DROP语句。mod记录所有ddl语句,外加数据修改语句例如INSERT, UPDATE、DELETE、TRUNCATE, 和COPY FROM
log_timezone = 'Asia/Shanghai'        # 设置在服务器日志中写入的时间戳的时区

# sql
statement_timeout = 300000            # 语句执行超时时间 5分钟
idle_in_transaction_session_timeout = 300000   # 事务空闲超时时间 5分钟
idle_session_timeout = 1800000        # 会话空闲超时时间 30分钟
lock_timeout = 60000                  # 等锁超时时间 1分钟
  • pg的postgresql.conf的配置可以参考以下网站

PGTune - calculate configuration for PostgreSQL based on the maximum performance for a given hardware configuration

开启数据库

pg_ctl start
psql postgres
# 修改postgres的密码
alter role postgres with password 'Aa123456';
# 创建主从同步用户
create role repl with login replication encrypted password 'Aa123456';
\q

从数据库配置(standby)

同步主库的数据文件

chmod 0700 $PGDATA
su - postgres
pg_basebackup -Fp --progress -D $PGDATA -R -h ty-m2 -p 5432 -U repl --password

创建文件standby.signal

vi $PGDATA/standby.signal
standby_mode = on #on为从库
primary_conninfo = 'host=ty-m2 port=5432 user=repl password=Aa123456' #主库信息
recovery_target_timeline = 'latest' #流复制同步最新数据

启动从数据库

pg_ctl start

主从状态验证

master上验证

su - postgres

psql postgres

select client_addr,sync_state from pg_stat_replication;

在这里插入图片描述

\x on
select * from pg_stat_replication;
\q

在这里插入图片描述

pg_controldata | grep 'Database cluster state'

在这里插入图片描述

standby上验证

pg_controldata | grep 'Database cluster state'

在这里插入图片描述

实操测试

  • 在主库建立一个pgtest的数据库
psql postgres
create database pgtest;
\connect pgtest
\q
  • 从库也同步建了一个pgtest的数据库
psql pgtest
\q

启用SSL安全连接

  • sslmode的默认值是prefer
  • 要允许服务器证书验证,必须将一个或者更多个根证书放置在用户主目录下的~/.postgresql/root.crt文件中(在Microsoft Windows上该文件名为%APPDATA%\postgresql\root.crt)。
  • 如果参数sslmode被设置为verify-ca,libpq将通过检查该证书是否链接到存储在客户端上的根证书来验证服务器
sslmode窃听保护MITM保护声明
disableNoNo我不关心安全性,并且我不想为加密增加负荷。
allow可能No我不关心安全性,但如果服务器坚持,我将承担加密带来的负荷。
prefer可能No我不关心安全性,但如果服务器支持,我希望承担加密带来的负荷。
requireYesNo我想要对数据加密,并且我接受因此带来的负荷。我信任该网络会保证我总是连接到想要连接的服务器。
verify-caYesDepends on CA policy我想要对数据加密,并且我接受因此带来的负荷。我想要确保我连接到的是我信任的服务器。
verify-fullYesYes我想要对数据加密,并且我接受因此带来的负荷。我想要确保我连接到的是我信任的服务器,并且就是我指定的那一个。
openssl req -new -nodes -text -out /etc/pki/tls/certs/root.csr \
  -keyout /etc/pki/tls/private/root.key -subj "/CN=root.yourdomain.com"

chmod og-rwx /etc/pki/tls/private/root.key

openssl x509 -req -in /etc/pki/tls/certs/root.csr -text -days 3650 \
  -extfile /etc/pki/tls/openssl.cnf -extensions v3_ca \
  -signkey /etc/pki/tls/private/root.key -out /etc/pki/ca-trust/source/anchors/root.crt

openssl req -new -nodes -text -out /etc/pki/tls/private/server.csr \
  -keyout /etc/pki/tls/private/server.key -subj "/CN=dbhost.yourdomain.com"

chmod og-rwx /etc/pki/tls/private/server.key

openssl x509 -req -in /etc/pki/tls/private/server.csr -text -days 365 \
  -CA /etc/pki/ca-trust/source/anchors/root.crt -CAkey /etc/pki/tls/private/root.key -CAcreateserial \
  -out $PGDATA/server.crt

cp /etc/pki/tls/private/server.key $PGDATA/

chown postgres.postgres $PGDATA/server.{crt,key}

su - postgres
vi $PGDATA/pg_hba.conf
# 修改成
hostssl    replication     repl            10.0.1.203/24           scram-sha-256
hostssl    all             all             0.0.0.0/0               scram-sha-256

psql postgres -h ty-m2
# 开启ssl
alter system set ssl=on;
# 重新加载 配置
select pg_reload_conf();
\q

#查看当前连接信息
\conninfo

#查看所有连接信息
select 
     pg_ssl.pid
    ,pg_ssl.ssl
    ,pg_ssl.version
    ,pg_sa.backend_type
    ,pg_sa.usename
    ,pg_sa.client_addr
from pg_stat_ssl pg_ssl
inner join pg_stat_activity pg_sa
on (pg_ssl.pid = pg_sa.pid);

密码安全策略

密码有效期

psql postgres -h ty-m2
alter role yuzhenchao valid until '2022-12-31 23:59:59';
select * from pg_user where usename='yuzhenchao';
\q

注意:

  • pg密码有效期仅针对客户端有效,服务器端不受限制
  • 网络访问控制文件中不能配置为trust认证方式

密码复杂度和验证失败延迟策略

ls -atl `pg_config | grep ^LIBDIR | awk -F' = ' '{print $2}'`/passwordcheck*
psql postgres -h ty-m2
alter system set shared_preload_libraries=pg_stat_statements,passwordcheck,auth_delay;
\q
pg_ctl restart -mf
psql postgres -h ty-m2
alter system set auth_delay.milliseconds=10000;
select pg_reload_conf();
\q

防止密码记录到数据库日志

  • 使用createuser命令加上-W选项创建用户

服务器日志

  • 外部表访问服务器日志
psql etl -U postgres
# 创建外部表插件
create extension file_fdw;
# 授权tool用户file_fdw的使用
grant all on foreign data wrapper file_fdw to tool;
# 授权读文件
grant pg_read_server_files to tool;
# 切换tool用户
\c etl tool
create server pg_log_server foreign data wrapper file_fdw;
# 创建外部表
drop foreign table if exists pg_log_mon;
create foreign table pg_log_mon(
     log_time timestamp(3)
    ,user_name text
    ,database_name text
    ,process_id integer
    ,connection_from text
    ,session_id text
    ,session_line_num bigint
    ,command_tag text
    ,session_start_time timestamp
    ,virtual_transaction_id text
    ,transaction_id bigint
    ,error_severity text
    ,sql_state_code text
    ,message text
    ,detail text
    ,hint text
    ,internal_query text
    ,internal_query_pos integer
    ,context text
    ,query text
    ,query_pos integer
    ,location text
    ,application_name text
    ,backend_type text
    ,leader_pid integer
    ,query_id bigint
) server pg_log_server options(
     filename '/data/pgsql/data/pg_log/postgresql.Mon.csv'
    ,format 'csv'
    ,header 'false'
    ,delimiter ','
    ,quote '"'
    ,escape '"'
);
comment on table pg_log_mon is '审计日志表-周一';
comment on column pg_log_mon.log_time is '日志时间戳';
comment on column pg_log_mon.user_name is '连接数据库的用户名';
comment on column pg_log_mon.database_name is '连接的数据库名称';
comment on column pg_log_mon.process_id is '后台进程ID';
comment on column pg_log_mon.connection_from is '连接来源IP地址';
comment on column pg_log_mon.session_id is '会话ID';
comment on column pg_log_mon.session_line_num is '会话行号';
comment on column pg_log_mon.command_tag is '执行的SQL命令标签';
comment on column pg_log_mon.session_start_time is '会话开始时间戳';
comment on column pg_log_mon.virtual_transaction_id is '虚拟事务ID';
comment on column pg_log_mon.transaction_id is '事务ID';
comment on column pg_log_mon.error_severity is '错误级别';
comment on column pg_log_mon.sql_state_code is 'SQL状态码';
comment on column pg_log_mon.message is '错误信息';
comment on column pg_log_mon.detail is '错误详细信息';
comment on column pg_log_mon.hint is '错误提示信息';
comment on column pg_log_mon.internal_query is '内部查询语句';
comment on column pg_log_mon.internal_query_pos is '内部查询语句位置';
comment on column pg_log_mon.context is '上下文信息';
comment on column pg_log_mon.query is '执行的SQL语句';
comment on column pg_log_mon.query_pos is 'SQL语句位置';
comment on column pg_log_mon.location is '错误位置';
comment on column pg_log_mon.application_name is '应用程序名称';
comment on column pg_log_mon.backend_type is '后台进程类型';
comment on column pg_log_mon.leader_pid is '分布式事务的leader进程ID';
comment on column pg_log_mon.query_id is '查询ID';
drop foreign table if exists pg_log_tue;
create foreign table pg_log_tue(
     log_time timestamp(3)
    ,user_name text
    ,database_name text
    ,process_id integer
    ,connection_from text
    ,session_id text
    ,session_line_num bigint
    ,command_tag text
    ,session_start_time timestamp
    ,virtual_transaction_id text
    ,transaction_id bigint
    ,error_severity text
    ,sql_state_code text
    ,message text
    ,detail text
    ,hint text
    ,internal_query text
    ,internal_query_pos integer
    ,context text
    ,query text
    ,query_pos integer
    ,location text
    ,application_name text
    ,backend_type text
    ,leader_pid integer
    ,query_id bigint
) server pg_log_server options(
     filename '/data/pgsql/data/pg_log/postgresql.Tue.csv'
    ,format 'csv'
    ,header 'false'
    ,delimiter ','
    ,quote '"'
    ,escape '"'
);
comment on table pg_log_tue is '审计日志表-周二';
comment on column pg_log_tue.log_time is '日志时间戳';
comment on column pg_log_tue.user_name is '连接数据库的用户名';
comment on column pg_log_tue.database_name is '连接的数据库名称';
comment on column pg_log_tue.process_id is '后台进程ID';
comment on column pg_log_tue.connection_from is '连接来源IP地址';
comment on column pg_log_tue.session_id is '会话ID';
comment on column pg_log_tue.session_line_num is '会话行号';
comment on column pg_log_tue.command_tag is '执行的SQL命令标签';
comment on column pg_log_tue.session_start_time is '会话开始时间戳';
comment on column pg_log_tue.virtual_transaction_id is '虚拟事务ID';
comment on column pg_log_tue.transaction_id is '事务ID';
comment on column pg_log_tue.error_severity is '错误级别';
comment on column pg_log_tue.sql_state_code is 'SQL状态码';
comment on column pg_log_tue.message is '错误信息';
comment on column pg_log_tue.detail is '错误详细信息';
comment on column pg_log_tue.hint is '错误提示信息';
comment on column pg_log_tue.internal_query is '内部查询语句';
comment on column pg_log_tue.internal_query_pos is '内部查询语句位置';
comment on column pg_log_tue.context is '上下文信息';
comment on column pg_log_tue.query is '执行的SQL语句';
comment on column pg_log_tue.query_pos is 'SQL语句位置';
comment on column pg_log_tue.location is '错误位置';
comment on column pg_log_tue.application_name is '应用程序名称';
comment on column pg_log_tue.backend_type is '后台进程类型';
comment on column pg_log_tue.leader_pid is '分布式事务的leader进程ID';
comment on column pg_log_tue.query_id is '查询ID';
drop foreign table if exists pg_log_wed;
create foreign table pg_log_wed(
     log_time timestamp(3)
    ,user_name text
    ,database_name text
    ,process_id integer
    ,connection_from text
    ,session_id text
    ,session_line_num bigint
    ,command_tag text
    ,session_start_time timestamp
    ,virtual_transaction_id text
    ,transaction_id bigint
    ,error_severity text
    ,sql_state_code text
    ,message text
    ,detail text
    ,hint text
    ,internal_query text
    ,internal_query_pos integer
    ,context text
    ,query text
    ,query_pos integer
    ,location text
    ,application_name text
    ,backend_type text
    ,leader_pid integer
    ,query_id bigint
) server pg_log_server options(
     filename '/data/pgsql/data/pg_log/postgresql.Wed.csv'
    ,format 'csv'
    ,header 'false'
    ,delimiter ','
    ,quote '"'
    ,escape '"'
);
comment on table pg_log_wed is '审计日志表-周三';
comment on column pg_log_wed.log_time is '日志时间戳';
comment on column pg_log_wed.user_name is '连接数据库的用户名';
comment on column pg_log_wed.database_name is '连接的数据库名称';
comment on column pg_log_wed.process_id is '后台进程ID';
comment on column pg_log_wed.connection_from is '连接来源IP地址';
comment on column pg_log_wed.session_id is '会话ID';
comment on column pg_log_wed.session_line_num is '会话行号';
comment on column pg_log_wed.command_tag is '执行的SQL命令标签';
comment on column pg_log_wed.session_start_time is '会话开始时间戳';
comment on column pg_log_wed.virtual_transaction_id is '虚拟事务ID';
comment on column pg_log_wed.transaction_id is '事务ID';
comment on column pg_log_wed.error_severity is '错误级别';
comment on column pg_log_wed.sql_state_code is 'SQL状态码';
comment on column pg_log_wed.message is '错误信息';
comment on column pg_log_wed.detail is '错误详细信息';
comment on column pg_log_wed.hint is '错误提示信息';
comment on column pg_log_wed.internal_query is '内部查询语句';
comment on column pg_log_wed.internal_query_pos is '内部查询语句位置';
comment on column pg_log_wed.context is '上下文信息';
comment on column pg_log_wed.query is '执行的SQL语句';
comment on column pg_log_wed.query_pos is 'SQL语句位置';
comment on column pg_log_wed.location is '错误位置';
comment on column pg_log_wed.application_name is '应用程序名称';
comment on column pg_log_wed.backend_type is '后台进程类型';
comment on column pg_log_wed.leader_pid is '分布式事务的leader进程ID';
comment on column pg_log_wed.query_id is '查询ID';
drop foreign table if exists pg_log_thu;
create foreign table pg_log_thu(
     log_time timestamp(3)
    ,user_name text
    ,database_name text
    ,process_id integer
    ,connection_from text
    ,session_id text
    ,session_line_num bigint
    ,command_tag text
    ,session_start_time timestamp
    ,virtual_transaction_id text
    ,transaction_id bigint
    ,error_severity text
    ,sql_state_code text
    ,message text
    ,detail text
    ,hint text
    ,internal_query text
    ,internal_query_pos integer
    ,context text
    ,query text
    ,query_pos integer
    ,location text
    ,application_name text
    ,backend_type text
    ,leader_pid integer
    ,query_id bigint
) server pg_log_server options(
     filename '/data/pgsql/data/pg_log/postgresql.Thu.csv'
    ,format 'csv'
    ,header 'false'
    ,delimiter ','
    ,quote '"'
    ,escape '"'
);
comment on table pg_log_thu is '审计日志表-周四';
comment on column pg_log_thu.log_time is '日志时间戳';
comment on column pg_log_thu.user_name is '连接数据库的用户名';
comment on column pg_log_thu.database_name is '连接的数据库名称';
comment on column pg_log_thu.process_id is '后台进程ID';
comment on column pg_log_thu.connection_from is '连接来源IP地址';
comment on column pg_log_thu.session_id is '会话ID';
comment on column pg_log_thu.session_line_num is '会话行号';
comment on column pg_log_thu.command_tag is '执行的SQL命令标签';
comment on column pg_log_thu.session_start_time is '会话开始时间戳';
comment on column pg_log_thu.virtual_transaction_id is '虚拟事务ID';
comment on column pg_log_thu.transaction_id is '事务ID';
comment on column pg_log_thu.error_severity is '错误级别';
comment on column pg_log_thu.sql_state_code is 'SQL状态码';
comment on column pg_log_thu.message is '错误信息';
comment on column pg_log_thu.detail is '错误详细信息';
comment on column pg_log_thu.hint is '错误提示信息';
comment on column pg_log_thu.internal_query is '内部查询语句';
comment on column pg_log_thu.internal_query_pos is '内部查询语句位置';
comment on column pg_log_thu.context is '上下文信息';
comment on column pg_log_thu.query is '执行的SQL语句';
comment on column pg_log_thu.query_pos is 'SQL语句位置';
comment on column pg_log_thu.location is '错误位置';
comment on column pg_log_thu.application_name is '应用程序名称';
comment on column pg_log_thu.backend_type is '后台进程类型';
comment on column pg_log_thu.leader_pid is '分布式事务的leader进程ID';
comment on column pg_log_thu.query_id is '查询ID';
drop foreign table if exists pg_log_fri;
create foreign table pg_log_fri(
     log_time timestamp(3)
    ,user_name text
    ,database_name text
    ,process_id integer
    ,connection_from text
    ,session_id text
    ,session_line_num bigint
    ,command_tag text
    ,session_start_time timestamp
    ,virtual_transaction_id text
    ,transaction_id bigint
    ,error_severity text
    ,sql_state_code text
    ,message text
    ,detail text
    ,hint text
    ,internal_query text
    ,internal_query_pos integer
    ,context text
    ,query text
    ,query_pos integer
    ,location text
    ,application_name text
    ,backend_type text
    ,leader_pid integer
    ,query_id bigint
) server pg_log_server options(
     filename '/data/pgsql/data/pg_log/postgresql.Fri.csv'
    ,format 'csv'
    ,header 'false'
    ,delimiter ','
    ,quote '"'
    ,escape '"'
);
comment on table pg_log_fri is '审计日志表-周五';
comment on column pg_log_fri.log_time is '日志时间戳';
comment on column pg_log_fri.user_name is '连接数据库的用户名';
comment on column pg_log_fri.database_name is '连接的数据库名称';
comment on column pg_log_fri.process_id is '后台进程ID';
comment on column pg_log_fri.connection_from is '连接来源IP地址';
comment on column pg_log_fri.session_id is '会话ID';
comment on column pg_log_fri.session_line_num is '会话行号';
comment on column pg_log_fri.command_tag is '执行的SQL命令标签';
comment on column pg_log_fri.session_start_time is '会话开始时间戳';
comment on column pg_log_fri.virtual_transaction_id is '虚拟事务ID';
comment on column pg_log_fri.transaction_id is '事务ID';
comment on column pg_log_fri.error_severity is '错误级别';
comment on column pg_log_fri.sql_state_code is 'SQL状态码';
comment on column pg_log_fri.message is '错误信息';
comment on column pg_log_fri.detail is '错误详细信息';
comment on column pg_log_fri.hint is '错误提示信息';
comment on column pg_log_fri.internal_query is '内部查询语句';
comment on column pg_log_fri.internal_query_pos is '内部查询语句位置';
comment on column pg_log_fri.context is '上下文信息';
comment on column pg_log_fri.query is '执行的SQL语句';
comment on column pg_log_fri.query_pos is 'SQL语句位置';
comment on column pg_log_fri.location is '错误位置';
comment on column pg_log_fri.application_name is '应用程序名称';
comment on column pg_log_fri.backend_type is '后台进程类型';
comment on column pg_log_fri.leader_pid is '分布式事务的leader进程ID';
comment on column pg_log_fri.query_id is '查询ID';
drop foreign table if exists pg_log_sat;
create foreign table pg_log_sat(
     log_time timestamp(3)
    ,user_name text
    ,database_name text
    ,process_id integer
    ,connection_from text
    ,session_id text
    ,session_line_num bigint
    ,command_tag text
    ,session_start_time timestamp
    ,virtual_transaction_id text
    ,transaction_id bigint
    ,error_severity text
    ,sql_state_code text
    ,message text
    ,detail text
    ,hint text
    ,internal_query text
    ,internal_query_pos integer
    ,context text
    ,query text
    ,query_pos integer
    ,location text
    ,application_name text
    ,backend_type text
    ,leader_pid integer
    ,query_id bigint
) server pg_log_server options(
     filename '/data/pgsql/data/pg_log/postgresql.Sat.csv'
    ,format 'csv'
    ,header 'false'
    ,delimiter ','
    ,quote '"'
    ,escape '"'
);
comment on table pg_log_sat is '审计日志表-周六';
comment on column pg_log_sat.log_time is '日志时间戳';
comment on column pg_log_sat.user_name is '连接数据库的用户名';
comment on column pg_log_sat.database_name is '连接的数据库名称';
comment on column pg_log_sat.process_id is '后台进程ID';
comment on column pg_log_sat.connection_from is '连接来源IP地址';
comment on column pg_log_sat.session_id is '会话ID';
comment on column pg_log_sat.session_line_num is '会话行号';
comment on column pg_log_sat.command_tag is '执行的SQL命令标签';
comment on column pg_log_sat.session_start_time is '会话开始时间戳';
comment on column pg_log_sat.virtual_transaction_id is '虚拟事务ID';
comment on column pg_log_sat.transaction_id is '事务ID';
comment on column pg_log_sat.error_severity is '错误级别';
comment on column pg_log_sat.sql_state_code is 'SQL状态码';
comment on column pg_log_sat.message is '错误信息';
comment on column pg_log_sat.detail is '错误详细信息';
comment on column pg_log_sat.hint is '错误提示信息';
comment on column pg_log_sat.internal_query is '内部查询语句';
comment on column pg_log_sat.internal_query_pos is '内部查询语句位置';
comment on column pg_log_sat.context is '上下文信息';
comment on column pg_log_sat.query is '执行的SQL语句';
comment on column pg_log_sat.query_pos is 'SQL语句位置';
comment on column pg_log_sat.location is '错误位置';
comment on column pg_log_sat.application_name is '应用程序名称';
comment on column pg_log_sat.backend_type is '后台进程类型';
comment on column pg_log_sat.leader_pid is '分布式事务的leader进程ID';
comment on column pg_log_sat.query_id is '查询ID';
drop foreign table if exists pg_log_sun;
create foreign table pg_log_sun(
     log_time timestamp(3)
    ,user_name text
    ,database_name text
    ,process_id integer
    ,connection_from text
    ,session_id text
    ,session_line_num bigint
    ,command_tag text
    ,session_start_time timestamp
    ,virtual_transaction_id text
    ,transaction_id bigint
    ,error_severity text
    ,sql_state_code text
    ,message text
    ,detail text
    ,hint text
    ,internal_query text
    ,internal_query_pos integer
    ,context text
    ,query text
    ,query_pos integer
    ,location text
    ,application_name text
    ,backend_type text
    ,leader_pid integer
    ,query_id bigint
) server pg_log_server options(
     filename '/data/pgsql/data/pg_log/postgresql.Sun.csv'
    ,format 'csv'
    ,header 'false'
    ,delimiter ','
    ,quote '"'
    ,escape '"'
);
comment on table pg_log_sun is '审计日志表-周日';
comment on column pg_log_sun.log_time is '日志时间戳';
comment on column pg_log_sun.user_name is '连接数据库的用户名';
comment on column pg_log_sun.database_name is '连接的数据库名称';
comment on column pg_log_sun.process_id is '后台进程ID';
comment on column pg_log_sun.connection_from is '连接来源IP地址';
comment on column pg_log_sun.session_id is '会话ID';
comment on column pg_log_sun.session_line_num is '会话行号';
comment on column pg_log_sun.command_tag is '执行的SQL命令标签';
comment on column pg_log_sun.session_start_time is '会话开始时间戳';
comment on column pg_log_sun.virtual_transaction_id is '虚拟事务ID';
comment on column pg_log_sun.transaction_id is '事务ID';
comment on column pg_log_sun.error_severity is '错误级别';
comment on column pg_log_sun.sql_state_code is 'SQL状态码';
comment on column pg_log_sun.message is '错误信息';
comment on column pg_log_sun.detail is '错误详细信息';
comment on column pg_log_sun.hint is '错误提示信息';
comment on column pg_log_sun.internal_query is '内部查询语句';
comment on column pg_log_sun.internal_query_pos is '内部查询语句位置';
comment on column pg_log_sun.context is '上下文信息';
comment on column pg_log_sun.query is '执行的SQL语句';
comment on column pg_log_sun.query_pos is 'SQL语句位置';
comment on column pg_log_sun.location is '错误位置';
comment on column pg_log_sun.application_name is '应用程序名称';
comment on column pg_log_sun.backend_type is '后台进程类型';
comment on column pg_log_sun.leader_pid is '分布式事务的leader进程ID';
comment on column pg_log_sun.query_id is '查询ID';
create or replace view pg_log_7days as 
select * from pg_log_mon
union all 
select * from pg_log_tue
union all 
select * from pg_log_wed
union all 
select * from pg_log_thu
union all 
select * from pg_log_fri
union all 
select * from pg_log_sat
union all 
select * from pg_log_sun
;
comment on view pg_log_7days is '审计日志-近7天,包含今天';
comment on column pg_log_7days.log_time is '日志时间戳';
comment on column pg_log_7days.user_name is '连接数据库的用户名';
comment on column pg_log_7days.database_name is '连接的数据库名称';
comment on column pg_log_7days.process_id is '后台进程ID';
comment on column pg_log_7days.connection_from is '连接来源IP地址';
comment on column pg_log_7days.session_id is '会话ID';
comment on column pg_log_7days.session_line_num is '会话行号';
comment on column pg_log_7days.command_tag is '执行的SQL命令标签';
comment on column pg_log_7days.session_start_time is '会话开始时间戳';
comment on column pg_log_7days.virtual_transaction_id is '虚拟事务ID';
comment on column pg_log_7days.transaction_id is '事务ID';
comment on column pg_log_7days.error_severity is '错误级别';
comment on column pg_log_7days.sql_state_code is 'SQL状态码';
comment on column pg_log_7days.message is '错误信息';
comment on column pg_log_7days.detail is '错误详细信息';
comment on column pg_log_7days.hint is '错误提示信息';
comment on column pg_log_7days.internal_query is '内部查询语句';
comment on column pg_log_7days.internal_query_pos is '内部查询语句位置';
comment on column pg_log_7days.context is '上下文信息';
comment on column pg_log_7days.query is '执行的SQL语句';
comment on column pg_log_7days.query_pos is 'SQL语句位置';
comment on column pg_log_7days.location is '错误位置';
comment on column pg_log_7days.application_name is '应用程序名称';
comment on column pg_log_7days.backend_type is '后台进程类型';
comment on column pg_log_7days.leader_pid is '分布式事务的leader进程ID';
comment on column pg_log_7days.query_id is '查询ID';


参考

26.2. 日志传送后备服务器

;