PostgreSQL的實踐一:數據類型(二)

PostgreSQL的實踐一:數據類型(一)

數據類型

複合類型

# 創建一個複合類型person 
# 命令格式:create type xxx AS ();
testdb=# create type person AS (
testdb(# name text,
testdb(# age int4,
testdb(# sex boolean
testdb(# );
CREATE TYPE
testdb=# \d person;
            合成類型 "public.person"
 欄位 |  類型   | Collation | Nullable | Default
------+---------+-----------+----------+---------
 name | text    |           |          |
 age  | integer |           |          |
 sex  | boolean |           |          |


testdb=# \dt person;
沒有找到任何名稱爲 "person" 的關聯。
testdb=# \d person;
            合成類型 "public.person"
 欄位 |  類型   | Collation | Nullable | Default
------+---------+-----------+----------+---------
 name | text    |           |          |
 age  | integer |           |          |
 sex  | boolean |           |          |

# 創建一個帶複合類型的表author
testdb=# create table author (
testdb(# id int4,
testdb(# people person,
testdb(# book text
testdb(# );
CREATE TABLE
testdb=# \d author;
              數據表 "public.author"
  欄位  |  類型   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 id     | integer |           |          |
 people | person  |           |          |
 book   | text    |           |          |

# 插入數據
testdb=# insert into author values(1, ' ("qkl", 29, TRUE)', '張三的自傳');
INSERT 0 1
# 缺省不填寫就是NULL,逗號間不要有空格
testdb=# insert into author values(2, '("zgq", , TRUE)', '張三的自傳2');
ERROR:  invalid input syntax for integer: " "
第1行insert into author values(2, '("zgq", , TRUE)', '張三的自傳2...
                                  ^
testdb=# insert into author values(2, '("zgq",,TRUE)', '張三的自傳2');
INSERT 0 1
testdb=# insert into author values(2, '("zgq",, TRUE)', '張三的自傳2');
INSERT 0 1
testdb=# insert into author values(3, '("",, TRUE)', '張三的自傳3');
INSERT 0 1

# ROW行插入,注意ROW後面的字符請用單引號,不能帶雙引號
testdb=# insert into author values(4, ROW("",, TRUE), '張三的自傳5');
ERROR:  zero-length delimited identifier at or near """"
第1行insert into author values(4, ROW("",, TRUE), '張三的自傳5');
                                      ^
testdb=# insert into author values(4, ROW('PCB',, TRUE), '張三的自傳5');
ERROR:  syntax error at or near ","
第1行insert into author values(4, ROW('PCB',, TRUE), '張三的自傳5...
                                            ^
# 省略ROW
testdb=# insert into author values(4, ('hgr',30, TRUE), '張三的自傳6');
INSERT 0 1

testdb=# select (people).name from author;
 name
------
 qkl
 zgq
 zgq

 hgr
(5 行記錄)

# 更新複合類型數據,複合類型名不帶括號
testdb=# update author set people.name='qkl2' where id=1;
UPDATE 1
testdb=# select people from author;
   people
-------------
 (zgq,,t)
 (zgq,,t)
 ("",,t)
 (hgr,30,t)
 (qkl2,29,t)
(5 行記錄)


testdb=# update author set people.age = people.age +1 where id=1;
ERROR:  missing FROM-clause entry for table "people"
第1行update author set people.age = people.age +1 where id=1;
                                    ^

# 如果更新的字段右邊也是複合類型必須攜帶括號              
testdb=# update author set people.age = (people).age +1 where id=1;
UPDATE 1
testdb=# select people from author;
   people
-------------
 (zgq,,t)
 (zgq,,t)
 ("",,t)
 (hgr,30,t)
 (qkl2,30,t)
(5 行記錄)

# 單獨插入複合類型的數據
testdb=# insert into author (id, people.name, people.age) values(7, 'ldh', 33);
INSERT 0 1
testdb=# select * from author;
 id |   people    |    book
----+-------------+-------------
  2 | (zgq,,t)    | 張三的自傳2
  2 | (zgq,,t)    | 張三的自傳2
  3 | ("",,t)     | 張三的自傳3
  4 | (hgr,30,t)  | 張三的自傳6
  1 | (qkl2,30,t) | 張三的自傳
  7 | (ldh,33,)   |
(6 行記錄)

XML類型

xm丨類型可以用於存儲XML數據。使用字符串類型(如text)也可以存儲XML數據,但text類型不能保證其中存儲的是合法XML數據,通常需要由應用程序來負責保證輸人數據的正確性,這將增加應用程序開發的難度。而使用xml類型就不存在此問題。數據庫會對輸人的數據進行檢查,讓一些不符合XML標準的數據不能存放到數據庫中,同時還提供了函數對其類型進行安全性檢査。
注意,要使用xml數據類型,在編譯PostgreSQL源碼時必須使用以下參數:
configure --with-libxml

xml存儲的XML數據杳兩種:

  1. 由 XML 標準走義的documents
  2. 由XML標準定義的content片段。

content片段可以有多個級元素或character節點.但documents只能有一個頂級元素。可以使用xmlvalue is DOCUMENT來判斷一個特定的XML值是一個documents還足content片段。
PostgreSQL的xmlopiions參數用來指定輸人的數據是documents還是content片段,默認情況下此值爲content片段。所以輸人的xml可以有多個頂級元素,但如果把此參數改成document,將不能輸人有多個頂級元素的內容。

PostgreSQL數據庫在客戶端與服務器之間傳遞數據時,會自動進行字符集的轉換。如果客戶端的字符集與服務端不一樣,PostgreSQL會自動進行字符集轉換。但也正是因爲有這個特性,在傳遞XML數據時需要格外注意。我們知道,對於XML文件來說,可以通過類似“encoding="XXX"”的方式指定自己文件的字符集,但當這些數據在PostgreSQL之間傳遞時, PostgreSQL會把其原始內容的宇符集變成數據庫服務端的字符集,而這就會導致問題,因爲這意味着XML數據中的字符集編碼聲明在客戶端和服務器之間傳遞時,可能變得無效。爲了應對該問題,提交輸人到xml類型的字符串中的編碼聲明將會被忽略掉.同時內容的字符集會被認爲是當前數據庫服務器的字符集。

正確處理XML字符集的方式是,先將XML數據的字符串在當前客戶端中編碼成當前客戶端的字符集,然後再發送到服務端,這樣就會被轉換成服務端的字符集存儲。當査詢xml 類型的值時,此數據又會被轉換成客戶端的字符集,所以客戶端收到的XML數據的字符集就是客戶端的字符集。
所以通常來說,如果XML數據的字符集編碼、客戶端字符集編碼,以及服務器字符集編碼完全一樣,那麼用PostgreSQL處理XML數據將會大大減少字符集問題,並且處理的效率也會很高。通常XML數據都是用UTF-8編碼格式處理的,因此把PostgreSQL數據庫服務器端編碼也設置成UTF-8將是一種較好的選擇。

postgres=# select xml'<code>hello postgres</code>';
             xml
-----------------------------
 <code>hello postgres</code>
(1 行記錄)


postgres=# show xmloption;
 xmloption
-----------
 content
(1 行記錄)


postgres=# set xmloption TO document;
SET
postgres=# show xmloption;
 xmloption
-----------
 document
(1 行記錄)

# document類型,插入多頂級元素,出錯
postgres=# select xml'<code>hello postgres</code><other>hello world</other>';
ERROR:  invalid XML document
第1行select xml'<code>hello postgres</code><other>hello world</ot...
               ^
描述:  line 1: Extra content at the end of the document
<code>hello postgres</code><other>hello world</other>
                           ^
postgres=# set xmloption TO content;
SET
postgres=# select xml'<code>hello postgres</code><other>hello world</other>';
                          xml
-------------------------------------------------------
 <code>hello postgres</code><other>hello world</other>
(1 行記錄)

# xmlparse函數
postgres=# select xmlparse(document'<?xml version="1.0"?><person><name>qkl</name></person>');
             xmlparse
-----------------------------------
 <person><name>qkl</name></person>
(1 行記錄)


postgres=# select xmlparse(content'<?xml version="1.0"?><person><name>qkl</name></person>');
             xmlparse
-----------------------------------
 <person><name>qkl</name></person>
(1 行記錄)

json類型

JSON數據類型是從P〇StgreSQL9.3開始提供的一種類型,9.3版中只有一種類型:JSON
在PosIgreSQL 9.4中乂提供了一種更高效的類型JSONB這兩種類型在使用上幾乎完全一致,主要的區別是,JSON類型是把輸人的數據原封不動地存放到數據庫中(當然在存放前會做JSON的語法檢查),使用的時候需要®:新解析數據,而JSONB類型是在存放時就把JSON解折成二進制格式了,使用的時候就不需要再次解析,所以JSONB在使用時性能會更高。另外,JSONB支持在其上逑索引,iftHSON則不能,這是JSONB類型的很大一個優點。
因爲JSON類型是把輸人的整個字符串原封不改動地保存到數據庫中的,因此JSON串中key之間多餘的空格也會保留。而且,如果JSON串中有重複的key,這些重複的key也會保留(默認處理時以最後一個爲準),同時也會保留輸人時JSON串中各個key的順序。而JSONB類塑則恰恰相反,+會保留多餘的空格,不會保留key的順序,也不會保留重複的key。
在PostgreSQL中只允許毎個數據庫用一種服務器編碼,如果數據庫的編碼不是UTF-8,PostgreSQL中的JSON類塑是無法嚴格符合JSON規範中對字符集的要求的。如果輸人中包含不能在服務器編碼中表示的字符數據,將無法導人到數據庫中。但是,能在服務器編碼中表示的
非UTF-8字符則是被允許的。可以使用uXXXX形式的轉義,從而忽視數據庫的字符集編碼。
當把一個JSON字符串轉換成JSONB類塑吋,JSON字符串內的數據類型實際上被轉換成rPostgreSQL數據庫中的類塑,兩者的映射關係見表5-30。耑要注意的是,如果是在JS0NB中,在PostgrcSQL 不能輸人超出numeric數據類型範鬧的值。

clipboard.png

基礎操作

testdb=# select '9'::json, '"osdba"'::json, 'true'::json, 'null'::json;
 json |  json   | json | json
------+---------+------+------
 9    | "osdba" | true | null
(1 行記錄)


testdb=# select '[9, true, "qkl", null]'::json, '[9, true, "qkl", null]'::jsonb;
          json          |         jsonb
------------------------+------------------------
 [9, true, "qkl", null] | [9, true, "qkl", null]
(1 行記錄)


testdb=# select json'{"name":"qkl", "age":18, "sex":true, "money":888.88}';
                         json
------------------------------------------------------
 {"name":"qkl", "age":18, "sex":true, "money":888.88}
(1 行記錄)

# json 存取的是浮點型,存在精度問題
testdb=# select json'{"p":1.684544545454e-27}';
           json
--------------------------
 {"p":1.684544545454e-27}
(1 行記錄)

# jsonb存取的是numeric
testdb=# select jsonb'{"p":1.684544545454e-27}';
                      jsonb
--------------------------------------------------
 {"p": 0.000000000000000000000000001684544545454}
(1 行記錄)

clipboard.png

更多和操作符

testdb=# select json'[1,2,3]'->0;
 ?column?
----------
 1
(1 行記錄)


testdb=# select json'[1,2,3]'->3;
 ?column?
----------

(1 行記錄)


testdb=# select json'[1,2,3]'->2;
 ?column?
----------
 3
(1 行記錄)


testdb=# select json'[1,2,3]'->>2;
 ?column?
----------
 3
(1 行記錄)


testdb=# select json'[1,2,3]'->>'2';
 ?column?
----------

(1 行記錄)


testdb=# select json'{"a":1,"b":22}'->>'a';
 ?column?
----------
 1
(1 行記錄)


testdb=# select json'{"a":{"a1":{"a11":111}},"b":22}'#>'{a,a1}'
testdb-# ;
  ?column?
-------------
 {"a11":111}
(1 行記錄)


testdb=# select json'{"a":{"a1":{"a11":111}},"b":22}'#>'{a,a1,a11}'
testdb-# ;
 ?column?
----------
 111
(1 行記錄)


testdb=# select json'{"a":{"a1":{"a11":111}},"b":22}'#>>'{a,a1,a11}';
 ?column?
----------
 111
(1 行記錄)


testdb=# select json'{"a":{"a1":{"a11":111}},"b":22}'#>>'{a,a1}';
  ?column?
-------------
 {"a11":111}
(1 行記錄)

clipboard.png

clipboard.png

Range類型

testdb=# select '(0,6)'::int4range;
 int4range
-----------
 [1,6)
(1 行記錄)


testdb=# select '[0,6)'::int4range;
 int4range
-----------
 [0,6)
(1 行記錄)


testdb=# select '[0,6]'::int4range;
 int4range
-----------
 [0,7)
(1 行記錄)


testdb=# select 'empty'::int4range;
 int4range
-----------
 empty
(1 行記錄)

# 上面我們看出,int4range總數會轉換成`[)`格式


testdb=# select '[0,6]'::numrange;
 numrange
----------
 [0,6]
(1 行記錄)


testdb=# select '[0,6)'::numrange;
 numrange
----------
 [0,6)
(1 行記錄)


testdb=# select '(0,6)'::numrange;
 numrange
----------
 (0,6)
(1 行記錄)


testdb=# select '(0,)'::numrange;
 numrange
----------
 (0,)
(1 行記錄)


testdb=# select '[1,)'::numrange;
 numrange
----------
 [1,)
(1 行記錄)

clipboard.png

clipboard.png

數組類型

PostgreSQL支持表的字段使用定長或可變長度的一維或多維數組,數組的類型可以是任何數據庫內建的類型、用戶自定義的類型、枚舉類型,以及組合類型。但目前還不支持domain類型。

create table test6 (id int, col1 int[], col2 int[10], col3 text[][]);
create table test7 (id int, col1 int[10], col2 int[], col3 text[]);

在第一個語句中第二列的聲明col2 int[10]與第二個語句中第二列的聲明col2 int[]其意思相同;而在第一個語句中第三列的聲明col3 text[][]與第二個語句中第三列的聲明col3 text[]的意思也是相同的

我們來查看下錶結構

testdb=# create table test6 (id int, col1 int[], col2 int[10], col3 text[][]);
CREATE TABLE
testdb=# create table test7 (id int, col1 int[10], col2 int[], col3 text[]);
CREATE TABLE
testdb=# \d
                  關聯列表
 架構模式 |     名稱     |  類型  |  擁有者
----------+--------------+--------+----------
 public   | author       | 數據表 | postgres
 public   | test1        | 數據表 | postgres
 public   | test2        | 數據表 | postgres
 public   | test2_id_seq | 序列數 | postgres
 public   | test3        | 數據表 | postgres
 public   | test3_id_seq | 序列數 | postgres
 public   | test6        | 數據表 | postgres
 public   | test7        | 數據表 | postgres
(8 行記錄)


testdb=# \d test6;
               數據表 "public.test6"
 欄位 |   類型    | Collation | Nullable | Default
------+-----------+-----------+----------+---------
 id   | integer   |           |          |
 col1 | integer[] |           |          |
 col2 | integer[] |           |          |
 col3 | text[]    |           |          |


testdb=# \d test7;
               數據表 "public.test7"
 欄位 |   類型    | Collation | Nullable | Default
------+-----------+-----------+----------+---------
 id   | integer   |           |          |
 col1 | integer[] |           |          |
 col2 | integer[] |           |          |
 col3 | text[]    |           |          |
testdb=# create table test66(id int, col1 int[]);
CREATE TABLE
testdb=# insert into test66 values(1, '{1,2,3}');
INSERT 0 1
testdb=# insert into test66 values(2, '{4,5,6}');
INSERT 0 1
testdb=# select * from test66;
 id |  col1
----+---------
  1 | {1,2,3}
  2 | {4,5,6}
(2 行記錄)


testdb=# create table test77(id int, col1 text[]);
CREATE TABLE
testdb=# insert into test77 values(1, '{how, howe, howl}');
INSERT 0 1
testdb=# select * from test77;
 id |      col1
----+-----------------
  1 | {how,howe,howl}
(1 行記錄)


testdb=# insert into test77 values(1, '{"how", "howe", "howl"}');
INSERT 0 1
testdb=# select * from test77;
 id |      col1
----+-----------------
  1 | {how,howe,howl}
  1 | {how,howe,howl}
(2 行記錄)

# 注意上面不同的類型,可能分隔符會不一樣,下面可查詢不同類型的分隔符
testdb=# select typname,typdelim from pg_type where typname in ('int4', 'int8', 'bool', 'char', 'box');
 typname | typdelim
---------+----------
 bool    | ,
 char    | ,
 int8    | ,
 int4    | ,
 box     | ;
(5 行記錄)

testdb=# create table test778(id int ,col1 box[]);
CREATE TABLE
testdb=# insert into test778 values(1, '{((1,1), (2,2)); ((3,3),(4,4))}');
INSERT 0 1
testdb=# select * from test778;
 id |           col1
----+---------------------------
  1 | {(2,2),(1,1);(4,4),(3,3)}
(1 行記錄)


# 上面輸人的字符串內容是沒有空格的,在有空格時,乂該如何輸人呢?見下面的例子:
testdb=# insert into test77 values(3,'{how many,how mach,how old}');
INSERT 0 1
可以看到有空格,也可以直接輸人。
那麼字符串中有逗號時怎麼辦呢?這時可以使用雙引號,如下:
testdb=# insert into test77 values(4,'{"who, what", "CO.,LTD."}');
INSERT 0 1
如果字符串中有單引號怎麼辦呢?這時可以使用兩個連接的單引號表示一個單引號:
testdb=# insert into test77  values(3,'{"who''s bread", "It''s ok"}');
INSERT 0 1
如果輸人的字符串中有括號"{"和"}"怎麼辦呢?只需把它們放到雙引號中即可:
testdb=# insert into test77 values (5, '{"{os, dba}", "{dba, os}"}');
INSERT 0 1
如果輸人的字符串中有雙引號怎麼辦呢?這時需要在雙引號前加反斜扛,如下所示:
testdb=# insert into test77 values(6,'{os\"dba}');

testdb=# select * from test77;
 id |               col1
----+-----------------------------------
  1 | {how,howe,howl}
  1 | {how,howe,howl}
  3 | {"how many","how mach","how old"}
  4 | {"who, what","CO.,LTD."}
  3 | {"who's bread","It's ok"}
  5 | {"{os, dba}","{dba, os}"}
  6 | {"os\"dba"}
(7 行記錄)

# ARRAY關鍵詞的使用,需帶單引號
testdb=# insert into test77 values(9, array['os', 'dba']);
INSERT 0 1
testdb=# insert into test77 values(9, array['os"win', 'dba']);
INSERT 0 1
testdb=# insert into test77 values(9, array['os''win', 'dba']);
INSERT 0 1
testdb=# select * from test77;
 id |               col1
----+-----------------------------------
  1 | {how,howe,howl}
  1 | {how,howe,howl}
  3 | {"how many","how mach","how old"}
  4 | {"who, what","CO.,LTD."}
  3 | {"who's bread","It's ok"}
  5 | {"{os, dba}","{dba, os}"}
  6 | {"os\"dba"}
  9 | {os,dba}
  9 | {"os\"win",dba}
  9 | {os'win,dba}
(10 行記錄)
# 多維數組
testdb=# create table test779 (id int , col1 test[][]);
ERROR:  type "test[]" does not exist
第1行create table test779 (id int , col1 test[][]);
testdb=# create table test779 (id int , col1 text[][]);
CREATE TABLE
testdb=# \d test779;
             數據表 "public.test779"
 欄位 |  類型   | Collation | Nullable | Default
------+---------+-----------+----------+---------
 id   | integer |           |          |
 col1 | text[]  |           |          |


testdb=# insert into test779 values(1, array[['aa'], ['cc']]);
INSERT 0 1
testdb=# insert into test779 values(1, array[['aa','bb'], ['cc','dd']]);
INSERT 0 1
testdb=# select * from test779;
 id |       col1
----+-------------------
  1 | {{aa},{cc}}
  1 | {{aa,bb},{cc,dd}}
(2 行記錄)


testdb=# insert into test779 values(1, array[['aa','bb'], ['cc','dd','dd']]);
ERROR:  multidimensional arrays must have array expressions with matching dimensions
testdb=# insert into test779 values(3, '{{aa, bb}, {cc, dd}}');
INSERT 0 1
testdb=# select * from test779;
 id |       col1
----+-------------------
  1 | {{aa},{cc}}
  1 | {{aa,bb},{cc,dd}}
  3 | {{aa,bb},{cc,dd}}
(3 行記錄)


# 默認情況下數組的下標是從1開始的,postgres的數組是可以指定開始下標的
testdb=# create table test7799 (id int[]);
CREATE TABLE
testdb=# insert into test7799 values('[2:4]={1,2,3}');
INSERT 0 1
testdb=# select id[2], id[3], id[4] from test7799;
 id | id | id
----+----+----
  1 |  2 |  3
(1 行記錄)


testdb=# select id from test7799;
      id
---------------
 [2:4]={1,2,3}
(1 行記錄)


testdb=# insert into test7799 values('{11,22,33}');
INSERT 0 1
testdb=# select id[2], id[3], id[4] from test7799;
 id | id | id
----+----+----
  1 |  2 |  3
 22 | 33 |
(2 行記錄)


testdb=# select id[1], id[2], id[3] from test7799;
 id | id | id
----+----+----
    |  1 |  2
 11 | 22 | 33
(2 行記錄)

# 從上面的例子可以看出,指定數組上下標的格式爲:
`[下標:上標] = [元素值1, 元素值2, 元素值3..]`
testdb=# create table test666 (id int, col1 int[][]);
CREATE TABLE
testdb=# \d test666;
              數據表 "public.test666"
 欄位 |   類型    | Collation | Nullable | Default
------+-----------+-----------+----------+---------
 id   | integer   |           |          |
 col1 | integer[] |           |          |


testdb=# insert into test666 values(1, '{{1,2,3}, {4,5,6}, {7,8,9}}');
INSERT 0 1
testdb=# select * from test666;
 id |           col1
----+---------------------------
  1 | {{1,2,3},{4,5,6},{7,8,9}}
(1 行記錄)

# 多維數組讀取
testdb=# select col1[1][1], col1[1][2], col1[2][1], col1[2][2] from test666;
 col1 | col1 | col1 | col1
------+------+------+------
    1 |    2 |    4 |    5
(1 行記錄)

# 無法直接獲取外層數組
testdb=# select id, col1[1] from test666;
 id | col1
----+------
  1 |
(1 行記錄)

# 數組切換可獲取
testdb=# select id, col1[1:1] from test666;
 id |   col1
----+-----------
  1 | {{1,2,3}}
(1 行記錄)


testdb=# select id, col1[1:2] from test666;
 id |       col1
----+-------------------
  1 | {{1,2,3},{4,5,6}}
(1 行記錄)

# 注意這裏的外層其實是col1[3][1:2] == col1[1:3][1:2]
testdb=# select id, col1[3][1:2] from test666;
 id |        col1
----+---------------------
  1 | {{1,2},{4,5},{7,8}}
(1 行記錄)


testdb=# select id, col1[1:3][1:2] from test666;
 id |        col1
----+---------------------
  1 | {{1,2},{4,5},{7,8}}
(1 行記錄)


# 注意這裏的外層其實是col1[1:2][2] == col1[1:2][1:2]
testdb=# select id, col1[1:2][2] from test666;
 id |     col1
----+---------------
  1 | {{1,2},{4,5}}
(1 行記錄)


testdb=# select id, col1[1:2][1:2] from test666;
 id |     col1
----+---------------
  1 | {{1,2},{4,5}}
(1 行記錄)


# 修改
# 可以修改整個字段數組值,或修改某一維度的單元素的值
# 不過無法直接修改某一維數組的值
testdb=# select * from test666;
 id |           col1
----+---------------------------
  1 | {{1,2,3},{4,5,6},{7,8,9}}
(1 行記錄)


testdb=# update test666 set col1[2][1] = 1000 where id=1;
UPDATE 1
testdb=# select * from test666;
 id |             col1
----+------------------------------
  1 | {{1,2,3},{1000,5,6},{7,8,9}}
(1 行記錄)


testdb=# update test666 set col1[2] = '{1,2,3}' where id=1;
ERROR:  invalid input syntax for integer: "{1,2,3}"
第1行update test666 set col1[2] = '{1,2,3}' where id=1;

clipboard.png

clipboard.png

clipboard.png

clipboard.png

僞類類型

僞類型(Pseudo-Types)是PostgreSQL中不能作爲字段的數據類型,但是它可以用於聲明
一個函數的參數或者結果類型。所包含的類型有:
□ any:用於指示函數的輸人蔘數可以是任意數據類型的。
□ anyelement:表示一個函數接受任何數據類型。
□ anyarray:表示一個函數接受任何數組類型。
□ anynonarray:表示一個函數接受任何非數組類塑。
□ anyenum:表示一個函數接受任何枚舉類型_。
□ anyrange:表示一個函數接受任何範類型。
□ cstring:表示一個函數接受或返回一個空字符(0)結尾的C語言字符串。
□ internal:表示一個函數接受或者返冋一種服務器內部的數據類型。
□ language_handler:聲明一個函數返回類型是pi語言的一個handler閒數。
□ fdw_handler:聲明一個函數的返冋類型楚foreign-data wrapper的handler函數。
□ record:標識一個函數返回一個未詳細定義各列的row類型。
□ trigger: 一個觸發器函數要聲明爲返回trigger類型。
□ void:表示一個函數沒有返問值。
□ opaque:已經過時的類型,舊的PostgreSQL版本中用於上面這些用途。

用C編寫的函數(不管是內H的還是動態裝載的)都可以盧明爲接受或返冋上面任意一種僞數據類型。在把僞類型用作函數參數類型時,PostgreSQL數據庫本身對類型檢査就少了很多,保證類沏正確的任務就交給了寫函數的幵發人員。
用過程語言編寫的函數不一定都能使用上面列出的全部僞類型,具體能使用哪些需要喪看相關的過程語言文襠,或者查C•過程語言的實現。通常,過程語言都不支持使用“any”類型,但基本都能支持使用void和record作爲結果類®,能支持多態閒數的過程語言還支持使
用“311)^〇^”、“311>^丨6咖111”、“3!1)^1111171”和‘‘311>010113〇'3>^’類型〇僞 類 型 “intemar用於聲明那種只能在數據庫系統內部調用的函數,它們不能直接在SQL査詢裏調用。如果函數至少有一個“internal”類型的參數,那麼就不能從SQL裏調用。
爲了保留這個限制的類塑安全,一定要遵循這樣的編碼規則:對於沒有任何一個"internal”參數的函數,不要把返回類型創建爲“internal”。

其他類型

UUID

UUID( Universally Unique Identifiers)用於存儲一個 UUID; UUID 定義在RFC 4122ISO/IEC 9834-8:2005中。它是一個128bit的數字
PostgreSQL核心源代碼中沒提供產生UUID的函數,contrib下的uuid-ossp模塊提供UUID的函數

PG_LSN

pg_lsn類切是PostgrcSQL9.4版本之後提供的表示LSN ( Log Sequence Number)的一種數據類擬。LSN表示WALfi志的位酋。在一些記錄WALti志信息的系統表中某些字段的類型就是pg_lsn類型

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章