PostgreSQL码农集散地

国产数据库PolarDB公开课-2 快速体验

参考文档点击文末阅读原文打开; 推荐《最好的PostgreSQL学习镜像》;


PolarDB公开课来了, 国庆拿下这个国产信创数据库

系列文章导航

1、国庆拿下这个国产信创数据库

2、国产数据库PolarDB公开课-1 架构解读

3种方法快速体验 PolarDB

1、使用云起实验室快速体验 PolarDB

通过阿里云的云起实验, 可以永久免费的体验PolarDB PostgreSQL以及PolarDB-X开源版. 你只需一个阿里云账号, 一个web浏览器, 就可以学习、体验PolarDB数据库. 由于环境统一, 所以也特别适合教学、考试等场景, 不会带来搭建环境、环境不一致导致的各种问题.

实验室链接地址如下:

  • https://developer.aliyun.com/adc/scenario/f55dbfac77c0467a9d3cd95ff6697a31

实验须知:

  • 本实验提供了多合一的数据库实验环境,如果您想基于此实验环境体验更多的 PolarDB | PostgreSQL 的业务场景,欢迎参考《PolarDB业务场景实战》实验手册,在本实验环境中进行操作。

说明:《PolarDB业务场景实战》系列课程的核心是教怎么用好数据库,面向对象是数据库的用户、应用开发者、应用架构师、数据库厂商的产品经理、售前售后专家、高校学生等角色。

部分课程:

  • 短视频推荐去重、UV统计分析场景
  • 电商高并发秒杀业务、跨境电商高并发队列消费业务
  • 营销场景, 根据用户画像的相似度进行目标人群圈选, 实现精准营销
  • 向量搜索应用:内容推荐|监控预测|人脸|指纹识别
  • AI大模型+向量数据库, 提升AI通用机器人在专业领域的精准度

更多场景实践类实验手册,请前往:PolarDB Gitee 仓库 polardb/whudb-course 进行查看与学习。

2、使用docker镜像快速体验 PostgreSQL&PolarDB

PostgreSQL是一个非常灵活的开源数据库, 通过插件可以集成丰富的功能, 例如

  • 近似计算
  • 营销场景的标签圈选
  • 存储引擎、分析加强
  • 多值列索引扩展加速
  • 时序、图计算、机器学习、流计算等多模型业务场景
  • 时空数据计算和检索
  • 向量搜索
  • 文本场景增强
  • 数据融合, 冷热分离
  • 扩展协议, 兼容其他产品
  • 存储过程和函数语言增强
  • 安全增强
  • 数据库管理、审计、性能优化等

为了方便大家学习, 我制作了2个docker 学习镜像, 集成了200余款热门的开源插件, 免费开放给大家使用.

x86_64版本docker image:

# 拉取镜像, 第一次拉取一次即可. 或者需要的时候执行, 将更新到最新镜像版本.    
docker pull registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts    

    # 启动容器    
docker run --platform linux/amd64 -d -it -P \
  --cap-add=SYS_PTRACE --cap-add SYS_ADMIN \
  --privileged=true --name pg --shm-size=1g \
  registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts  

  ##### 如果你想学习备份恢复、修改参数等需要重启数据库实例的case, 换个启动参数, 使用参数--entrypoint将容器根进程换成bash更好. 如下:   
docker run -d -it -P --cap-add=SYS_PTRACE \
  --cap-add SYS_ADMIN --privileged=true --name pg \
  --shm-size=1g --entrypoint /bin/bash \
  registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts  
##### 以上启动方式需要进入容器后手工启动数据库实例: su - postgres; pg_ctl start;    

    # 进入容器    
docker exec -ti pg bash    

    # 连接数据库    
psql    

# 进入polardb用户启动polardb

# PolarDB 5432, su - polardb, pg_ctl start, psql

ARM64版本docker image:

# 拉取镜像, 第一次拉取一次即可. 或者需要的时候执行, 将更新到最新镜像版本.    
docker pull registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts_arm64    

    # 启动容器    
docker run -d -it -P --cap-add=SYS_PTRACE \
  --cap-add SYS_ADMIN --privileged=true --name pg \
  --shm-size=1g \
  registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts_arm64  

  ##### 如果你想学习备份恢复、修改参数等需要重启数据库实例的case, 换个启动参数, 使用参数--entrypoint将容器根进程换成bash更好. 如下:   
docker run -d -it -P --cap-add=SYS_PTRACE \
  --cap-add SYS_ADMIN --privileged=true --name pg \
  --shm-size=1g --entrypoint /bin/bash \
  registry.cn-hangzhou.aliyuncs.com/digoal/opensource_database:pg14_with_exts_arm64    
##### 以上启动方式需要进入容器后手工启动数据库实例: su - postgres; pg_ctl start;    

    # 进入容器    
docker exec -ti pg bash    

    # 连接数据库    
psql    

# 进入polardb用户启动polardb

# PolarDB 5432, su - polardb, pg_ctl start, psql

3、通过PolarDB官方开发镜像编译体验PolarDB

参考:

  • https://apsaradb.github.io/PolarDB-for-PostgreSQL/zh/development/dev-on-docker.html
  • https://apsaradb.github.io/PolarDB-for-PostgreSQL/zh/development/customize-dev-env.html

1、PolarDB开源社区提供了多种环境的Docker镜像作为开发环境供开发者选择.

支持的 CPU 架构包含:

linux/amd64(x86_64)  
linux/arm64  

支持的 Linux 发行版包含:

CentOS 7  
Anolis 8  
Rocky 8  
Rocky 9  
Ubuntu 20.04  
Ubuntu 22.04  
Ubuntu 24.04  

通过如下方式即可拉取相应发行版的镜像:

docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:centos7  
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:anolis8  
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:rocky8  
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:rocky9  
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu20.04  
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu22.04  
docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu24.04  

另外,也提供了构建上述开发镜像的 Dockerfile,您可以根据自己的需要在 Dockerfile 中添加更多依赖,然后构建自己的开发镜像。

2、搭建PolarDB开发环境, 在开发环境中通过源码编译安装PolarDB

拉取一个你熟悉的PolarDB开发环境Docker镜像, 例如

docker pull registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu22.04  

创建并运行容器

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

进入容器后,克隆PolarDB源码(根据需要选择对应的分支),编译部署 PolarDB-PG 实例。

# 进入容器
docker exec -ti polardb_pg_devel bash  

  cd /tmp   

# 例如这里拉取 POLARDB_11_STABLE 分支, 截止2024.9.24 PolarDB开源的最新分支为 POLARDB_15_STABLE  
git clone --depth 1 -b POLARDB_11_STABLE https://github.com/ApsaraDB/PolarDB-for-PostgreSQL  
cd /tmp/PolarDB-for-PostgreSQL  
./polardb_build.sh --without-fbl --debug=off  

  # 验证PolarDB-PG  
psql -c 'SELECT version();'

              version               
--------------------------------  
 PostgreSQL 11.9 (POLARDB 11.9)  
(1 row)

# 在容器内关闭、启动PolarDB数据库:   
pg_ctl stop -m fast -D ~/tmp_master_dir_polardb_pg_1100_bld     
pg_ctl start -D ~/tmp_master_dir_polardb_pg_1100_bld    

# 查看PolarDB的编译选项
pg_config

BINDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/bin
DOCDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/share/doc
HTMLDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/share/doc
INCLUDEDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/include
PKGINCLUDEDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/include
INCLUDEDIR-SERVER = /home/postgres/tmp_basedir_polardb_pg_1100_bld/include/server
LIBDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/lib
PKGLIBDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/lib
LOCALEDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/share/locale
MANDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/share/man
SHAREDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/share
SYSCONFDIR = /home/postgres/tmp_basedir_polardb_pg_1100_bld/etc
PGXS = /home/postgres/tmp_basedir_polardb_pg_1100_bld/lib/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix=/home/postgres/tmp_basedir_polardb_pg_1100_bld' '--with-pgport=5432' '--with-openssl' '--with-libxml' '--with-perl' '--with-python' '--with-tcl' '--with-pam' '--with-gssapi' '--enable-nls' '--with-libxslt' '--with-ldap' '--with-uuid=e2fs' '--with-icu' '--with-llvm' 'CFLAGS=  -g -pipe -Wall -grecord-gcc-switches -I/usr/include/et -O3' 'LDFLAGS=-Wl,-rpath,'''/../lib'''' 'CXXFLAGS=-g -pipe -Wall -grecord-gcc-switches -I/usr/include/et -O3'
CC = gcc
CPPFLAGS = -D_GNU_SOURCE -I/usr/include/libxml2
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation   -g -pipe -Wall -grecord-gcc-switches -I/usr/include/et -O3
CFLAGS_SL = -fPIC
LDFLAGS = -L ../../src/backend/polar_dma/libconsensus/polar_wrapper/lib -Wl,-rpath,'/../lib' -L/usr/lib/llvm-15/lib -Wl,--as-needed -Wl,-rpath,'/home/postgres/tmp_basedir_polardb_pg_1100_bld/lib',--enable-new-dtags
LDFLAGS_EX = 
LDFLAGS_SL = 
LIBS = -lpgcommon -lpgport -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lcrypt -lm 
VERSION = PostgreSQL 11.9
PX_VERSION_STR = PolarDB PX version 1.1

3、polardb_build.sh 构建选项说明

如无定制的需求,则可以按照下面给出的选项编译部署不同形态的 PolarDB-PG 集群并进行测试。

polardb_build.sh --help  

    This script is to be used to compile PG core source code (PG engine code without files within contrib and external)  
  It can be called with following options:  
  --basedir=<temp dir for PG installation>, specifies which dir to install PG to, note this dir would be cleaned up before being used  
  --datadir=<temp dir for databases>], specifies which dir to store database cluster, note this dir would be cleaned up before being used  
  --conf=<file path for postgresql.conf>, specifies the configure file to use  
  --user=<user to start PG>, specifies which user to run PG as  
  --port=<port to run PG on>, specifies which port to run PG on  
  --debug=[on|off], specifies whether to compile PG with debug mode (affecting gcc flags)  
  -c,--coverage, specifies whether to build PG with coverage option  
  --nc,--nocompile, prevents re-compilation, re-installation, and re-initialization  
  -t,-r,--regress, runs regression test after compilation and installation.  
  -m --minimal compile with minimal extention set  
  --withrep init the database with a hot standby replica  
  --withstandby init the database with a hot standby replica  
  --pg_bld_rep_port=<port to run PG rep on>, specifies which port to run PG replica on  
  --pg_bld_standby_port=<port to run PG standby on>, specifies which port to run PG standby on  
  --repdir=<temp dir for databases>], specifies which dir to store replica data, note this dir would be cleaned up before being used  
  --storage=localfs, specify storage type  
  -e,--extension, run extension test  
  --with-tde, TDE enable  
  --with-dma, DMA enable  
  --with-pfsd, PFSD enable  
  --fault-injector, faultinjector enable  
  --without-fbl, run without flashback log  
  --extra-conf, add an extra conf file  

    Please lookup the following secion to find the default values for above options.  

    Typical command patterns to kick off this script:  

    1) To just cleanup, re-compile, re-install and get PG restart:  
  polardb_build.sh  
  2) To run all steps included 1), as well as run the ALL regression test cases:  
  polardb_build.sh -t  
  3) To cleanup and re-compile with code coverage option:  
  polardb_build.sh -c  
  4) To run the tests besides 3).  
  polardb_build.sh -c -t  
  5) To run with specific port, user, and/or configuration file  
  polardb_build.sh --port=5501 --user=pg001 --conf=/root/data/postgresql.conf  
  6) To run on local pfs  
  polardb_build.sh --storage=localfs  
  7) To run with a replica (it also works with --storage=localfs)  
  polardb_build.sh --withrep  
  8) To run with a standby (it also works with --storage=localfs)  
  polardb_build.sh --withstandby  
  9) To run all the tests (make check)(include src/test,src/pl,src/interfaces/ecpg,contrib,external)  
  polardb_build.sh -r-check-all  
  10) To run all the tests (make installcheck)(include src/test,src/pl,src/interfaces/ecpg,contrib,external)  
  polardb_build.sh -r-installcheck-all  

4、构建一写多读的PolarDB集群, 可以测试ePQ MPP优化器功能.

在一个新的容器中测试

# 创新容器, 名为polardb_pg_devel_epq  
docker run -d -it -P --shm-size=1g --cap-add=SYS_PTRACE --cap-add SYS_ADMIN --privileged=true --name polardb_pg_devel_epq registry.cn-hangzhou.aliyuncs.com/polardb_pg/polardb_pg_devel:ubuntu22.04 bash

# 进入容器
docker exec -ti polardb_pg_devel_epq bash

  # 下载PolarDB代码  
cd /tmp  
git clone --depth 1 -b POLARDB_11_STABLE https://github.com/ApsaraDB/PolarDB-for-PostgreSQL    
cd /tmp/PolarDB-for-PostgreSQL    

  # 编译PolarDB, 并初始化一写多读的PolarDB集群  
./polardb_build.sh --without-fbl --debug=off --withrep --initpx --storage=localfs  

      # 检查集群状态  
psql  

  psql (11.9)  
Type "help" for help.  

  postgres=# select * from pg_stat_replication ;  
  pid  | usesysid | usename  | application_name | client_addr | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_lsn  | write_lsn | flush_lsn | replay_lsn   
| write_lag | flush_lag | replay_lag | sync_priority | sync_state   
-------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+-----------+-----------+-----------+------------  
+-----------+-----------+------------+---------------+------------  
 19713 |       10 | postgres | replica2         | 127.0.0.1   |                 |       45658 | 2024-09-25 10:16:17.738143+08 |              | streaming | 0/174FE60 | 0/174FE60 | 0/174FE60 | 0/174FE60    
|           |           |            |             0 | async  
 19334 |       10 | postgres | replica1         | 127.0.0.1   |                 |       59062 | 2024-09-25 10:16:15.719794+08 |              | streaming | 0/174FE60 | 0/174FE60 | 0/174FE60 | 0/174FE60    
|           |           |            |             1 | sync  
(2 rows)  

测试epq, 参考如下文章“PolarDB/PostgreSQL TPCH测试”章节:

  • 《开源PolarDB|PostgreSQL 应用开发者&DBA 公开课 - 5.7 PolarDB开源版本必学特性 - PolarDB 应用实践实验》

常见错误排查

如果在编译并启动PolarDB集群时报内存不足的错误, 可能是docker desktop的内存资源限制太少了, 可以修改一下(修改配置后需要重启docker daemon. 例如Linux: systemctl restart docker). 例如 16G内存的Mac我配置了limit 8G内存和4G swap. 配置请参考docker手册:

  • https://docs.docker.com/desktop/settings/

图形界面配置路径: Settings-Resources-Advanced-Memory/Swap

或者直接修改docker settings.json file at:

Mac: ~/Library/"Group Containers"/group.com.docker/settings.json
Windows: C:\Users\[USERNAME]\AppData\Roaming\Docker\settings.json
Linux: ~/.docker/desktop/settings.json

彩蛋0:PolarDB 各个版本傻傻分不清???

信创名单查询:

  • http://www.itsec.gov.cn/aqkkcp/cpgg/202312/t20231226_162074.html

PolarDB 各个版本的介绍 (线下软件订阅版、云服务版、开源版):

  • https://www.aliyun.com/activity/database/polardb-v2

PolarDB 线下软件订阅版 询价:

  • https://market.aliyun.com/products/56024006/cmfw00066651.html

PolarDB 大使活动(分销赢奖励):

  • https://www.aliyun.com/activity/new/polardb-yunparter

彩蛋1:数据库生态工具 & 信创开源DB

用好周边工具, 数据库管理水平战胜90%老司机

1、管控软件

云猿生开源的kubeblocks, 如果你要管理很多套并且种类很多的数据库产品, 推荐选择.

  • https://github.com/apecloud/kubeblocks

乘数开源的clup, 专门用来管理PostgreSQL和PolarDB的集群管理软件, 如果你要管理很多套数据库, 推荐选择. 并且clup还提供了企业版、自研的连接池、分布式存储、一体机、备份平台等, 企业可以关注一下.

  • https://www.csudata.com/

若航开源的pigsty, 集成了300多个PG插件的PG集群和PolarDB集群管理软件, 如果你要管理很多套数据库, 并且对插件有特别多的需求, 推荐选择.

  • https://pigsty.cc/zh/

2、审计监控诊断优化

海信聚好看的 DBdoctor, 采用ebpf技术, 在对数据库几乎没有影响的情况下实时监控数据库和服务器的各项指标, 发现和诊断问题根因非常方便.

  • https://www.dbdoctor.cn/

Bytebase 的目标非常远大, 是位于您和数据库之间的中间件。它是数据库 DevOps 的 GitLab/GitHub,专为开发人员、DBA 和平台工程师打造。

  • https://bytebase.cc/docs/introduction/what-is-bytebase/

D-Smart, Oracle老前辈白老大他们搞的, 专注企业级市场, 将业界顶级DBA经验的产品化作品, 产品功能包括数据库监控、诊断、优化等.

  • https://www.modb.pro/db/567140

3、数据同步&迁移&备份恢复

NineData, 老领导出去创业做的产品, 产品涵盖了数据同步、迁移、备份、比对、devops、chatDBA等.

  • https://www.ninedata.cloud/home

DSG, 非常老牌的数据库同步迁移企业级产品, 支持各种数据库的异构和同构迁移, 用他们的话说, 没有dsg搞不定的迁移, 比goldengate还牛.

  • https://www.dsgdata.com/

通过信创并且开源的数据库:

PolarDB for PostgreSQL

  • https://github.com/ApsaraDB/PolarDB-for-PostgreSQL

以下PG系国产数据库也非常值得关注: HaloDB(基于PG兼容PostgreSQL、Oracle、MySQL. http://www.halodbtech.com/ )、IvorySQL(基于开源PG兼容PG、Oracle. https://www.ivorysql.org/zh-cn/ )、ProtonBase(云原生分布式数仓. https://protonbase.com/ ).

参考文档点击阅读原文获得


感谢关注我的github (https://github.com/digoal/blog) 及视频号:

Image

彩蛋2:全国大学生数据库创新设计赛  点击报名