2

我正在使用以下查询来发现 (1) 主键列和 (2) 列是否具有来自 Postgresql 9.1 中的 information_schema 的默认值。

SELECT kcu.column_name, (c.column_default is not null) AS has_default 
FROM information_schema.key_column_usage kcu 
JOIN information_schema.table_constraints tc ON tc.constraint_name = kcu.constraint_name 
JOIN information_schema.columns c on c.column_name = kcu.column_name and c.table_name = kcu.table_name  
WHERE tc.constraint_type = 'PRIMARY KEY' AND kcu.table_name like :tablename

作为数据库所有者运行时它工作正常,但是当我作为“只读”用户运行它时(我需要在我的应用程序中这样做),它不会返回任何数据。一些研究表明问题出在 information.table_constraints 视图上;从文档中:

视图 table_constraints 包含属于当前用户拥有或具有某些非 SELECT 权限的表的所有约束。

那么为了检索table_constraints,我的登录角色需要的不仅仅是表上的SELECT?如果不授予登录角色写入权限,是否无法从 information_schema 获取信息?

4

3 回答 3

2

使用pg_*视图而不是information_schema视图。
pg_*视图显示有关授予权限的所有信息。

试试这个查询:

select
    t.relname as table_name,
    i.relname as index_name,
    a.attname as column_name,
    d.adsrc   as default_value
from
    pg_class t
    join pg_attribute a on a.attrelid = t.oid
    join pg_index ix    on t.oid = ix.indrelid AND a.attnum = ANY(ix.indkey)
    join pg_class i     on i.oid = ix.indexrelid
    left join pg_attrdef d on d.adrelid = t.oid and d.adnum = a.attnum  
where
    t.relkind = 'r'
    and t.relname in ( 'aa', 'bb', 'cc' )
order by
    t.relname,
    i.relname,
    a.attnum;

查询结果示例:

create table aa(
  x int primary KEY
);

create table bb(
  x int default 1,
  constraint pk primary key ( x )
);

create table cc(
  x int default 20,
  y varchar(10) default 'something',
  constraint cc_pk primary key ( x, y )
);

 table_name | index_name | column_name |         default_value
------------+------------+-------------+--------------------------------
 aa         | aa_pkey    | x           |
 bb         | pk         | x           | 1
 cc         | cc_pk      | x           | 20
 cc         | cc_pk      | y           | 'something'::character varying
于 2013-08-29T20:46:15.733 回答
0

This is correct, the official postgresql query is below

http://wiki.postgresql.org/wiki/Retrieve_primary_key_columns

if schema is needed the query is as follows

SELECT               
  pg_attribute.attname, 
  format_type(pg_attribute.atttypid, pg_attribute.atttypmod) 
FROM pg_index, pg_class, pg_attribute, pg_namespace 
WHERE 
  pg_class.oid = 'MY TABLE'::regclass AND 
  indrelid = pg_class.oid AND 
  nspname = 'MY CLASS' AND 
  pg_class.relnamespace = pg_namespace.oid AND 
  pg_attribute.attrelid = pg_class.oid AND 
  pg_attribute.attnum = any(pg_index.indkey)
  AND indisprimary

The difference can be up to 6000~7000 times. The pg_ one runs often in 0.56ms where the schema based one can run up 6500ms. This is a huge difference especially if you have a high load on the server.

于 2013-12-13T04:22:32.837 回答
0

还有另一种方法可以访问information_schema.

A. 用SECURITY DEFINER修饰符将 SQL 封装在一个函数中

CREATE FUNCTION fn_inf(name)
RETURNS TABLE (column_name information_schema.sql_identifier, has_default bool)
LANGUAGE SQL
SECURITY DEFINER
AS $$
SELECT kcu.column_name, (c.column_default is not null) AS has_default 
    FROM information_schema.key_column_usage kcu 
        JOIN information_schema.table_constraints tc ON tc.constraint_name = kcu.constraint_name 
        JOIN information_schema.columns c on c.column_name = kcu.column_name and c.table_name = kcu.table_name  
    WHERE tc.constraint_type = 'PRIMARY KEY' AND kcu.table_name like $1;
$$;

B. GRANT EXECUTE 给用户read_only

GRANT EXECUTE ON FUNCTION fn_inf to read_only;

C. 作为用户使用read_only

SELECT * FROM fn_inf('spatial_ref_sys');
列名 has_default
斯里德 错误的
于 2021-09-04T11:18:55.623 回答