]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/opr_sanity.sql
Revise handling of index-type-specific indexscan cost estimation, per
[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_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 -- NOTE hardwired assumptions about standard types:
19 --                type bool has OID 16
20 --                type float8 has OID 701
21 --
22
23 -- **************** pg_proc ****************
24
25 -- Look for illegal values in pg_proc fields.
26 -- NOTE: currently there are a few pg_proc entries that have prorettype = 0.
27 -- Someday that ought to be cleaned up.
28
29 SELECT p1.oid, p1.proname
30 FROM pg_proc as p1
31 WHERE (p1.prolang = 0 OR p1.prorettype = 0 OR
32     p1.pronargs < 0 OR p1.pronargs > 9)
33         AND p1.proname !~ '^pl[^_]+_call_handler$'
34         AND p1.proname !~ '^RI_FKey_'
35         AND p1.proname !~ 'costestimate$'
36         AND p1.proname != 'update_pg_pwd';
37
38 -- Look for conflicting proc definitions (same names and input datatypes).
39
40 SELECT p1.oid, p1.proname, p2.oid, p2.proname
41 FROM pg_proc AS p1, pg_proc AS p2
42 WHERE p1.oid != p2.oid AND
43     p1.proname = p2.proname AND
44     p1.pronargs = p2.pronargs AND
45     p1.proargtypes = p2.proargtypes;
46
47 -- Considering only built-in procs (prolang = 11), look for multiple uses
48 -- of the same internal function (ie, matching prosrc fields).  It's OK to
49 -- have several entries with different pronames for the same internal function,
50 -- but conflicts in the number of arguments and other critical items should
51 -- be complained of.
52
53 SELECT p1.oid, p1.proname, p2.oid, p2.proname
54 FROM pg_proc AS p1, pg_proc AS p2
55 WHERE p1.oid != p2.oid AND
56     p1.prosrc = p2.prosrc AND p1.prolang = 11 AND p2.prolang = 11 AND
57     (p1.proisinh != p2.proisinh OR
58      p1.proistrusted != p2.proistrusted OR
59      p1.proiscachable != p2.proiscachable OR
60      p1.pronargs != p2.pronargs OR
61      p1.proretset != p2.proretset);
62
63 -- Look for uses of different type OIDs in the argument/result type fields
64 -- for different aliases of the same built-in function.
65 -- This indicates that the types are being presumed to be binary-equivalent.
66 -- That's not wrong, necessarily, but we make lists of all the types being
67 -- so treated.  Note that the expected output of this part of the test will
68 -- need to be modified whenever new pairs of types are made binary-equivalent!
69
70 SELECT DISTINCT p1.prorettype, p2.prorettype
71 FROM pg_proc AS p1, pg_proc AS p2
72 WHERE p1.oid != p2.oid AND
73     p1.prosrc = p2.prosrc AND p1.prolang = 11 AND p2.prolang = 11 AND
74     (p1.prorettype < p2.prorettype);
75
76 SELECT DISTINCT p1.proargtypes[0], p2.proargtypes[0]
77 FROM pg_proc AS p1, pg_proc AS p2
78 WHERE p1.oid != p2.oid AND
79     p1.prosrc = p2.prosrc AND p1.prolang = 11 AND p2.prolang = 11 AND
80     (p1.proargtypes[0] < p2.proargtypes[0]);
81
82 SELECT DISTINCT p1.proargtypes[1], p2.proargtypes[1]
83 FROM pg_proc AS p1, pg_proc AS p2
84 WHERE p1.oid != p2.oid AND
85     p1.prosrc = p2.prosrc AND p1.prolang = 11 AND p2.prolang = 11 AND
86     (p1.proargtypes[1] < p2.proargtypes[1]);
87
88 SELECT DISTINCT p1.proargtypes[2], p2.proargtypes[2]
89 FROM pg_proc AS p1, pg_proc AS p2
90 WHERE p1.oid != p2.oid AND
91     p1.prosrc = p2.prosrc AND p1.prolang = 11 AND p2.prolang = 11 AND
92     (p1.proargtypes[2] < p2.proargtypes[2]);
93
94 SELECT DISTINCT p1.proargtypes[3], p2.proargtypes[3]
95 FROM pg_proc AS p1, pg_proc AS p2
96 WHERE p1.oid != p2.oid AND
97     p1.prosrc = p2.prosrc AND p1.prolang = 11 AND p2.prolang = 11 AND
98     (p1.proargtypes[3] < p2.proargtypes[3]);
99
100 SELECT DISTINCT p1.proargtypes[4], p2.proargtypes[4]
101 FROM pg_proc AS p1, pg_proc AS p2
102 WHERE p1.oid != p2.oid AND
103     p1.prosrc = p2.prosrc AND p1.prolang = 11 AND p2.prolang = 11 AND
104     (p1.proargtypes[4] < p2.proargtypes[4]);
105
106 SELECT DISTINCT p1.proargtypes[5], p2.proargtypes[5]
107 FROM pg_proc AS p1, pg_proc AS p2
108 WHERE p1.oid != p2.oid AND
109     p1.prosrc = p2.prosrc AND p1.prolang = 11 AND p2.prolang = 11 AND
110     (p1.proargtypes[5] < p2.proargtypes[5]);
111
112 SELECT DISTINCT p1.proargtypes[6], p2.proargtypes[6]
113 FROM pg_proc AS p1, pg_proc AS p2
114 WHERE p1.oid != p2.oid AND
115     p1.prosrc = p2.prosrc AND p1.prolang = 11 AND p2.prolang = 11 AND
116     (p1.proargtypes[6] < p2.proargtypes[6]);
117
118 SELECT DISTINCT p1.proargtypes[7], p2.proargtypes[7]
119 FROM pg_proc AS p1, pg_proc AS p2
120 WHERE p1.oid != p2.oid AND
121     p1.prosrc = p2.prosrc AND p1.prolang = 11 AND p2.prolang = 11 AND
122     (p1.proargtypes[7] < p2.proargtypes[7]);
123
124 -- **************** pg_operator ****************
125
126 -- Look for illegal values in pg_operator fields.
127
128 SELECT p1.oid, p1.oprname
129 FROM pg_operator as p1
130 WHERE (p1.oprkind != 'b' AND p1.oprkind != 'l' AND p1.oprkind != 'r') OR
131     p1.oprresult = 0 OR p1.oprcode = 0;
132
133 -- Look for missing or unwanted operand types
134
135 SELECT p1.oid, p1.oprname
136 FROM pg_operator as p1
137 WHERE (p1.oprleft = 0 and p1.oprkind != 'l') OR
138     (p1.oprleft != 0 and p1.oprkind = 'l') OR
139     (p1.oprright = 0 and p1.oprkind != 'r') OR
140     (p1.oprright != 0 and p1.oprkind = 'r');
141
142 -- Look for conflicting operator definitions (same names and input datatypes).
143
144 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
145 FROM pg_operator AS p1, pg_operator AS p2
146 WHERE p1.oid != p2.oid AND
147     p1.oprname = p2.oprname AND
148     p1.oprkind = p2.oprkind AND
149     p1.oprleft = p2.oprleft AND
150     p1.oprright = p2.oprright;
151
152 -- Look for commutative operators that don't commute.
153 -- DEFINITIONAL NOTE: If A.oprcom = B, then x A y has the same result as y B x.
154 -- We expect that B will always say that B.oprcom = A as well; that's not
155 -- inherently essential, but it would be inefficient not to mark it so.
156
157 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
158 FROM pg_operator AS p1, pg_operator AS p2
159 WHERE p1.oprcom = p2.oid AND
160     (p1.oprkind != 'b' OR
161      p1.oprleft != p2.oprright OR
162      p1.oprright != p2.oprleft OR
163      p1.oprresult != p2.oprresult OR
164      p1.oid != p2.oprcom);
165
166 -- Look for negatory operators that don't agree.
167 -- DEFINITIONAL NOTE: If A.oprnegate = B, then both A and B must yield
168 -- boolean results, and (x A y) == ! (x B y), or the equivalent for
169 -- single-operand operators.
170 -- We expect that B will always say that B.oprnegate = A as well; that's not
171 -- inherently essential, but it would be inefficient not to mark it so.
172
173 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
174 FROM pg_operator AS p1, pg_operator AS p2
175 WHERE p1.oprnegate = p2.oid AND
176     (p1.oprkind != p2.oprkind OR
177      p1.oprleft != p2.oprleft OR
178      p1.oprright != p2.oprright OR
179      p1.oprresult != 16 OR
180      p2.oprresult != 16 OR
181      p1.oid != p2.oprnegate);
182
183 -- Look for mergejoin operators that don't match their links.
184 -- A mergejoin link leads from an '=' operator to the
185 -- sort operator ('<' operator) that's appropriate for
186 -- its left-side or right-side data type.
187
188 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
189 FROM pg_operator AS p1, pg_operator AS p2
190 WHERE p1.oprlsortop = p2.oid AND
191     (p1.oprname != '=' OR p2.oprname != '<' OR
192      p1.oprkind != 'b' OR p2.oprkind != 'b' OR
193      p1.oprleft != p2.oprleft OR
194      p1.oprleft != p2.oprright OR
195      p1.oprresult != 16 OR
196      p2.oprresult != 16 OR
197      p1.oprrsortop = 0);
198
199 SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
200 FROM pg_operator AS p1, pg_operator AS p2
201 WHERE p1.oprrsortop = p2.oid AND
202     (p1.oprname != '=' OR p2.oprname != '<' OR
203      p1.oprkind != 'b' OR p2.oprkind != 'b' OR
204      p1.oprright != p2.oprleft OR
205      p1.oprright != p2.oprright OR
206      p1.oprresult != 16 OR
207      p2.oprresult != 16 OR
208      p1.oprlsortop = 0);
209
210 -- A mergejoinable = operator must have a commutator (usually itself)
211 -- as well as corresponding < and > operators.  Note that the "corresponding"
212 -- operators have the same L and R input datatypes as the = operator,
213 -- whereas the operators linked to by oprlsortop and oprrsortop have input
214 -- datatypes L,L and R,R respectively.
215
216 SELECT p1.oid, p1.oprname FROM pg_operator AS p1
217 WHERE p1.oprlsortop != 0 AND
218       p1.oprcom = 0;
219
220 SELECT p1.oid, p1.oprname FROM pg_operator AS p1
221 WHERE p1.oprlsortop != 0 AND NOT
222       EXISTS(SELECT * FROM pg_operator AS p2 WHERE
223         p2.oprname = '<' AND
224         p2.oprleft = p1.oprleft AND
225         p2.oprright = p1.oprright AND
226         p2.oprkind = 'b');
227
228 SELECT p1.oid, p1.oprname FROM pg_operator AS p1
229 WHERE p1.oprlsortop != 0 AND NOT
230       EXISTS(SELECT * FROM pg_operator AS p2 WHERE
231         p2.oprname = '>' AND
232         p2.oprleft = p1.oprleft AND
233         p2.oprright = p1.oprright AND
234         p2.oprkind = 'b');
235
236 -- Hashing only works on simple equality operators "type = sametype",
237 -- since the hash itself depends on the bitwise representation of the type.
238 -- Check that allegedly hashable operators look like they might be "=".
239 -- NOTE: in 6.5, this search finds int4eqoid and oideqint4.  Until we have
240 -- some cleaner way of dealing with binary-equivalent types, just leave
241 -- those two tuples in the expected output.
242
243 SELECT p1.oid, p1.oprname
244 FROM pg_operator AS p1
245 WHERE p1.oprcanhash AND NOT
246     (p1.oprkind = 'b' AND p1.oprresult = 16 AND p1.oprleft = p1.oprright AND
247      p1.oprname = '=' AND p1.oprcom = p1.oid);
248
249 -- In 6.5 we accepted hashable array equality operators when the array element
250 -- type is hashable.  However, what we actually need to make hashjoin work on
251 -- an array is a hashable element type *and* no padding between elements in
252 -- the array storage (or, perhaps, guaranteed-zero padding).  Currently,
253 -- since the padding code in arrayfuncs.c is pretty bogus, it seems safest
254 -- to just forbid hashjoin on array equality ops.
255 -- This should be reconsidered someday.
256
257 -- -- Look for array equality operators that are hashable when the underlying
258 -- -- type is not, or vice versa.  This is presumably bogus.
259 -- 
260 -- SELECT p1.oid, p1.oprcanhash, p2.oid, p2.oprcanhash, t1.typname, t2.typname
261 -- FROM pg_operator AS p1, pg_operator AS p2, pg_type AS t1, pg_type AS t2
262 -- WHERE p1.oprname = '=' AND p1.oprleft = p1.oprright AND 
263 --     p2.oprname = '=' AND p2.oprleft = p2.oprright AND
264 --     p1.oprleft = t1.oid AND p2.oprleft = t2.oid AND t1.typelem = t2.oid AND
265 --     p1.oprcanhash != p2.oprcanhash;
266
267 -- Substitute check: forbid hashable array ops, period.
268 SELECT p1.oid, p1.oprname
269 FROM pg_operator AS p1, pg_proc AS p2
270 WHERE p1.oprcanhash AND p1.oprcode = p2.oid AND p2.proname = 'array_eq';
271
272 -- Check that each operator defined in pg_operator matches its oprcode entry
273 -- in pg_proc.  Easiest to do this separately for each oprkind.
274 -- FIXME: want to check that argument/result types match, but how to do that
275 -- in the face of binary-compatible types?
276
277 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
278 FROM pg_operator AS p1, pg_proc AS p2
279 WHERE p1.oprcode = p2.oid AND
280     p1.oprkind = 'b' AND
281     (p2.pronargs != 2
282 -- diked out until we find a way of marking binary-compatible types
283 -- OR
284 --     p1.oprresult != p2.prorettype OR
285 --     (p1.oprleft != p2.proargtypes[0] AND p2.proargtypes[0] != 0) OR
286 --     (p1.oprright != p2.proargtypes[1] AND p2.proargtypes[1] != 0)
287 );
288
289 -- These two selects can be left as-is because there are no binary-compatible
290 -- cases that they trip over, at least in 6.5:
291
292 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
293 FROM pg_operator AS p1, pg_proc AS p2
294 WHERE p1.oprcode = p2.oid AND
295     p1.oprkind = 'l' AND
296     (p2.pronargs != 1 OR
297      p1.oprresult != p2.prorettype OR
298      (p1.oprright != p2.proargtypes[0] AND p2.proargtypes[0] != 0) OR
299      p1.oprleft != 0);
300
301 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
302 FROM pg_operator AS p1, pg_proc AS p2
303 WHERE p1.oprcode = p2.oid AND
304     p1.oprkind = 'r' AND
305     (p2.pronargs != 1 OR
306      p1.oprresult != p2.prorettype OR
307      (p1.oprleft != p2.proargtypes[0] AND p2.proargtypes[0] != 0) OR
308      p1.oprright != 0);
309
310 -- If oprrest is set, the operator must return boolean,
311 -- and it must link to a proc with the right signature
312 -- to be a restriction selectivity estimator.
313 -- The proc signature we want is: float8 proc(oid, oid, int2, <any>, int4)
314
315 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
316 FROM pg_operator AS p1, pg_proc AS p2
317 WHERE p1.oprrest = p2.oid AND
318     (p1.oprresult != 16 OR
319      p2.prorettype != 701 OR p2.proretset OR
320      p2.pronargs != 5 OR
321      p2.proargtypes[0] != 26 OR p2.proargtypes[1] != 26 OR
322      p2.proargtypes[2] != 21 OR p2.proargtypes[3] != 0 OR
323      p2.proargtypes[4] != 23);
324
325 -- If oprjoin is set, the operator must be a binary boolean op,
326 -- and it must link to a proc with the right signature
327 -- to be a join selectivity estimator.
328 -- The proc signature we want is: float8 proc(oid, oid, int2, oid, int2)
329
330 SELECT p1.oid, p1.oprname, p2.oid, p2.proname
331 FROM pg_operator AS p1, pg_proc AS p2
332 WHERE p1.oprjoin = p2.oid AND
333     (p1.oprkind != 'b' OR p1.oprresult != 16 OR
334      p2.prorettype != 701 OR p2.proretset OR
335      p2.pronargs != 5 OR
336      p2.proargtypes[0] != 26 OR p2.proargtypes[1] != 26 OR
337      p2.proargtypes[2] != 21 OR p2.proargtypes[3] != 26 OR
338      p2.proargtypes[4] != 21);
339
340 -- **************** pg_aggregate ****************
341
342 -- Look for illegal values in pg_aggregate fields.
343 -- aggbasetype can only be 0 if transfn1 is not present (eg, count(*))
344 -- or itself takes a wild-card input; we check the latter case below.
345
346 SELECT p1.oid, p1.aggname
347 FROM pg_aggregate as p1
348 WHERE (p1.aggbasetype = 0 AND p1.aggtransfn1 != 0) OR aggfinaltype = 0;
349
350 -- Check combinations of transfer functions.
351 -- Although either transfn1 or transfn2 can be null,
352 -- it makes no sense for both to be.  And if both are defined,
353 -- presumably there should be a finalfn to combine their results.
354 -- We also check that transtypes are null just when corresponding
355 -- transfns are.  Also, if there is no finalfn then the output type
356 -- must be the transtype the result will be taken from.
357
358 SELECT p1.oid, p1.aggname
359 FROM pg_aggregate as p1
360 WHERE p1.aggtransfn1 = 0 AND p1.aggtransfn2 = 0;
361
362 SELECT p1.oid, p1.aggname
363 FROM pg_aggregate as p1
364 WHERE p1.aggtransfn1 != 0 AND p1.aggtransfn2 = 0 AND
365     (p1.aggtranstype1 = 0 OR p1.aggtranstype2 != 0 OR
366      (p1.aggfinalfn = 0 AND p1.aggfinaltype != p1.aggtranstype1));
367
368 SELECT p1.oid, p1.aggname
369 FROM pg_aggregate as p1
370 WHERE p1.aggtransfn1 = 0 AND p1.aggtransfn2 != 0 AND
371     (p1.aggtranstype1 != 0 OR p1.aggtranstype2 = 0 OR
372      (p1.aggfinalfn = 0 AND p1.aggfinaltype != p1.aggtranstype2));
373
374 SELECT p1.oid, p1.aggname
375 FROM pg_aggregate as p1
376 WHERE p1.aggtransfn1 != 0 AND p1.aggtransfn2 != 0 AND
377     (p1.aggtranstype1 = 0 OR p1.aggtranstype2 = 0 OR
378      p1.aggfinalfn = 0);
379
380 -- Cross-check transfn1 (if present) against its entry in pg_proc.
381 -- FIXME: what about binary-compatible types?
382
383 SELECT p1.oid, p1.aggname, p2.oid, p2.proname
384 FROM pg_aggregate AS p1, pg_proc AS p2
385 WHERE p1.aggtransfn1 = p2.oid AND
386     (p2.proretset OR p2.pronargs != 2
387 -- diked out until we find a way of marking binary-compatible types
388 -- OR
389 --     p1.aggtranstype1 != p2.prorettype OR
390 --     p1.aggtranstype1 != p2.proargtypes[0] OR
391 --     p1.aggbasetype != p2.proargtypes[1]
392 );
393
394 -- Cross-check transfn2 (if present) against its entry in pg_proc.
395 -- FIXME: what about binary-compatible types?
396
397 SELECT p1.oid, p1.aggname, p2.oid, p2.proname
398 FROM pg_aggregate AS p1, pg_proc AS p2
399 WHERE p1.aggtransfn2 = p2.oid AND
400     (p2.proretset OR p1.aggtranstype2 != p2.prorettype OR
401      p2.pronargs != 1 OR
402      p1.aggtranstype2 != p2.proargtypes[0]);
403
404 -- Cross-check finalfn (if present) against its entry in pg_proc.
405 -- FIXME: what about binary-compatible types?
406
407 SELECT p1.oid, p1.aggname, p2.oid, p2.proname
408 FROM pg_aggregate AS p1, pg_proc AS p2
409 WHERE p1.aggfinalfn = p2.oid AND
410     (p2.proretset OR p1.aggfinaltype != p2.prorettype OR
411      p2.pronargs != 2 OR
412      p1.aggtranstype1 != p2.proargtypes[0] OR
413      p1.aggtranstype2 != p2.proargtypes[1]);
414
415 -- **************** pg_amop ****************
416
417 -- Look for illegal values in pg_amop fields
418
419 SELECT p1.oid
420 FROM pg_amop as p1
421 WHERE p1.amopid = 0 OR p1.amopclaid = 0 OR p1.amopopr = 0 OR
422     p1.amopstrategy <= 0;
423
424 -- Look for duplicate pg_amop entries
425
426 SELECT p1.oid, p2.oid
427 FROM pg_amop AS p1, pg_amop AS p2
428 WHERE p1.oid != p2.oid AND
429     p1.amopid = p2.amopid AND
430     p1.amopclaid = p2.amopclaid AND
431     p1.amopstrategy = p2.amopstrategy;
432
433 -- Cross-check amopstrategy index against parent AM
434
435 SELECT p1.oid, p2.oid, p2.amname
436 FROM pg_amop AS p1, pg_am AS p2
437 WHERE p1.amopid = p2.oid AND p1.amopstrategy > p2.amstrategies;
438
439 -- Detect missing pg_amop entries: should have as many strategy functions
440 -- as AM expects for each opclass, unless there are none at all
441 -- (some opclasses only offer support for a limited set of AMs...)
442
443 SELECT p1.oid, p1.amname, p2.oid, p2.opcname
444 FROM pg_am AS p1, pg_opclass AS p2
445 WHERE p1.amstrategies != (SELECT count(*) FROM pg_amop AS p3
446                           WHERE p3.amopid = p1.oid AND p3.amopclaid = p2.oid)
447               AND EXISTS (SELECT * FROM pg_amop AS p3
448                           WHERE p3.amopid = p1.oid AND p3.amopclaid = p2.oid);
449
450 -- Check that amopopr points at a reasonable-looking operator
451
452 SELECT p1.oid, p2.oid, p2.oprname
453 FROM pg_amop AS p1, pg_operator AS p2
454 WHERE p1.amopopr = p2.oid AND
455     (p2.oprkind != 'b' OR p2.oprresult != 16);
456
457 -- If opclass is for a specific type, operator inputs should be of that type
458
459 SELECT p1.oid, p2.oid, p2.oprname, p3.oid, p3.opcname
460 FROM pg_amop AS p1, pg_operator AS p2, pg_opclass AS p3
461 WHERE p1.amopopr = p2.oid AND p1.amopclaid = p3.oid AND
462     p3.opcdeftype != 0 AND
463     (p3.opcdeftype != p2.oprleft OR p3.opcdeftype != p2.oprright);
464
465 -- **************** pg_amproc ****************
466
467 -- Look for illegal values in pg_amproc fields
468
469 SELECT p1.oid
470 FROM pg_amproc as p1
471 WHERE p1.amid = 0 OR p1.amopclaid = 0 OR p1.amproc = 0 OR
472     p1.amprocnum <= 0;
473
474 -- Look for duplicate pg_amproc entries
475
476 SELECT p1.oid, p2.oid
477 FROM pg_amproc AS p1, pg_amproc AS p2
478 WHERE p1.oid != p2.oid AND
479     p1.amid = p2.amid AND
480     p1.amopclaid = p2.amopclaid AND
481     p1.amprocnum = p2.amprocnum;
482
483 -- Cross-check amprocnum index against parent AM
484
485 SELECT p1.oid, p2.oid, p2.amname
486 FROM pg_amproc AS p1, pg_am AS p2
487 WHERE p1.amid = p2.oid AND p1.amprocnum > p2.amsupport;
488
489 -- Detect missing pg_amproc entries: should have as many support functions
490 -- as AM expects for each opclass, unless there are none at all
491 -- (some opclasses only offer support for a limited set of AMs...)
492
493 SELECT p1.oid, p1.amname, p2.oid, p2.opcname
494 FROM pg_am AS p1, pg_opclass AS p2
495 WHERE p1.amsupport != (SELECT count(*) FROM pg_amproc AS p3
496                        WHERE p3.amid = p1.oid AND p3.amopclaid = p2.oid)
497            AND EXISTS (SELECT * FROM pg_amproc AS p3
498                        WHERE p3.amid = p1.oid AND p3.amopclaid = p2.oid);
499
500 -- Unfortunately, we can't check the amproc link very well because the
501 -- signature of the function may be different for different support routines
502 -- or different base data types.
503 -- We can check that all the referenced instances of the same support
504 -- routine number take the same number of parameters, but that's about it...
505
506 SELECT p1.oid, p2.oid, p2.proname, p3.oid, p4.oid, p4.proname
507 FROM pg_amproc AS p1, pg_proc AS p2, pg_amproc AS p3, pg_proc AS p4
508 WHERE p1.oid != p3.oid AND
509     p1.amid = p3.amid AND p1.amprocnum = p3.amprocnum AND
510     p1.amproc = p2.oid AND p3.amproc = p4.oid AND
511     (p2.proretset OR p4.proretset OR p2.pronargs != p4.pronargs);