1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*-------------------------------------------------------------------------
*
* amvalidate.c
* Support routines for index access methods' amvalidate and
* amadjustmembers functions.
*
* Copyright (c) 2016-2021, PostgreSQL Global Development Group
*
*
* IDENTIFICATION
* src/backend/access/index/amvalidate.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/amvalidate.h"
#include "access/htup_details.h"
#include "catalog/pg_am.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_amproc.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "parser/parse_coerce.h"
#include "utils/syscache.h"
/*
* identify_opfamily_groups() returns a List of OpFamilyOpFuncGroup structs,
* one for each combination of lefttype/righttype present in the family's
* operator and support function lists. If amopstrategy K is present for
* this datatype combination, we set bit 1 << K in operatorset, and similarly
* for the support functions. With uint64 fields we can handle operator and
* function numbers up to 63, which is plenty for the foreseeable future.
*
* The given CatCLists are expected to represent a single opfamily fetched
* from the AMOPSTRATEGY and AMPROCNUM caches, so that they will be in
* order by those caches' second and third cache keys, namely the datatypes.
*/
List *
identify_opfamily_groups(CatCList *oprlist, CatCList *proclist)
{
List *result = NIL;
OpFamilyOpFuncGroup *thisgroup;
Form_pg_amop oprform;
Form_pg_amproc procform;
int io,
ip;
/* We need the lists to be ordered; should be true in normal operation */
if (!oprlist->ordered || !proclist->ordered)
elog(ERROR, "cannot validate operator family without ordered data");
/*
* Advance through the lists concurrently. Thanks to the ordering, we
* should see all operators and functions of a given datatype pair
* consecutively.
*/
thisgroup = NULL;
io = ip = 0;
if (io < oprlist->n_members)
{
oprform = (Form_pg_amop) GETSTRUCT(&oprlist->members[io]->tuple);
io++;
}
else
oprform = NULL;
if (ip < proclist->n_members)
{
procform = (Form_pg_amproc) GETSTRUCT(&proclist->members[ip]->tuple);
ip++;
}
else
procform = NULL;
while (oprform || procform)
{
if (oprform && thisgroup &&
oprform->amoplefttype == thisgroup->lefttype &&
oprform->amoprighttype == thisgroup->righttype)
{
/* Operator belongs to current group; include it and advance */
/* Ignore strategy numbers outside supported range */
if (oprform->amopstrategy > 0 && oprform->amopstrategy < 64)
thisgroup->operatorset |= ((uint64) 1) << oprform->amopstrategy;
if (io < oprlist->n_members)
{
oprform = (Form_pg_amop) GETSTRUCT(&oprlist->members[io]->tuple);
io++;
}
else
oprform = NULL;
continue;
}
if (procform && thisgroup &&
procform->amproclefttype == thisgroup->lefttype &&
procform->amprocrighttype == thisgroup->righttype)
{
/* Procedure belongs to current group; include it and advance */
/* Ignore function numbers outside supported range */
if (procform->amprocnum > 0 && procform->amprocnum < 64)
thisgroup->functionset |= ((uint64) 1) << procform->amprocnum;
if (ip < proclist->n_members)
{
procform = (Form_pg_amproc) GETSTRUCT(&proclist->members[ip]->tuple);
ip++;
}
else
procform = NULL;
continue;
}
/* Time for a new group */
thisgroup = (OpFamilyOpFuncGroup *) palloc(sizeof(OpFamilyOpFuncGroup));
if (oprform &&
(!procform ||
(oprform->amoplefttype < procform->amproclefttype ||
(oprform->amoplefttype == procform->amproclefttype &&
oprform->amoprighttype < procform->amprocrighttype))))
{
thisgroup->lefttype = oprform->amoplefttype;
thisgroup->righttype = oprform->amoprighttype;
}
else
{
thisgroup->lefttype = procform->amproclefttype;
thisgroup->righttype = procform->amprocrighttype;
}
thisgroup->operatorset = thisgroup->functionset = 0;
result = lappend(result, thisgroup);
}
return result;
}
/*
* Validate the signature (argument and result types) of an opclass support
* function. Return true if OK, false if not.
*
* The "..." represents maxargs argument-type OIDs. If "exact" is true, they
* must match the function arg types exactly, else only binary-coercibly.
* In any case the function result type must match restype exactly.
*/
bool
check_amproc_signature(Oid funcid, Oid restype, bool exact,
int minargs, int maxargs,...)
{
bool result = true;
HeapTuple tp;
Form_pg_proc procform;
va_list ap;
int i;
tp = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
if (!HeapTupleIsValid(tp))
elog(ERROR, "cache lookup failed for function %u", funcid);
procform = (Form_pg_proc) GETSTRUCT(tp);
if (procform->prorettype != restype || procform->proretset ||
procform->pronargs < minargs || procform->pronargs > maxargs)
result = false;
va_start(ap, maxargs);
for (i = 0; i < maxargs; i++)
{
Oid argtype = va_arg(ap, Oid);
if (i >= procform->pronargs)
continue;
if (exact ? (argtype != procform->proargtypes.values[i]) :
!IsBinaryCoercible(argtype, procform->proargtypes.values[i]))
result = false;
}
va_end(ap);
ReleaseSysCache(tp);
return result;
}
/*
* Validate the signature of an opclass options support function, that should
* be 'void(internal)'.
*/
bool
check_amoptsproc_signature(Oid funcid)
{
return check_amproc_signature(funcid, VOIDOID, true, 1, 1, INTERNALOID);
}
/*
* Validate the signature (argument and result types) of an opclass operator.
* Return true if OK, false if not.
*
* Currently, we can hard-wire this as accepting only binary operators. Also,
* we can insist on exact type matches, since the given lefttype/righttype
* come from pg_amop and should always match the operator exactly.
*/
bool
check_amop_signature(Oid opno, Oid restype, Oid lefttype, Oid righttype)
{
bool result = true;
HeapTuple tp;
Form_pg_operator opform;
tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
if (!HeapTupleIsValid(tp)) /* shouldn't happen */
elog(ERROR, "cache lookup failed for operator %u", opno);
opform = (Form_pg_operator) GETSTRUCT(tp);
if (opform->oprresult != restype || opform->oprkind != 'b' ||
opform->oprleft != lefttype || opform->oprright != righttype)
result = false;
ReleaseSysCache(tp);
return result;
}
/*
* Get the OID of the opclass belonging to an opfamily and accepting
* the specified type as input type. Returns InvalidOid if no such opclass.
*
* If there is more than one such opclass, you get a random one of them.
* Since that shouldn't happen, we don't waste cycles checking.
*
* We could look up the AM's OID from the opfamily, but all existing callers
* know that or can get it without an extra lookup, so we make them pass it.
*/
Oid
opclass_for_family_datatype(Oid amoid, Oid opfamilyoid, Oid datatypeoid)
{
Oid result = InvalidOid;
CatCList *opclist;
int i;
/*
* We search through all the AM's opclasses to see if one matches. This
* is a bit inefficient but there is no better index available. It also
* saves making an explicit check that the opfamily belongs to the AM.
*/
opclist = SearchSysCacheList1(CLAAMNAMENSP, ObjectIdGetDatum(amoid));
for (i = 0; i < opclist->n_members; i++)
{
HeapTuple classtup = &opclist->members[i]->tuple;
Form_pg_opclass classform = (Form_pg_opclass) GETSTRUCT(classtup);
if (classform->opcfamily == opfamilyoid &&
classform->opcintype == datatypeoid)
{
result = classform->oid;
break;
}
}
ReleaseCatCacheList(opclist);
return result;
}
/*
* Is the datatype a legitimate input type for the btree opfamily?
*/
bool
opfamily_can_sort_type(Oid opfamilyoid, Oid datatypeoid)
{
return OidIsValid(opclass_for_family_datatype(BTREE_AM_OID,
opfamilyoid,
datatypeoid));
}
|