1--
2-- MISC_SANITY
3-- Sanity checks for common errors in making system tables that don't fit
4-- comfortably into either opr_sanity or type_sanity.
5--
6-- Every test failure in this file should be closely inspected.
7-- The description of the failing test should be read carefully before
8-- adjusting the expected output.  In most cases, the queries should
9-- not find *any* matching entries.
10--
11-- NB: run this test early, because some later tests create bogus entries.
12
13
14-- **************** pg_depend ****************
15
16-- Look for illegal values in pg_depend fields.
17-- classid/objid can be zero, but only in 'p' entries
18
19SELECT *
20FROM pg_depend as d1
21WHERE refclassid = 0 OR refobjid = 0 OR
22      deptype NOT IN ('a', 'e', 'i', 'n', 'p') OR
23      (deptype != 'p' AND (classid = 0 OR objid = 0)) OR
24      (deptype = 'p' AND (classid != 0 OR objid != 0 OR objsubid != 0));
25
26-- **************** pg_shdepend ****************
27
28-- Look for illegal values in pg_shdepend fields.
29-- classid/objid can be zero, but only in 'p' entries
30
31SELECT *
32FROM pg_shdepend as d1
33WHERE refclassid = 0 OR refobjid = 0 OR
34      deptype NOT IN ('a', 'o', 'p', 'r') OR
35      (deptype != 'p' AND (classid = 0 OR objid = 0)) OR
36      (deptype = 'p' AND (dbid != 0 OR classid != 0 OR objid != 0 OR objsubid != 0));
37
38
39-- Check each OID-containing system catalog to see if its lowest-numbered OID
40-- is pinned.  If not, and if that OID was generated during initdb, then
41-- perhaps initdb forgot to scan that catalog for pinnable entries.
42-- Generally, it's okay for a catalog to be listed in the output of this
43-- test if that catalog is scanned by initdb.c's setup_depend() function;
44-- whatever OID the test is complaining about must have been added later
45-- in initdb, where it intentionally isn't pinned.  Legitimate exceptions
46-- to that rule are listed in the comments in setup_depend().
47
48do $$
49declare relnm text;
50  reloid oid;
51  shared bool;
52  lowoid oid;
53  pinned bool;
54begin
55for relnm, reloid, shared in
56  select relname, oid, relisshared from pg_class
57  where EXISTS(
58      SELECT * FROM pg_attribute
59      WHERE attrelid = pg_class.oid AND attname = 'oid')
60    and relkind = 'r' and oid < 16384 order by 1
61loop
62  execute 'select min(oid) from ' || relnm into lowoid;
63  continue when lowoid is null or lowoid >= 16384;
64  if shared then
65    pinned := exists(select 1 from pg_shdepend
66                     where refclassid = reloid and refobjid = lowoid
67                     and deptype = 'p');
68  else
69    pinned := exists(select 1 from pg_depend
70                     where refclassid = reloid and refobjid = lowoid
71                     and deptype = 'p');
72  end if;
73  if not pinned then
74    raise notice '% contains unpinned initdb-created object(s)', relnm;
75  end if;
76end loop;
77end$$;
78
79-- **************** pg_class ****************
80
81-- Look for system tables with varlena columns but no toast table. All
82-- system tables with toastable columns should have toast tables, with
83-- the following exceptions:
84-- 1. pg_class, pg_attribute, and pg_index, due to fear of recursive
85-- dependencies as toast tables depend on them.
86-- 2. pg_largeobject and pg_largeobject_metadata.  Large object catalogs
87-- and toast tables are mutually exclusive and large object data is handled
88-- as user data by pg_upgrade, which would cause failures.
89
90SELECT relname, attname, atttypid::regtype
91FROM pg_class c JOIN pg_attribute a ON c.oid = attrelid
92WHERE c.oid < 16384 AND
93      reltoastrelid = 0 AND
94      relkind = 'r' AND
95      attstorage != 'p'
96ORDER BY 1, 2;
97