|
| 1 | +-- |
| 2 | +-- Sanity checks for common errors in making type-related system tables: |
| 3 | +-- pg_type, pg_class, pg_attribute. |
| 4 | +-- |
| 5 | +-- None of the SELECTs here should ever find any matching entries, |
| 6 | +-- so the expected output is easy to maintain ;-). |
| 7 | +-- A test failure indicates someone messed up an entry in the system tables. |
| 8 | +-- |
| 9 | +-- NB: we assume the oidjoins test will have caught any dangling links, |
| 10 | +-- that is OID or REGPROC fields that are not zero and do not match some |
| 11 | +-- row in the linked-to table. However, if we want to enforce that a link |
| 12 | +-- field can't be 0, we have to check it here. |
| 13 | + |
| 14 | +-- **************** pg_type **************** |
| 15 | + |
| 16 | +-- Look for illegal values in pg_type fields. |
| 17 | + |
| 18 | +SELECTp1.oid,p1.typname |
| 19 | +FROM pg_typeas p1 |
| 20 | +WHERE (p1.typlen<=0ANDp1.typlen!=-1)OR |
| 21 | + (p1.typtype!='b'ANDp1.typtype!='c')OR |
| 22 | + NOTp1.typisdefinedOR |
| 23 | + (p1.typalign!='c'ANDp1.typalign!='s'AND |
| 24 | +p1.typalign!='i'ANDp1.typalign!='d'); |
| 25 | + |
| 26 | +-- Look for "pass by value" types that can't be passed by value. |
| 27 | + |
| 28 | +SELECTp1.oid,p1.typname |
| 29 | +FROM pg_typeas p1 |
| 30 | +WHEREp1.typbyvalAND |
| 31 | + (p1.typlen!=1ORp1.typalign!='c')AND |
| 32 | + (p1.typlen!=2ORp1.typalign!='s')AND |
| 33 | + (p1.typlen!=4ORp1.typalign!='i'); |
| 34 | + |
| 35 | +-- Look for complex types that do not have a typrelid entry, |
| 36 | +-- or basic types that do. |
| 37 | + |
| 38 | +SELECTp1.oid,p1.typname |
| 39 | +FROM pg_typeas p1 |
| 40 | +WHERE (p1.typtype='c'ANDp1.typrelid=0)OR |
| 41 | + (p1.typtype!='c'ANDp1.typrelid!=0); |
| 42 | + |
| 43 | +-- Conversion routines must be provided except in 'c' entries. |
| 44 | + |
| 45 | +SELECTp1.oid,p1.typname |
| 46 | +FROM pg_typeas p1 |
| 47 | +WHEREp1.typtype!='c'AND |
| 48 | + (p1.typinput=0ORp1.typoutput=0OR |
| 49 | +p1.typreceive=0ORp1.typsend=0); |
| 50 | + |
| 51 | +-- Check for bogus typinput routines |
| 52 | +-- The first OR subclause detects bogus non-array cases, |
| 53 | +-- the second one detects bogus array cases. |
| 54 | +-- FIXME: ought to check prorettype, but there are special cases that make it |
| 55 | +-- hard: prorettype might be binary-compatible with the type but not the same, |
| 56 | +-- and for array types array_in's result has nothing to do with anything. |
| 57 | + |
| 58 | +SELECTp1.oid,p1.typname,p2.oid,p2.proname |
| 59 | +FROM pg_typeAS p1, pg_procAS p2 |
| 60 | +WHEREp1.typinput=p2.oidANDp1.typtype='b'AND |
| 61 | + (p2.pronargs!=1ORp2.proretset)AND |
| 62 | + (p2.pronargs<2ORp2.pronargs>3ORp2.proretsetORp1.typelem=0); |
| 63 | + |
| 64 | +-- Check for bogus typoutput routines |
| 65 | +-- The first OR subclause detects bogus non-array cases, |
| 66 | +-- the second one detects bogus array cases. |
| 67 | +-- FIXME: ought to check prorettype, but not clear what it should be. |
| 68 | + |
| 69 | +SELECTp1.oid,p1.typname,p2.oid,p2.proname |
| 70 | +FROM pg_typeAS p1, pg_procAS p2 |
| 71 | +WHEREp1.typoutput=p2.oidANDp1.typtype='b'AND |
| 72 | + (p2.pronargs!=1ORp2.proretset)AND |
| 73 | + (p2.pronargs!=2ORp2.proretsetORp1.typelem=0); |
| 74 | + |
| 75 | +-- Check for bogus typreceive routines |
| 76 | +-- The first OR subclause detects bogus non-array cases, |
| 77 | +-- the second one detects bogus array cases. |
| 78 | +-- FIXME: ought to check prorettype, but there are special cases that make it |
| 79 | +-- hard: prorettype might be binary-compatible with the type but not the same, |
| 80 | +-- and for array types array_in's result has nothing to do with anything. |
| 81 | + |
| 82 | +SELECTp1.oid,p1.typname,p2.oid,p2.proname |
| 83 | +FROM pg_typeAS p1, pg_procAS p2 |
| 84 | +WHEREp1.typreceive=p2.oidANDp1.typtype='b'AND |
| 85 | + (p2.pronargs!=1ORp2.proretset)AND |
| 86 | + (p2.pronargs<2ORp2.pronargs>3ORp2.proretsetORp1.typelem=0); |
| 87 | + |
| 88 | +-- Check for bogus typsend routines |
| 89 | +-- The first OR subclause detects bogus non-array cases, |
| 90 | +-- the second one detects bogus array cases. |
| 91 | +-- FIXME: ought to check prorettype, but not clear what it should be. |
| 92 | + |
| 93 | +SELECTp1.oid,p1.typname,p2.oid,p2.proname |
| 94 | +FROM pg_typeAS p1, pg_procAS p2 |
| 95 | +WHEREp1.typsend=p2.oidANDp1.typtype='b'AND |
| 96 | + (p2.pronargs!=1ORp2.proretset)AND |
| 97 | + (p2.pronargs!=2ORp2.proretsetORp1.typelem=0); |
| 98 | + |
| 99 | +-- **************** pg_class **************** |
| 100 | + |
| 101 | +-- Look for illegal values in pg_class fields |
| 102 | + |
| 103 | +SELECTp1.oid,p1.relname |
| 104 | +FROM pg_classas p1 |
| 105 | +WHERE (p1.relkind!='r'ANDp1.relkind!='i'ANDp1.relkind!='s'); |
| 106 | + |
| 107 | +-- Indexes should have an access method, others not. |
| 108 | + |
| 109 | +SELECTp1.oid,p1.relname |
| 110 | +FROM pg_classas p1 |
| 111 | +WHERE (p1.relkind='i'ANDp1.relam=0)OR |
| 112 | + (p1.relkind!='i'ANDp1.relam!=0); |
| 113 | + |
| 114 | +-- **************** pg_attribute **************** |
| 115 | + |
| 116 | +-- Look for illegal values in pg_attribute fields |
| 117 | + |
| 118 | +SELECTp1.oid,p1.attrelid,p1.attname |
| 119 | +FROM pg_attributeas p1 |
| 120 | +WHEREp1.attrelid=0ORp1.atttypid=0ORp1.attnum=0OR |
| 121 | +p1.attcacheoff!=-1; |
| 122 | + |
| 123 | +-- Look for duplicate pg_attribute entries |
| 124 | +-- (This would not be necessary if the indexes on pg_attribute were UNIQUE?) |
| 125 | + |
| 126 | +SELECTp1.oid,p1.attname,p2.oid,p2.attname |
| 127 | +FROM pg_attributeAS p1, pg_attributeAS p2 |
| 128 | +WHEREp1.oid!=p2.oidAND |
| 129 | +p1.attrelid=p2.attrelidAND |
| 130 | + (p1.attname=p2.attnameORp1.attnum=p2.attnum); |
| 131 | + |
| 132 | +-- Cross-check attnum against parent relation |
| 133 | + |
| 134 | +SELECTp1.oid,p1.attname,p2.oid,p2.relname |
| 135 | +FROM pg_attributeAS p1, pg_classAS p2 |
| 136 | +WHEREp1.attrelid=p2.oidANDp1.attnum>p2.relnatts; |
| 137 | + |
| 138 | +-- Detect missing pg_attribute entries: should have as many non-system |
| 139 | +-- attributes as parent relation expects |
| 140 | + |
| 141 | +SELECTp1.oid,p1.relname |
| 142 | +FROM pg_classAS p1 |
| 143 | +WHEREp1.relnatts!= (SELECTcount(*)FROM pg_attributeAS p2 |
| 144 | +WHEREp2.attrelid=p1.oidANDp2.attnum>0); |
| 145 | + |
| 146 | +-- Cross-check against pg_type entry |
| 147 | + |
| 148 | +SELECTp1.oid,p1.attname,p2.oid,p2.typname |
| 149 | +FROM pg_attributeAS p1, pg_typeAS p2 |
| 150 | +WHEREp1.atttypid=p2.oidAND |
| 151 | + (p1.attlen!=p2.typlenOR |
| 152 | +p1.attalign!=p2.typalignOR |
| 153 | +p1.attbyval!=p2.typbyval); |