Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 43493cc

Browse files
committed
Add get_opfamily_name() function
This refactors and simplifies various existing code to make use of the new function. Reviewed-by: Mark Dilger <mark.dilger@enterprisedb.com> Discussion: https://www.postgresql.org/message-id/flat/E72EAA49-354D-4C2E-8EB9-255197F55330@enterprisedb.com
1 parent a5709b5 commit 43493cc

File tree

10 files changed

+40
-98
lines changed

10 files changed

+40
-98
lines changed

contrib/bloom/blvalidate.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include "catalog/pg_amop.h"
1919
#include "catalog/pg_amproc.h"
2020
#include "catalog/pg_opclass.h"
21-
#include "catalog/pg_opfamily.h"
2221
#include "catalog/pg_type.h"
22+
#include "utils/lsyscache.h"
2323
#include "utils/regproc.h"
2424
#include "utils/syscache.h"
2525

@@ -36,8 +36,6 @@ blvalidate(Oid opclassoid)
3636
Oid opcintype;
3737
Oid opckeytype;
3838
char *opclassname;
39-
HeapTuple familytup;
40-
Form_pg_opfamily familyform;
4139
char *opfamilyname;
4240
CatCList *proclist,
4341
*oprlist;
@@ -60,12 +58,7 @@ blvalidate(Oid opclassoid)
6058
opclassname = NameStr(classform->opcname);
6159

6260
/* Fetch opfamily information */
63-
familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
64-
if (!HeapTupleIsValid(familytup))
65-
elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
66-
familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
67-
68-
opfamilyname = NameStr(familyform->opfname);
61+
opfamilyname = get_opfamily_name(opfamilyoid, false);
6962

7063
/* Fetch all operators and support functions of the opfamily */
7164
oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
@@ -216,7 +209,6 @@ blvalidate(Oid opclassoid)
216209

217210
ReleaseCatCacheList(proclist);
218211
ReleaseCatCacheList(oprlist);
219-
ReleaseSysCache(familytup);
220212
ReleaseSysCache(classtup);
221213

222214
return result;

src/backend/access/brin/brin_validate.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include "catalog/pg_amop.h"
2020
#include "catalog/pg_amproc.h"
2121
#include "catalog/pg_opclass.h"
22-
#include "catalog/pg_opfamily.h"
2322
#include "catalog/pg_type.h"
2423
#include "utils/builtins.h"
24+
#include "utils/lsyscache.h"
2525
#include "utils/regproc.h"
2626
#include "utils/syscache.h"
2727

@@ -42,8 +42,6 @@ brinvalidate(Oid opclassoid)
4242
Oid opfamilyoid;
4343
Oid opcintype;
4444
char *opclassname;
45-
HeapTuple familytup;
46-
Form_pg_opfamily familyform;
4745
char *opfamilyname;
4846
CatCList *proclist,
4947
*oprlist;
@@ -65,12 +63,7 @@ brinvalidate(Oid opclassoid)
6563
opclassname = NameStr(classform->opcname);
6664

6765
/* Fetch opfamily information */
68-
familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
69-
if (!HeapTupleIsValid(familytup))
70-
elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
71-
familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
72-
73-
opfamilyname = NameStr(familyform->opfname);
66+
opfamilyname = get_opfamily_name(opfamilyoid, false);
7467

7568
/* Fetch all operators and support functions of the opfamily */
7669
oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
@@ -274,7 +267,6 @@ brinvalidate(Oid opclassoid)
274267

275268
ReleaseCatCacheList(proclist);
276269
ReleaseCatCacheList(oprlist);
277-
ReleaseSysCache(familytup);
278270
ReleaseSysCache(classtup);
279271

280272
return result;

src/backend/access/gin/ginvalidate.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "catalog/pg_amop.h"
2020
#include "catalog/pg_amproc.h"
2121
#include "catalog/pg_opclass.h"
22-
#include "catalog/pg_opfamily.h"
2322
#include "catalog/pg_type.h"
23+
#include "utils/lsyscache.h"
2424
#include "utils/regproc.h"
2525
#include "utils/syscache.h"
2626

@@ -37,8 +37,6 @@ ginvalidate(Oid opclassoid)
3737
Oid opcintype;
3838
Oid opckeytype;
3939
char *opclassname;
40-
HeapTuple familytup;
41-
Form_pg_opfamily familyform;
4240
char *opfamilyname;
4341
CatCList *proclist,
4442
*oprlist;
@@ -61,12 +59,7 @@ ginvalidate(Oid opclassoid)
6159
opclassname = NameStr(classform->opcname);
6260

6361
/* Fetch opfamily information */
64-
familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
65-
if (!HeapTupleIsValid(familytup))
66-
elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
67-
familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
68-
69-
opfamilyname = NameStr(familyform->opfname);
62+
opfamilyname = get_opfamily_name(opfamilyoid, false);
7063

7164
/* Fetch all operators and support functions of the opfamily */
7265
oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
@@ -264,7 +257,6 @@ ginvalidate(Oid opclassoid)
264257

265258
ReleaseCatCacheList(proclist);
266259
ReleaseCatCacheList(oprlist);
267-
ReleaseSysCache(familytup);
268260
ReleaseSysCache(classtup);
269261

270262
return result;

src/backend/access/gist/gistvalidate.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "catalog/pg_amop.h"
2020
#include "catalog/pg_amproc.h"
2121
#include "catalog/pg_opclass.h"
22-
#include "catalog/pg_opfamily.h"
2322
#include "catalog/pg_type.h"
2423
#include "utils/lsyscache.h"
2524
#include "utils/regproc.h"
@@ -39,8 +38,6 @@ gistvalidate(Oid opclassoid)
3938
Oid opcintype;
4039
Oid opckeytype;
4140
char *opclassname;
42-
HeapTuple familytup;
43-
Form_pg_opfamily familyform;
4441
char *opfamilyname;
4542
CatCList *proclist,
4643
*oprlist;
@@ -63,12 +60,7 @@ gistvalidate(Oid opclassoid)
6360
opclassname = NameStr(classform->opcname);
6461

6562
/* Fetch opfamily information */
66-
familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
67-
if (!HeapTupleIsValid(familytup))
68-
elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
69-
familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
70-
71-
opfamilyname = NameStr(familyform->opfname);
63+
opfamilyname = get_opfamily_name(opfamilyoid, false);
7264

7365
/* Fetch all operators and support functions of the opfamily */
7466
oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
@@ -282,7 +274,6 @@ gistvalidate(Oid opclassoid)
282274

283275
ReleaseCatCacheList(proclist);
284276
ReleaseCatCacheList(oprlist);
285-
ReleaseSysCache(familytup);
286277
ReleaseSysCache(classtup);
287278

288279
return result;

src/backend/access/hash/hashvalidate.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "catalog/pg_amop.h"
2222
#include "catalog/pg_amproc.h"
2323
#include "catalog/pg_opclass.h"
24-
#include "catalog/pg_opfamily.h"
2524
#include "catalog/pg_type.h"
2625
#include "utils/builtins.h"
2726
#include "utils/lsyscache.h"
@@ -46,8 +45,6 @@ hashvalidate(Oid opclassoid)
4645
Oid opfamilyoid;
4746
Oid opcintype;
4847
char *opclassname;
49-
HeapTuple familytup;
50-
Form_pg_opfamily familyform;
5148
char *opfamilyname;
5249
CatCList *proclist,
5350
*oprlist;
@@ -68,12 +65,7 @@ hashvalidate(Oid opclassoid)
6865
opclassname = NameStr(classform->opcname);
6966

7067
/* Fetch opfamily information */
71-
familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
72-
if (!HeapTupleIsValid(familytup))
73-
elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
74-
familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
75-
76-
opfamilyname = NameStr(familyform->opfname);
68+
opfamilyname = get_opfamily_name(opfamilyoid, false);
7769

7870
/* Fetch all operators and support functions of the opfamily */
7971
oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
@@ -258,7 +250,6 @@ hashvalidate(Oid opclassoid)
258250

259251
ReleaseCatCacheList(proclist);
260252
ReleaseCatCacheList(oprlist);
261-
ReleaseSysCache(familytup);
262253
ReleaseSysCache(classtup);
263254

264255
return result;

src/backend/access/nbtree/nbtvalidate.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "catalog/pg_amop.h"
2222
#include "catalog/pg_amproc.h"
2323
#include "catalog/pg_opclass.h"
24-
#include "catalog/pg_opfamily.h"
2524
#include "catalog/pg_type.h"
2625
#include "utils/builtins.h"
2726
#include "utils/lsyscache.h"
@@ -46,8 +45,6 @@ btvalidate(Oid opclassoid)
4645
Oid opfamilyoid;
4746
Oid opcintype;
4847
char *opclassname;
49-
HeapTuple familytup;
50-
Form_pg_opfamily familyform;
5148
char *opfamilyname;
5249
CatCList *proclist,
5350
*oprlist;
@@ -69,12 +66,7 @@ btvalidate(Oid opclassoid)
6966
opclassname = NameStr(classform->opcname);
7067

7168
/* Fetch opfamily information */
72-
familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
73-
if (!HeapTupleIsValid(familytup))
74-
elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
75-
familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
76-
77-
opfamilyname = NameStr(familyform->opfname);
69+
opfamilyname = get_opfamily_name(opfamilyoid, false);
7870

7971
/* Fetch all operators and support functions of the opfamily */
8072
oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
@@ -280,7 +272,6 @@ btvalidate(Oid opclassoid)
280272

281273
ReleaseCatCacheList(proclist);
282274
ReleaseCatCacheList(oprlist);
283-
ReleaseSysCache(familytup);
284275
ReleaseSysCache(classtup);
285276

286277
return result;

src/backend/access/spgist/spgvalidate.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "catalog/pg_amop.h"
2020
#include "catalog/pg_amproc.h"
2121
#include "catalog/pg_opclass.h"
22-
#include "catalog/pg_opfamily.h"
2322
#include "catalog/pg_type.h"
2423
#include "utils/builtins.h"
2524
#include "utils/lsyscache.h"
@@ -45,8 +44,6 @@ spgvalidate(Oid opclassoid)
4544
Oid opcintype;
4645
Oid opckeytype;
4746
char *opclassname;
48-
HeapTuple familytup;
49-
Form_pg_opfamily familyform;
5047
char *opfamilyname;
5148
CatCList *proclist,
5249
*oprlist;
@@ -72,12 +69,7 @@ spgvalidate(Oid opclassoid)
7269
opclassname = NameStr(classform->opcname);
7370

7471
/* Fetch opfamily information */
75-
familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
76-
if (!HeapTupleIsValid(familytup))
77-
elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
78-
familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
79-
80-
opfamilyname = NameStr(familyform->opfname);
72+
opfamilyname = get_opfamily_name(opfamilyoid, false);
8173

8274
/* Fetch all operators and support functions of the opfamily */
8375
oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
@@ -319,7 +311,6 @@ spgvalidate(Oid opclassoid)
319311

320312
ReleaseCatCacheList(proclist);
321313
ReleaseCatCacheList(oprlist);
322-
ReleaseSysCache(familytup);
323314
ReleaseSysCache(classtup);
324315

325316
return result;

src/backend/commands/indexcmds.c

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "catalog/pg_inherits.h"
3636
#include "catalog/pg_namespace.h"
3737
#include "catalog/pg_opclass.h"
38-
#include "catalog/pg_opfamily.h"
3938
#include "catalog/pg_tablespace.h"
4039
#include "catalog/pg_type.h"
4140
#include "commands/comment.h"
@@ -2147,29 +2146,12 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
21472146
opfamily = get_opclass_family(opclassOids[attn]);
21482147
strat = get_op_opfamily_strategy(opid, opfamily);
21492148
if (strat == 0)
2150-
{
2151-
HeapTuple opftuple;
2152-
Form_pg_opfamily opfform;
2153-
2154-
/*
2155-
* attribute->opclass might not explicitly name the opfamily,
2156-
* so fetch the name of the selected opfamily for use in the
2157-
* error message.
2158-
*/
2159-
opftuple = SearchSysCache1(OPFAMILYOID,
2160-
ObjectIdGetDatum(opfamily));
2161-
if (!HeapTupleIsValid(opftuple))
2162-
elog(ERROR, "cache lookup failed for opfamily %u",
2163-
opfamily);
2164-
opfform = (Form_pg_opfamily) GETSTRUCT(opftuple);
2165-
21662149
ereport(ERROR,
21672150
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
21682151
errmsg("operator %s is not a member of operator family \"%s\"",
21692152
format_operator(opid),
2170-
NameStr(opfform->opfname)),
2153+
get_opfamily_name(opfamily, false)),
21712154
errdetail("The exclusion operator must be related to the index operator class for the constraint.")));
2172-
}
21732155

21742156
indexInfo->ii_ExclusionOps[attn] = opid;
21752157
indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid);
@@ -2484,21 +2466,13 @@ GetOperatorFromCompareType(Oid opclass, Oid rhstype, CompareType cmptype,
24842466
}
24852467

24862468
if (!OidIsValid(*opid))
2487-
{
2488-
HeapTuple tuple;
2489-
2490-
tuple = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamily));
2491-
if (!HeapTupleIsValid(tuple))
2492-
elog(ERROR, "cache lookup failed for operator family %u", opfamily);
2493-
24942469
ereport(ERROR,
24952470
errcode(ERRCODE_UNDEFINED_OBJECT),
24962471
cmptype == COMPARE_EQ ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
24972472
cmptype == COMPARE_OVERLAP ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
24982473
cmptype == COMPARE_CONTAINED_BY ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
24992474
errdetail("There is no suitable operator in operator family \"%s\" for access method \"%s\".",
2500-
NameStr(((Form_pg_opfamily) GETSTRUCT(tuple))->opfname), "gist"));
2501-
}
2475+
get_opfamily_name(opfamily, false), "gist"));
25022476
}
25032477

25042478
/*

src/backend/utils/cache/lsyscache.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "catalog/pg_language.h"
3131
#include "catalog/pg_namespace.h"
3232
#include "catalog/pg_opclass.h"
33+
#include "catalog/pg_opfamily.h"
3334
#include "catalog/pg_operator.h"
3435
#include "catalog/pg_proc.h"
3536
#include "catalog/pg_publication.h"
@@ -1273,6 +1274,32 @@ get_opclass_method(Oid opclass)
12731274
return result;
12741275
}
12751276

1277+
/* ---------- OPFAMILY CACHE ---------- */
1278+
1279+
char *
1280+
get_opfamily_name(Oid opfid, bool missing_ok)
1281+
{
1282+
HeapTuple tup;
1283+
char *opfname;
1284+
Form_pg_opfamily opfform;
1285+
1286+
tup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfid));
1287+
1288+
if (!HeapTupleIsValid(tup))
1289+
{
1290+
if (!missing_ok)
1291+
elog(ERROR, "cache lookup failed for operator family %u", opfid);
1292+
return NULL;
1293+
}
1294+
1295+
opfform = (Form_pg_opfamily) GETSTRUCT(tup);
1296+
opfname = pstrdup(NameStr(opfform->opfname));
1297+
1298+
ReleaseSysCache(tup);
1299+
1300+
return opfname;
1301+
}
1302+
12761303
/* ---------- OPERATOR CACHE ---------- */
12771304

12781305
/*

src/include/utils/lsyscache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ extern Oid get_opclass_input_type(Oid opclass);
108108
extern bool get_opclass_opfamily_and_input_type(Oid opclass,
109109
Oid *opfamily, Oid *opcintype);
110110
extern Oid get_opclass_method(Oid opclass);
111+
extern char *get_opfamily_name(Oid opfid, bool missing_ok);
111112
extern RegProcedure get_opcode(Oid opno);
112113
extern char *get_opname(Oid opno);
113114
extern Oid get_op_rettype(Oid opno);

0 commit comments

Comments
 (0)