Postgresql 系统相关函数介绍

Postgresql xlog 
先看下pg_xlog下物理文件的命名,大小在数据库编译安装的时候-with-wal-segsize指定
000000010000000200000019
XLogFileName宏里定义的,分别由时间线ID、日志ID、段ID的八位16进制数依次构成
日志ID最大为0xFFFFFFFFF,段ID最大为0x000000F

相关函数:
pg_current_xlog_insert_location()  获取写入wal buffer的事物号
pg_current_xlog_location()     获取写入wal日志的事物号

postgres=# select pg_current_xlog_location(),pg_current_xlog_insert_location();
 pg_current_xlog_location | pg_current_xlog_insert_location 
--------------------------+---------------------------------
 3C64/2EAC2000            | 3C64/2EAC2780
可以看到写入buffer是要早于写入wal日志的
pg_xlogfile_name() 把事务日志的位置字符串转换为文件名
postgres=# select pg_xlogfile_name('3C64/2EACBD08');
     pg_xlogfile_name     
--------------------------
 0000000300003C640000000B


 pg_xlogfile_name_offset()  把事务日志的位置字符串转换为文件名和文件内部十进制字节的偏移量

 postgres=# select pg_xlogfile_name_offset('3C64/2EACBD08');
       pg_xlogfile_name_offset       
-------------------------------------
 (0000000300003C640000000B,44875016)

可以通过以下语句查看插入buffer的位置和最后发送到standby节点的事物相差多少,是否有长时间的延迟
select pg_xlog_location_diff(pg_current_xlog_insert_location(),sent_location)  from pg_stat_replication;

Name Return Type Description
current_setting(setting_name) text 获取当前参数值 
set_config(setting_name, new_value, is_local) text 设置新的参数值
  以下相关函数之前博客介绍过,不再详解

Name Return Type Description
pg_cancel_backend(pidint) boolean Cancel a backend's current query. You can execute this against another backend that has exactly the same role as the user calling the function. In all other cases, you must be a superuser.
pg_reload_conf() boolean Cause server processes to reload their configuration files
pg_rotate_logfile() boolean Rotate server's log file
pg_terminate_backend(pidint) boolean Terminate a backend. You can execute this against another backend that has exactly the same role as the user calling the function. In all other cases, you must be a superuser.
  备份控制函数
Name
Return Type Description
pg_create_restore_point(name text) pg_lsn Create a named point for performing restore (restricted to superusers)
pg_current_xlog_insert_location() pg_lsn 获取写入wal buffer的事物号
pg_current_xlog_location() pg_lsn 获取写入wal日志的事物号
pg_start_backup(label text [, fast boolean ]) pg_lsn 主备在线备份开始
pg_stop_backup() pg_lsn 在线备份结束
pg_is_in_backup() bool 如果返回true,说明正在线备份
pg_backup_start_time() timestamp with time zone 获取在线备份的开始时间
pg_switch_xlog() pg_lsn 强制切换到新的事物日志
pg_xlogfile_name(location pg_lsn) text 转换事物号为文件名
pg_xlogfile_name_offset(location pg_lsn)
text, integer 转换事物号为文件名,并带有十进制字节偏移量
pg_xlog_location_diff(location pg_lsn, location pg_lsn) numeric 计算两个事物之间的相差多少字节
  恢复控制函数
Name Return Type Description
pg_is_in_recovery() bool 如果返回true,说明在做recovery,流复制环境也就是说是备节点
pg_last_xlog_receive_location() pg_lsn Get last transaction log location received and synced to disk by streaming replication. While streaming replication is in progress this will increase monotonically. If recovery has completed this will remain static at the value of the last WAL record received and synced to disk during recovery.If streaming replication is disabled, or if it has not yet started, the function returns NULL.
pg_last_xlog_replay_location() pg_lsn Get last transaction log location replayed during recovery. If recovery is still in progress this will increase monotonically. If recovery has completed then this value will remain static at the value of the last WAL record applied during that recovery. When the server has been started normally without recovery the function returns NULL.
pg_last_xact_replay_timestamp() timestamp with time zone Get time stamp of last transaction replayed during recovery. This is the time at which the commit or abort WAL record for that transaction was generated on the primary. If no transactions have been replayed during recovery, this function returns NULL. Otherwise, if recovery is still in progress this will increase monotonically. If recovery has completed then this value will remain static at the value of the last transaction applied during that recovery. When the server has been started normally without recovery the function returns NULL.
Name Return Type Description
pg_is_xlog_replay_paused() bool 如果recovery被暂停,那么这个函数返回true
pg_xlog_replay_pause() void 暂停recovery
pg_xlog_replay_resume() void 暂停后重新recovery
  这里如果是流复制,那么在备库执行pg_is_xlog_replay_paused(),可以查看备库是否暂停recovery,如果执行了pg_xlog_replay_pause(),那么备节点就 会暂停同步,再次执行pg_xlog_replay_resume()可以重新同步。

数据库对象大小查询函数
Name Return Type Description
pg_column_size(any) int 查询列字节数
pg_database_size(oid) bigint 通过OID查询数据库大小
pg_database_size(name) bigint 通过数据库名称查询数据库大小
pg_indexes_size(regclass) bigint 查询指定索引大小
pg_relation_size(relation regclass, fork text) bigint Disk space used by the specified fork ('main', 'fsm', 'vm', or 'init') of the specified table or index
pg_relation_size(relation regclass) bigint Shorthand for pg_relation_size(..., 'main')
pg_size_pretty(bigint) text 格式化为64bit人类易读的形式
pg_size_pretty(numeric) text 同上,只是格式化为numeric类型
pg_table_size(regclass) bigint 查询指定表的大小,不包括索引,(但是包括 TOAST, free space map, and visibility map)
pg_tablespace_size(oid) bigint 通过oid查询指定表空间的大小
pg_tablespace_size(name) bigint 通过空间名称查询指定表空间大小
pg_total_relation_size(regclass) bigint 查询包括表的大小以及索引的大小,并且包括TOAST数据

  数据库对象物理位置函数
Name Return Type Description
pg_relation_filenode(relation regclass) oid 返回数据对象的filenode
pg_relation_filepath(relation regclass) text 返回数据对象的文件路径
pg_filenode_relation(tablespace oid, filenode oid) regclass 通过给tablespace oid,以及filenode返回表名称
hank=> select pg_relation_filenode('t1'); 
 pg_relation_filenode 
----------------------
                21865
(1 row)

hank=> select pg_relation_filepath('t1');
             pg_relation_filepath             
----------------------------------------------
 pg_tblspc/21862/PG_9.4_201409291/21863/21865

hank=> select oid,* from pg_tablespace;
  oid  |  spcname   | spcowner |                spcacl                 | spcoptions 
-------+------------+----------+---------------------------------------+------------
  1663 | pg_default |       10 |                                       | 
  1664 | pg_global  |       10 |                                       | 
 21862 | tbs_hank   |       10 | {postgres=C/postgres,hank=C/postgres} | 

hank=> select pg_filenode_relation(21862,21865);
 pg_filenode_relation 
----------------------
 t1
(1 row)

这里只介绍了部分常用的系统函数,对部分函数做了实例

参考:http://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-ADMIN-BACKUP-TABLE

你可能感兴趣的:(Postgresql 系统相关函数介绍)