使用 file_fdw 读取 pglog,以便实时监控日志。

创建 extension

CREATE EXTENSION file_fdw;

创建 server

CREATE SERVER file_fdw_server FOREIGN DATA WRAPPER file_fdw;

获取最新日志文件

select t00.*
from (select t1.file,t1.file_ls,(pg_stat_file(t1.file)).modification as last_update_time,round( ((pg_stat_file(t1.file)).size)/1024/1024*1.0,1) as log_size_mbfrom (select dir||'/'||pg_ls_dir(t0.dir) as file,pg_ls_dir(t0.dir) as file_lsfrom ( select setting as dir from pg_settings where name='log_directory') t0) t1 where 1=1order by (pg_stat_file(file)).modification desc) t00
where 1=1and t00.file_ls like '%.csv'
limit 1
;

创建外部表

select name,setting
from pg_settings where name ~ 'log';CREATE FOREIGN TABLE pglog_current (  log_time timestamp(3) without time zone,  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 with time zone,  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)
server file_fdw_server
options(filename '/var/log/postgresql/postgresql-2017-08-29.csv',format 'csv')
;

查询外部表

select count(1)
from pglog_current
where 1=1
;select plc.*
from pglog_current plc
where 1=1
order by plc.log_time
;

可以和德哥一样写个存储过程来判断和创建。

参考:
http://blog.163.com/digoal@126/blog/static/16387704020121023105322442/
https://www.postgresql.org/docs/current/static/sql-createforeigntable.html
https://www.postgresql.org/docs/current/static/file-fdw.html

转载于:https://www.cnblogs.com/ctypyb2002/p/9793135.html

centos 6.8 + postgresql 9.6 + file_fdw相关推荐

  1. CentOS 7 安装 PostgreSQL 教程

    CentOS的源中自带有PostgreSQL,可以通过 yum list | grep postgresql 查看系统自带的版本,我这边看到的是9.2版本,个人还是比较建议安装更新的版本. 1.安装 ...

  2. Centos 7 安装 PostgreSQL

    本文只讲PostgreSQL在CentOS 7.x 下的安装,其他系统请查看:https://www.postgresql.org/download PostgreSQL 所用版本为:PostgreS ...

  3. centos 7.4 + postgresql 10.1 + pg_amqp

    上传 pg_amqp 到 /tmp 目录下 [root@tpg-1 tmp]# yum install lrzsz [root@tpg-1 tmp]# rz[root@tpg-1 tmp]# ls - ...

  4. Redhat、centos安装配置postgresql

    一.安装postgresql 本文仅以 redhat,postgresql9.4为例,使用yum方式进行介绍. 官网:http://www.postgresql.org/download/linux/ ...

  5. centos下nginx+postgresql+php+memcached+apc安装与配置

    2019独角兽企业重金招聘Python工程师标准>>> 具体安装步骤如下: 具体安装软件包: nginx1.0.13wget http://nginx.org/download/ng ...

  6. CentOS下安装postgresql

    一.说明 postgresql版本:9.4.1 安装包: postgresql94-server-9.4.1-1PGDG.rhel6.x86_64.rpm postgresql94-libs-9.4. ...

  7. CentOS 8 安装 PostgreSQL 10

    首先进入 PostgreSQL 官网,选择当前系统及版本,就能得到下面第一步中的安装语句.其实命令和之前的差别不大,主要是使用了 dnf ,所以选择对应的版本后就可以愉快的开始安装了. 一.安装 Po ...

  8. 在虚拟机(centos)配置postgresql数据库(1) - 安装篇

    一.安装 参考: http://zhidao.baidu.com/link?url=XZQH28IBTUK--2109zwxNKGoMFfZ_xCYS9VUt8iRX0APcadfTG1OabjSyE ...

  9. CentOS 离线安装 postgresql 12

    下载地址为: postgresqllinux安装文件-其它文档类资源-CSDN下载版本是12.3更多下载资源.学习资料请访问CSDN下载频道.https://download.csdn.net/dow ...

最新文章

  1. Python使用QRCode模块生成二维码
  2. 新年新气象,祝所有朋友心想事成
  3. 名词解释说明用英语怎么说_“用英语怎么说”译成How to say in English,典型的中式英语!...
  4. CLR Via C# 3rd 阅读摘要 -- Chapter 24 – Runtime Serialization
  5. matlab 多次求解偏微分方程 ode45
  6. 2015年,戴尔存储如何助力“智慧视频”落地?
  7. CacheDependency用法
  8. springbot集成finereport后在web中使用iframe引用报表
  9. 还在用SELECT COUNT统计数据库表的行数?Out了
  10. bootstrap 黑边框表格样式_bootstrap3.0教程之多种表格效果(条纹状表格、条纹状表格、鼠标悬停等)...
  11. PostGIS测试 - 坐标系统(spatial_ref_sys)
  12. STREAM内存带宽测试工具介绍及其内部实现
  13. 移动端 touch 手机拖动 css停止问题
  14. PrintStream和System.setOut(PrintStream ps)用法
  15. 服务器的1U、2U、4U是指什么?
  16. nds文件解包_下載wwpacker 1.84 更新 給wad解包和封包的工具
  17. 广度优先搜索:迷宫问题
  18. 自动驾驶仿真时如何采集数据做地图
  19. Android自定义View(一)
  20. IDEA在import时飘红的解决办法

热门文章

  1. 安装Terminator和快捷键使用
  2. 11.23关于微信JSAPI缺少参数的问题解决
  3. 大div套多个小div,怎样设置外div的高度自适应?
  4. 获取Json中特定的值
  5. matlab从工作区读取一维数组和结构体
  6. Git 简易食用指南 v2.0
  7. windows中的oracle12SE后启动的系统服务的列表
  8. Ubuntu16.04 换阿里源
  9. 30.MIN() 函数
  10. java学习笔记(2):获取文件名和自定义文件过滤器