本文将从日常使用的角度对比学习国产数据库openGauss/MogDB与开源数据库PostgreSQL之间的差异点。
smartquitafterallclientshavedisconnectedfastquitdirectly,withpropershutdown(default)immediatequitwithoutcompleteshutdown;willleadtorecoveryonrestart
fastquitdirectly,withpropershutdownimmediatequitwithoutcompleteshutdown;willleadtorecoveryonrestart
smart模式下,数据库会等待所有的客户端链接主动断开连接,此时数据库服务不允许接受新连接请求,但允许现有连接继续操作,直到所有连接都主动退出之后,数据库主进程才会发送SIGUSR2信号给checkpointer进程去执行shutdowncheckpoint类型的检查点。这样所有客户端连接提交的数据都已刷盘,下次PG启动时不需要做crash恢复。
fast模式是默认关闭方式,这种模式下数据库会通过发送SIGTERM信息来立刻终止所有打开的事务,同时也不允许接受新的连接请求。终止客户端连接之后也会发起shutdowncheckpoint类型的检查点来避免下次启动服务时做restartpoint。这种模式也是生产中推荐的使用方式,会终止当前的客户端连接。
上述三种模式,smart其实极少使用,生产通常也是使用fast模式,immediate模式只有在紧急情况下才会考虑。
例如查看事务日志点位信息
归档开关参数都是archive_mode,但是归档命令参数有区别。
archive_command='cp%p/opt/archive/%f'pg里面是配置archive_command参数,可以配置shell命令。
archive_dest='/opt/archive'MogDB是配置archive_dest参数,直接配置归档路径即可。
pg里面创建的用户默认是具有public模式的使用及创建权限,owner用户可以管理自己的对象。
postgres=>select*frompg_namespacewherenspname='public';oid|nspname|nspowner|nspacl------+---------+----------+-------------------------------------2200|public|10|{postgres=UC/postgres,=UC/postgres}(1row)上面从schema元数据列nspacl也可以看到默认是具有UC权限(U:usage,Ccreate)
gs_initdb初始化时使用-S,--security后,用户默认将不具有public模式的使用权限。
openGauss=#selectnspaclfrompg_namespacewherenspname='public';nspacl--------------{omm=UC/omm}(1row)上面从schema元数据列nspacl也可以看到默认不具有模式的使用权限。
下面进行测试,可以看到有报错提示
用户口令加密的参数名称有区别,同时sha256算法有不同的实现,互相不兼容。
password_encryption='scram-sha-256'pg里面是配置password_encryption参数,可以配置md5或者scram-sha-256。
password_encryption_type=1MogDB是配置password_encryption_type参数,0只支持标准的md5口令存储,与PG保持兼容,1支持md5和国密的sha256,2只支持国密sha256,不兼容PG。
copy的分隔符:
copy的容错机制
表结构
CREATETEMPTABLEfoo(abigint,btext);数据文件tab.dat
1 one2 3 three 111four45 five普通的copy导入,因为有多余的数据项会报错
omm=#copyfoofrom'/home/omm/tab.dat';ERROR:extradataafterlastexpectedcolumnCONTEXT:COPYfoo,line3:"3 three 111"copy使用ignore_extra_data忽略多余的列
omm=#copyfoofrom'/home/omm/tab.dat'with(ignore_extra_data);ERROR:invalidinputsyntaxfortypebigint:"four4"CONTEXT:COPYfoo,line4,columna:"four4"copy使用log_errors和reject_limit
omm=#copyfoofrom'/home/omm/tab.dat'with(ignore_extra_data,log_errors,reject_limit'unlimited');COPY4reject_limit参数对copyfrom的容错机制设置数值上限,一旦错误数据超过选项指定条数,则会按照原有机制报错。取值范围:正整数(1-INTMAX),‘unlimited’(无最大值限制)此时已经导入成功了4条数据,可以从下面的查询看出。
omm=#select*fromfoo;a|b---+-------1|one2|3|three5|five(4rows)还有一条报错的信息记录在系统自动创建的pgxc_copy_error_log表中。
omm=#\xExpandeddisplayison.omm=#select*frompgxc_copy_error_log;-[RECORD1]-------------------------------------------------relname|pg_temp_gaussdb_8_1_140368156813056.foobegintime|2021-06-2711:29:00.529073+00filename|/home/omm/tab.datlineno|4rawrecord|detail|invalidinputsyntaxfortypebigint:"four4"上面rawrecord是没有内容的,使用log_errors_data替代log_errors会记录rawrecord
truncatetablepgxc_copy_error_log;truncatetablefoo;copyfoofrom'/home/omm/tab.dat'with(ignore_extra_data,log_errors_data,reject_limit'unlimited');omm=#select*frompgxc_copy_error_log;-[RECORD1]-------------------------------------------------relname|pg_temp_gaussdb_8_1_140368156813056.foobegintime|2021-06-2711:34:34.104653+00filename|/home/omm/tab.datlineno|4rawrecord|four4detail|invalidinputsyntaxfortypebigint:"four4"9.Upsert(insertorupdate)PostgreSQLcreatetabletest_upsert(idintprimarykey,infovarchar);postgres=#insertintotest_upsert(id,info)postgres-#values(1,'aaa');INSERT01下面插入主键重复的数据
postgres=#insertintotest_upsert(id,info)postgres-#values(1,'bbb');ERROR:duplicatekeyvalueviolatesuniqueconstraint"test_upsert_pkey"DETAIL:Key(id)=(1)alreadyexists.不使用upsert语法,会报错返回给客户端,不太友好
postgres=#insertintotest_upsert(id,info)postgres-#values(1,'bbb')postgres-#onconflict(id)doupdatesetinfo=excluded.info;INSERT01postgres=#select*fromtest_upsert;id|info----+------1|bbb(1row)使用upsert语法,有冲突时可选择donothing跳过或者doupdate覆盖更新