@@ -444,22 +444,19 @@ CREATE VIEW check_constraints AS
444
444
WHERE pg_has_role(coalesce(c .relowner , t .typowner ), ' USAGE' )
445
445
AND con .contype = ' c'
446
446
447
- UNION
447
+ UNION ALL
448
448
-- not-null constraints
449
-
450
- SELECT CAST(current_database() AS sql_identifier) AS constraint_catalog,
451
- CAST(n .nspname AS sql_identifier) AS constraint_schema,
452
- CAST(CAST(n .oid AS text ) || ' _' || CAST(r .oid AS text ) || ' _' || CAST(a .attnum AS text ) || ' _not_null' AS sql_identifier) AS constraint_name, -- XXX
453
- CAST(a .attname || ' IS NOT NULL' AS character_data)
454
- AS check_clause
455
- FROM pg_namespace n, pg_class r, pg_attribute a
456
- WHERE n .oid = r .relnamespace
457
- AND r .oid = a .attrelid
458
- AND a .attnum > 0
459
- AND NOT a .attisdropped
460
- AND a .attnotnull
461
- AND r .relkind IN (' r' , ' p' )
462
- AND pg_has_role(r .relowner , ' USAGE' );
449
+ SELECT current_database()::information_schema .sql_identifier AS constraint_catalog,
450
+ rs .nspname ::information_schema .sql_identifier AS constraint_schema,
451
+ con .conname ::information_schema .sql_identifier AS constraint_name,
452
+ pg_catalog .format (' CHECK (%s IS NOT NULL)' , at .attname )::information_schema .character_data AS check_clause
453
+ FROM pg_constraint con
454
+ LEFT JOIN pg_namespace rs ON rs .oid = con .connamespace
455
+ LEFT JOIN pg_class c ON c .oid = con .conrelid
456
+ LEFT JOIN pg_type t ON t .oid = con .contypid
457
+ LEFT JOIN pg_attribute at ON (con .conrelid = at .attrelid AND con .conkey [1 ] = at .attnum )
458
+ WHERE pg_has_role(coalesce(c .relowner , t .typowner ), ' USAGE' ::text )
459
+ AND con .contype = ' n' ;
463
460
464
461
GRANT SELECT ON check_constraints TO PUBLIC;
465
462
@@ -826,6 +823,20 @@ CREATE VIEW constraint_column_usage AS
826
823
AND r .relkind IN (' r' , ' p' )
827
824
AND NOT a .attisdropped
828
825
826
+ UNION ALL
827
+
828
+ /* not-null constraints */
829
+ SELECT DISTINCT nr .nspname , r .relname , r .relowner , a .attname , nc .nspname , c .conname
830
+ FROM pg_namespace nr, pg_class r, pg_attribute a, pg_namespace nc, pg_constraint c
831
+ WHERE nr .oid = r .relnamespace
832
+ AND r .oid = a .attrelid
833
+ AND r .oid = c .conrelid
834
+ AND a .attnum = c .conkey [1 ]
835
+ AND c .connamespace = nc .oid
836
+ AND c .contype = ' n'
837
+ AND r .relkind in (' r' , ' p' )
838
+ AND not a .attisdropped
839
+
829
840
UNION ALL
830
841
831
842
/* unique/primary key/foreign key constraints */
@@ -1828,6 +1839,7 @@ CREATE VIEW table_constraints AS
1828
1839
CAST(r .relname AS sql_identifier) AS table_name,
1829
1840
CAST(
1830
1841
CASE c .contype WHEN ' c' THEN ' CHECK'
1842
+ WHEN ' n' THEN ' CHECK'
1831
1843
WHEN ' f' THEN ' FOREIGN KEY'
1832
1844
WHEN ' p' THEN ' PRIMARY KEY'
1833
1845
WHEN ' u' THEN ' UNIQUE' END
@@ -1852,38 +1864,6 @@ CREATE VIEW table_constraints AS
1852
1864
AND c .contype NOT IN (' t' , ' x' ) -- ignore nonstandard constraints
1853
1865
AND r .relkind IN (' r' , ' p' )
1854
1866
AND (NOT pg_is_other_temp_schema(nr .oid ))
1855
- AND (pg_has_role(r .relowner , ' USAGE' )
1856
- -- SELECT privilege omitted, per SQL standard
1857
- OR has_table_privilege(r .oid , ' INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER' )
1858
- OR has_any_column_privilege(r .oid , ' INSERT, UPDATE, REFERENCES' ) )
1859
-
1860
- UNION ALL
1861
-
1862
- -- not-null constraints
1863
-
1864
- SELECT CAST(current_database() AS sql_identifier) AS constraint_catalog,
1865
- CAST(nr .nspname AS sql_identifier) AS constraint_schema,
1866
- CAST(CAST(nr .oid AS text ) || ' _' || CAST(r .oid AS text ) || ' _' || CAST(a .attnum AS text ) || ' _not_null' AS sql_identifier) AS constraint_name, -- XXX
1867
- CAST(current_database() AS sql_identifier) AS table_catalog,
1868
- CAST(nr .nspname AS sql_identifier) AS table_schema,
1869
- CAST(r .relname AS sql_identifier) AS table_name,
1870
- CAST(' CHECK' AS character_data) AS constraint_type,
1871
- CAST(' NO' AS yes_or_no) AS is_deferrable,
1872
- CAST(' NO' AS yes_or_no) AS initially_deferred,
1873
- CAST(' YES' AS yes_or_no) AS enforced,
1874
- CAST(NULL AS yes_or_no) AS nulls_distinct
1875
-
1876
- FROM pg_namespace nr,
1877
- pg_class r,
1878
- pg_attribute a
1879
-
1880
- WHERE nr .oid = r .relnamespace
1881
- AND r .oid = a .attrelid
1882
- AND a .attnotnull
1883
- AND a .attnum > 0
1884
- AND NOT a .attisdropped
1885
- AND r .relkind IN (' r' , ' p' )
1886
- AND (NOT pg_is_other_temp_schema(nr .oid ))
1887
1867
AND (pg_has_role(r .relowner , ' USAGE' )
1888
1868
-- SELECT privilege omitted, per SQL standard
1889
1869
OR has_table_privilege(r .oid , ' INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER' )
0 commit comments