Skip to content

Commit 3af7217

Browse files
alvherrepetere
andcommitted
Update information_schema definition for not-null constraints
Now that we have catalogued not-null constraints, our information_schema definition can be updated to grab those rather than fabricate synthetic definitions. Note that we still don't have catalog rows for not-null constraints on domains, but we've never had not-null constraints listed in information_schema, so that's a problem to be solved separately. Co-authored-by: Peter Eisentraut <peter.eisentraut@enterprisedb.com> Co-authored-by: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/81b461c4-edab-5d8c-2f88-203108425340@enterprisedb.com Discussion: https://postgr.es/m/202309041710.psytrxlsiqex@alvherre.pgsql
1 parent e1c6db6 commit 3af7217

File tree

2 files changed

+28
-48
lines changed

2 files changed

+28
-48
lines changed

src/backend/catalog/information_schema.sql

+27-47
Original file line numberDiff line numberDiff line change
@@ -444,22 +444,19 @@ CREATE VIEW check_constraints AS
444444
WHERE pg_has_role(coalesce(c.relowner, t.typowner), 'USAGE')
445445
AND con.contype = 'c'
446446

447-
UNION
447+
UNION ALL
448448
-- 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';
463460

464461
GRANT SELECT ON check_constraints TO PUBLIC;
465462

@@ -826,6 +823,20 @@ CREATE VIEW constraint_column_usage AS
826823
AND r.relkind IN ('r', 'p')
827824
AND NOT a.attisdropped
828825

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+
829840
UNION ALL
830841

831842
/* unique/primary key/foreign key constraints */
@@ -1828,6 +1839,7 @@ CREATE VIEW table_constraints AS
18281839
CAST(r.relname AS sql_identifier) AS table_name,
18291840
CAST(
18301841
CASE c.contype WHEN 'c' THEN 'CHECK'
1842+
WHEN 'n' THEN 'CHECK'
18311843
WHEN 'f' THEN 'FOREIGN KEY'
18321844
WHEN 'p' THEN 'PRIMARY KEY'
18331845
WHEN 'u' THEN 'UNIQUE' END
@@ -1852,38 +1864,6 @@ CREATE VIEW table_constraints AS
18521864
AND c.contype NOT IN ('t', 'x') -- ignore nonstandard constraints
18531865
AND r.relkind IN ('r', 'p')
18541866
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))
18871867
AND (pg_has_role(r.relowner, 'USAGE')
18881868
-- SELECT privilege omitted, per SQL standard
18891869
OR has_table_privilege(r.oid, 'INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER')

src/include/catalog/catversion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202308251
60+
#define CATALOG_VERSION_NO 202309061
6161

6262
#endif

0 commit comments

Comments
 (0)