我正在构建一个查询,该查询检索pgsql-9.1.6上的序列列表。请参阅下面的SQL集......
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
我的序列列表?
答案 0 :(得分: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=#