citus中DDL操作规范有哪些
本篇内容主要讲解“citus中DDL操作规范有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“citus中DDL操作规范有哪些”吧!
背景citus是PG的一个插件,插件主要针对普通SQL(非UTILITY)加HOOK进行了一些ROUTE处理,同时使用UDF对表进行新建分区的操作。
如果用户如果要执行DDL,那么CITUS无法接管,应该如何操作呢?
分两种情况,一种需要在所有节点(CN以及WORKER)执行,还有一些只需要在CN节点执行。
需要在所有节点(CN以及WORKER)执行的DDL由于这些DDL在CN节点执行时,不会自动在WORKER执行,所以需要在所有节点执行。
常用的DDL包括:
1、新建用户
Command:CREATEROLEDescription:defineanewdatabaseroleSyntax:CREATEROLEname[[WITH]option[...]]whereoptioncanbe:SUPERUSER|NOSUPERUSER|CREATEDB|NOCREATEDB|CREATEROLE|NOCREATEROLE|INHERIT|NOINHERIT|LOGIN|NOLOGIN|REPLICATION|NOREPLICATION|BYPASSRLS|NOBYPASSRLS|CONNECTIONLIMITconnlimit|[ENCRYPTED]PASSWORD'password'|VALIDUNTIL'timestamp'|INROLErole_name[,...]|INGROUProle_name[,...]|ROLErole_name[,...]|ADMINrole_name[,...]|USERrole_name[,...]|SYSIDuid
2、新建数据库
Command:CREATEDATABASEDescription:createanewdatabaseSyntax:CREATEDATABASEname[[WITH][OWNER[=]user_name][TEMPLATE[=]template][ENCODING[=]encoding][LC_COLLATE[=]lc_collate][LC_CTYPE[=]lc_ctype][TABLESPACE[=]tablespace_name][ALLOW_CONNECTIONS[=]allowconn][CONNECTIONLIMIT[=]connlimit][IS_TEMPLATE[=]istemplate]]
所有节点新建数据库后一定不要忘记:
2.1、在所有节点新增的DB内添加citus插件
createextensioncitus;
2.2、仅在CN节点,连到新建数据库里面添加worker节点。(千万不要忘记,每新建一个DB,都需要重复做。所以可以把它做到模板库里面,新建DB时,使用模板新建。)
例如
su-postgres-c"psql-c\"SELECT*frommaster_add_node('xxx.xxx.xxx.224',1921);\""su-postgres-c"psql-c\"SELECT*frommaster_add_node('xxx.xxx.xxx.230',1921);\""su-postgres-c"psql-c\"SELECT*frommaster_add_node('xxx.xxx.xxx.231',1921);\""su-postgres-c"psql-c\"SELECT*frommaster_add_node('xxx.xxx.xxx.225',1921);\""su-postgres-c"psql-c\"SELECT*frommaster_add_node('xxx.xxx.xxx.227',1921);\""su-postgres-c"psql-c\"SELECT*frommaster_add_node('xxx.xxx.xxx.232',1921);\""su-postgres-c"psql-c\"SELECT*frommaster_add_node('xxx.xxx.xxx.226',1921);\""su-postgres-c"psql-c\"SELECT*frommaster_add_node('xxx.xxx.xxx.229',1921);\""postgres=#SELECT*FROMmaster_get_active_worker_nodes();node_name|node_port----------------+-----------xxx.xxx.xxx.227|1921xxx.xxx.xxx.229|1921xxx.xxx.xxx.231|1921xxx.xxx.xxx.225|1921xxx.xxx.xxx.224|1921xxx.xxx.xxx.226|1921xxx.xxx.xxx.230|1921xxx.xxx.xxx.232|1921(8rows)
2.3、如果使用了MX功能,还需要添加一遍。
参考
《PostgreSQL sharding : citus 系列1 - 多机部署(含OLTP(TPC-B)测试)》
###CNMX:OLTP读、写能力扩展
3、新建schema
Command:CREATESCHEMADescription:defineanewschemaSyntax:CREATESCHEMAschema_name[AUTHORIZATIONrole_specification][schema_element[...]]CREATESCHEMAAUTHORIZATIONrole_specification[schema_element[...]]CREATESCHEMAIFNOTEXISTSschema_name[AUTHORIZATIONrole_specification]CREATESCHEMAIFNOTEXISTSAUTHORIZATIONrole_specificationwhererole_specificationcanbe:user_name|CURRENT_USER|SESSION_USER
4、新建函数
自定义函数Command:CREATEFUNCTIONDescription:defineanewfunctionSyntax:CREATE[ORREPLACE]FUNCTIONname([[argmode][argname]argtype[{DEFAULT|=}default_expr][,...]])[RETURNSrettype|RETURNSTABLE(column_namecolumn_type[,...])]{LANGUAGElang_name|TRANSFORM{FORTYPEtype_name}[,...]|WINDOW|IMMUTABLE|STABLE|VOLATILE|[NOT]LEAKPROOF|CALLEDONNULLINPUT|RETURNSNULLONNULLINPUT|STRICT|[EXTERNAL]SECURITYINVOKER|[EXTERNAL]SECURITYDEFINER|PARALLEL{UNSAFE|RESTRICTED|SAFE}|COSTexecution_cost|ROWSresult_rows|SETconfiguration_parameter{TOvalue|=value|FROMCURRENT}|AS'definition'|AS'obj_file','link_symbol'}...[WITH(attribute[,...])]
5、新建操作符
自定义操作符
6、新建类型
自定义类型
7、新建插件
Command:CREATEEXTENSIONDescription:installanextensionSyntax:CREATEEXTENSION[IFNOTEXISTS]extension_name[WITH][SCHEMAschema_name][VERSIONversion][FROMold_version][CASCADE]
以上为常用的DDL,需要在所有节点执行。
仅需要在CN节点执行的DDL1、新建表,调用citus函数创建分片。
例子
createtabletest(idintprimarykey,infotext);selectcreate_distributed_table('test','id');
2、新建视图
Command:CREATEVIEWDescription:defineanewviewSyntax:CREATE[ORREPLACE][TEMP|TEMPORARY][RECURSIVE]VIEWname[(column_name[,...])][WITH(view_option_name[=view_option_value][,...])]ASquery[WITH[CASCADED|LOCAL]CHECKOPTION]
视图仅需在CN节点操作,因为SQL最后会解析成视图里面的内容。例如
createviewv2asselect*frompgbench_accountswhereaid=1;createviewv3asselect*fromv2;postgres=#explainverboseselect*fromv2;QUERYPLAN-------------------------------------------------------------------------------------------------------------------------------------------------CustomScan(CitusRouter)(cost=0.00..0.00rows=0width=0)Output:remote_scan.aid,remote_scan.bid,remote_scan.abalance,remote_scan.fillerTaskCount:1TasksShown:All->TaskNode:host=172.24.211.232port=1921dbname=postgres->IndexScanusingpgbench_accounts_pkey_106819onpublic.pgbench_accounts_106819pgbench_accounts(cost=0.28..2.50rows=1width=97)Output:pgbench_accounts.aid,pgbench_accounts.bid,pgbench_accounts.abalance,pgbench_accounts.fillerIndexCond:(pgbench_accounts.aid=1)(9rows)postgres=#explainverboseselect*fromv3;QUERYPLAN-------------------------------------------------------------------------------------------------------------------------------------------------CustomScan(CitusRouter)(cost=0.00..0.00rows=0width=0)Output:remote_scan.aid,remote_scan.bid,remote_scan.abalance,remote_scan.fillerTaskCount:1TasksShown:All->TaskNode:host=172.24.211.232port=1921dbname=postgres->IndexScanusingpgbench_accounts_pkey_106819onpublic.pgbench_accounts_106819pgbench_accounts(cost=0.28..2.50rows=1width=97)Output:pgbench_accounts.aid,pgbench_accounts.bid,pgbench_accounts.abalance,pgbench_accounts.fillerIndexCond:(pgbench_accounts.aid=1)(9rows)
到此,相信大家对“citus中DDL操作规范有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。