GaussDB/postgresql/opengauss中双冒号的含义

---------- start ----------

原因是我想查看pg_tables的表定义,发现其中有一些双冒号符,搜查了一些资料,特此解释

hcie=# \d+ pg_tables
View "pg_catalog.pg_tables"
Column | Type | Modifiers | Storage | Description
---------------+--------------------------+-----------+---------+-------------
schemaname | name | | plain |
tablename | name | | plain |
tableowner | name | | plain |
tablespace | name | | plain |
hasindexes | boolean | | plain |
hasrules | boolean | | plain |
hastriggers | boolean | | plain |
tablecreator | name | | plain |
created | timestamp with time zone | | plain |
last_ddl_time | timestamp with time zone | | plain |
View definition:
SELECT n.nspname AS schemaname, c.relname AS tablename,
pg_get_userbyid(c.relowner) AS tableowner, t.spcname AS tablespace,
c.relhasindex AS hasindexes, c.relhasrules AS hasrules,
c.relhastriggers AS hastriggers,
CASE
WHEN pg_check_authid(po.creator) THEN pg_get_userbyid(po.creator)
ELSE NULL::name
END AS tablecreator,
po.ctime AS created, po.mtime AS last_ddl_time
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
LEFT JOIN pg_object po ON po.object_oid = c.oid AND po.object_type = 'r'::"char"
WHERE c.relkind = 'r'::"char";

双冒号语法(::)是GaussDB中的显示类型转换的符号,相当于CAST函数

语法格式为:

expression::data_type
-- 类似于CAST函数的语法
SELECT CAST(expression AS data_type);

不过官方并不建议使用双冒号符来作强制类型转换,GaussDB中使用双冒号将函数入参转换为期望类型可能导致结果超出预期

双冒号符使用示例

--- 不加任何修饰符的数值
hcie=# select 1;
?column?
----------
1
(1 row)
--- 加上双冒号转换符
hcie=# select 1::int;
int4
------
1
(1 row)
hcie=# select 1::integer;
int4
------
1
hcie=# SELECT '2024-01-02'::date;
timestamp
---------------------
2024-01-02 00:00:00
(1 row)
hcie=# SELECT '01-OCT-2024'::date;
timestamp
---------------------
2024-10-01 00:00:00
(1 row)

posted on   yq1DB  阅读(16)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示