3 -- Sanity checks for common errors in making operator/procedure system tables:
4 -- pg_operator, pg_proc, pg_cast, pg_aggregate, pg_am, pg_amop, pg_amproc, pg_opclass.
6 -- None of the SELECTs here should ever find any matching entries,
7 -- so the expected output is easy to maintain ;-).
8 -- A test failure indicates someone messed up an entry in the system tables.
10 -- NB: we assume the oidjoins test will have caught any dangling links,
11 -- that is OID or REGPROC fields that are not zero and do not match some
12 -- row in the linked-to table. However, if we want to enforce that a link
13 -- field can't be 0, we have to check it here.
15 -- NB: run this test earlier than the create_operator test, because
16 -- that test creates some bogus operators...
19 -- Helper functions to deal with cases where binary-coercible matches are
22 -- This should match IsBinaryCoercible() in parse_coerce.c.
23 create function binary_coercible(oid, oid) returns bool as $$
25 EXISTS(select 1 from pg_catalog.pg_cast where
26 castsource = $1 and casttarget = $2 and
27 castfunc = 0 and castcontext = 'i') OR
28 ($2 = 'pg_catalog.anyarray'::pg_catalog.regtype AND
29 EXISTS(select 1 from pg_catalog.pg_type where
30 oid = $1 and typelem != 0 and typlen = -1))
31 $$ language sql strict stable;
33 -- This one ignores castcontext, so it considers only physical equivalence
34 -- and not whether the coercion can be invoked implicitly.
35 create function physically_coercible(oid, oid) returns bool as $$
37 EXISTS(select 1 from pg_catalog.pg_cast where
38 castsource = $1 and casttarget = $2 and
40 ($2 = 'pg_catalog.anyarray'::pg_catalog.regtype AND
41 EXISTS(select 1 from pg_catalog.pg_type where
42 oid = $1 and typelem != 0 and typlen = -1))
43 $$ language sql strict stable;
45 -- **************** pg_proc ****************
47 -- Look for illegal values in pg_proc fields.
49 SELECT p1.oid, p1.proname
51 WHERE p1.prolang = 0 OR p1.prorettype = 0 OR
53 array_lower(p1.proargtypes, 1) != 0 OR
54 array_upper(p1.proargtypes, 1) != p1.pronargs-1 OR
55 0::oid = ANY (p1.proargtypes);
57 -- Look for conflicting proc definitions (same names and input datatypes).
58 -- (This test should be dead code now that we have the unique index
59 -- pg_proc_proname_args_nsp_index, but I'll leave it in anyway.)
61 SELECT p1.oid, p1.proname, p2.oid, p2.proname
62 FROM pg_proc AS p1, pg_proc AS p2
63 WHERE p1.oid != p2.oid AND
64 p1.proname = p2.proname AND
65 p1.pronargs = p2.pronargs AND
66 p1.proargtypes = p2.proargtypes;
68 -- Considering only built-in procs (prolang = 12), look for multiple uses
69 -- of the same internal function (ie, matching prosrc fields). It's OK to
70 -- have several entries with different pronames for the same internal function,
71 -- but conflicts in the number of arguments and other critical items should
72 -- be complained of. (We don't check data types here; see next query.)
73 -- Note: ignore aggregate functions here, since they all point to the same
74 -- dummy built-in function.
76 SELECT p1.oid, p1.proname, p2.oid, p2.proname
77 FROM pg_proc AS p1, pg_proc AS p2
78 WHERE p1.oid < p2.oid AND
79 p1.prosrc = p2.prosrc AND
80 p1.prolang = 12 AND p2.prolang = 12 AND
81 (p1.proisagg = false OR p2.proisagg = false) AND
82 (p1.prolang != p2.prolang OR
83 p1.proisagg != p2.proisagg OR
84 p1.prosecdef != p2.prosecdef OR
85 p1.proisstrict != p2.proisstrict OR
86 p1.proretset != p2.proretset OR
87 p1.provolatile != p2.provolatile OR
88 p1.pronargs != p2.pronargs);
90 -- Look for uses of different type OIDs in the argument/result type fields
91 -- for different aliases of the same built-in function.
92 -- This indicates that the types are being presumed to be binary-equivalent,
93 -- or that the built-in function is prepared to deal with different types.
94 -- That's not wrong, necessarily, but we make lists of all the types being
95 -- so treated. Note that the expected output of this part of the test will
96 -- need to be modified whenever new pairs of types are made binary-equivalent,
97 -- or when new polymorphic built-in functions are added!
98 -- Note: ignore aggregate functions here, since they all point to the same
99 -- dummy built-in function.
101 SELECT DISTINCT p1.prorettype, p2.prorettype
102 FROM pg_proc AS p1, pg_proc AS p2
103 WHERE p1.oid != p2.oid AND
104 p1.prosrc = p2.prosrc AND
105 p1.prolang = 12 AND p2.prolang = 12 AND
106 NOT p1.proisagg AND NOT p2.proisagg AND
107 (p1.prorettype < p2.prorettype);
109 SELECT DISTINCT p1.proargtypes[0], p2.proargtypes[0]
110 FROM pg_proc AS p1, pg_proc AS p2
111 WHERE p1.oid != p2.oid AND
112 p1.prosrc = p2.prosrc AND
113 p1.prolang = 12 AND p2.prolang = 12 AND
114 NOT p1.proisagg AND NOT p2.proisagg AND
115 (p1.proargtypes[0] < p2.proargtypes[0]);
117 SELECT DISTINCT p1.proargtypes[1], p2.proargtypes[1]
118 FROM pg_proc AS p1, pg_proc AS p2
119 WHERE p1.oid != p2.oid AND
120 p1.prosrc = p2.prosrc AND
121 p1.prolang = 12 AND p2.prolang = 12 AND
122 NOT p1.proisagg AND NOT p2.proisagg AND
123 (p1.proargtypes[1] < p2.proargtypes[1]);
125 SELECT DISTINCT p1.proargtypes[2], p2.proargtypes[2]
126 FROM pg_proc AS p1, pg_proc AS p2
127 WHERE p1.oid != p2.oid AND
128 p1.prosrc = p2.prosrc AND
129 p1.prolang = 12 AND p2.prolang = 12 AND
130 NOT p1.proisagg AND NOT p2.proisagg AND
131 (p1.proargtypes[2] < p2.proargtypes[2]);
133 SELECT DISTINCT p1.proargtypes[3], p2.proargtypes[3]
134 FROM pg_proc AS p1, pg_proc AS p2
135 WHERE p1.oid != p2.oid AND
136 p1.prosrc = p2.prosrc AND
137 p1.prolang = 12 AND p2.prolang = 12 AND
138 NOT p1.proisagg AND NOT p2.proisagg AND
139 (p1.proargtypes[3] < p2.proargtypes[3]);
141 SELECT DISTINCT p1.proargtypes[4], p2.proargtypes[4]
142 FROM pg_proc AS p1, pg_proc AS p2
143 WHERE p1.oid != p2.oid AND
144 p1.prosrc = p2.prosrc AND
145 p1.prolang = 12 AND p2.prolang = 12 AND
146 NOT p1.proisagg AND NOT p2.proisagg AND
147 (p1.proargtypes[4] < p2.proargtypes[4]);
149 SELECT DISTINCT p1.proargtypes[5], p2.proargtypes[5]
150 FROM pg_proc AS p1, pg_proc AS p2
151 WHERE p1.oid != p2.oid AND
152 p1.prosrc = p2.prosrc AND
153 p1.prolang = 12 AND p2.prolang = 12 AND
154 NOT p1.proisagg AND NOT p2.proisagg AND
155 (p1.proargtypes[5] < p2.proargtypes[5]);
157 SELECT DISTINCT p1.proargtypes[6], p2.proargtypes[6]
158 FROM pg_proc AS p1, pg_proc AS p2
159 WHERE p1.oid != p2.oid AND
160 p1.prosrc = p2.prosrc AND
161 p1.prolang = 12 AND p2.prolang = 12 AND
162 NOT p1.proisagg AND NOT p2.proisagg AND
163 (p1.proargtypes[6] < p2.proargtypes[6]);
165 SELECT DISTINCT p1.proargtypes[7], p2.proargtypes[7]
166 FROM pg_proc AS p1, pg_proc AS p2
167 WHERE p1.oid != p2.oid AND
168 p1.prosrc = p2.prosrc AND
169 p1.prolang = 12 AND p2.prolang = 12 AND
170 NOT p1.proisagg AND NOT p2.proisagg AND
171 (p1.proargtypes[7] < p2.proargtypes[7]);
173 -- Look for functions that return type "internal" and do not have any
174 -- "internal" argument. Such a function would be a security hole since
175 -- it might be used to call an internal function from an SQL command.
176 -- As of 7.3 this query should find only internal_in.
178 SELECT p1.oid, p1.proname
180 WHERE p1.prorettype = 'internal'::regtype AND NOT
181 'internal'::regtype = ANY (p1.proargtypes);
184 -- **************** pg_cast ****************
186 -- Catch bogus values in pg_cast columns (other than cases detected by
191 WHERE castsource = 0 OR casttarget = 0 OR castcontext NOT IN ('e', 'a', 'i');
193 -- Look for casts to/from the same type that aren't length coercion functions.
194 -- (We assume they are length coercions if they take multiple arguments.)
195 -- Such entries are not necessarily harmful, but they are useless.
199 WHERE castsource = casttarget AND castfunc = 0;
202 FROM pg_cast c, pg_proc p
203 WHERE c.castfunc = p.oid AND p.pronargs < 2 AND castsource = casttarget;
205 -- Look for cast functions that don't have the right signature. The
206 -- argument and result types in pg_proc must be the same as, or binary
207 -- compatible with, what it says in pg_cast.
208 -- As a special case, we allow casts from CHAR(n) that use functions
209 -- declared to take TEXT. This does not pass the binary-coercibility test
210 -- because CHAR(n)-to-TEXT normally invokes rtrim(). However, the results
211 -- are the same, so long as the function is one that ignores trailing blanks.
214 FROM pg_cast c, pg_proc p
215 WHERE c.castfunc = p.oid AND
216 (p.pronargs < 1 OR p.pronargs > 3
217 OR NOT (binary_coercible(c.castsource, p.proargtypes[0])
218 OR (c.castsource = 'character'::regtype AND
219 p.proargtypes[0] = 'text'::regtype))
220 OR NOT binary_coercible(p.prorettype, c.casttarget));
223 FROM pg_cast c, pg_proc p
224 WHERE c.castfunc = p.oid AND
225 ((p.pronargs > 1 AND p.proargtypes[1] != 'int4'::regtype) OR
226 (p.pronargs > 2 AND p.proargtypes[2] != 'bool'::regtype));
228 -- Look for binary compatible casts that do not have the reverse
229 -- direction registered as well, or where the reverse direction is not
230 -- also binary compatible. This is legal, but usually not intended.
232 -- As of 7.4, this finds the casts from text and varchar to bpchar, because
233 -- those are binary-compatible while the reverse way goes through rtrim().
235 -- As of 8.2, this finds the cast from cidr to inet, because that is a
236 -- trivial binary coercion while the other way goes through inet_to_cidr().
240 WHERE c.castfunc = 0 AND
241 NOT EXISTS (SELECT 1 FROM pg_cast k
242 WHERE k.castfunc = 0 AND
243 k.castsource = c.casttarget AND
244 k.casttarget = c.castsource);
246 -- **************** pg_operator ****************
248 -- Look for illegal values in pg_operator fields.
250 SELECT p1.oid, p1.oprname
251 FROM pg_operator as p1
252 WHERE (p1.oprkind != 'b' AND p1.oprkind != 'l' AND p1.oprkind != 'r') OR
253 p1.oprresult = 0 OR p1.oprcode = 0;
255 -- Look for missing or unwanted operand types
257 SELECT p1.oid, p1.oprname
258 FROM pg_operator as p1
259 WHERE (p1.oprleft = 0 and p1.oprkind != 'l') OR
260 (p1.oprleft != 0 and p1.oprkind = 'l') OR
261 (p1.oprright = 0 and p1.oprkind != 'r') OR
262 (p1.oprright != 0 and p1.oprkind = 'r');
264 -- Look for conflicting operator definitions (same names and input datatypes).
266 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
267 FROM pg_operator AS p1, pg_operator AS p2
268 WHERE p1.oid != p2.oid AND
269 p1.oprname = p2.oprname AND
270 p1.oprkind = p2.oprkind AND
271 p1.oprleft = p2.oprleft AND
272 p1.oprright = p2.oprright;
274 -- Look for commutative operators that don't commute.
275 -- DEFINITIONAL NOTE: If A.oprcom = B, then x A y has the same result as y B x.
276 -- We expect that B will always say that B.oprcom = A as well; that's not
277 -- inherently essential, but it would be inefficient not to mark it so.
279 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
280 FROM pg_operator AS p1, pg_operator AS p2
281 WHERE p1.oprcom = p2.oid AND
282 (p1.oprkind != 'b' OR
283 p1.oprleft != p2.oprright OR
284 p1.oprright != p2.oprleft OR
285 p1.oprresult != p2.oprresult OR
286 p1.oid != p2.oprcom);
288 -- Look for negatory operators that don't agree.
289 -- DEFINITIONAL NOTE: If A.oprnegate = B, then both A and B must yield
290 -- boolean results, and (x A y) == ! (x B y), or the equivalent for
291 -- single-operand operators.
292 -- We expect that B will always say that B.oprnegate = A as well; that's not
293 -- inherently essential, but it would be inefficient not to mark it so.
294 -- Also, A and B had better not be the same operator.
296 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
297 FROM pg_operator AS p1, pg_operator AS p2
298 WHERE p1.oprnegate = p2.oid AND
299 (p1.oprkind != p2.oprkind OR
300 p1.oprleft != p2.oprleft OR
301 p1.oprright != p2.oprright OR
302 p1.oprresult != 'bool'::regtype OR
303 p2.oprresult != 'bool'::regtype OR
304 p1.oid != p2.oprnegate OR
307 -- Look for mergejoin operators that don't match their links.
308 -- An lsortop/rsortop link leads from an '=' operator to the
309 -- sort operator ('<' operator) that's appropriate for
310 -- its left-side or right-side data type.
311 -- An ltcmpop/gtcmpop link leads from an '=' operator to the
312 -- '<' or '>' operator of the same input datatypes.
313 -- (If the '=' operator has identical L and R input datatypes,
314 -- then lsortop, rsortop, and ltcmpop are all the same operator.)
316 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
317 FROM pg_operator AS p1, pg_operator AS p2
318 WHERE p1.oprlsortop = p2.oid AND
319 (p1.oprname NOT IN ('=', '~=~') OR p2.oprname NOT IN ('<', '~<~') OR
320 p1.oprkind != 'b' OR p2.oprkind != 'b' OR
321 p1.oprleft != p2.oprleft OR
322 p1.oprleft != p2.oprright OR
323 p1.oprresult != 'bool'::regtype OR
324 p2.oprresult != 'bool'::regtype);
326 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
327 FROM pg_operator AS p1, pg_operator AS p2
328 WHERE p1.oprrsortop = p2.oid AND
329 (p1.oprname NOT IN ('=', '~=~') OR p2.oprname NOT IN ('<', '~<~') OR
330 p1.oprkind != 'b' OR p2.oprkind != 'b' OR
331 p1.oprright != p2.oprleft OR
332 p1.oprright != p2.oprright OR
333 p1.oprresult != 'bool'::regtype OR
334 p2.oprresult != 'bool'::regtype);
336 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
337 FROM pg_operator AS p1, pg_operator AS p2
338 WHERE p1.oprltcmpop = p2.oid AND
339 (p1.oprname NOT IN ('=', '~=~') OR p2.oprname NOT IN ('<', '~<~') OR
340 p1.oprkind != 'b' OR p2.oprkind != 'b' OR
341 p1.oprleft != p2.oprleft OR
342 p1.oprright != p2.oprright OR
343 p1.oprresult != 'bool'::regtype OR
344 p2.oprresult != 'bool'::regtype);
346 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
347 FROM pg_operator AS p1, pg_operator AS p2
348 WHERE p1.oprgtcmpop = p2.oid AND
349 (p1.oprname NOT IN ('=', '~=~') OR p2.oprname NOT IN ('>', '~>~') OR
350 p1.oprkind != 'b' OR p2.oprkind != 'b' OR
351 p1.oprleft != p2.oprleft OR
352 p1.oprright != p2.oprright OR
353 p1.oprresult != 'bool'::regtype OR
354 p2.oprresult != 'bool'::regtype);
356 -- Make sure all four links are specified if any are.
358 SELECT p1.oid, p1.oprcode
359 FROM pg_operator AS p1
360 WHERE NOT ((oprlsortop = 0 AND oprrsortop = 0 AND
361 oprltcmpop = 0 AND oprgtcmpop = 0) OR
362 (oprlsortop != 0 AND oprrsortop != 0 AND
363 oprltcmpop != 0 AND oprgtcmpop != 0));
365 -- A mergejoinable = operator must have a commutator (usually itself).
367 SELECT p1.oid, p1.oprname FROM pg_operator AS p1
368 WHERE p1.oprlsortop != 0 AND
371 -- Mergejoinable operators across datatypes must come in closed sets, that
372 -- is if you provide int2 = int4 and int4 = int8 then you must also provide
373 -- int2 = int8 (and commutators of all these). This is necessary because
374 -- the planner tries to deduce additional qual clauses from transitivity
375 -- of mergejoinable operators. If there are clauses int2var = int4var and
376 -- int4var = int8var, the planner will deduce int2var = int8var ... and it
377 -- had better have a way to represent it.
379 SELECT p1.oid, p2.oid FROM pg_operator AS p1, pg_operator AS p2
380 WHERE p1.oprlsortop != p1.oprrsortop AND
381 p1.oprrsortop = p2.oprlsortop AND
382 p2.oprlsortop != p2.oprrsortop AND
383 NOT EXISTS (SELECT 1 FROM pg_operator p3 WHERE
384 p3.oprlsortop = p1.oprlsortop AND p3.oprrsortop = p2.oprrsortop);
387 -- Hashing only works on simple equality operators "type = sametype",
388 -- since the hash itself depends on the bitwise representation of the type.
389 -- Check that allegedly hashable operators look like they might be "=".
391 SELECT p1.oid, p1.oprname
392 FROM pg_operator AS p1
393 WHERE p1.oprcanhash AND NOT
394 (p1.oprkind = 'b' AND p1.oprresult = 'bool'::regtype AND
395 p1.oprleft = p1.oprright AND p1.oprname IN ('=', '~=~') AND
398 -- In 6.5 we accepted hashable array equality operators when the array element
399 -- type is hashable. However, what we actually need to make hashjoin work on
400 -- an array is a hashable element type *and* no padding between elements in
401 -- the array storage (or, perhaps, guaranteed-zero padding). Currently,
402 -- since the padding code in arrayfuncs.c is pretty bogus, it seems safest
403 -- to just forbid hashjoin on array equality ops.
404 -- This should be reconsidered someday.
406 -- -- Look for array equality operators that are hashable when the underlying
407 -- -- type is not, or vice versa. This is presumably bogus.
409 -- SELECT p1.oid, p1.oprcanhash, p2.oid, p2.oprcanhash, t1.typname, t2.typname
410 -- FROM pg_operator AS p1, pg_operator AS p2, pg_type AS t1, pg_type AS t2
411 -- WHERE p1.oprname = '=' AND p1.oprleft = p1.oprright AND
412 -- p2.oprname = '=' AND p2.oprleft = p2.oprright AND
413 -- p1.oprleft = t1.oid AND p2.oprleft = t2.oid AND t1.typelem = t2.oid AND
414 -- p1.oprcanhash != p2.oprcanhash;
416 -- Substitute check: forbid hashable array ops, period.
417 SELECT p1.oid, p1.oprname
418 FROM pg_operator AS p1, pg_proc AS p2
419 WHERE p1.oprcanhash AND p1.oprcode = p2.oid AND p2.proname = 'array_eq';
421 -- Hashable operators should appear as members of hash index opclasses.
423 SELECT p1.oid, p1.oprname
424 FROM pg_operator AS p1
425 WHERE p1.oprcanhash AND NOT EXISTS
426 (SELECT 1 FROM pg_opclass op JOIN pg_amop p ON op.oid = amopclaid
427 WHERE opcamid = (SELECT oid FROM pg_am WHERE amname = 'hash') AND
432 SELECT p1.oid, p1.oprname, op.opcname
433 FROM pg_operator AS p1, pg_opclass op, pg_amop p
434 WHERE amopopr = p1.oid AND amopclaid = op.oid
435 AND opcamid = (SELECT oid FROM pg_am WHERE amname = 'hash')
436 AND NOT p1.oprcanhash;
438 -- Check that each operator defined in pg_operator matches its oprcode entry
439 -- in pg_proc. Easiest to do this separately for each oprkind.
441 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
442 FROM pg_operator AS p1, pg_proc AS p2
443 WHERE p1.oprcode = p2.oid AND
446 OR NOT binary_coercible(p2.prorettype, p1.oprresult)
447 OR NOT binary_coercible(p1.oprleft, p2.proargtypes[0])
448 OR NOT binary_coercible(p1.oprright, p2.proargtypes[1]));
450 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
451 FROM pg_operator AS p1, pg_proc AS p2
452 WHERE p1.oprcode = p2.oid AND
455 OR NOT binary_coercible(p2.prorettype, p1.oprresult)
456 OR NOT binary_coercible(p1.oprright, p2.proargtypes[0])
459 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
460 FROM pg_operator AS p1, pg_proc AS p2
461 WHERE p1.oprcode = p2.oid AND
464 OR NOT binary_coercible(p2.prorettype, p1.oprresult)
465 OR NOT binary_coercible(p1.oprleft, p2.proargtypes[0])
466 OR p1.oprright != 0);
468 -- If the operator is mergejoinable or hashjoinable, its underlying function
469 -- should not be volatile.
471 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
472 FROM pg_operator AS p1, pg_proc AS p2
473 WHERE p1.oprcode = p2.oid AND
474 (p1.oprlsortop != 0 OR p1.oprcanhash) AND
475 p2.provolatile = 'v';
477 -- If oprrest is set, the operator must return boolean,
478 -- and it must link to a proc with the right signature
479 -- to be a restriction selectivity estimator.
480 -- The proc signature we want is: float8 proc(internal, oid, internal, int4)
482 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
483 FROM pg_operator AS p1, pg_proc AS p2
484 WHERE p1.oprrest = p2.oid AND
485 (p1.oprresult != 'bool'::regtype OR
486 p2.prorettype != 'float8'::regtype OR p2.proretset OR
488 p2.proargtypes[0] != 'internal'::regtype OR
489 p2.proargtypes[1] != 'oid'::regtype OR
490 p2.proargtypes[2] != 'internal'::regtype OR
491 p2.proargtypes[3] != 'int4'::regtype);
493 -- If oprjoin is set, the operator must be a binary boolean op,
494 -- and it must link to a proc with the right signature
495 -- to be a join selectivity estimator.
496 -- The proc signature we want is: float8 proc(internal, oid, internal, int2)
498 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
499 FROM pg_operator AS p1, pg_proc AS p2
500 WHERE p1.oprjoin = p2.oid AND
501 (p1.oprkind != 'b' OR p1.oprresult != 'bool'::regtype OR
502 p2.prorettype != 'float8'::regtype OR p2.proretset OR
504 p2.proargtypes[0] != 'internal'::regtype OR
505 p2.proargtypes[1] != 'oid'::regtype OR
506 p2.proargtypes[2] != 'internal'::regtype OR
507 p2.proargtypes[3] != 'int2'::regtype);
509 -- **************** pg_aggregate ****************
511 -- Look for illegal values in pg_aggregate fields.
513 SELECT ctid, aggfnoid::oid
514 FROM pg_aggregate as p1
515 WHERE aggfnoid = 0 OR aggtransfn = 0 OR aggtranstype = 0;
517 -- Make sure the matching pg_proc entry is sensible, too.
519 SELECT a.aggfnoid::oid, p.proname
520 FROM pg_aggregate as a, pg_proc as p
521 WHERE a.aggfnoid = p.oid AND
522 (NOT p.proisagg OR p.proretset);
524 -- Make sure there are no proisagg pg_proc entries without matches.
529 NOT EXISTS (SELECT 1 FROM pg_aggregate a WHERE a.aggfnoid = p.oid);
531 -- If there is no finalfn then the output type must be the transtype.
533 SELECT a.aggfnoid::oid, p.proname
534 FROM pg_aggregate as a, pg_proc as p
535 WHERE a.aggfnoid = p.oid AND
536 a.aggfinalfn = 0 AND p.prorettype != a.aggtranstype;
538 -- Cross-check transfn against its entry in pg_proc.
539 -- NOTE: use physically_coercible here, not binary_coercible, because
540 -- max and min on abstime are implemented using int4larger/int4smaller.
541 SELECT a.aggfnoid::oid, p.proname, ptr.oid, ptr.proname
542 FROM pg_aggregate AS a, pg_proc AS p, pg_proc AS ptr
543 WHERE a.aggfnoid = p.oid AND
544 a.aggtransfn = ptr.oid AND
546 OR NOT (ptr.pronargs = p.pronargs + 1)
547 OR NOT physically_coercible(ptr.prorettype, a.aggtranstype)
548 OR NOT physically_coercible(a.aggtranstype, ptr.proargtypes[0])
549 OR (p.pronargs > 0 AND
550 NOT physically_coercible(p.proargtypes[0], ptr.proargtypes[1]))
551 OR (p.pronargs > 1 AND
552 NOT physically_coercible(p.proargtypes[1], ptr.proargtypes[2]))
553 OR (p.pronargs > 2 AND
554 NOT physically_coercible(p.proargtypes[2], ptr.proargtypes[3]))
555 -- we could carry the check further, but that's enough for now
558 -- Cross-check finalfn (if present) against its entry in pg_proc.
560 SELECT a.aggfnoid::oid, p.proname, pfn.oid, pfn.proname
561 FROM pg_aggregate AS a, pg_proc AS p, pg_proc AS pfn
562 WHERE a.aggfnoid = p.oid AND
563 a.aggfinalfn = pfn.oid AND
565 OR NOT binary_coercible(pfn.prorettype, p.prorettype)
567 OR NOT binary_coercible(a.aggtranstype, pfn.proargtypes[0]));
569 -- If transfn is strict then either initval should be non-NULL, or
570 -- input type should match transtype so that the first non-null input
571 -- can be assigned as the state value.
573 SELECT a.aggfnoid::oid, p.proname, ptr.oid, ptr.proname
574 FROM pg_aggregate AS a, pg_proc AS p, pg_proc AS ptr
575 WHERE a.aggfnoid = p.oid AND
576 a.aggtransfn = ptr.oid AND ptr.proisstrict AND
577 a.agginitval IS NULL AND
578 NOT binary_coercible(p.proargtypes[0], a.aggtranstype);
580 -- Cross-check aggsortop (if present) against pg_operator.
581 -- We expect to find only "<" for "min" and ">" for "max".
583 SELECT DISTINCT proname, oprname
584 FROM pg_operator AS o, pg_aggregate AS a, pg_proc AS p
585 WHERE a.aggfnoid = p.oid AND a.aggsortop = o.oid
588 -- Check datatypes match
590 SELECT a.aggfnoid::oid, o.oid
591 FROM pg_operator AS o, pg_aggregate AS a, pg_proc AS p
592 WHERE a.aggfnoid = p.oid AND a.aggsortop = o.oid AND
593 (oprkind != 'b' OR oprresult != 'boolean'::regtype
594 OR oprleft != p.proargtypes[0] OR oprright != p.proargtypes[0]);
596 -- Check operator is a suitable btree opclass member
598 SELECT a.aggfnoid::oid, o.oid
599 FROM pg_operator AS o, pg_aggregate AS a, pg_proc AS p
600 WHERE a.aggfnoid = p.oid AND a.aggsortop = o.oid AND
601 NOT EXISTS(SELECT 1 FROM pg_amop ao, pg_opclass oc
602 WHERE amopclaid = oc.oid AND amopsubtype = 0
603 AND amopopr = o.oid AND opcamid = 403
604 AND opcintype = o.oprleft AND opcdefault);
606 -- Check correspondence of btree strategies and names
608 SELECT DISTINCT proname, oprname, amopstrategy
609 FROM pg_operator AS o, pg_aggregate AS a, pg_proc AS p,
610 pg_amop as ao, pg_opclass oc
611 WHERE a.aggfnoid = p.oid AND a.aggsortop = o.oid AND
612 amopclaid = oc.oid AND amopopr = o.oid AND opcamid = 403
615 -- **************** pg_opclass ****************
617 -- Look for illegal values in pg_opclass fields
620 FROM pg_opclass as p1
621 WHERE p1.opcamid = 0 OR p1.opcintype = 0;
623 -- There should not be multiple entries in pg_opclass with opcdefault true
624 -- and the same opcamid/opcintype combination.
626 SELECT p1.oid, p2.oid
627 FROM pg_opclass AS p1, pg_opclass AS p2
628 WHERE p1.oid != p2.oid AND
629 p1.opcamid = p2.opcamid AND p1.opcintype = p2.opcintype AND
630 p1.opcdefault AND p2.opcdefault;
632 -- **************** pg_amop ****************
634 -- Look for illegal values in pg_amop fields
636 SELECT p1.amopclaid, p1.amopstrategy
638 WHERE p1.amopclaid = 0 OR p1.amopstrategy <= 0 OR p1.amopopr = 0;
640 -- Cross-check amopstrategy index against parent AM
642 SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.amname
643 FROM pg_amop AS p1, pg_am AS p2, pg_opclass AS p3
644 WHERE p1.amopclaid = p3.oid AND p3.opcamid = p2.oid AND
645 p1.amopstrategy > p2.amstrategies;
647 -- Detect missing pg_amop entries: should have as many strategy operators
648 -- as AM expects for each opclass for the AM. When nondefault subtypes are
649 -- present, enforce condition separately for each subtype.
650 -- We have to exclude GiST and GIN, unfortunately, since they haven't got
651 -- any fixed requirements about strategy operators.
653 SELECT p1.oid, p1.amname, p2.oid, p2.opcname, p3.amopsubtype
654 FROM pg_am AS p1, pg_opclass AS p2, pg_amop AS p3
655 WHERE p2.opcamid = p1.oid AND p3.amopclaid = p2.oid AND
656 p1.amname != 'gist' AND p1.amname != 'gin' AND
657 p1.amstrategies != (SELECT count(*) FROM pg_amop AS p4
658 WHERE p4.amopclaid = p2.oid AND
659 p4.amopsubtype = p3.amopsubtype);
661 -- Check that amopopr points at a reasonable-looking operator, ie a binary
662 -- operator yielding boolean.
664 SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.oprname
665 FROM pg_amop AS p1, pg_operator AS p2
666 WHERE p1.amopopr = p2.oid AND
667 (p2.oprkind != 'b' OR p2.oprresult != 'bool'::regtype);
669 -- Make a list of all the distinct operator names being used in particular
670 -- strategy slots. This is a bit hokey, since the list might need to change
671 -- in future releases, but it's an effective way of spotting mistakes such as
672 -- swapping two operators within a class.
674 SELECT DISTINCT opcamid, amopstrategy, oprname
675 FROM pg_amop p1 LEFT JOIN pg_opclass p2 ON amopclaid = p2.oid
676 LEFT JOIN pg_operator p3 ON amopopr = p3.oid
679 -- Check that all operators linked to by opclass entries have selectivity
680 -- estimators. This is not absolutely required, but it seems a reasonable
681 -- thing to insist on for all standard datatypes.
683 SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.oprname
684 FROM pg_amop AS p1, pg_operator AS p2
685 WHERE p1.amopopr = p2.oid AND
686 (p2.oprrest = 0 OR p2.oprjoin = 0);
688 -- Check that operator input types match the opclass
689 -- For 8.0, we require that oprleft match opcintype (possibly by coercion).
690 -- When amopsubtype is zero (default), oprright must equal oprleft;
691 -- when amopsubtype is not zero, oprright must equal amopsubtype.
693 SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.oprname, p3.opcname
694 FROM pg_amop AS p1, pg_operator AS p2, pg_opclass AS p3
695 WHERE p1.amopopr = p2.oid AND p1.amopclaid = p3.oid AND
696 NOT binary_coercible(p3.opcintype, p2.oprleft);
698 SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.oprname, p3.opcname
699 FROM pg_amop AS p1, pg_operator AS p2, pg_opclass AS p3
700 WHERE p1.amopopr = p2.oid AND p1.amopclaid = p3.oid AND
701 p1.amopsubtype = 0 AND
702 p2.oprleft != p2.oprright;
704 SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.oprname, p3.opcname
705 FROM pg_amop AS p1, pg_operator AS p2, pg_opclass AS p3
706 WHERE p1.amopopr = p2.oid AND p1.amopclaid = p3.oid AND
707 p1.amopsubtype != 0 AND
708 p1.amopsubtype != p2.oprright;
710 -- Operators that are primary members of opclasses must be immutable (else
711 -- it suggests that the index ordering isn't fixed). Operators that are
712 -- cross-type members need only be stable, since they are just shorthands
713 -- for index probe queries.
715 SELECT p1.amopclaid, p1.amopopr, p2.oprname, p3.prosrc
716 FROM pg_amop AS p1, pg_operator AS p2, pg_proc AS p3
717 WHERE p1.amopopr = p2.oid AND p2.oprcode = p3.oid AND
718 p1.amopsubtype = 0 AND
719 p3.provolatile != 'i';
721 SELECT p1.amopclaid, p1.amopopr, p2.oprname, p3.prosrc
722 FROM pg_amop AS p1, pg_operator AS p2, pg_proc AS p3
723 WHERE p1.amopopr = p2.oid AND p2.oprcode = p3.oid AND
724 p1.amopsubtype != 0 AND
725 p3.provolatile = 'v';
727 -- **************** pg_amproc ****************
729 -- Look for illegal values in pg_amproc fields
731 SELECT p1.amopclaid, p1.amprocnum
733 WHERE p1.amopclaid = 0 OR p1.amprocnum <= 0 OR p1.amproc = 0;
735 -- Cross-check amprocnum index against parent AM
737 SELECT p1.amopclaid, p1.amprocnum, p2.oid, p2.amname
738 FROM pg_amproc AS p1, pg_am AS p2, pg_opclass AS p3
739 WHERE p1.amopclaid = p3.oid AND p3.opcamid = p2.oid AND
740 p1.amprocnum > p2.amsupport;
742 -- Detect missing pg_amproc entries: should have as many support functions
743 -- as AM expects for each opclass for the AM. When nondefault subtypes are
744 -- present, enforce condition separately for each subtype.
746 SELECT p1.oid, p1.amname, p2.oid, p2.opcname, p3.amprocsubtype
747 FROM pg_am AS p1, pg_opclass AS p2, pg_amproc AS p3
748 WHERE p2.opcamid = p1.oid AND p3.amopclaid = p2.oid AND
749 p1.amsupport != (SELECT count(*) FROM pg_amproc AS p4
750 WHERE p4.amopclaid = p2.oid AND
751 p4.amprocsubtype = p3.amprocsubtype);
753 -- Unfortunately, we can't check the amproc link very well because the
754 -- signature of the function may be different for different support routines
755 -- or different base data types.
756 -- We can check that all the referenced instances of the same support
757 -- routine number take the same number of parameters, but that's about it
758 -- for a general check...
760 SELECT p1.amopclaid, p1.amprocnum,
763 p4.amopclaid, p4.amprocnum,
766 FROM pg_amproc AS p1, pg_proc AS p2, pg_opclass AS p3,
767 pg_amproc AS p4, pg_proc AS p5, pg_opclass AS p6
768 WHERE p1.amopclaid = p3.oid AND p4.amopclaid = p6.oid AND
769 p3.opcamid = p6.opcamid AND p1.amprocnum = p4.amprocnum AND
770 p1.amproc = p2.oid AND p4.amproc = p5.oid AND
771 (p2.proretset OR p5.proretset OR p2.pronargs != p5.pronargs);
773 -- For btree, though, we can do better since we know the support routines
774 -- must be of the form cmp(input, input) returns int4 in the default case
775 -- (subtype = 0), and cmp(input, subtype) returns int4 when subtype != 0.
777 SELECT p1.amopclaid, p1.amprocnum,
780 FROM pg_amproc AS p1, pg_proc AS p2, pg_opclass AS p3
781 WHERE p3.opcamid = (SELECT oid FROM pg_am WHERE amname = 'btree')
782 AND p1.amopclaid = p3.oid AND p1.amproc = p2.oid AND
783 amprocsubtype = 0 AND
789 OR NOT binary_coercible(opcintype, proargtypes[0])
790 OR proargtypes[0] != proargtypes[1]);
792 SELECT p1.amopclaid, p1.amprocnum,
795 FROM pg_amproc AS p1, pg_proc AS p2, pg_opclass AS p3
796 WHERE p3.opcamid = (SELECT oid FROM pg_am WHERE amname = 'btree')
797 AND p1.amopclaid = p3.oid AND p1.amproc = p2.oid AND
798 amprocsubtype != 0 AND
804 OR NOT binary_coercible(opcintype, proargtypes[0])
805 OR proargtypes[1] != amprocsubtype);
807 -- For hash we can also do a little better: the support routines must be
808 -- of the form hash(something) returns int4. Ideally we'd check that the
809 -- opcintype is binary-coercible to the function's input, but there are
810 -- enough cases where that fails that I'll just leave out the check for now.
812 SELECT p1.amopclaid, p1.amprocnum,
815 FROM pg_amproc AS p1, pg_proc AS p2, pg_opclass AS p3
816 WHERE p3.opcamid = (SELECT oid FROM pg_am WHERE amname = 'hash')
817 AND p1.amopclaid = p3.oid AND p1.amproc = p2.oid AND
823 -- OR NOT physically_coercible(opcintype, proargtypes[0])
826 -- Support routines that are primary members of opclasses must be immutable
827 -- (else it suggests that the index ordering isn't fixed). But cross-type
828 -- members need only be stable, since they are just shorthands
829 -- for index probe queries.
831 SELECT p1.amopclaid, p1.amproc, p2.prosrc
832 FROM pg_amproc AS p1, pg_proc AS p2
833 WHERE p1.amproc = p2.oid AND
834 p1.amprocsubtype = 0 AND
835 p2.provolatile != 'i';
837 SELECT p1.amopclaid, p1.amproc, p2.prosrc
838 FROM pg_amproc AS p1, pg_proc AS p2
839 WHERE p1.amproc = p2.oid AND
840 p1.amprocsubtype != 0 AND
841 p2.provolatile = 'v';