LCOV - differential code coverage report
Current view: top level - src/backend/access/spgist - spgvalidate.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage HEAD vs 15 Lines: 79.1 % 134 106 28 106
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 2 2 2
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (240..) days: 79.1 % 134 106 28 106
Legend: Lines: hit not hit Function coverage date bins:
(240..) days: 100.0 % 2 2 2

 Age         Owner                  TLA  Line data    Source code
                                  1                 : /*-------------------------------------------------------------------------
                                  2                 :  *
                                  3                 :  * spgvalidate.c
                                  4                 :  *    Opclass validator for SP-GiST.
                                  5                 :  *
                                  6                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
                                  7                 :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :  *
                                  9                 :  * IDENTIFICATION
                                 10                 :  *          src/backend/access/spgist/spgvalidate.c
                                 11                 :  *
                                 12                 :  *-------------------------------------------------------------------------
                                 13                 :  */
                                 14                 : #include "postgres.h"
                                 15                 : 
                                 16                 : #include "access/amvalidate.h"
                                 17                 : #include "access/htup_details.h"
                                 18                 : #include "access/spgist_private.h"
                                 19                 : #include "catalog/pg_amop.h"
                                 20                 : #include "catalog/pg_amproc.h"
                                 21                 : #include "catalog/pg_opclass.h"
                                 22                 : #include "catalog/pg_opfamily.h"
                                 23                 : #include "catalog/pg_type.h"
                                 24                 : #include "utils/builtins.h"
                                 25                 : #include "utils/lsyscache.h"
                                 26                 : #include "utils/regproc.h"
                                 27                 : #include "utils/syscache.h"
                                 28                 : 
                                 29                 : 
                                 30                 : /*
                                 31                 :  * Validator for an SP-GiST opclass.
                                 32                 :  *
                                 33                 :  * Some of the checks done here cover the whole opfamily, and therefore are
                                 34                 :  * redundant when checking each opclass in a family.  But they don't run long
                                 35                 :  * enough to be much of a problem, so we accept the duplication rather than
                                 36                 :  * complicate the amvalidate API.
                                 37                 :  */
                                 38                 : bool
 2639 tgl                        39 CBC          23 : spgvalidate(Oid opclassoid)
                                 40                 : {
 2635                            41              23 :     bool        result = true;
                                 42                 :     HeapTuple   classtup;
                                 43                 :     Form_pg_opclass classform;
                                 44                 :     Oid         opfamilyoid;
                                 45                 :     Oid         opcintype;
                                 46                 :     Oid         opckeytype;
                                 47                 :     char       *opclassname;
                                 48                 :     HeapTuple   familytup;
                                 49                 :     Form_pg_opfamily familyform;
                                 50                 :     char       *opfamilyname;
                                 51                 :     CatCList   *proclist,
                                 52                 :                *oprlist;
                                 53                 :     List       *grouplist;
                                 54                 :     OpFamilyOpFuncGroup *opclassgroup;
                                 55                 :     int         i;
                                 56                 :     ListCell   *lc;
                                 57                 :     spgConfigIn configIn;
                                 58                 :     spgConfigOut configOut;
 1934 teodor                     59              23 :     Oid         configOutLefttype = InvalidOid;
                                 60              23 :     Oid         configOutRighttype = InvalidOid;
  735 tgl                        61              23 :     Oid         configOutLeafType = InvalidOid;
                                 62                 : 
                                 63                 :     /* Fetch opclass information */
 2639                            64              23 :     classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
                                 65              23 :     if (!HeapTupleIsValid(classtup))
 2639 tgl                        66 UBC           0 :         elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
 2639 tgl                        67 CBC          23 :     classform = (Form_pg_opclass) GETSTRUCT(classtup);
                                 68                 : 
                                 69              23 :     opfamilyoid = classform->opcfamily;
                                 70              23 :     opcintype = classform->opcintype;
  735                            71              23 :     opckeytype = classform->opckeytype;
 2635                            72              23 :     opclassname = NameStr(classform->opcname);
                                 73                 : 
                                 74                 :     /* Fetch opfamily information */
                                 75              23 :     familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
                                 76              23 :     if (!HeapTupleIsValid(familytup))
 2635 tgl                        77 UBC           0 :         elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
 2635 tgl                        78 CBC          23 :     familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
                                 79                 : 
                                 80              23 :     opfamilyname = NameStr(familyform->opfname);
                                 81                 : 
                                 82                 :     /* Fetch all operators and support functions of the opfamily */
 2639                            83              23 :     oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
                                 84              23 :     proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
 1934 teodor                     85              23 :     grouplist = identify_opfamily_groups(oprlist, proclist);
                                 86                 : 
                                 87                 :     /* Check individual support functions */
 2639 tgl                        88             143 :     for (i = 0; i < proclist->n_members; i++)
                                 89                 :     {
                                 90             120 :         HeapTuple   proctup = &proclist->members[i]->tuple;
                                 91             120 :         Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
                                 92                 :         bool        ok;
                                 93                 : 
                                 94                 :         /*
                                 95                 :          * All SP-GiST support functions should be registered with matching
                                 96                 :          * left/right types
                                 97                 :          */
 2635                            98             120 :         if (procform->amproclefttype != procform->amprocrighttype)
                                 99                 :         {
 2635 tgl                       100 UBC           0 :             ereport(INFO,
                                101                 :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                102                 :                      errmsg("operator family \"%s\" of access method %s contains support function %s with different left and right input types",
                                103                 :                             opfamilyname, "spgist",
                                104                 :                             format_procedure(procform->amproc))));
                                105               0 :             result = false;
                                106                 :         }
                                107                 : 
                                108                 :         /* Check procedure numbers and function signatures */
 2635 tgl                       109 CBC         120 :         switch (procform->amprocnum)
                                110                 :         {
                                111              23 :             case SPGIST_CONFIG_PROC:
 1934 teodor                    112              23 :                 ok = check_amproc_signature(procform->amproc, VOIDOID, true,
                                113                 :                                             2, 2, INTERNALOID, INTERNALOID);
                                114              23 :                 configIn.attType = procform->amproclefttype;
                                115              23 :                 memset(&configOut, 0, sizeof(configOut));
                                116                 : 
                                117              23 :                 OidFunctionCall2(procform->amproc,
                                118                 :                                  PointerGetDatum(&configIn),
                                119                 :                                  PointerGetDatum(&configOut));
                                120                 : 
                                121              23 :                 configOutLefttype = procform->amproclefttype;
                                122              23 :                 configOutRighttype = procform->amprocrighttype;
                                123                 : 
                                124                 :                 /* Default leaf type is opckeytype or input type */
  735 tgl                       125              23 :                 if (OidIsValid(opckeytype))
                                126               4 :                     configOutLeafType = opckeytype;
                                127                 :                 else
                                128              19 :                     configOutLeafType = procform->amproclefttype;
                                129                 : 
                                130                 :                 /* If some other leaf datum type is specified, warn */
                                131              23 :                 if (OidIsValid(configOut.leafType) &&
                                132               5 :                     configOutLeafType != configOut.leafType)
                                133                 :                 {
                                134               1 :                     ereport(INFO,
                                135                 :                             (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                136                 :                              errmsg("SP-GiST leaf data type %s does not match declared type %s",
                                137                 :                                     format_type_be(configOut.leafType),
                                138                 :                                     format_type_be(configOutLeafType))));
                                139               1 :                     result = false;
                                140               1 :                     configOutLeafType = configOut.leafType;
                                141                 :                 }
                                142                 : 
                                143                 :                 /*
                                144                 :                  * When leaf and attribute types are the same, compress
                                145                 :                  * function is not required and we set corresponding bit in
                                146                 :                  * functionset for later group consistency check.
                                147                 :                  */
                                148              23 :                 if (configOutLeafType == configIn.attType)
                                149                 :                 {
 1934 teodor                    150              24 :                     foreach(lc, grouplist)
                                151                 :                     {
                                152              24 :                         OpFamilyOpFuncGroup *group = lfirst(lc);
                                153                 : 
                                154              24 :                         if (group->lefttype == procform->amproclefttype &&
                                155              24 :                             group->righttype == procform->amprocrighttype)
                                156                 :                         {
                                157              18 :                             group->functionset |=
                                158                 :                                 ((uint64) 1) << SPGIST_COMPRESS_PROC;
                                159              18 :                             break;
                                160                 :                         }
                                161                 :                     }
                                162                 :                 }
                                163              23 :                 break;
 2635 tgl                       164              69 :             case SPGIST_CHOOSE_PROC:
                                165                 :             case SPGIST_PICKSPLIT_PROC:
                                166                 :             case SPGIST_INNER_CONSISTENT_PROC:
                                167              69 :                 ok = check_amproc_signature(procform->amproc, VOIDOID, true,
                                168                 :                                             2, 2, INTERNALOID, INTERNALOID);
                                169              69 :                 break;
                                170              23 :             case SPGIST_LEAF_CONSISTENT_PROC:
                                171              23 :                 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
                                172                 :                                             2, 2, INTERNALOID, INTERNALOID);
                                173              23 :                 break;
 1934 teodor                    174               5 :             case SPGIST_COMPRESS_PROC:
                                175               5 :                 if (configOutLefttype != procform->amproclefttype ||
                                176               5 :                     configOutRighttype != procform->amprocrighttype)
 1934 teodor                    177 UBC           0 :                     ok = false;
                                178                 :                 else
 1934 teodor                    179 CBC           5 :                     ok = check_amproc_signature(procform->amproc,
                                180                 :                                                 configOutLeafType, true,
                                181                 :                                                 1, 1, procform->amproclefttype);
                                182               5 :                 break;
 1105 akorotkov                 183 UBC           0 :             case SPGIST_OPTIONS_PROC:
                                184               0 :                 ok = check_amoptsproc_signature(procform->amproc);
                                185               0 :                 break;
 2635 tgl                       186               0 :             default:
                                187               0 :                 ereport(INFO,
                                188                 :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                189                 :                          errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
                                190                 :                                 opfamilyname, "spgist",
                                191                 :                                 format_procedure(procform->amproc),
                                192                 :                                 procform->amprocnum)));
                                193               0 :                 result = false;
                                194               0 :                 continue;       /* don't want additional message */
                                195                 :         }
                                196                 : 
 2635 tgl                       197 CBC         120 :         if (!ok)
                                198                 :         {
 2635 tgl                       199 UBC           0 :             ereport(INFO,
                                200                 :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                201                 :                      errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
                                202                 :                             opfamilyname, "spgist",
                                203                 :                             format_procedure(procform->amproc),
                                204                 :                             procform->amprocnum)));
                                205               0 :             result = false;
                                206                 :         }
                                207                 :     }
                                208                 : 
                                209                 :     /* Check individual operators */
 2639 tgl                       210 CBC         258 :     for (i = 0; i < oprlist->n_members; i++)
                                211                 :     {
                                212             235 :         HeapTuple   oprtup = &oprlist->members[i]->tuple;
                                213             235 :         Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
                                214                 :         Oid         op_rettype;
                                215                 : 
                                216                 :         /* TODO: Check that only allowed strategy numbers exist */
 2635                           217             235 :         if (oprform->amopstrategy < 1 || oprform->amopstrategy > 63)
                                218                 :         {
 2635 tgl                       219 UBC           0 :             ereport(INFO,
                                220                 :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                221                 :                      errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
                                222                 :                             opfamilyname, "spgist",
                                223                 :                             format_operator(oprform->amopopr),
                                224                 :                             oprform->amopstrategy)));
                                225               0 :             result = false;
                                226                 :         }
                                227                 : 
                                228                 :         /* spgist supports ORDER BY operators */
 1663 akorotkov                 229 CBC         235 :         if (oprform->amoppurpose != AMOP_SEARCH)
                                230                 :         {
                                231                 :             /* ... and operator result must match the claimed btree opfamily */
                                232              12 :             op_rettype = get_op_rettype(oprform->amopopr);
                                233              12 :             if (!opfamily_can_sort_type(oprform->amopsortfamily, op_rettype))
                                234                 :             {
 1663 akorotkov                 235 UBC           0 :                 ereport(INFO,
                                236                 :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                237                 :                          errmsg("operator family \"%s\" of access method %s contains invalid ORDER BY specification for operator %s",
                                238                 :                                 opfamilyname, "spgist",
                                239                 :                                 format_operator(oprform->amopopr))));
                                240               0 :                 result = false;
                                241                 :             }
                                242                 :         }
                                243                 :         else
 1663 akorotkov                 244 CBC         223 :             op_rettype = BOOLOID;
                                245                 : 
                                246                 :         /* Check operator signature --- same for all spgist strategies */
                                247             235 :         if (!check_amop_signature(oprform->amopopr, op_rettype,
                                248                 :                                   oprform->amoplefttype,
                                249                 :                                   oprform->amoprighttype))
                                250                 :         {
 2635 tgl                       251 UBC           0 :             ereport(INFO,
                                252                 :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                253                 :                      errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
                                254                 :                             opfamilyname, "spgist",
                                255                 :                             format_operator(oprform->amopopr))));
                                256               0 :             result = false;
                                257                 :         }
                                258                 :     }
                                259                 : 
                                260                 :     /* Now check for inconsistent groups of operators/functions */
 2635 tgl                       261 CBC          23 :     opclassgroup = NULL;
                                262              61 :     foreach(lc, grouplist)
                                263                 :     {
                                264              38 :         OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
                                265                 : 
                                266                 :         /* Remember the group exactly matching the test opclass */
                                267              38 :         if (thisgroup->lefttype == opcintype &&
                                268              38 :             thisgroup->righttype == opcintype)
                                269              23 :             opclassgroup = thisgroup;
                                270                 : 
                                271                 :         /*
                                272                 :          * Complain if there are any datatype pairs with functions but no
                                273                 :          * operators.  This is about the best we can do for now to detect
                                274                 :          * missing operators.
                                275                 :          */
                                276              38 :         if (thisgroup->operatorset == 0)
                                277                 :         {
 2635 tgl                       278 UBC           0 :             ereport(INFO,
                                279                 :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                280                 :                      errmsg("operator family \"%s\" of access method %s is missing operator(s) for types %s and %s",
                                281                 :                             opfamilyname, "spgist",
                                282                 :                             format_type_be(thisgroup->lefttype),
                                283                 :                             format_type_be(thisgroup->righttype))));
                                284               0 :             result = false;
                                285                 :         }
                                286                 : 
                                287                 :         /*
                                288                 :          * Complain if we're missing functions for any datatype, remembering
                                289                 :          * that SP-GiST doesn't use cross-type support functions.
                                290                 :          */
 2635 tgl                       291 CBC          38 :         if (thisgroup->lefttype != thisgroup->righttype)
                                292              15 :             continue;
                                293                 : 
                                294             184 :         for (i = 1; i <= SPGISTNProc; i++)
                                295                 :         {
                                296             161 :             if ((thisgroup->functionset & (((uint64) 1) << i)) != 0)
                                297             138 :                 continue;       /* got it */
 1105 akorotkov                 298              23 :             if (i == SPGIST_OPTIONS_PROC)
 1060 tgl                       299              23 :                 continue;       /* optional method */
 2635 tgl                       300 UBC           0 :             ereport(INFO,
                                301                 :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                302                 :                      errmsg("operator family \"%s\" of access method %s is missing support function %d for type %s",
                                303                 :                             opfamilyname, "spgist", i,
                                304                 :                             format_type_be(thisgroup->lefttype))));
                                305               0 :             result = false;
                                306                 :         }
                                307                 :     }
                                308                 : 
                                309                 :     /* Check that the originally-named opclass is supported */
                                310                 :     /* (if group is there, we already checked it adequately above) */
 2635 tgl                       311 CBC          23 :     if (!opclassgroup)
                                312                 :     {
 2635 tgl                       313 UBC           0 :         ereport(INFO,
                                314                 :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                315                 :                  errmsg("operator class \"%s\" of access method %s is missing operator(s)",
                                316                 :                         opclassname, "spgist")));
                                317               0 :         result = false;
                                318                 :     }
                                319                 : 
 2639 tgl                       320 CBC          23 :     ReleaseCatCacheList(proclist);
                                321              23 :     ReleaseCatCacheList(oprlist);
 2635                           322              23 :     ReleaseSysCache(familytup);
                                323              23 :     ReleaseSysCache(classtup);
                                324                 : 
                                325              23 :     return result;
                                326                 : }
                                327                 : 
                                328                 : /*
                                329                 :  * Prechecking function for adding operators/functions to an SP-GiST opfamily.
                                330                 :  */
                                331                 : void
  981                           332               2 : spgadjustmembers(Oid opfamilyoid,
                                333                 :                  Oid opclassoid,
                                334                 :                  List *operators,
                                335                 :                  List *functions)
                                336                 : {
                                337                 :     ListCell   *lc;
                                338                 : 
                                339                 :     /*
                                340                 :      * Operator members of an SP-GiST opfamily should never have hard
                                341                 :      * dependencies, since their connection to the opfamily depends only on
                                342                 :      * what the support functions think, and that can be altered.  For
                                343                 :      * consistency, we make all soft dependencies point to the opfamily,
                                344                 :      * though a soft dependency on the opclass would work as well in the
                                345                 :      * CREATE OPERATOR CLASS case.
                                346                 :      */
                                347              12 :     foreach(lc, operators)
                                348                 :     {
                                349              10 :         OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
                                350                 : 
                                351              10 :         op->ref_is_hard = false;
                                352              10 :         op->ref_is_family = true;
                                353              10 :         op->refobjid = opfamilyoid;
                                354                 :     }
                                355                 : 
                                356                 :     /*
                                357                 :      * Required support functions should have hard dependencies.  Preferably
                                358                 :      * those are just dependencies on the opclass, but if we're in ALTER
                                359                 :      * OPERATOR FAMILY, we leave the dependency pointing at the whole
                                360                 :      * opfamily.  (Given that SP-GiST opclasses generally don't share
                                361                 :      * opfamilies, it seems unlikely to be worth working harder.)
                                362                 :      */
                                363              14 :     foreach(lc, functions)
                                364                 :     {
                                365              12 :         OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
                                366                 : 
                                367              12 :         switch (op->number)
                                368                 :         {
                                369              10 :             case SPGIST_CONFIG_PROC:
                                370                 :             case SPGIST_CHOOSE_PROC:
                                371                 :             case SPGIST_PICKSPLIT_PROC:
                                372                 :             case SPGIST_INNER_CONSISTENT_PROC:
                                373                 :             case SPGIST_LEAF_CONSISTENT_PROC:
                                374                 :                 /* Required support function */
                                375              10 :                 op->ref_is_hard = true;
                                376              10 :                 break;
                                377               2 :             case SPGIST_COMPRESS_PROC:
                                378                 :             case SPGIST_OPTIONS_PROC:
                                379                 :                 /* Optional, so force it to be a soft family dependency */
                                380               2 :                 op->ref_is_hard = false;
                                381               2 :                 op->ref_is_family = true;
                                382               2 :                 op->refobjid = opfamilyoid;
                                383               2 :                 break;
  981 tgl                       384 UBC           0 :             default:
                                385               0 :                 ereport(ERROR,
                                386                 :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                387                 :                          errmsg("support function number %d is invalid for access method %s",
                                388                 :                                 op->number, "spgist")));
                                389                 :                 break;
                                390                 :         }
                                391                 :     }
  981 tgl                       392 CBC           2 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a