-
substring_index(string, delim, count) 描述:按照区分大小写匹配查找delimiter分隔符,返回string字符串中第count次出现delim分隔符之前的子串。如果count为负数,则从末尾向前查找delim分隔符。如果参数含有NULL值,返回NULL。该函数仅8.2.0及以上集群版本支持。 返回值类型:text 示例:按照区分大小写匹配查找delimiter分隔符".wWw.",返回string字符串"www.wWw.cloud.wWw.com"中第2次出现delimiter分隔符之前的子串"www.wWw.cloud"。 1
2
3
4
5 SELECT SUBSTRING_INDEX('www.wWw.cloud.wWw.com', '.wWw.', 2) AS RESULT;
result
---------------
www.wWw.cloud
(1 row)
-
btrim(string bytea,bytes bytea) 描述:从string的开头和结尾删除只包含bytes中字节的最长的字符串。 返回值类型:bytea 示例: 1
2
3
4
5 SELECT btrim(E'\\000trim\\000'::bytea, E'\\000'::bytea) AS RESULT;
result
------------
\x7472696d
(1 row)
-
substring(string [from int] [for int]) 描述:截取子串。 返回值类型:bytea 示例: 1
2
3
4
5 SELECT substring(E'Th\\000omas'::bytea from 2 for 3) AS RESULT;
result
----------
\x68006f
(1 row)
截取时间,获取小时数: 1
2
3
4
5 SELECT substring('2022-07-18 24:38:15',12,2)AS RESULT;
result
-----------
24
(1 row)
-
trim([both] bytes from string) 描述:从string的开头和结尾删除只包含bytes中字节的最长字符串。 返回值类型:bytea 示例: 1
2
3
4
5 SELECT trim(E'\\000'::bytea from E'\\000Tom\\000'::bytea) AS RESULT;
result
----------
\x546f6d
(1 row)
-
overlay(string placing string from int [for int]) 描述:替换子串。 返回值类型:bytea 示例: 1
2
3
4
5 SELECT overlay(E'Th\\000omas'::bytea placing E'\\002\\003'::bytea from 2 for 3) AS RESULT;
result
----------------
\x5402036d6173
(1 row)
-
应用示例 查询当前节点上所有MemoryContext的使用情况。 根据sessid定位到该MemoryContext是在哪个线程中创建和使用的,依据totalsize,freesize及usedsize来确认内存的使用情况是否符合预期,预先判断是否可能存在内存泄露的风险。 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 SELECT * FROM PV_SESSION_MEMORY_DETAIL order by totalsize desc;
sessid | sesstype | contextname | level | parent | totalsize | freesize | usedsize
----------------------------+-------------------------+---------------------------------------------+-------+------------------------------+-----------+----------+----------
0.139975915622720 | postmaster | gs_signal | 1 | TopMemoryContext | 17209904 | 8081136 | 9128768
1667462258.139973631031040 | postgres | SRF multi-call context | 5 | FunctionScan_139973631031040 | 1725504 | 3168 | 1722336
1667461280.139973666686720 | postgres | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 284456 | 1188088
1667450443.139973877479168 | postgres | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 356088 | 1116456
1667462258.139973631031040 | postgres | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 128216 | 1344328
1667461250.139973915236096 | postgres | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 226352 | 1246192
1667450439.139974010144512 | WLMarbiter | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 386736 | 1085808
1667450439.139974151726848 | WDRSnapshot | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 159720 | 1312824
1667450439.139974026925824 | WLMmonitor | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 297976 | 1174568
1667451036.139973746386688 | postgres | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 208064 | 1264480
1667461250.139973950891776 | postgres | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 270016 | 1202528
1667450439.139974076212992 | WLMCalSpaceInfo | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 393952 | 1078592
1667450439.139974092994304 | WLMCollectWorker | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 94848 | 1377696
1667461254.139973971343104 | postgres | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 338544 | 1134000
1667461280.139973822945024 | postgres | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 284456 | 1188088
1667450439.139974202070784 | JobScheduler | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 216728 | 1255816
1667450454.139973860697856 | postgres | CacheMemoryContext | 1 | TopMemoryContext | 1472544 | 388384 | 1084160
0.139975915622720 | postmaster | Postmaster | 1 | TopMemoryContext | 1004288 | 88792 | 915496
1667450439.139974218852096 | AutoVacLauncher | CacheMemoryContext | 1 | TopMemoryContext | 948256 | 183488 | 764768
1667461250.139973915236096 | postgres | TempSmallContextGroup | 0 | | 584448 | 148032 | 119
1667462258.139973631031040 | postgres | TempSmallContextGroup | 0 | | 579712 | 162128 | 123
-
时间/日期操作符 用户在使用时间和日期操作符时,对应的操作数请使用明确的类型前缀修饰,以确保数据库在解析操作数的时候能够与用户预期一致,不会产生用户非预期的结果。 比如下面示例没有明确数据类型就会出现异常错误。 1 SELECT date '2001-10-01' - '7' AS RESULT;
表1 时间和日期操作符 操作符 示例 + date类型参数与integer参数相加,获取时间间隔为7天后的时间: 1
2
3
4
5 SELECT date '2001-09-28' + integer '7' AS RESULT;
result
---------------------
2001-10-05
(1 row)
date类型参数与interval参数相加,获取时间间隔为1小时后的时间: 1
2
3
4
5 SELECT date '2001-09-28' + interval '1 hour' AS RESULT;
result
---------------------
2001-09-28 01:00:00
(1 row)
date类型参数与interval参数相加,获取时间间隔为1个月的时间: date函数对于日期相加减超过月份的日期范围,会对齐到对应月份最后一天,不超过则不处理。 1
2
3
4
5 SELECT date '2021-01-31' + interval '1 month' AS RESULT;
result
---------------------
2021-02-28 00:00:00
(1 row)
1
2
3
4
5 SELECT date '2021-02-28' + interval '1 month' AS RESULT;
result
---------------------
2021-03-28 00:00:00
(1 row)
date类型参数与time类型参数相加,获取具体的日期和时间结果: 1
2
3
4
5 SELECT date '2001-09-28' + time '03:00' AS RESULT;
result
---------------------
2001-09-28 03:00:00
(1 row)
interval参数相加,获取两个时间间隔之和: 1
2
3
4
5 SELECT interval '1 day' + interval '1 hour' AS RESULT;
result
----------------
1 day 01:00:00
(1 row)
timestamp时间类型参数与interval参数相加,获取间隔23小时后的时间: 1
2
3
4
5 SELECT timestamp '2001-09-28 01:00' + interval '23 hours' AS RESULT;
result
---------------------
2001-09-29 00:00:00
(1 row)
time类型参数与interval参数相加,获取间隔时间为3小时后的时间: 1
2
3
4
5 SELECT time '01:00' + interval '3 hours' AS RESULT;
result
----------
04:00:00
(1 row)
- date类型参数相减,获取两个日期的时间差: 1
2
3
4
5 SELECT date '2001-10-01' - date '2001-09-28' AS RESULT;
result
--------
3
(1 row)
date类型参数与integer参数相减,返回timestamp类型,获取两者的时间差: 1
2
3
4
5 SELECT date '2001-10-01' - integer '7' AS RESULT;
result
---------------------
2001-09-24
(1 row)
date类型参数与interval参数相减,获取两者的日期、时间差: 1
2
3
4
5 SELECT date '2001-09-28' - interval '1 hour' AS RESULT;
result
---------------------
2001-09-27 23:00:00
(1 row)
time类型参数相减,获取两参数的时间差: 1
2
3
4
5 SELECT time '05:00' - time '03:00' AS RESULT;
result
----------
02:00:00
(1 row)
time类型参数与interval相减,获取两参数的时间差: 1
2
3
4
5 SELECT time '05:00' - interval '2 hours' AS RESULT;
result
----------
03:00:00
(1 row)
timestamp类型参数与interval相减,从时间戳中减去时间间隔,获取两者的日期时间差: 1
2
3
4
5 SELECT timestamp '2001-09-28 23:00' - interval '23 hours' AS RESULT;
result
---------------------
2001-09-28 00:00:00
(1 row)
interval参数相减,获取两者的时间差: 1
2
3
4
5 SELECT interval '1 day' - interval '1 hour' AS RESULT;
result
----------
23:00:00
(1 row)
timestamp类型参数相减,获取两者的日期时间差: 1
2
3
4
5 SELECT timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00' AS RESULT;
result
----------------
1 day 15:00:00
(1 row)
获取当前日期的前一天: 1
2
3
4
5 select now() - interval '1 day'AS RESULT;
result
-------------------------------
2022-08-08 01:46:15.555406+00
(1 row)
* 将时间间隔乘以数量: 1
2
3
4
5 SELECT 900 * interval '1 second' AS RESULT;
result
----------
00:15:00
(1 row)
1
2
3
4
5 SELECT 21 * interval '1 day' AS RESULT;
result
---------
21 days
(1 row)
1
2
3
4
5 SELECT double precision '3.5' * interval '1 hour' AS RESULT;
result
----------
03:30:00
(1 row)
/ 用时间间隔除以数量,获取一段时间中的某一段: 1
2
3
4
5 SELECT interval '1 hour' / double precision '1.5' AS RESULT;
result
----------
00:40:00
(1 row)
父主题: 时间、日期处理函数和操作符
-
hll_union_agg(hll) 描述:将多个hll类型数据union成一个hll。 返回值类型:hll 示例: 将各组中的hll数据union成一个hll,并计算distinct值。 1
2
3
4
5 SELECT #hll_union_agg(c) as cardinality FROM t_a_c_hll;
cardinality
------------------
496.628982624022
(1 row)
-
hll_add_agg(hll_hashval, int32 log2m, int32 regwidth, int64 expthresh) 描述:把哈希后的数据按照分组放到hll中,依次指定参数log2m、regwidth、expthresh。expthresh的取值范围是-1~7之间的整数,该参数可以用来设置从Explicit模式到Sparse模式的阈值大小。-1表示自动模式,0表示跳过Explicit模式,取1~7表示在基数到达 2expthresh时切换模式。 返回值类型:hll 示例: 1
2
3
4
5 SELECT hll_cardinality(hll_add_agg(hll_hash_text(c), NULL, 1, 4)) FROM t_data;
hll_cardinality
------------------
496.628982624022
(1 row)
-
hll_add_agg(hll_hashval, int32 log2m, int32 regwidth, int64 expthresh, int32 sparseon) 描述:把哈希后的数据按照分组放到hll中,依次指定参数log2m、regwidth、expthresh、sparseon。sparseon取值范围为0或者1。 返回值类型:hll 示例: 1
2
3
4
5 SELECT hll_cardinality(hll_add_agg(hll_hash_text(c), NULL, 1, 4, 0)) FROM t_data;
hll_cardinality
------------------
496.628982624022
(1 row)
-
hll_add_agg(hll_hashval) 描述:把哈希后的数据按照分组放到hll中。 返回值类型:hll 示例: 准备数据。 1
2
3
4 CREATE EXTERNAL TABLE t_id(id int) store AS orc;
INSERT INTO t_id SELECT generate_series(1,500);
CREATE EXTERNAL TABLE t_data(a int, c text) store AS orc;
INSERT INTO t_data SELECT mod(id,2), id FROM t_id;
创建表并指定列为varchar(当前不支持 hll 列,因此需要进行显示转换)。 1 CREATE EXTERNAL TABLE t_a_c_hll(a int, c varchar) store AS orc;
根据a列group by对数据分组,把各组数据加到hll中。 1 INSERT INTO t_a_c_hll SELECT a, hll_add_agg(hll_hash_text(c)) FROM t_data GROUP BY a;
得到每组数据中hll的Distinct值。 1
2
3
4
5
6 SELECT a, #c::hll as cardinality FROM t_a_c_hll order by a;
a | cardinality
---+------------------
0 | 250.741759091658
1 | 250.741759091658
(2 rows)
-
hll_add_agg(hll_hashval, int32 log2m) 描述:把哈希后的数据按照分组放到hll中。并指定参数log2m,取值范围为10~16。 返回值类型:hll 示例: 1
2
3
4
5 SELECT hll_cardinality(hll_add_agg(hll_hash_text(c), 10)) FROM t_data;
hll_cardinality
------------------
503.932348927339
(1 row)
-
hll_add_agg(hll_hashval, int32 log2m, int32 regwidth) 描述:把哈希后的数据按照分组放到hll中。依次指定参数log2m,regwidth。 regwidth取值范围为1~5。 返回值类型:hll 示例: 1
2
3
4
5 SELECT hll_cardinality(hll_add_agg(hll_hash_text(c), NULL, 1)) FROM t_data;
hll_cardinality
------------------
496.628982624022
(1 row)
-
PG_CONSTRAINT PG_CONSTRAINT系统表存储表上的检查约束、主键、唯一约束和外键约束。 表1 PG_CONSTRAINT字段 名称 类型 描述 conname name 约束名称(不一定是唯一的)。 connamespace oid 包含约束的命名空间的OID。 contype "char" c = 检查约束 f = 外键约束 p = 主键约束 u = 唯一约束 t = 触发器约束 condeferrable boolean 该约束是否可以推迟。 condeferred boolean 缺省时该约束是否可以推迟。 convalidated boolean 约束是否有效。目前,只有外键和CHECK约束可将其设置为FALSE。 conrelid oid 该约束所在的表;如果不是表约束则为0。 contypid oid 该约束所在的域;如果不是一个域约束则为0。 conindid oid 与约束关联的索引ID。 confrelid oid 如果是外键,则为参考的表;否则为0。 confupdtype "char" 外键更新动作代码。 a = 没动作 r = 限制 c = 级联 n =设置为null d =设置为缺省 confdeltype "char" 外键删除动作代码。 a = 没动作 r = 限制 c = 级联 n =设置为null d =设置为缺省 confmatchtype "char" 外键匹配类型。 f = 全部 p = 部分 u = 简单(未指定) conislocal boolean 是否是为关系创建的本地约束。 coninhcount integer 约束直接继承父表的数目。继承父表数非零时,不能删除或重命名该约束。 connoinherit boolean 是否可以被继承。 consoft boolean 是否为信息约束(Informational Constraint)。 conopt boolean 是否使用信息约束优化执行计划。 conkey smallint[] 如果是表约束,则是约束控制的字段列表。 confkey smallint[] 如果是一个外键,则是参考的字段的列表。 conpfeqop oid[] 如果是一个外键,是做PK=FK比较的相等操作符ID的列表。 conppeqop oid[] 如果是一个外键,是做PK=PK比较的相等操作符ID的列表。 conffeqop oid[] 如果是一个外键,是做FK=FK比较的相等操作符ID的列表。 conexclop oid[] 如果是一个排他约束,是列的排他操作符ID列表。 conbin pg_node_tree 如果是检查约束,则是其表达式的内部形式。 consrc text 如果是检查约束,则是表达式的人类可读形式。 当被引用的对象改变时,consrc不能被更新。例如,它不会跟踪字段的重命名。最好还是使用pg_get_constraintdef()来抽取一个检查约束的定义,而不是依赖这个字段。 pg_class.relchecks需要和每个关系在此目录中的检查约束数量保持一致。 父主题: 系统表
-
PG_FOREIGN_SERVER PG_FOREIGN_SERVER系统表存储外部服务器定义。一个外部服务器描述了一个外部数据源,例如一个远程服务器。外部服务器通过外部数据封装器访问。 表1 PG_FOREIGN_SERVER字段 名字 类型 引用 描述 oid oid - 行标识符(隐藏属性,必须明确选择才会显示)。 srvname name - 外部服务器名。 srvowner oid PG_AUTHID.oid 外部服务器的所有者。 srvfdw oid PG_FOREIGN_DATA_WRAPPER.oid 此外部服务器的外部数据封装器的OID。 srvtype text - 服务器的类型(可选)。 srvversion text - 服务器的版本(可选)。 srvacl aclitem[] - 访问权限。 srvoptions text[] - 外部服务器指定选项,使用“keyword=value”格式的字符串。 父主题: 系统表