PostgreSQL码农集散地

进阶课 14 纯享单机版

PolarDB 进阶课程14 纯享单机版

穷鬼玩PolarDB RAC一写多读集群系列已经写了几篇:

  • 《在Docker容器中用loop设备模拟共享存储》
  • 《如何搭建PolarDB容灾(Standby)节点》
  • 《共享存储在线扩容》
  • 《计算节点 Switchover》
  • 《在线备份》
  • 《在线归档》
  • 《实时归档》
  • 《时间点恢复(PITR)》
  • 《读写分离》
  • 《主机全毁, 只剩共享存储的PolarDB还有救吗?》
  • 《激活容灾(Standby)节点》
  • 《将“共享存储实例”转换为“本地存储实例”》
  • 《将“本地存储实例”转换为“共享存储实例”》
  • 《升级vector插件》
  • 《使用图数据库插件AGE》
  • 《接入私有化大模型服务》
  • 《接入PostGIS插件全功能》
  • 《接入pg_duckdb支持数据湖功能,且OLAP性能数量级提升》
  • 《pg_bulkload适配PFS加速批量导入》

是不是觉得PolarDB只有基于“共享存储+多计算节点”的集群版? 错了, PolarDB还支持单机版, 就和开源PostgreSQL类似, 加上PolarDB的其他高级功能.

其实大多数时候可能单机版就够用了. 使用单机版时PostgreSQL生态的插件、管控等都可以直接拿来使用, 例如patroni HA软件.

PS: 需要注意, 如果使用纯粹的单机版, 将来要转换为共享存储实例就比较麻烦, 所以不是不得已还是建议使用PFS模拟本地文件系统的伪“单机版”.

PolarDB 单机版怎么部署?

b站视频链接

Youtube视频链接

如何部署PolarDB 单机版? 非常简单, 安装软件的时候, 参数不太一样, 不需要with pfs. 初始化数据库实例时, 不需要把共享数据从primary节点中剥离开来, 参数配置也略有不同.

1、启动容器


mkdir ~/pb_local    
cd ~/pb_local    

docker run -d -it -v ${PWD}:/data -P --shm-size=1g --cap-add=SYS_PTRACE --cap-add SYS_ADMIN --privileged=true --name pb_local1 registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu22.04 bash    

2、进入容器


docker exec -ti pb_local1 bash   

3、克隆PolarDB 15代码


cd /data/  
git clone --depth 1 -b POLARDB_15_STABLE https://github.com/ApsaraDB/PolarDB-for-PostgreSQL    

4、编译PolarDB软件, 注意编译参数, 不用with pfsd.


cd /data/PolarDB-for-PostgreSQL    
./build.sh --prefix=/data/polardb --jobs=4 --debug=on --port=5432 --ni   

5、将编译好的PolarDB软件拷贝到目标位置


cp -r /data/polardb/tmp_polardb_pg_15_base ~/

# 或者使用软链
ln -s /data/polardb/tmp_polardb_pg_15_base ~/

6、初始化PolarDB单机版实例


primary_dir=/data/polardb/primary_dir  
# data_dir=/data/polardb/shared_dir  # 这个目录不需要, 集群版才需要把共享数据剥离出来.    
port=5432  


# 配置环境变量 
echo"  
export PGHOME=~/tmp_polardb_pg_15_base  
export PGDATA=$primary_dir
export PGPORT=$port
"
 >> ~/.bashrc  


# 应用环境变量
. ~/.bashrc  

# 初始化PolarDB实例  
initdb -D $primary_dir -E UTF8 -k --lc-collate=C --lc-ctype=en_US.utf-8 -U postgres     

7、配置PolarDB 单机版数据库实例参数

配置 postgresql.conf


echo"  
port = $port

# from \$PGHOME/share/postgresql/polardb.conf.sample    
#------------------------------------------------------------------------------  
# Common Parameters for PostgreSQL  
#------------------------------------------------------------------------------  
full_page_writes = on  
listen_addresses = '0.0.0.0'  
logging_collector = on  
max_prepared_transactions = 10  
max_worker_processes = 128  
max_connections = 1000  
shared_buffers = 256MB  
# shared_preload_libraries ='\$libdir/polar_vfs,\$libdir/polar_io_stat,\$libdir/polar_monitor_preload,\$libdir/polar_worker'  
shared_preload_libraries ='\$libdir/polar_io_stat,\$libdir/polar_monitor_preload,\$libdir/polar_worker'

#------------------------------------------------------------------------------  
# Common Parameters for PolarDB  
#------------------------------------------------------------------------------  
polar_disk_name = 'home'  
polar_enable_shared_storage_mode = off  # 如果是集群版, 要设置为on  
# polar_vfs.localfs_mode = true  
polar_logindex_mem_size=64MB  
polar_xlog_queue_buffers=64MB  
polar_xlog_page_buffers=64MB  
polar_has_partial_write = off  # 研发同学回复: 单机版应该开 full_page_writes 就够了。polar_has_partial_write 应该是遇到半写页的时候去 retry,应该主要是给 RO 用的。
# 如果你的文件系统是带cow功能的, 就不需要开启full page write. 例如zfs.  

# if use local_fs 模拟共享存储 , uncomment it    
# polar_datadir = 'file-dio://\$data_dir'    

# if cgroup 报错, 参考:  ../202412/20241216_03.md     
# WARNING:  Failed to get the instance memory usage    
polar_resource_manager.enable_resource_manager = off      
"
 >> $primary_dir/postgresql.conf  

配置 pg_hba.conf


echo"host all all 0.0.0.0/0 md5" >> $primary_dir/pg_hba.conf  

如果是集群版, 我们就需要把共享存储从计算节点剥离出来, 详见polar-initdb.sh脚本内容.


# if use local_fs 模拟共享存储, 使用本地文件系统模拟共享存储, 则使用如下方法:   
# mkdir -p $data_dir   
# $base_dir/bin/polar-initdb.sh $primary_dir/ $data_dir/ primary localfs  

启动PolarDB单机版


pg_ctl start  

查看PolarDB单机版


$ psql  
psql (PostgreSQL 15.10 (PolarDB 15.10.3.0 build bbc102d8 debug) on aarch64-linux-gnu)  
Type "help"forhelp.  

postgres=# \l  
                                                 List of databases  
     Name      |  Owner   | Encoding | Collate |    Ctype    | ICU Locale | Locale Provider |   Access privileges     
---------------+----------+----------+---------+-------------+------------+-----------------+-----------------------  
 polardb_admin | postgres | UTF8     | C       | en_US.utf-8 |            | libc            | =T/postgres          +  
               |          |          |         |             |            |                 | postgres=CTc/postgres  
 postgres      | postgres | UTF8     | C       | en_US.utf-8 |            | libc            |   
 template0     | postgres | UTF8     | C       | en_US.utf-8 |            | libc            | =c/postgres          +  
               |          |          |         |             |            |                 | postgres=CTc/postgres  
 template1     | postgres | UTF8     | C       | en_US.utf-8 |            | libc            | =c/postgres          +  
               |          |          |         |             |            |                 | postgres=CTc/postgres  
(4 rows)  
postgres=# select version();  
                                    version                                       
--------------------------------------------------------------------------------  
 PostgreSQL 15.10 (PolarDB 15.10.3.0 build bbc102d8 debug) on aarch64-linux-gnu  
(1 row)

postgres=# show polar_vfs.localfs_mode;
 polar_vfs.localfs_mode 
------------------------
 off
(1 row)

postgres=# show polar_datadir;
 polar_datadir 
---------------

(1 row)

创建表空间测试:


$ mkdir /data/polardb/tbs1

$ psql
psql (PostgreSQL 15.10 (PolarDB 15.10.3.0 build bbc102d8 debug) on aarch64-linux-gnu)
Type "help"forhelp.

postgres=# create tablespace tbs1 location '/data/polardb/tbs1';
CREATE TABLESPACE
postgres=# create table test (id int) tablespace tbs1;
CREATE TABLE
postgres=# insert into test select generate_series(1,100);
INSERT 0 100
postgres=# \dt
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | test | table | postgres
(1 row)

postgres=# \d test
                Table "public.test"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 id     | integer |           |          | 
Tablespace: "tbs1"

参考

PolarDB-for-PostgreSQL/build.sh

/home/postgres/tmp_polardb_pg_15_base/bin/polar-initdb.sh

《穷鬼玩PolarDB RAC一写多读集群系列 | 在Docker容器中用loop设备模拟共享存储》

《穷鬼玩PolarDB RAC一写多读集群系列 | 如何搭建PolarDB容灾(Standby)节点》

《穷鬼玩PolarDB RAC一写多读集群系列 | 共享存储在线扩容》

《穷鬼玩PolarDB RAC一写多读集群系列 | 计算节点 Switchover》

《穷鬼玩PolarDB RAC一写多读集群系列 | 在线备份》

《穷鬼玩PolarDB RAC一写多读集群系列 | 在线归档》

《穷鬼玩PolarDB RAC一写多读集群系列 | 实时归档》

《穷鬼玩PolarDB RAC一写多读集群系列 | 时间点恢复(PITR)》

《穷鬼玩PolarDB RAC一写多读集群系列 | 读写分离》

《穷鬼玩PolarDB RAC一写多读集群系列 | 主机全毁, 只剩共享存储的PolarDB还有救吗?》

《穷鬼玩PolarDB RAC一写多读集群系列 | 激活容灾(Standby)节点》

《穷鬼玩PolarDB RAC一写多读集群系列 | 将“共享存储实例”转换为“本地存储实例”》

《穷鬼玩PolarDB RAC一写多读集群系列 | 将“本地存储实例”转换为“共享存储实例”》

《穷鬼玩PolarDB RAC一写多读集群系列 | 升级vector插件》

《穷鬼玩PolarDB RAC一写多读集群系列 | 使用图数据库插件AGE》

《穷鬼玩PolarDB RAC一写多读集群系列 | 接入私有化大模型服务》

《穷鬼玩PolarDB RAC一写多读集群系列 | 接入PostGIS插件全功能》

《穷鬼玩PolarDB RAC一写多读集群系列 | 接入pg_duckdb支持数据湖功能,且OLAP性能数量级提升》

《穷鬼玩PolarDB RAC一写多读集群系列 | pg_bulkload适配PFS加速批量导入》