当前位置:   article > 正文

Postgresql stream复制高可用集群搭建——筑梦之路_pg stream

pg stream
  1. 规划:
  2. 复制技术:PostgreSQL Stream
  3. 主从架构:一主两从
  4. 数据库版本:13.6
  5. 操作系统:Centos 7.9
  6. 192.168.50.10 Centos 7 13.6 主 软件+DB
  7. 192.168.50.11 Centos 7 13.6 从 软件
  8. 192.168.50.12 Centos 7 13.6 从 软件
  9. 部署流程为:
  10. 1、在三个节点分别安装PG软件;
  11. 2、在作为主节点的节点上初始化数据库;
  12. 3、修改postgresql.conf,pg_hba.conf配置文件;
  13. 4、通过pg_basebackup做主备数据恢复,
  14. 5、启动备机实例,完成部署。
  15. 在所有节点上安装PG软件,只在作为主节点的节点上初始化数据库
  16. #在所有节点安装同版本的PG
  17. #配置主节点
  18. ##创建用户,命令中已经指明权限包括:REPLICATION和LOGIN
  19. CREATE USER repusr
  20. REPLICATION
  21. LOGIN
  22. CONNECTION LIMIT 2
  23. ENCRYPTED PASSWORD 'repusr';
  24. ##修改postgresql.conf
  25. listen_addresses = '*'
  26. archive_mode = on
  27. archive_command = 'cp %p /opt/pgsql/pg_archive/%f'
  28. wal_level = hot_standby
  29. max_wal_senders = 10
  30. wal_sender_timeout = 60s
  31. max_connections = 100
  32. wal_log_hints = on
  33. ##修改pg_hba.conf,增加两台从服务器的白名单信息,这里我们暂时用相对较弱的md5加密策略
  34. # IPv4 local connections:
  35. host all all 127.0.0.1/32 trust
  36. host replication repusr 192.168.50.11/32 md5
  37. host replication repusr 192.168.50.12/32 md5
  38. ##重启数据库
  39. pg_ctl restart -D $PGDATA
  40. #从节点通过pg_basebackup恢复数据并建立主从关系
  41. 分别在两台从节点上执行,需要注意两点:
  42. 使用postgres用户;
  43. 从机上要提前建好$PGDATA目录,权限要求为700或者750,如果时其他权限会报权限过大问题。
  44. $ pg_basebackup -h 192.168.50.10 -U repusr -D /data/pgdata -X stream -P -R
  45. Password:
  46. 122597/122597 kB (100%), 1/1 tablespace
  47. -bash-4.2$ ls -lrt
  48. -X表示以stream方式包含所需的WAL文件;
  49. -P表示显示进度;
  50. -R表示为replication写配置信息;
  51. #启动从库,验证主从关系
  52. pg_ctl start -D /data/pgdata
  53. #登录主库,验证主从关系
  54. $ psql
  55. postgres=# \x
  56. postgres=# select * from pg_stat_replication;
  1. psql元命令是指以反斜线开头的命令,元命令使我们可以更便捷地管理数据库,比如列出中数据库各种对象的名称等,不需要书写sql语句,直接使用元命令就可以简单地查看
  2. \?: 查看元命令的帮助。
  3. \l: 列出所有数据库。
  4. \encoding: 查看字符集。
  5. \encoding 字符集: 设置字符集。
  6. \password user_name: 修改用户密码。
  7. \x: 以列显示的开关。相当于mysql中的\G。执行一次为打开,再执行一次为关闭。
  8. \timing on|off: 设置是否显示执行时长。
  9. \set AUTOCOMMIT on|off: 打开/关闭自动提交功能。
  10. \conninfo: 显示连接信息。
  11. \! : 执行shell命令。如:\! date, 输出当前日期。
  12. \i filename: 执行filename文件中的sql语句,也可用psql -s filename。
  13. \q: 退出psql命令行环境。
  14. \e:打开文本编辑器。
  15. ## \pset
  16. \pset border 0/1/2:设置执行结果的边框样式。
  17. \pset border 0: 输出内容无边框,无任何|
  18. \pset border 1: 边框只在内部有,无外边框
  19. \pset border 2: 内外都有边框
  20. \gexec:将当前查询缓冲区发送到服务器,然后将查询输出(如果有的话)的每一行的每一列都要作为要执行的SQL语句处理。这个用法可参考:Psql之\gexec命令。
  21. ## \c
  22. \c: 查看当前数据库和用户。查询当前数据库等同于select current_database();查询当前用户等同于select current_user;
  23. \c db_name: 进入指定的数据库。
  24. \c database user_name : 切换到某个数据库下某个角色
  25. ## \d
  26. \dn: 列出当前库下所有schema。
  27. \d: 查看当前数据库下的所有表、视图和序列。
  28. \dt: 只查看数据库中的所有表。
  29. \d tb_name: 查看表结构定义。
  30. \dt+ tb_name: 查看表大小等属性。
  31. \db: 查看表空间。
  32. \du: 列出所有用户及其用户权限。
  33. \ds: 查看用户自定义序列。
  34. \df: 查看用户自定义函数。
  35. 怎么查看查看元命令执行的具体sql语句?
  36. 在psql启动命令行中加入-E。
  37. 例:/usr/local/postgresql/bin/psql -h 127.0.01 -p 5432 -d postgres -E
  38. 如果是已经登入服务器,可以通过 \set ECHO_HIDDEN on|off 开启或关闭输出执行SQL的功能。
  39. ## 查看数据库版本。
  40. select version();
  41. ## 查看表空间
  42. select * from pg_tablespace;
  43. ## 大小相关的。
  44. 1)查看表空间大小
  45. select pg_tablespace_size('pg_default');
  46. 2)查看各个表空间的大小
  47. select spcname, pg_size_pretty(pg_tablespace_size(spcname)) from pg_tablespace;
  48. 3)查看DB大小
  49. select pg_size_pretty(pg_database_size(db_name));
  50. 4)查看所有数据库的大小
  51. select pg_database.datname, pg_size_pretty (pg_database_size(pg_database.datname)) AS size from pg_database;
  52. 5)查看表大小
  53. select pg_size_pretty(pg_relation_size(table_name))
  54. 6)按占空间大小,顺序查看所有表的大小
  55. select relname, pg_size_pretty(pg_relation_size(relid)) from pg_stat_user_tables where schemaname='public' order by pg_relation_size(relid) desc;
  56. select schemaname ,round(sum(pg_total_relation_size(schemaname||'.'||tablename))/1024/1024) "Size_MB" from pg_tables where schemaname='public' group by 1;
  57. 7)按占空间大小,顺序查看索引大小
  58. select indexrelname, pg_size_pretty(pg_relation_size(relid)) from pg_stat_user_indexes where schemaname='public' order by pg_relation_size(relid) desc;
  59. select schemaname ,round(sum(pg_total_relation_size(schemaname||'.'||indexname))/1024/1024) "Size_MB" from pg_indexes where schemaname='public' group by 1;
  60. ## 查看各数据库数据创建时间
  61. select datname,(pg_stat_file(format('%s/%s/PG_VERSION',case when spcname='pg_default' then 'base' else 'pg_tblspc/'||t2.oid||'/PG_11_201804061/' end, t1.oid))).* from pg_database t1,pg_tablespace t2 where t1.dattablespace=t2.oid;
  62. ## 索引相关
  63. 1)创建索引。
  64. CREATE INDEX index_name ON table_name (column_name, ...);
  65. 2) 并发创建索引(在线创建索引)。
  66. CREATE INDEX CONCURRENTLY
  67. 3)删除索引
  68. DROP INDEX idx_name;
  69. drop index concurrently idx_name;
  70. 4)查看表的所有索引信息
  71. select * from pg_indexes where tablename='student';
  72. 5)显示关于访问特定索引的I/O统计信息。
  73. select * from pg_statio_all_indexes where relname='events';
  74. 6)显示索引类型
  75. select relname,n.amname as index_type from pg_class m,pg_am n where m.relam = n.oid and m.oid in (
  76. select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'events');
  77. 7)显示索引大小
  78. SELECT c.relname,c2.relname, c2.relpages*8 as size_kb
  79. FROM pg_class c, pg_class c2, pg_index i
  80. WHERE c.relname = 'events' AND
  81. c.oid = i.indrelid AND
  82. c2.oid = i.indexrelid
  83. ORDER BY c2.relname;
  84. ## 查看表的约束
  85. select a.relname as table_name,b.conname as constraint_name,b.contype as constraint_type from pg_class a,pg_constraint b where a.oid = b.conrelid and a.relname = 'events';
  86. ## 查看表所对应的数据文件路径与大小
  87. SELECT pg_relation_filepath(oid), relpages FROM pg_class WHERE relname = 'events';
  88. ## 序列相关
  89. 1)查看序列
  90. select * from information_schema.sequences where sequence_schema = 'public';
  91. 2)创建序列:
  92. create sequence seq_user_camera_version increment by 1 minvalue 1 no maxvalue start with 1;
  93. 3)建表,并用上面的序列作为主键自增序列
  94. CREATE TABLE public.user_camera_version (
  95. id int4 NOT NULL DEFAULT nextval('seq_user_camera_version'::regclass),
  96. user_id int4 NULL,
  97. user_type varchar(1) NULL,
  98. hardware_version varchar(100) NULL,
  99. software_version varchar(100) NULL,
  100. modify_date timestamp NULL,
  101. CONSTRAINT user_camera_version_pkey PRIMARY KEY (id)
  102. )
  103. WITH (
  104. OIDS=FALSE
  105. ) ;
  106. ## 函数相关
  107. 1)查看所有用户自定义函数。
  108. \df
  109. SELECT n.nspname as "Schema",
  110. p.proname as "Name",
  111. pg_catalog.pg_get_function_result(p.oid) as "Result data type",
  112. pg_catalog.pg_get_function_arguments(p.oid) as "Argument data types",
  113. CASE p.prokind
  114. WHEN 'a' THEN 'agg'
  115. WHEN 'w' THEN 'window'
  116. WHEN 'p' THEN 'proc'
  117. ELSE 'func'
  118. END as "Type"
  119. FROM pg_catalog.pg_proc p
  120. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
  121. WHERE pg_catalog.pg_function_is_visible(p.oid)
  122. AND n.nspname <> 'pg_catalog'
  123. AND n.nspname <> 'information_schema'
  124. ORDER BY 1, 2, 4;
  125. 2)查看函数定义
  126. select oid,* from pg_proc where proname = 'insert_platform_action_exist';
  127. select * from pg_get_functiondef(oid);
  128. 3)创建函数
  129. CREATE FUNCTION add1(integer, integer) RETURNS integer
  130. AS 'select $1 + $2;'
  131. LANGUAGE SQL
  132. IMMUTABLE
  133. RETURNS NULL ON NULL INPUT;
  134. ## 查看视图
  135. select * from pg_views where schemaname = 'public';
  136. select * from information_schema.views where table_schema = 'public';
  137. ## 锁相关
  138. 1)查看锁等待信息。
  139. select * from pg_locks where granted is not true;
  140. 2)查看会话。
  141. select * from pg_stat_activity;
  142. ## 查看postgresql数据库启动时间
  143. select pg_postmaster_start_time();
  144. ## 查询当前客户端的端口号
  145. select inet_client_port();
  146. 查看与当前会话相关联的服务器进程ID
  147. select pg_backend_pid();
  148. 查看配置文件最后一次载入时间
  149. select pg_conf_load_time
  150. ## 查看参数文件
  151. show config_file;
  152. show hba_file;
  153. show ident_file;
  154. ## 查看当前会话的参数值
  155. show all;
  156. ## 查看参数值
  157. select * from pg_settings;
  158. ## 查看某个参数值,比如参数work_mem
  159. show work_mem
  160. ##修改某个参数值,比如参数work_mem
  161. alter system set work_mem='8MB'
  162. --使用alter system命令将修改postgresql.auto.conf文件,而不是postgresql.conf,这样可以很好的保护postgresql.conf文件,加入你使用很多alter system命令后搞的一团糟,那么你只需要删除postgresql.auto.conf,再执行pg_ctl reload加载postgresql.conf文件即可实现参数的重新加载。
  163. ## 查看是否开启归档
  164. show archive_mode;
  165. ## 运行日志相关
  166. --运行日志包括Error信息,定位慢查询SQL,数据库的启动关闭信息,checkpoint过于频繁等的告警信息。
  167. show logging_collector;--启动日志收集
  168. show log_directory;--日志输出路径
  169. show log_filename;--日志文件名
  170. show log_truncate_on_rotation;--当生成新的文件时如果文件名已存在,是否覆盖同名旧文件名
  171. show log_statement;--设置日志记录内容
  172. show log_min_duration_statement;--运行XX毫秒的语句会被记录到日志中,-1表示禁用这个功能,0表示记录所有语句,类似mysql的慢查询配置
  173. ## 用户和权限相关
  174. 1)创建用户
  175. create user u2 with login CREATEROLE CREATEDB password 'u2';
  176. create user u2 password 'u2';
  177. 2)修改数据库owner
  178. ALTER DATABASE name OWNER TO new_owner;
  179. 3)设置用户对某个数据库的所有权限
  180. GRANT ALL PRIVILEGES ON DATABASE database_name to username;
  181. 4)授予用户CONNECT到数据库的权限
  182. GRANT CONNECT ON DATABASE database_name TO username;
  183. 5) 授予public模式中所有表的所有权限给用户。
  184. GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO username;
  185. 6)授予public模式中所有序列的所有权限给用户:
  186. GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO username;
  1. 查看表的建表语句SQL
  2. CREATE OR REPLACE FUNCTION generate_create_table_statement(p_table_name varchar)
  3. RETURNS text AS
  4. $BODY$
  5. DECLARE
  6. v_table_ddl text;
  7. column_record record;
  8. BEGIN
  9. FOR column_record IN
  10. SELECT
  11. b.nspname as schema_name,
  12. b.relname as table_name,
  13. a.attname as column_name,
  14. pg_catalog.format_type(a.atttypid, a.atttypmod) as column_type,
  15. CASE WHEN
  16. (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
  17. FROM pg_catalog.pg_attrdef d
  18. WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) IS NOT NULL THEN
  19. 'DEFAULT '|| (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
  20. FROM pg_catalog.pg_attrdef d
  21. WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
  22. ELSE
  23. ''
  24. END as column_default_value,
  25. CASE WHEN a.attnotnull = true THEN
  26. 'NOT NULL'
  27. ELSE
  28. 'NULL'
  29. END as column_not_null,
  30. a.attnum as attnum,
  31. e.max_attnum as max_attnum
  32. FROM
  33. pg_catalog.pg_attribute a
  34. INNER JOIN
  35. (SELECT c.oid,
  36. n.nspname,
  37. c.relname
  38. FROM pg_catalog.pg_class c
  39. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  40. WHERE c.relname ~ ('^('||p_table_name||')$')
  41. AND pg_catalog.pg_table_is_visible(c.oid)
  42. ORDER BY 2, 3) b
  43. ON a.attrelid = b.oid
  44. INNER JOIN
  45. (SELECT
  46. a.attrelid,
  47. max(a.attnum) as max_attnum
  48. FROM pg_catalog.pg_attribute a
  49. WHERE a.attnum > 0
  50. AND NOT a.attisdropped
  51. GROUP BY a.attrelid) e
  52. ON a.attrelid=e.attrelid
  53. WHERE a.attnum > 0
  54. AND NOT a.attisdropped
  55. ORDER BY a.attnum
  56. LOOP
  57. IF column_record.attnum = 1 THEN
  58. v_table_ddl:='CREATE TABLE '||column_record.schema_name||'.'||column_record.table_name||' (';
  59. ELSE
  60. v_table_ddl:=v_table_ddl||',';
  61. END IF;
  62. IF column_record.attnum <= column_record.max_attnum THEN
  63. v_table_ddl:=v_table_ddl||chr(10)||
  64. ' '||column_record.column_name||' '||column_record.column_type||' '||column_record.column_default_value||' '||column_record.column_not_null;
  65. END IF;
  66. END LOOP;
  67. v_table_ddl:=v_table_ddl||');';
  68. RETURN v_table_ddl;
  69. END;
  70. $BODY$
  71. LANGUAGE 'plpgsql' COST 100.0 SECURITY INVOKER;
  1. 查看当前事务锁等待、持锁信息的SQL
  2. with
  3. t_wait as
  4. (
  5. select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,
  6. a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,
  7. b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name
  8. from pg_locks a,pg_stat_activity b where a.pid=b.pid and not a.granted
  9. ),
  10. t_run as
  11. (
  12. select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,
  13. a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,
  14. b.state,b.query,b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name
  15. from pg_locks a,pg_stat_activity b where a.pid=b.pid and a.granted
  16. ),
  17. t_overlap as
  18. (
  19. select r.* from t_wait w join t_run r on
  20. (
  21. r.locktype is not distinct from w.locktype and
  22. r.database is not distinct from w.database and
  23. r.relation is not distinct from w.relation and
  24. r.page is not distinct from w.page and
  25. r.tuple is not distinct from w.tuple and
  26. r.virtualxid is not distinct from w.virtualxid and
  27. r.transactionid is not distinct from w.transactionid and
  28. r.classid is not distinct from w.classid and
  29. r.objid is not distinct from w.objid and
  30. r.objsubid is not distinct from w.objsubid and
  31. r.pid <> w.pid
  32. )
  33. ),
  34. t_unionall as
  35. (
  36. select r.* from t_overlap r
  37. union all
  38. select w.* from t_wait w
  39. )
  40. select locktype,datname,relation::regclass,page,tuple,virtualxid,transactionid::text,classid::regclass,objid,objsubid,
  41. string_agg(
  42. 'Pid: '||case when pid is null then 'NULL' else pid::text end||chr(10)||
  43. 'Lock_Granted: '||case when granted is null then 'NULL' else granted::text end||' , Mode: '||case when mode is null then 'NULL' else mode::text end||' , FastPath: '||case when fastpath is null then 'NULL' else fastpath::text end||' , VirtualTransaction: '||case when virtualtransaction is null then 'NULL' else virtualtransaction::text end||' , Session_State: '||case when state is null then 'NULL' else state::text end||chr(10)||
  44. 'Username: '||case when usename is null then 'NULL' else usename::text end||' , Database: '||case when datname is null then 'NULL' else datname::text end||' , Client_Addr: '||case when client_addr is null then 'NULL' else client_addr::text end||' , Client_Port: '||case when client_port is null then 'NULL' else client_port::text end||' , Application_Name: '||case when application_name is null then 'NULL' else application_name::text end||chr(10)||
  45. 'Xact_Start: '||case when xact_start is null then 'NULL' else xact_start::text end||' , Query_Start: '||case when query_start is null then 'NULL' else query_start::text end||' , Xact_Elapse: '||case when (now()-xact_start) is null then 'NULL' else (now()-xact_start)::text end||' , Query_Elapse: '||case when (now()-query_start) is null then 'NULL' else (now()-query_start)::text end||chr(10)||
  46. 'SQL (Current SQL in Transaction): '||chr(10)||
  47. case when query is null then 'NULL' else query::text end,
  48. chr(10)||'--------'||chr(10)
  49. order by
  50. ( case mode
  51. when 'INVALID' then 0
  52. when 'AccessShareLock' then 1
  53. when 'RowShareLock' then 2
  54. when 'RowExclusiveLock' then 3
  55. when 'ShareUpdateExclusiveLock' then 4
  56. when 'ShareLock' then 5
  57. when 'ShareRowExclusiveLock' then 6
  58. when 'ExclusiveLock' then 7
  59. when 'AccessExclusiveLock' then 8
  60. else 0
  61. end ) desc,
  62. (case when granted then 0 else 1 end)
  63. ) as lock_conflict
  64. from t_unionall
  65. group by
  66. locktype,datname,relation,page,tuple,virtualxid,transactionid::text,classid,objid,objsubid ;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/449871
推荐阅读
相关标签
  

闽ICP备14008679号