2013-10-10 79 views
1

我正在構建一個查詢,它檢索pgsql-9.1.6上的序列列表。請參閱我下面的SQL設置...哪個視圖具有cache_value的序列?

mydb=# create sequence seq1 cache 10; 
CREATE SEQUENCE 
mydb=# create sequence seq2 cache 20; 
CREATE SEQUENCE 
mydb=# \ds 
     List of relations 
Schema | Name | Type | Owner 
--------+------+----------+------- 
public | seq1 | sequence | pgdba 
public | seq2 | sequence | pgdba 
(2 rows) 

mydb=# \x 
Expanded display is on. 
mydb=# select * from seq1; 
-[ RECORD 1 ]-+-------------------- 
sequence_name | seq1 
last_value | 1 
start_value | 1 
increment_by | 1 
max_value  | 9223372036854775807 
min_value  | 1 
cache_value | 10 
log_cnt  | 0 
is_cycled  | f 
is_called  | f 

mydb=# select * from seq2; 
-[ RECORD 1 ]-+-------------------- 
sequence_name | seq2 
last_value | 1 
start_value | 1 
increment_by | 1 
max_value  | 9223372036854775807 
min_value  | 1 
cache_value | 20 
log_cnt  | 0 
is_cycled  | f 
is_called  | f 

mydb=# select * from information_schema.sequences; 
-[ RECORD 1 ]-----------+-------------------- 
sequence_catalog  | mydb 
sequence_schema   | public 
sequence_name   | seq1 
data_type    | bigint 
numeric_precision  | 64 
numeric_precision_radix | 2 
numeric_scale   | 0 
start_value    | 1 
minimum_value   | 1 
maximum_value   | 9223372036854775807 
increment    | 1 
cycle_option   | NO 
-[ RECORD 2 ]-----------+-------------------- 
sequence_catalog  | mydb 
sequence_schema   | public 
sequence_name   | seq2 
data_type    | bigint 
numeric_precision  | 64 
numeric_precision_radix | 2 
numeric_scale   | 0 
start_value    | 1 
minimum_value   | 1 
maximum_value   | 9223372036854775807 
increment    | 1 
cycle_option   | NO 

information_schema.sequences沒有cache_value。我可以加入哪個視圖以獲得cache_value的序列列表?

回答

1

最好我知道,你實際上正在查看這裏的數據存儲在那裏......表名是序列本身的名稱。因爲它是一個與Postgres相關的實現細節,所以在編排模式中沒有任何視圖。

備註:Postgres使用pg_catalog在信息模式中創建視圖。後者確實是一個跨平臺的便利;真正的細節在目錄中。千萬不要錯過psql的--echo-hidden選項,以瞭解更多的內部:

http://www.postgresql.org/docs/current/static/app-psql.html


# Output using `psql -E` 

test=# create sequence test; 
CREATE SEQUENCE 
test=# \d+ test 
********* QUERY ********** 
SELECT c.oid, 
    n.nspname, 
    c.relname 
FROM pg_catalog.pg_class c 
    LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace 
WHERE c.relname ~ '^(test)$' 
    AND pg_catalog.pg_table_is_visible(c.oid) 
ORDER BY 2, 3; 
************************** 

********* QUERY ********** 
SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, c.relhastriggers, c.relhasoids, pg_catalog.array_to_string(c.reloptions || array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ') 
, c.reltablespace, CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, c.relpersistence 
FROM pg_catalog.pg_class c 
LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid) 
WHERE c.oid = '25356'; 
************************** 

********* QUERY ********** 
SELECT * FROM public.test; 
************************** 

********* QUERY ********** 
SELECT a.attname, 
    pg_catalog.format_type(a.atttypid, a.atttypmod), 
    (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) 
    FROM pg_catalog.pg_attrdef d 
    WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef), 
    a.attnotnull, a.attnum, 
    (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t 
    WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation, 
    NULL AS indexdef, 
    NULL AS attfdwoptions, 
    a.attstorage, 
    CASE WHEN a.attstattarget=-1 THEN NULL ELSE a.attstattarget END AS attstattarget 
FROM pg_catalog.pg_attribute a 
WHERE a.attrelid = '25356' AND a.attnum > 0 AND NOT a.attisdropped 
ORDER BY a.attnum; 
************************** 

********* QUERY ********** 
SELECT pg_catalog.quote_ident(nspname) || '.' || 
    pg_catalog.quote_ident(relname) || '.' || 
    pg_catalog.quote_ident(attname) 
FROM pg_catalog.pg_class c 
INNER JOIN pg_catalog.pg_depend d ON c.oid=d.refobjid 
INNER JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace 
INNER JOIN pg_catalog.pg_attribute a ON (
a.attrelid=c.oid AND 
a.attnum=d.refobjsubid) 
WHERE d.classid='pg_catalog.pg_class'::pg_catalog.regclass 
AND d.refclassid='pg_catalog.pg_class'::pg_catalog.regclass 
AND d.objid=25356 
AND d.deptype='a' 
************************** 

       Sequence "public.test" 
    Column  | Type |  Value  | Storage 
---------------+---------+---------------------+--------- 
sequence_name | name | test    | plain 
last_value | bigint | 1     | plain 
start_value | bigint | 1     | plain 
increment_by | bigint | 1     | plain 
max_value  | bigint | 9223372036854775807 | plain 
min_value  | bigint | 1     | plain 
cache_value | bigint | 1     | plain 
log_cnt  | bigint | 0     | plain 
is_cycled  | boolean | f     | plain 
is_called  | boolean | f     | plain 

test=# 
+0

有什麼altanative方式獲得的? – KIM

+0

不是我所知道的,但是對內部更熟悉的人可能會發出不同的聲音。但據我瞭解,Postgres序列相當於一個單行表,其中有關數據與該行內的序列(除名稱外)有關。如果啓用'-E'選項(以回顯隱藏的查詢),根據我更新的答案,您會注意到信息是從pg_class,pg_attributes等以及「序列/表」本身提取的:SELECT *從public.test'。 –

相關問題