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 2025-02-04 18:14  yq1DB  阅读(85)  评论(0)    收藏  举报

导航