2013-05-30 2 views
10

정보 스키마의 제약 관련 데이터에 액세스하려면 관계 소유자 여야합니까? 나는 다음과 같은 것을 테스트했으며, 나는 그 소유자가되어야한다고 생각합니다.PostgreSQL의 다른 소유자가있는 모든 테이블에 대한 제약 조건

create schema rights_test; 

create table rights_test.t1 (id int primary key); 
create table rights_test.t2 (id int references rights_test.t1(id)); 

select 
     tc.constraint_name, 
     tc.constraint_schema || '.' || tc.table_name || '.' || kcu.column_name as physical_full_name, 
     tc.constraint_schema, 
     tc.table_name, 
     kcu.column_name, 
     ccu.table_name as foreign_table_name, 
     ccu.column_name as foreign_column_name, 
     tc.constraint_type 
    from 
     information_schema.table_constraints as tc 
     join information_schema.key_column_usage as kcu on (tc.constraint_name = kcu.constraint_name and tc.table_name = kcu.table_name) 
     join information_schema.constraint_column_usage as ccu on ccu.constraint_name = tc.constraint_name 
    where 
     constraint_type in ('PRIMARY KEY','FOREIGN KEY') 
     and tc.constraint_schema = 'rights_test' 

/* 
This will produce desired output: 
t1_pkey;rights_test.t1.id;rights_test;t1;id;t1;id;PRIMARY KEY 
t2_id_fkey;rights_test.t2.id;rights_test;t2;id;t1;id;FOREIGN KEY 
*/ 

create user rights_test_role with password 'password'; 

grant all on rights_test.t1 to rights_test_role; 
grant all on rights_test.t2 to rights_test_role; 

/* Now login as rights_test_role and try the same constraint select. 
    For rights_test_role it returns nothing although I've added ALL privileges 
*/ 

관계의 소유자가 아닌 경우 동일한 정보를 얻는 다른 방법이 있습니까?

답변

9

모든 제약 관련 데이터가 "보호 된"것은 아닙니다.

  • table_constraints
  • key_column_usage
  • constraint_column_usage

은 처음 두가 한정되지 않고 constraint_column_usage에 대한 문서가 당신을 알려줍니다 : 당신은 당신의 쿼리에 세 가지 관계를 사용

constraint_column_usage 뷰는 현재 dat의 모든 열을 식별합니다. 어떤 제한에 의해 사용되는 근원. 현재 활성화 된 역할이 소유 한 테이블에 포함 된 열만 표시됩니다. information_schema.constraint_column_usage 이후

는이다, 당신은 psql의 쉘에서

\d+ information_schema.constraint_column_usage 

를 사용하여 정의를 볼 수 있습니다. 결과는 언뜻보기에는 무섭지 만 실제로 그렇게 나쁘지는 않습니다. 당신이 소유자가 아닌 rights_test_role하여 열려있는 psql의 쉘에 정의를 붙여 그 마지막 줄에 당신을 삭제하면

WHERE pg_has_role(x.tblowner, 'USAGE'::text); 

: 가장 흥미로운 것은 - - 첫 번째 테스트는 매우 마지막 줄에있는 부분입니다 원하는 결과를 얻을 수 있습니다. 이는 기본 메타 데이터가 시스템에 의해 보호되지 않기 때문에 유용합니다. 따라서 뷰 정의를 제거하여 실제로 필요한 부분 만 포함 할 수 있습니다.

+2

'\ d를 +'당신의 친구입니다. –

16

다음을 사용해보십시오. 모든 제약 조건 이름과 제약 조건 설명을 입력하십시오.

  • 외래 키
  • 확인
  • 기본 키
  • 고유

처럼 :

select conrelid::regclass AS table_from, conname, pg_get_constraintdef(c.oid) 
from pg_constraint c 
join pg_namespace n ON n.oid = c.connamespace 
where contype in ('f', 'p','c','u') order by contype 
관련 문제