分类目录归档:PostgreSQL

PostgreSQL运行日志管理

PostgreSQL目前配置中,不直接记录日志文件,这样的情况给数据库后期出现问题(特别是无法正常启动的情况)分析带来很大麻烦,不知道具体问题所在,建议在PG安装完成之后,启用日志功能,便于数据库运行状态检查和错误跟踪,主要日志参数涉及以下配置

log_destination = 'stderr'              # Valid values are combinations of
                                        # stderr, csvlog, jsonlog, syslog, and
                                        # eventlog, depending on platform.
                                        # csvlog and jsonlog require
                                        # logging_collector to be on.

# This is used when logging to stderr:
logging_collector = on          # Enable capturing of stderr, jsonlog,
                                        # and csvlog into log files. Required
                                        # to be on for csvlogs and jsonlogs.
                                        # (change requires restart)
//是否将日志重定向至文件中,默认是off。

# These are only used if logging_collector is on:
log_directory = 'pg_log'                   # directory where log files are written,
                                        # can be absolute or relative to PGDATA
//日志文件目录,默认是PGDATA的相对路径,即PGDATA的相对路径,即{PGDATA}/pg_log,也可以改为绝对路径。
//日志文件可能会非常多,建议将日志重定向到其他目录或分区。
//将此配置修改其他目录时,必须先创建此目录,并修改权限,使得postgres用户对该目录有写权限。

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,
                                        # can include strftime() escapes
log_file_mode = 0600                    # creation mode for log files,
                                        # begin with 0 to use octal notation
log_rotation_age = 1d                   # Automatic rotation of logfiles will
                                        # happen after that time.  0 disables.
log_rotation_size = 10MB                # Automatic rotation of logfiles will
                                        # happen after that much log output.
                                        # 0 disables.
#log_truncate_on_rotation = off         # If on, an existing log file with the
                                        # same name as the new log file will be
                                        # truncated rather than appended to.
                                        # But such truncation only occurs on
                                        # time-driven rotation, not on restarts
                                        # or size-driven rotation.  Default is
                                        # off, meaning append to existing files
                                        # in all cases.
//当日志文件已存在时,该配置如果为off,新生成的日志将在文件尾部追加,如果为on,则会覆盖原来的日志。

上述配置得到的结果如下

[postgres@xifenfei pg_log]$ pwd
/data/pg/15/data/pg_log
[postgres@xifenfei pg_log]$ ls -l
total 4
-rw------- 1 postgres postgres 1263 Apr  3 22:37 postgresql-2025-04-03_223117.log

pglog


发表在 PostgreSQL | 标签为 , | 评论关闭

pg单个数据库目录恢复-pdu恢复单个数据库目录数据

在某些情况下,无法获取pg完整的PGDATA目录中的所有数据库文件目录和文件,只能恢复出来一个数据库目录,对于这种情况,可以通过pdu进行直接恢复,比如有一个目录16805

[postgres@localhost 16805]$ pwd
/tmp/16805
[postgres@localhost 16805]$ ls 
112        16841     2613      2682      3079      3596      4159
113        174       2615      2683      3079_fsm  3597      4160
1247       175       2615_fsm  2684      3079_vm   3598      4163
1247_fsm   2187      2615_vm   2685      3080      3599      4164
1247_vm    2224      2616      2686      3081      3600      4165
1249       2228      2616_fsm  2687      3085      3600_fsm  4166
1249_fsm   2328      2616_vm   2688      3118      3600_vm   4167
1249_vm    2336      2617      2689      3119      3601      4168
1255       2337      2617_fsm  2690      3164      3601_fsm  4169
1255_fsm   2579      2617_vm   2691      3256      3601_vm   4170
1255_vm    2600      2618      2692      3257      3602      4171
1259       2600_fsm  2618_fsm  2693      3258      3602_fsm  4172
1259_fsm   2600_vm   2618_vm   2696      3350      3602_vm   4173
1259_vm    2601      2619      2699      3351      3603      4174
13362      2601_fsm  2619_fsm  2701      3379      3603_fsm  5002
13362_fsm  2601_vm   2619_vm   2702      3380      3603_vm   548
13362_vm   2602      2620      2703      3381      3604      549
13365      2602_fsm  2650      2704      3394      3605      6102
13366      2602_vm   2651      2753      3394_fsm  3606      6104
13367      2603      2652      2753_fsm  3394_vm   3607      6106
13367_fsm  2603_fsm  2653      2753_vm   3395      3608      6110
13367_vm   2603_vm   2654      2754      3429      3609      6111
13370      2604      2655      2755      3430      3712      6112
13371      2605      2656      2756      3431      3764      6113
13372      2605_fsm  2657      2757      3433      3764_fsm  6116
13372_fsm  2605_vm   2658      2830      3439      3764_vm   6117
13372_vm   2606      2659      2831      3440      3766      6175
13375      2606_fsm  2660      2832      3455      3767      6176
13376      2606_vm   2661      2833      3456      3997      6228
13377      2607      2662      2834      3456_fsm  4143      6229
13377_fsm  2607_fsm  2663      2835      3456_vm   4144      6237
13377_vm   2607_vm   2664      2836      3466      4145      6238
13380      2608      2665      2836_fsm  3467      4146      6239
13381      2608_fsm  2666      2836_vm   3468      4147      826
1417       2608_vm   2667      2837      3501      4148      827
1418       2609      2668      2838      3502      4149      828
16806      2609_fsm  2669      2838_fsm  3503      4150      pg_filenode.map
16806_fsm  2609_vm   2670      2838_vm   3534      4151      pg_internal.init
16806_vm   2610      2673      2839      3541      4152      PG_VERSION
16809      2610_fsm  2674      2840      3541_fsm  4153
16810      2610_vm   2675      2840_fsm  3541_vm   4154
16833      2611      2678      2840_vm   3542      4155
16833_fsm  2612      2679      2841      3574      4156
16833_vm   2612_fsm  2680      2995      3575      4157
16840      2612_vm   2681      2996      3576      4158
[postgres@localhost 16805]$ 

利用pdu进行恢复

PDU.public=# restore db xifenfei /tmp/16805;
      -pg_schema:</tmp/16805/2615>
      -pg_class:</tmp/16805/1259>,共86行
      -pg_attribute:</tmp/16805/1249>,共3274行
      模式:
        -->public,2张表
PDU.public=# use xifenfei;
|----------------------------------------|
|          模式             |  表数量    |
|----------------------------------------|
|    public                 |  2         |
|----------------------------------------|
xifenfei.public=# set public;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_cas_paymentbill                |  8.24 MB   |
|    t_auth                           |  80.00 KB  |
|--------------------------------------------------|

        仅显示表大小排名前 2 的表名
xifenfei.public=# \dt;
|--------------------------------------------------|
|               表名                  |  表大小    |
|--------------------------------------------------|
|    t_cas_paymentbill                |  8.24 MB   |
|    t_auth                           |  80.00 KB  |
|--------------------------------------------------|

        共计 2 张表
xifenfei.public=# unload sch public;
正在解析表 <t_cas_paymentbill>. 已解析数据页: 1055, 已解析数据: 9636 条
表名<t_cas_paymentbill>-</tmp/16805//16806> 解析完成, 1055 个数据页 ,共计 9636 条数据. 成功 9636 条; 失败【0】条 
COPY文件路径为:<xifenfei/public/t_cas_paymentbill.csv>

正在解析表 <t_auth>. 已解析数据页: 10, 已解析数据: 129 条
表名<t_auth>-</tmp/16805//16833> 解析完成, 10 个数据页 ,共计 129 条数据. 成功 129 条; 失败【0】条 
COPY文件路径为:<xifenfei/public/t_auth.csv>



模式<public>共 2 张表。成功:2, 失败【0】
日志路径:log/log/xifenfei_unload_schema_public_err.txt 

COPY命令导出完成, 文件路径: xifenfei/COPY/public_copy.sql,共找到2个csv文件

DDL导出完成. 文件路径: xifenfei/DDL/public_ddl.sql, 共计 2 张表
xifenfei.public=# 
xifenfei.public=# unload ddl;

DDL导出完成. 文件路径: xifenfei/DDL/public_ddl.sql, 共计 2 张表

通过pdu解析和恢复,该目录中一共两个业务表,均正常恢复出来,创建新库,导入数据测试

postgres=# create database xifenfei;
CREATE DATABASE
postgres=# \c xifenfei;
You are now connected to database "xifenfei" as user "postgres".
xifenfei=# \i /data/tools/pdu/xifenfei/DDL/public_ddl.sql
psql:/data/tools/pdu/xifenfei/DDL/public_ddl.sql:1: ERROR:  schema "public" already exists
SET
CREATE TABLE
CREATE TABLE
xifenfei=# \i /data/tools/pdu/xifenfei/COPY/public_copy.sql
SET
COPY 9636
COPY 129
xifenfei=# select count(1) from t_auth;
 count 
-------
   129
(1 row)

xifenfei=# select count(1) from t_cas_paymentbill;
 count 
-------
  9636
(1 row)
发表在 pdu工具 | 标签为 , | 评论关闭

pg删除数据恢复—pdu恢复pg delete数据

误操作,在应用端选择每页50条记录然后点击删除,类似这样误操作删除了近10000条记录,现在希望对其进行恢复.对于这种表被误删除数据的,可以使用pdu进行解析wal然后进行恢复.
1. 配置pdu.ini指定wal日志路径

PGDATA=/data/pg/16/data
ARCHIVE_DEST=/data/arch

2. 初始化字典信息

PDU.public=# b;
开始初始化...
 -pg_database:</data/pg/16/data/global/1262>
    【postgres】
      -pg_schema:</data/pg/16/data/base/5/2615>
      -pg_class:</data/pg/16/data/base/5/1259>,共86行
      -pg_attribute:</data/pg/16/data/base/5/1249>,共3273行
      模式:
        -->public,2张表

3. 关于删除恢复相关帮助

**误删数据恢复**
scan t1;                                │ 扫描被误删的表
restore del <Tx Number>;                │ 通过 事务号 恢复被误删的数据
restore del all;                        │ 通过 时间区间 恢复被误删的数据
--------------------------------------------------------------------------------------------------
add <filenode> <tablename> <attibutes>; │ 将特定表信息手动添加到restore库中
例如: <add 12345 t1 varchar,varchar,timestamp,varchar,numeric,varchar,varchar,varchar,numeric;>
--------------------------------------------------------------------------------------------------
restore db <dbname> <DB Path>;          │ 初始化特定的数据库目录 (例: restore db xmandb /home/postgres/data/base/290113;)

**参数设置**
param startwal 000000010000000800000008;│ 设置scan扫描的起始WAL文件,如果未设置则默认是归档目录的第一个文件
param endwal 000000010000000800000009;  │ 设置scan扫描的结束WAL文件,如果未设置则默认是归档目录的最后一个文件
param resmode tx|time;                  │ 设置restore恢复的模式,选择按照【事务号/TX】或【时间区间/TIME】进行恢复
param starttime 2025-01-01_00:00:00;    │ 设置scan扫描的起始时间 
param endtime 2025-01-01_00:00:00;      │ 设置scan扫描的结束时间 
reset <param name>                      │ 重置某个参数 
show;                                   │ 查看当前所有参数

4.可以通过param相关参数设置扫描wal日志范围,时间,恢复模式等.我们先使用事务模式确认开始时间

postgres.public=# scan t_delete;

正在扫描表<t_delete>的删除记录...

开始扫描归档目录
|-起始文件<000000030000052500000050>
|-终点文件<0000000300000525000000EF>

正在扫描中

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
当前为 事务号恢复 模式,扫描结果将以【事务号】为单位进行输出
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━



扫描结束,当前扫描的时间段为
|-开始时间:2025-03-05 18:05:38.006348 CST
|-结束时间:2025-03-05 19:00:19.515825 CST

--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:06:20.420863 CST | 事务号:1360118630 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:38:06.749572 CST | 事务号:1360430105 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.947320 CST | 事务号:1360532435 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.948734 CST | 事务号:1360532430 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.948754 CST | 事务号:1360532434 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:41.948781 CST | 事务号:1360532432 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
……………………
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:56.872903 CST | 事务号:1360536441 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:50 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:48:57.125587 CST | 事务号:1360536457 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:36 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:56:24.931723 CST | 事务号:1360596835 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
 时间戳:2025-03-05 18:59:38.314220 CST | 事务号:1360621790 | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:1 
--------------------------------------------------------------------------------------------------------------------------------
postgres.public=# 

5.基于上述数据,事务情况,可以确认第一次发生删除时间为2025-03-05 18:48:41.947320,最后删除时间为:2025-03-05 18:48:57.125587,重新设置基于时间的恢复模式

postgres.public=# param starttime 2025-03-05_18:48:40; 
OK! starttime=2025-03-05_18:48:40
postgres.public=# param endtime 2025-03-05_18:49:00;
OK! endtime=2025-03-05_18:49:00
postgres.public=# param resmode time;
OK! restoreMode=time
postgres.public=# show;
---------------------------------------------------
|       参数       |             当前值           |
---------------------------------------------------
|startwal          |                              |
|endwal            |                              |
|starttime         |2025-03-05 18:48:40.000000 CST|
|endtime           |2025-03-05 18:49:00.000000 CST|
|resmode           |             TIME             |
---------------------------------------------------

6.重新扫描需要恢复表

postgres.public=# scan t_delete;

正在扫描表<t_delete>的删除记录...

开始扫描归档目录
|-起始文件<000000030000052500000050>
|-终点文件<0000000300000525000000EF>

正在扫描中

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
当前为 时间区间恢复 模式,将输出该表在时间区间内的所有删除结果
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━



扫描结束,当前扫描的时间段为
|-开始时间:2025-03-05 18:48:40.000000 CST
|-结束时间:2025-03-05 18:49:00.000000 CST

----------------------------------------------------------------------------------------------------------------------------------------------------
 开始时间:2025-03-05 18:48:40.000000 CST | 结束时间:2025-03-05 18:49:00.000000 CST | 数据文件oid:725686 | toast文件oid:725689 |待恢复条数:9636 
----------------------------------------------------------------------------------------------------------------------------------------------------
postgres.public=# 

7.确认删除的记录9636条全部被扫描出来,现在对其进行恢复

postgres.public=# restore del all; 
开始扫描归档目录
|-起始文件<000000030000052500000050>
|-终点文件<0000000300000525000000EF>

正在扫描中
|-已解析出9636条数据 

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
解析结果:成功 9636,【失败 0】(即对应FPW未找到), 共计9636条数据
恢复出的csv文件路径为<restore/public/t_delete_2025-03-05 18:48:40.000000 CST_2025-03-05 18:49:00.000000 CST.csv>
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

扫描结束,当前扫描的时间段为
|-开始时间:2025-03-05 18:48:40.000000 CST
|-结束时间:2025-03-05 18:49:00.000000 CST

restore完成
postgres.public=#

8.恢复数据导入库中

[postgres@localhost pdu]$ psql -f postgres/COPY/public_copy.sql
SET
COPY 9636

QQ20250315-235527

发表在 pdu工具 | 评论关闭