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