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_cast where
26
castsource = $1 and casttarget = $2 and
27
castfunc = 0 and castcontext = ''i'')'
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
34
EXISTS(select 1 from pg_cast where
35
castsource = $1 and casttarget = $2 and
39
-- **************** pg_proc ****************
41
-- Look for illegal values in pg_proc fields.
42
-- NOTE: in reality pronargs could be more than 10, but I'm too lazy to put
43
-- a larger number of proargtypes check clauses in here. If we ever have
44
-- more-than-10-arg functions in the standard catalogs, extend this query.
46
SELECT p1.oid, p1.proname
48
WHERE p1.prolang = 0 OR p1.prorettype = 0 OR
49
p1.pronargs < 0 OR p1.pronargs > 10 OR
50
(p1.proargtypes[0] = 0 AND p1.pronargs > 0) OR
51
(p1.proargtypes[1] = 0 AND p1.pronargs > 1) OR
52
(p1.proargtypes[2] = 0 AND p1.pronargs > 2) OR
53
(p1.proargtypes[3] = 0 AND p1.pronargs > 3) OR
54
(p1.proargtypes[4] = 0 AND p1.pronargs > 4) OR
55
(p1.proargtypes[5] = 0 AND p1.pronargs > 5) OR
56
(p1.proargtypes[6] = 0 AND p1.pronargs > 6) OR
57
(p1.proargtypes[7] = 0 AND p1.pronargs > 7) OR
58
(p1.proargtypes[8] = 0 AND p1.pronargs > 8) OR
59
(p1.proargtypes[9] = 0 AND p1.pronargs > 9);
61
-- Look for conflicting proc definitions (same names and input datatypes).
62
-- (This test should be dead code now that we have the unique index
63
-- pg_proc_proname_narg_type_index, but I'll leave it in anyway.)
65
SELECT p1.oid, p1.proname, p2.oid, p2.proname
66
FROM pg_proc AS p1, pg_proc AS p2
67
WHERE p1.oid != p2.oid AND
68
p1.proname = p2.proname AND
69
p1.pronargs = p2.pronargs AND
70
p1.proargtypes = p2.proargtypes;
72
-- Considering only built-in procs (prolang = 12), look for multiple uses
73
-- of the same internal function (ie, matching prosrc fields). It's OK to
74
-- have several entries with different pronames for the same internal function,
75
-- but conflicts in the number of arguments and other critical items should
78
SELECT p1.oid, p1.proname, p2.oid, p2.proname
79
FROM pg_proc AS p1, pg_proc AS p2
80
WHERE p1.oid != p2.oid AND
81
p1.prosrc = p2.prosrc AND
82
p1.prolang = 12 AND p2.prolang = 12 AND
83
(p1.prolang != p2.prolang OR
84
p1.proisagg != p2.proisagg OR
85
p1.prosecdef != p2.prosecdef OR
86
p1.proisstrict != p2.proisstrict OR
87
p1.proretset != p2.proretset OR
88
p1.provolatile != p2.provolatile OR
89
p1.pronargs != p2.pronargs);
91
-- Look for uses of different type OIDs in the argument/result type fields
92
-- for different aliases of the same built-in function.
93
-- This indicates that the types are being presumed to be binary-equivalent,
94
-- or that the built-in function is prepared to deal with different types.
95
-- That's not wrong, necessarily, but we make lists of all the types being
96
-- so treated. Note that the expected output of this part of the test will
97
-- need to be modified whenever new pairs of types are made binary-equivalent,
98
-- or when new polymorphic built-in functions are added!
99
-- Note: ignore aggregate functions here, since they all point to the same
100
-- dummy built-in function.
102
SELECT DISTINCT p1.prorettype, p2.prorettype
103
FROM pg_proc AS p1, pg_proc AS p2
104
WHERE p1.oid != p2.oid AND
105
p1.prosrc = p2.prosrc AND
106
p1.prolang = 12 AND p2.prolang = 12 AND
107
NOT p1.proisagg AND NOT p2.proisagg AND
108
(p1.prorettype < p2.prorettype);
110
SELECT DISTINCT p1.proargtypes[0], p2.proargtypes[0]
111
FROM pg_proc AS p1, pg_proc AS p2
112
WHERE p1.oid != p2.oid AND
113
p1.prosrc = p2.prosrc AND
114
p1.prolang = 12 AND p2.prolang = 12 AND
115
NOT p1.proisagg AND NOT p2.proisagg AND
116
(p1.proargtypes[0] < p2.proargtypes[0]);
118
SELECT DISTINCT p1.proargtypes[1], p2.proargtypes[1]
119
FROM pg_proc AS p1, pg_proc AS p2
120
WHERE p1.oid != p2.oid AND
121
p1.prosrc = p2.prosrc AND
122
p1.prolang = 12 AND p2.prolang = 12 AND
123
NOT p1.proisagg AND NOT p2.proisagg AND
124
(p1.proargtypes[1] < p2.proargtypes[1]);
126
SELECT DISTINCT p1.proargtypes[2], p2.proargtypes[2]
127
FROM pg_proc AS p1, pg_proc AS p2
128
WHERE p1.oid != p2.oid AND
129
p1.prosrc = p2.prosrc AND
130
p1.prolang = 12 AND p2.prolang = 12 AND
131
NOT p1.proisagg AND NOT p2.proisagg AND
132
(p1.proargtypes[2] < p2.proargtypes[2]);
134
SELECT DISTINCT p1.proargtypes[3], p2.proargtypes[3]
135
FROM pg_proc AS p1, pg_proc AS p2
136
WHERE p1.oid != p2.oid AND
137
p1.prosrc = p2.prosrc AND
138
p1.prolang = 12 AND p2.prolang = 12 AND
139
NOT p1.proisagg AND NOT p2.proisagg AND
140
(p1.proargtypes[3] < p2.proargtypes[3]);
142
SELECT DISTINCT p1.proargtypes[4], p2.proargtypes[4]
143
FROM pg_proc AS p1, pg_proc AS p2
144
WHERE p1.oid != p2.oid AND
145
p1.prosrc = p2.prosrc AND
146
p1.prolang = 12 AND p2.prolang = 12 AND
147
NOT p1.proisagg AND NOT p2.proisagg AND
148
(p1.proargtypes[4] < p2.proargtypes[4]);
150
SELECT DISTINCT p1.proargtypes[5], p2.proargtypes[5]
151
FROM pg_proc AS p1, pg_proc AS p2
152
WHERE p1.oid != p2.oid AND
153
p1.prosrc = p2.prosrc AND
154
p1.prolang = 12 AND p2.prolang = 12 AND
155
NOT p1.proisagg AND NOT p2.proisagg AND
156
(p1.proargtypes[5] < p2.proargtypes[5]);
158
SELECT DISTINCT p1.proargtypes[6], p2.proargtypes[6]
159
FROM pg_proc AS p1, pg_proc AS p2
160
WHERE p1.oid != p2.oid AND
161
p1.prosrc = p2.prosrc AND
162
p1.prolang = 12 AND p2.prolang = 12 AND
163
NOT p1.proisagg AND NOT p2.proisagg AND
164
(p1.proargtypes[6] < p2.proargtypes[6]);
166
SELECT DISTINCT p1.proargtypes[7], p2.proargtypes[7]
167
FROM pg_proc AS p1, pg_proc AS p2
168
WHERE p1.oid != p2.oid AND
169
p1.prosrc = p2.prosrc AND
170
p1.prolang = 12 AND p2.prolang = 12 AND
171
NOT p1.proisagg AND NOT p2.proisagg AND
172
(p1.proargtypes[7] < p2.proargtypes[7]);
174
-- Look for functions that return type "internal" and do not have any
175
-- "internal" argument. Such a function would be a security hole since
176
-- it might be used to call an internal function from an SQL command.
177
-- As of 7.3 this query should find only internal_in.
179
SELECT p1.oid, p1.proname
181
WHERE p1.prorettype = 'internal'::regtype AND NOT
182
('(' || oidvectortypes(p1.proargtypes) || ')') ~ '[^a-z0-9_]internal[^a-z0-9_]';
185
-- **************** pg_cast ****************
187
-- Catch bogus values in pg_cast columns (other than cases detected by
192
WHERE castsource = 0 OR casttarget = 0 OR castcontext NOT IN ('e', 'a', 'i');
194
-- Look for casts to/from the same type that aren't length coercion functions.
195
-- (We assume they are length coercions if they take multiple arguments.)
196
-- Such entries are not necessarily harmful, but they are useless.
200
WHERE castsource = casttarget AND castfunc = 0;
203
FROM pg_cast c, pg_proc p
204
WHERE c.castfunc = p.oid AND p.pronargs < 2 AND castsource = casttarget;
206
-- Look for cast functions that don't have the right signature. The
207
-- argument and result types in pg_proc must be the same as, or binary
208
-- compatible with, what it says in pg_cast.
209
-- As a special case, we allow casts from CHAR(n) that use functions
210
-- declared to take TEXT. This does not pass the binary-coercibility test
211
-- because CHAR(n)-to-TEXT normally invokes rtrim(). However, the results
212
-- are the same, so long as the function is one that ignores trailing blanks.
215
FROM pg_cast c, pg_proc p
216
WHERE c.castfunc = p.oid AND
217
(p.pronargs < 1 OR p.pronargs > 3
218
OR NOT (binary_coercible(c.castsource, p.proargtypes[0])
219
OR (c.castsource = 'character'::regtype AND
220
p.proargtypes[0] = 'text'::regtype))
221
OR NOT binary_coercible(p.prorettype, c.casttarget));
224
FROM pg_cast c, pg_proc p
225
WHERE c.castfunc = p.oid AND
226
((p.pronargs > 1 AND p.proargtypes[1] != 'int4'::regtype) OR
227
(p.pronargs > 2 AND p.proargtypes[2] != 'bool'::regtype));
229
-- Look for binary compatible casts that do not have the reverse
230
-- direction registered as well, or where the reverse direction is not
231
-- also binary compatible. This is legal, but usually not intended.
233
-- As of 7.4, this finds the casts from text and varchar to bpchar, because
234
-- those are binary-compatible while the reverse way goes through rtrim().
238
WHERE c.castfunc = 0 AND
239
NOT EXISTS (SELECT 1 FROM pg_cast k
240
WHERE k.castfunc = 0 AND
241
k.castsource = c.casttarget AND
242
k.casttarget = c.castsource);
244
-- **************** pg_operator ****************
246
-- Look for illegal values in pg_operator fields.
248
SELECT p1.oid, p1.oprname
249
FROM pg_operator as p1
250
WHERE (p1.oprkind != 'b' AND p1.oprkind != 'l' AND p1.oprkind != 'r') OR
251
p1.oprresult = 0 OR p1.oprcode = 0;
253
-- Look for missing or unwanted operand types
255
SELECT p1.oid, p1.oprname
256
FROM pg_operator as p1
257
WHERE (p1.oprleft = 0 and p1.oprkind != 'l') OR
258
(p1.oprleft != 0 and p1.oprkind = 'l') OR
259
(p1.oprright = 0 and p1.oprkind != 'r') OR
260
(p1.oprright != 0 and p1.oprkind = 'r');
262
-- Look for conflicting operator definitions (same names and input datatypes).
264
SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
265
FROM pg_operator AS p1, pg_operator AS p2
266
WHERE p1.oid != p2.oid AND
267
p1.oprname = p2.oprname AND
268
p1.oprkind = p2.oprkind AND
269
p1.oprleft = p2.oprleft AND
270
p1.oprright = p2.oprright;
272
-- Look for commutative operators that don't commute.
273
-- DEFINITIONAL NOTE: If A.oprcom = B, then x A y has the same result as y B x.
274
-- We expect that B will always say that B.oprcom = A as well; that's not
275
-- inherently essential, but it would be inefficient not to mark it so.
277
SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
278
FROM pg_operator AS p1, pg_operator AS p2
279
WHERE p1.oprcom = p2.oid AND
280
(p1.oprkind != 'b' OR
281
p1.oprleft != p2.oprright OR
282
p1.oprright != p2.oprleft OR
283
p1.oprresult != p2.oprresult OR
284
p1.oid != p2.oprcom);
286
-- Look for negatory operators that don't agree.
287
-- DEFINITIONAL NOTE: If A.oprnegate = B, then both A and B must yield
288
-- boolean results, and (x A y) == ! (x B y), or the equivalent for
289
-- single-operand operators.
290
-- We expect that B will always say that B.oprnegate = A as well; that's not
291
-- inherently essential, but it would be inefficient not to mark it so.
292
-- Also, A and B had better not be the same operator.
294
SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
295
FROM pg_operator AS p1, pg_operator AS p2
296
WHERE p1.oprnegate = p2.oid AND
297
(p1.oprkind != p2.oprkind OR
298
p1.oprleft != p2.oprleft OR
299
p1.oprright != p2.oprright OR
300
p1.oprresult != 'bool'::regtype OR
301
p2.oprresult != 'bool'::regtype OR
302
p1.oid != p2.oprnegate OR
305
-- Look for mergejoin operators that don't match their links.
306
-- An lsortop/rsortop link leads from an '=' operator to the
307
-- sort operator ('<' operator) that's appropriate for
308
-- its left-side or right-side data type.
309
-- An ltcmpop/gtcmpop link leads from an '=' operator to the
310
-- '<' or '>' operator of the same input datatypes.
311
-- (If the '=' operator has identical L and R input datatypes,
312
-- then lsortop, rsortop, and ltcmpop are all the same operator.)
314
SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
315
FROM pg_operator AS p1, pg_operator AS p2
316
WHERE p1.oprlsortop = p2.oid AND
317
(p1.oprname NOT IN ('=', '~=~') OR p2.oprname NOT IN ('<', '~<~') OR
318
p1.oprkind != 'b' OR p2.oprkind != 'b' OR
319
p1.oprleft != p2.oprleft OR
320
p1.oprleft != p2.oprright OR
321
p1.oprresult != 'bool'::regtype OR
322
p2.oprresult != 'bool'::regtype);
324
SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
325
FROM pg_operator AS p1, pg_operator AS p2
326
WHERE p1.oprrsortop = p2.oid AND
327
(p1.oprname NOT IN ('=', '~=~') OR p2.oprname NOT IN ('<', '~<~') OR
328
p1.oprkind != 'b' OR p2.oprkind != 'b' OR
329
p1.oprright != p2.oprleft OR
330
p1.oprright != p2.oprright OR
331
p1.oprresult != 'bool'::regtype OR
332
p2.oprresult != 'bool'::regtype);
334
SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
335
FROM pg_operator AS p1, pg_operator AS p2
336
WHERE p1.oprltcmpop = p2.oid AND
337
(p1.oprname NOT IN ('=', '~=~') OR p2.oprname NOT IN ('<', '~<~') OR
338
p1.oprkind != 'b' OR p2.oprkind != 'b' OR
339
p1.oprleft != p2.oprleft OR
340
p1.oprright != p2.oprright OR
341
p1.oprresult != 'bool'::regtype OR
342
p2.oprresult != 'bool'::regtype);
344
SELECT p1.oid, p1.oprcode, p2.oid, p2.oprcode
345
FROM pg_operator AS p1, pg_operator AS p2
346
WHERE p1.oprgtcmpop = p2.oid AND
347
(p1.oprname NOT IN ('=', '~=~') OR p2.oprname NOT IN ('>', '~>~') OR
348
p1.oprkind != 'b' OR p2.oprkind != 'b' OR
349
p1.oprleft != p2.oprleft OR
350
p1.oprright != p2.oprright OR
351
p1.oprresult != 'bool'::regtype OR
352
p2.oprresult != 'bool'::regtype);
354
-- Make sure all four links are specified if any are.
356
SELECT p1.oid, p1.oprcode
357
FROM pg_operator AS p1
358
WHERE NOT ((oprlsortop = 0 AND oprrsortop = 0 AND
359
oprltcmpop = 0 AND oprgtcmpop = 0) OR
360
(oprlsortop != 0 AND oprrsortop != 0 AND
361
oprltcmpop != 0 AND oprgtcmpop != 0));
363
-- A mergejoinable = operator must have a commutator (usually itself).
365
SELECT p1.oid, p1.oprname FROM pg_operator AS p1
366
WHERE p1.oprlsortop != 0 AND
369
-- Mergejoinable operators across datatypes must come in closed sets, that
370
-- is if you provide int2 = int4 and int4 = int8 then you must also provide
371
-- int2 = int8 (and commutators of all these). This is necessary because
372
-- the planner tries to deduce additional qual clauses from transitivity
373
-- of mergejoinable operators. If there are clauses int2var = int4var and
374
-- int4var = int8var, the planner will deduce int2var = int8var ... and it
375
-- had better have a way to represent it.
377
SELECT p1.oid, p2.oid FROM pg_operator AS p1, pg_operator AS p2
378
WHERE p1.oprlsortop != p1.oprrsortop AND
379
p1.oprrsortop = p2.oprlsortop AND
380
p2.oprlsortop != p2.oprrsortop AND
381
NOT EXISTS (SELECT 1 FROM pg_operator p3 WHERE
382
p3.oprlsortop = p1.oprlsortop AND p3.oprrsortop = p2.oprrsortop);
385
-- Hashing only works on simple equality operators "type = sametype",
386
-- since the hash itself depends on the bitwise representation of the type.
387
-- Check that allegedly hashable operators look like they might be "=".
389
SELECT p1.oid, p1.oprname
390
FROM pg_operator AS p1
391
WHERE p1.oprcanhash AND NOT
392
(p1.oprkind = 'b' AND p1.oprresult = 'bool'::regtype AND
393
p1.oprleft = p1.oprright AND p1.oprname IN ('=', '~=~') AND
396
-- In 6.5 we accepted hashable array equality operators when the array element
397
-- type is hashable. However, what we actually need to make hashjoin work on
398
-- an array is a hashable element type *and* no padding between elements in
399
-- the array storage (or, perhaps, guaranteed-zero padding). Currently,
400
-- since the padding code in arrayfuncs.c is pretty bogus, it seems safest
401
-- to just forbid hashjoin on array equality ops.
402
-- This should be reconsidered someday.
404
-- -- Look for array equality operators that are hashable when the underlying
405
-- -- type is not, or vice versa. This is presumably bogus.
407
-- SELECT p1.oid, p1.oprcanhash, p2.oid, p2.oprcanhash, t1.typname, t2.typname
408
-- FROM pg_operator AS p1, pg_operator AS p2, pg_type AS t1, pg_type AS t2
409
-- WHERE p1.oprname = '=' AND p1.oprleft = p1.oprright AND
410
-- p2.oprname = '=' AND p2.oprleft = p2.oprright AND
411
-- p1.oprleft = t1.oid AND p2.oprleft = t2.oid AND t1.typelem = t2.oid AND
412
-- p1.oprcanhash != p2.oprcanhash;
414
-- Substitute check: forbid hashable array ops, period.
415
SELECT p1.oid, p1.oprname
416
FROM pg_operator AS p1, pg_proc AS p2
417
WHERE p1.oprcanhash AND p1.oprcode = p2.oid AND p2.proname = 'array_eq';
419
-- Hashable operators should appear as members of hash index opclasses.
421
SELECT p1.oid, p1.oprname
422
FROM pg_operator AS p1
423
WHERE p1.oprcanhash AND NOT EXISTS
424
(SELECT 1 FROM pg_opclass op JOIN pg_amop p ON op.oid = amopclaid
425
WHERE opcamid = (SELECT oid FROM pg_am WHERE amname = 'hash') AND
430
SELECT p1.oid, p1.oprname, op.opcname
431
FROM pg_operator AS p1, pg_opclass op, pg_amop p
432
WHERE amopopr = p1.oid AND amopclaid = op.oid
433
AND opcamid = (SELECT oid FROM pg_am WHERE amname = 'hash')
434
AND NOT p1.oprcanhash;
436
-- Check that each operator defined in pg_operator matches its oprcode entry
437
-- in pg_proc. Easiest to do this separately for each oprkind.
439
SELECT p1.oid, p1.oprname, p2.oid, p2.proname
440
FROM pg_operator AS p1, pg_proc AS p2
441
WHERE p1.oprcode = p2.oid AND
444
OR NOT binary_coercible(p2.prorettype, p1.oprresult)
445
OR NOT binary_coercible(p1.oprleft, p2.proargtypes[0])
446
OR NOT binary_coercible(p1.oprright, p2.proargtypes[1]));
448
SELECT p1.oid, p1.oprname, p2.oid, p2.proname
449
FROM pg_operator AS p1, pg_proc AS p2
450
WHERE p1.oprcode = p2.oid AND
453
OR NOT binary_coercible(p2.prorettype, p1.oprresult)
454
OR NOT binary_coercible(p1.oprright, p2.proargtypes[0])
457
SELECT p1.oid, p1.oprname, p2.oid, p2.proname
458
FROM pg_operator AS p1, pg_proc AS p2
459
WHERE p1.oprcode = p2.oid AND
462
OR NOT binary_coercible(p2.prorettype, p1.oprresult)
463
OR NOT binary_coercible(p1.oprleft, p2.proargtypes[0])
464
OR p1.oprright != 0);
466
-- If the operator is mergejoinable or hashjoinable, its underlying function
467
-- should not be volatile.
469
SELECT p1.oid, p1.oprname, p2.oid, p2.proname
470
FROM pg_operator AS p1, pg_proc AS p2
471
WHERE p1.oprcode = p2.oid AND
472
(p1.oprlsortop != 0 OR p1.oprcanhash) AND
473
p2.provolatile = 'v';
475
-- If oprrest is set, the operator must return boolean,
476
-- and it must link to a proc with the right signature
477
-- to be a restriction selectivity estimator.
478
-- The proc signature we want is: float8 proc(internal, oid, internal, int4)
480
SELECT p1.oid, p1.oprname, p2.oid, p2.proname
481
FROM pg_operator AS p1, pg_proc AS p2
482
WHERE p1.oprrest = p2.oid AND
483
(p1.oprresult != 'bool'::regtype OR
484
p2.prorettype != 'float8'::regtype OR p2.proretset OR
486
p2.proargtypes[0] != 'internal'::regtype OR
487
p2.proargtypes[1] != 'oid'::regtype OR
488
p2.proargtypes[2] != 'internal'::regtype OR
489
p2.proargtypes[3] != 'int4'::regtype);
491
-- If oprjoin is set, the operator must be a binary boolean op,
492
-- and it must link to a proc with the right signature
493
-- to be a join selectivity estimator.
494
-- The proc signature we want is: float8 proc(internal, oid, internal, int2)
496
SELECT p1.oid, p1.oprname, p2.oid, p2.proname
497
FROM pg_operator AS p1, pg_proc AS p2
498
WHERE p1.oprjoin = p2.oid AND
499
(p1.oprkind != 'b' OR p1.oprresult != 'bool'::regtype OR
500
p2.prorettype != 'float8'::regtype OR p2.proretset OR
502
p2.proargtypes[0] != 'internal'::regtype OR
503
p2.proargtypes[1] != 'oid'::regtype OR
504
p2.proargtypes[2] != 'internal'::regtype OR
505
p2.proargtypes[3] != 'int2'::regtype);
507
-- **************** pg_aggregate ****************
509
-- Look for illegal values in pg_aggregate fields.
511
SELECT ctid, aggfnoid::oid
512
FROM pg_aggregate as p1
513
WHERE aggfnoid = 0 OR aggtransfn = 0 OR aggtranstype = 0;
515
-- Make sure the matching pg_proc entry is sensible, too.
517
SELECT a.aggfnoid::oid, p.proname
518
FROM pg_aggregate as a, pg_proc as p
519
WHERE a.aggfnoid = p.oid AND
520
(NOT p.proisagg OR p.pronargs != 1 OR p.proretset);
522
-- Make sure there are no proisagg pg_proc entries without matches.
527
NOT EXISTS (SELECT 1 FROM pg_aggregate a WHERE a.aggfnoid = p.oid);
529
-- If there is no finalfn then the output type must be the transtype.
531
SELECT a.aggfnoid::oid, p.proname
532
FROM pg_aggregate as a, pg_proc as p
533
WHERE a.aggfnoid = p.oid AND
534
a.aggfinalfn = 0 AND p.prorettype != a.aggtranstype;
536
-- Cross-check transfn against its entry in pg_proc.
537
-- NOTE: use physically_coercible here, not binary_coercible, because
538
-- max and min on abstime are implemented using int4larger/int4smaller.
539
SELECT a.aggfnoid::oid, p.proname, ptr.oid, ptr.proname
540
FROM pg_aggregate AS a, pg_proc AS p, pg_proc AS ptr
541
WHERE a.aggfnoid = p.oid AND
542
a.aggtransfn = ptr.oid AND
544
OR NOT physically_coercible(ptr.prorettype, a.aggtranstype)
545
OR NOT physically_coercible(a.aggtranstype, ptr.proargtypes[0])
546
OR NOT ((ptr.pronargs = 2 AND
547
physically_coercible(p.proargtypes[0], ptr.proargtypes[1]))
549
(ptr.pronargs = 1 AND
550
p.proargtypes[0] = '"any"'::regtype)));
552
-- Cross-check finalfn (if present) against its entry in pg_proc.
554
SELECT a.aggfnoid::oid, p.proname, pfn.oid, pfn.proname
555
FROM pg_aggregate AS a, pg_proc AS p, pg_proc AS pfn
556
WHERE a.aggfnoid = p.oid AND
557
a.aggfinalfn = pfn.oid AND
559
OR NOT binary_coercible(pfn.prorettype, p.prorettype)
561
OR NOT binary_coercible(a.aggtranstype, pfn.proargtypes[0]));
563
-- If transfn is strict then either initval should be non-NULL, or
564
-- input type should match transtype so that the first non-null input
565
-- can be assigned as the state value.
567
SELECT a.aggfnoid::oid, p.proname, ptr.oid, ptr.proname
568
FROM pg_aggregate AS a, pg_proc AS p, pg_proc AS ptr
569
WHERE a.aggfnoid = p.oid AND
570
a.aggtransfn = ptr.oid AND ptr.proisstrict AND
571
a.agginitval IS NULL AND
572
NOT binary_coercible(p.proargtypes[0], a.aggtranstype);
574
-- **************** pg_opclass ****************
576
-- Look for illegal values in pg_opclass fields
579
FROM pg_opclass as p1
580
WHERE p1.opcamid = 0 OR p1.opcintype = 0;
582
-- There should not be multiple entries in pg_opclass with opcdefault true
583
-- and the same opcamid/opcintype combination.
585
SELECT p1.oid, p2.oid
586
FROM pg_opclass AS p1, pg_opclass AS p2
587
WHERE p1.oid != p2.oid AND
588
p1.opcamid = p2.opcamid AND p1.opcintype = p2.opcintype AND
589
p1.opcdefault AND p2.opcdefault;
591
-- **************** pg_amop ****************
593
-- Look for illegal values in pg_amop fields
595
SELECT p1.amopclaid, p1.amopstrategy
597
WHERE p1.amopclaid = 0 OR p1.amopstrategy <= 0 OR p1.amopopr = 0;
599
-- Cross-check amopstrategy index against parent AM
601
SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.amname
602
FROM pg_amop AS p1, pg_am AS p2, pg_opclass AS p3
603
WHERE p1.amopclaid = p3.oid AND p3.opcamid = p2.oid AND
604
p1.amopstrategy > p2.amstrategies;
606
-- Detect missing pg_amop entries: should have as many strategy operators
607
-- as AM expects for each opclass for the AM. When nondefault subtypes are
608
-- present, enforce condition separately for each subtype.
610
SELECT p1.oid, p1.amname, p2.oid, p2.opcname, p3.amopsubtype
611
FROM pg_am AS p1, pg_opclass AS p2, pg_amop AS p3
612
WHERE p2.opcamid = p1.oid AND p3.amopclaid = p2.oid AND
613
p1.amstrategies != (SELECT count(*) FROM pg_amop AS p4
614
WHERE p4.amopclaid = p2.oid AND
615
p4.amopsubtype = p3.amopsubtype);
617
-- Check that amopopr points at a reasonable-looking operator, ie a binary
618
-- operator yielding boolean.
620
SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.oprname
621
FROM pg_amop AS p1, pg_operator AS p2
622
WHERE p1.amopopr = p2.oid AND
623
(p2.oprkind != 'b' OR p2.oprresult != 'bool'::regtype);
625
-- Make a list of all the distinct operator names being used in particular
626
-- strategy slots. This is a bit hokey, since the list might need to change
627
-- in future releases, but it's an effective way of spotting mistakes such as
628
-- swapping two operators within a class.
630
SELECT DISTINCT opcamid, amopstrategy, oprname
631
FROM pg_amop p1 LEFT JOIN pg_opclass p2 ON amopclaid = p2.oid
632
LEFT JOIN pg_operator p3 ON amopopr = p3.oid
635
-- Check that all operators linked to by opclass entries have selectivity
636
-- estimators. This is not absolutely required, but it seems a reasonable
637
-- thing to insist on for all standard datatypes.
639
SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.oprname
640
FROM pg_amop AS p1, pg_operator AS p2
641
WHERE p1.amopopr = p2.oid AND
642
(p2.oprrest = 0 OR p2.oprjoin = 0);
644
-- Check that operator input types match the opclass
645
-- For 8.0, we require that oprleft match opcintype (possibly by coercion).
646
-- When amopsubtype is zero (default), oprright must equal oprleft;
647
-- when amopsubtype is not zero, oprright must equal amopsubtype.
649
SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.oprname, p3.opcname
650
FROM pg_amop AS p1, pg_operator AS p2, pg_opclass AS p3
651
WHERE p1.amopopr = p2.oid AND p1.amopclaid = p3.oid AND
652
NOT binary_coercible(p3.opcintype, p2.oprleft);
654
SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.oprname, p3.opcname
655
FROM pg_amop AS p1, pg_operator AS p2, pg_opclass AS p3
656
WHERE p1.amopopr = p2.oid AND p1.amopclaid = p3.oid AND
657
p1.amopsubtype = 0 AND
658
p2.oprleft != p2.oprright;
660
SELECT p1.amopclaid, p1.amopopr, p2.oid, p2.oprname, p3.opcname
661
FROM pg_amop AS p1, pg_operator AS p2, pg_opclass AS p3
662
WHERE p1.amopopr = p2.oid AND p1.amopclaid = p3.oid AND
663
p1.amopsubtype != 0 AND
664
p1.amopsubtype != p2.oprright;
666
-- Operators that are primary members of opclasses must be immutable (else
667
-- it suggests that the index ordering isn't fixed). Operators that are
668
-- cross-type members need only be stable, since they are just shorthands
669
-- for index probe queries.
671
SELECT p1.amopclaid, p1.amopopr, p2.oprname, p3.prosrc
672
FROM pg_amop AS p1, pg_operator AS p2, pg_proc AS p3
673
WHERE p1.amopopr = p2.oid AND p2.oprcode = p3.oid AND
674
p1.amopsubtype = 0 AND
675
p3.provolatile != 'i';
677
SELECT p1.amopclaid, p1.amopopr, p2.oprname, p3.prosrc
678
FROM pg_amop AS p1, pg_operator AS p2, pg_proc AS p3
679
WHERE p1.amopopr = p2.oid AND p2.oprcode = p3.oid AND
680
p1.amopsubtype != 0 AND
681
p3.provolatile = 'v';
683
-- **************** pg_amproc ****************
685
-- Look for illegal values in pg_amproc fields
687
SELECT p1.amopclaid, p1.amprocnum
689
WHERE p1.amopclaid = 0 OR p1.amprocnum <= 0 OR p1.amproc = 0;
691
-- Cross-check amprocnum index against parent AM
693
SELECT p1.amopclaid, p1.amprocnum, p2.oid, p2.amname
694
FROM pg_amproc AS p1, pg_am AS p2, pg_opclass AS p3
695
WHERE p1.amopclaid = p3.oid AND p3.opcamid = p2.oid AND
696
p1.amprocnum > p2.amsupport;
698
-- Detect missing pg_amproc entries: should have as many support functions
699
-- as AM expects for each opclass for the AM. When nondefault subtypes are
700
-- present, enforce condition separately for each subtype.
702
SELECT p1.oid, p1.amname, p2.oid, p2.opcname, p3.amprocsubtype
703
FROM pg_am AS p1, pg_opclass AS p2, pg_amproc AS p3
704
WHERE p2.opcamid = p1.oid AND p3.amopclaid = p2.oid AND
705
p1.amsupport != (SELECT count(*) FROM pg_amproc AS p4
706
WHERE p4.amopclaid = p2.oid AND
707
p4.amprocsubtype = p3.amprocsubtype);
709
-- Unfortunately, we can't check the amproc link very well because the
710
-- signature of the function may be different for different support routines
711
-- or different base data types.
712
-- We can check that all the referenced instances of the same support
713
-- routine number take the same number of parameters, but that's about it
714
-- for a general check...
716
SELECT p1.amopclaid, p1.amprocnum,
719
p4.amopclaid, p4.amprocnum,
722
FROM pg_amproc AS p1, pg_proc AS p2, pg_opclass AS p3,
723
pg_amproc AS p4, pg_proc AS p5, pg_opclass AS p6
724
WHERE p1.amopclaid = p3.oid AND p4.amopclaid = p6.oid AND
725
p3.opcamid = p6.opcamid AND p1.amprocnum = p4.amprocnum AND
726
p1.amproc = p2.oid AND p4.amproc = p5.oid AND
727
(p2.proretset OR p5.proretset OR p2.pronargs != p5.pronargs);
729
-- For btree, though, we can do better since we know the support routines
730
-- must be of the form cmp(input, input) returns int4 in the default case
731
-- (subtype = 0), and cmp(input, subtype) returns int4 when subtype != 0.
733
SELECT p1.amopclaid, p1.amprocnum,
736
FROM pg_amproc AS p1, pg_proc AS p2, pg_opclass AS p3
737
WHERE p3.opcamid = (SELECT oid FROM pg_am WHERE amname = 'btree')
738
AND p1.amopclaid = p3.oid AND p1.amproc = p2.oid AND
739
amprocsubtype = 0 AND
745
OR NOT binary_coercible(opcintype, proargtypes[0])
746
OR proargtypes[0] != proargtypes[1]);
748
SELECT p1.amopclaid, p1.amprocnum,
751
FROM pg_amproc AS p1, pg_proc AS p2, pg_opclass AS p3
752
WHERE p3.opcamid = (SELECT oid FROM pg_am WHERE amname = 'btree')
753
AND p1.amopclaid = p3.oid AND p1.amproc = p2.oid AND
754
amprocsubtype != 0 AND
760
OR NOT binary_coercible(opcintype, proargtypes[0])
761
OR proargtypes[1] != amprocsubtype);
763
-- For hash we can also do a little better: the support routines must be
764
-- of the form hash(something) returns int4. Ideally we'd check that the
765
-- opcintype is binary-coercible to the function's input, but there are
766
-- enough cases where that fails that I'll just leave out the check for now.
768
SELECT p1.amopclaid, p1.amprocnum,
771
FROM pg_amproc AS p1, pg_proc AS p2, pg_opclass AS p3
772
WHERE p3.opcamid = (SELECT oid FROM pg_am WHERE amname = 'hash')
773
AND p1.amopclaid = p3.oid AND p1.amproc = p2.oid AND
779
-- OR NOT physically_coercible(opcintype, proargtypes[0])
782
-- Support routines that are primary members of opclasses must be immutable
783
-- (else it suggests that the index ordering isn't fixed). But cross-type
784
-- members need only be stable, since they are just shorthands
785
-- for index probe queries.
787
SELECT p1.amopclaid, p1.amproc, p2.prosrc
788
FROM pg_amproc AS p1, pg_proc AS p2
789
WHERE p1.amproc = p2.oid AND
790
p1.amprocsubtype = 0 AND
791
p2.provolatile != 'i';
793
SELECT p1.amopclaid, p1.amproc, p2.prosrc
794
FROM pg_amproc AS p1, pg_proc AS p2
795
WHERE p1.amproc = p2.oid AND
796
p1.amprocsubtype != 0 AND
797
p2.provolatile = 'v';