LCOV - code coverage report
Current view: top level - src/backend/access/spgist - spgvalidate.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 79 105 75.2 %
Date: 2020-06-03 10:06:28 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * spgvalidate.c
       4             :  *    Opclass validator for SP-GiST.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2020, 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
      39          28 : spgvalidate(Oid opclassoid)
      40             : {
      41          28 :     bool        result = true;
      42             :     HeapTuple   classtup;
      43             :     Form_pg_opclass classform;
      44             :     Oid         opfamilyoid;
      45             :     Oid         opcintype;
      46             :     char       *opclassname;
      47             :     HeapTuple   familytup;
      48             :     Form_pg_opfamily familyform;
      49             :     char       *opfamilyname;
      50             :     CatCList   *proclist,
      51             :                *oprlist;
      52             :     List       *grouplist;
      53             :     OpFamilyOpFuncGroup *opclassgroup;
      54             :     int         i;
      55             :     ListCell   *lc;
      56             :     spgConfigIn configIn;
      57             :     spgConfigOut configOut;
      58          28 :     Oid         configOutLefttype = InvalidOid;
      59          28 :     Oid         configOutRighttype = InvalidOid;
      60             : 
      61             :     /* Fetch opclass information */
      62          28 :     classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
      63          28 :     if (!HeapTupleIsValid(classtup))
      64           0 :         elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
      65          28 :     classform = (Form_pg_opclass) GETSTRUCT(classtup);
      66             : 
      67          28 :     opfamilyoid = classform->opcfamily;
      68          28 :     opcintype = classform->opcintype;
      69          28 :     opclassname = NameStr(classform->opcname);
      70             : 
      71             :     /* Fetch opfamily information */
      72          28 :     familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
      73          28 :     if (!HeapTupleIsValid(familytup))
      74           0 :         elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
      75          28 :     familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
      76             : 
      77          28 :     opfamilyname = NameStr(familyform->opfname);
      78             : 
      79             :     /* Fetch all operators and support functions of the opfamily */
      80          28 :     oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
      81          28 :     proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
      82          28 :     grouplist = identify_opfamily_groups(oprlist, proclist);
      83             : 
      84             :     /* Check individual support functions */
      85         172 :     for (i = 0; i < proclist->n_members; i++)
      86             :     {
      87         144 :         HeapTuple   proctup = &proclist->members[i]->tuple;
      88         144 :         Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
      89             :         bool        ok;
      90             : 
      91             :         /*
      92             :          * All SP-GiST support functions should be registered with matching
      93             :          * left/right types
      94             :          */
      95         144 :         if (procform->amproclefttype != procform->amprocrighttype)
      96             :         {
      97           0 :             ereport(INFO,
      98             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
      99             :                      errmsg("operator family \"%s\" of access method %s contains support function %s with different left and right input types",
     100             :                             opfamilyname, "spgist",
     101             :                             format_procedure(procform->amproc))));
     102           0 :             result = false;
     103             :         }
     104             : 
     105             :         /* Check procedure numbers and function signatures */
     106         144 :         switch (procform->amprocnum)
     107             :         {
     108          28 :             case SPGIST_CONFIG_PROC:
     109          28 :                 ok = check_amproc_signature(procform->amproc, VOIDOID, true,
     110             :                                             2, 2, INTERNALOID, INTERNALOID);
     111          28 :                 configIn.attType = procform->amproclefttype;
     112          28 :                 memset(&configOut, 0, sizeof(configOut));
     113             : 
     114          28 :                 OidFunctionCall2(procform->amproc,
     115             :                                  PointerGetDatum(&configIn),
     116             :                                  PointerGetDatum(&configOut));
     117             : 
     118          28 :                 configOutLefttype = procform->amproclefttype;
     119          28 :                 configOutRighttype = procform->amprocrighttype;
     120             : 
     121             :                 /*
     122             :                  * When leaf and attribute types are the same, compress
     123             :                  * function is not required and we set corresponding bit in
     124             :                  * functionset for later group consistency check.
     125             :                  */
     126          28 :                 if (!OidIsValid(configOut.leafType) ||
     127           4 :                     configOut.leafType == configIn.attType)
     128             :                 {
     129          32 :                     foreach(lc, grouplist)
     130             :                     {
     131          32 :                         OpFamilyOpFuncGroup *group = lfirst(lc);
     132             : 
     133          32 :                         if (group->lefttype == procform->amproclefttype &&
     134          32 :                             group->righttype == procform->amprocrighttype)
     135             :                         {
     136          24 :                             group->functionset |=
     137             :                                 ((uint64) 1) << SPGIST_COMPRESS_PROC;
     138          24 :                             break;
     139             :                         }
     140             :                     }
     141             :                 }
     142          28 :                 break;
     143          84 :             case SPGIST_CHOOSE_PROC:
     144             :             case SPGIST_PICKSPLIT_PROC:
     145             :             case SPGIST_INNER_CONSISTENT_PROC:
     146          84 :                 ok = check_amproc_signature(procform->amproc, VOIDOID, true,
     147             :                                             2, 2, INTERNALOID, INTERNALOID);
     148          84 :                 break;
     149          28 :             case SPGIST_LEAF_CONSISTENT_PROC:
     150          28 :                 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
     151             :                                             2, 2, INTERNALOID, INTERNALOID);
     152          28 :                 break;
     153           4 :             case SPGIST_COMPRESS_PROC:
     154           4 :                 if (configOutLefttype != procform->amproclefttype ||
     155           4 :                     configOutRighttype != procform->amprocrighttype)
     156           0 :                     ok = false;
     157             :                 else
     158           4 :                     ok = check_amproc_signature(procform->amproc,
     159             :                                                 configOut.leafType, true,
     160             :                                                 1, 1, procform->amproclefttype);
     161           4 :                 break;
     162           0 :             case SPGIST_OPTIONS_PROC:
     163           0 :                 ok = check_amoptsproc_signature(procform->amproc);
     164           0 :                 break;
     165           0 :             default:
     166           0 :                 ereport(INFO,
     167             :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     168             :                          errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
     169             :                                 opfamilyname, "spgist",
     170             :                                 format_procedure(procform->amproc),
     171             :                                 procform->amprocnum)));
     172           0 :                 result = false;
     173           0 :                 continue;       /* don't want additional message */
     174             :         }
     175             : 
     176         144 :         if (!ok)
     177             :         {
     178           0 :             ereport(INFO,
     179             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     180             :                      errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
     181             :                             opfamilyname, "spgist",
     182             :                             format_procedure(procform->amproc),
     183             :                             procform->amprocnum)));
     184           0 :             result = false;
     185             :         }
     186             :     }
     187             : 
     188             :     /* Check individual operators */
     189         312 :     for (i = 0; i < oprlist->n_members; i++)
     190             :     {
     191         284 :         HeapTuple   oprtup = &oprlist->members[i]->tuple;
     192         284 :         Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
     193             :         Oid         op_rettype;
     194             : 
     195             :         /* TODO: Check that only allowed strategy numbers exist */
     196         284 :         if (oprform->amopstrategy < 1 || oprform->amopstrategy > 63)
     197             :         {
     198           0 :             ereport(INFO,
     199             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     200             :                      errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
     201             :                             opfamilyname, "spgist",
     202             :                             format_operator(oprform->amopopr),
     203             :                             oprform->amopstrategy)));
     204           0 :             result = false;
     205             :         }
     206             : 
     207             :         /* spgist supports ORDER BY operators */
     208         284 :         if (oprform->amoppurpose != AMOP_SEARCH)
     209             :         {
     210             :             /* ... and operator result must match the claimed btree opfamily */
     211          16 :             op_rettype = get_op_rettype(oprform->amopopr);
     212          16 :             if (!opfamily_can_sort_type(oprform->amopsortfamily, op_rettype))
     213             :             {
     214           0 :                 ereport(INFO,
     215             :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     216             :                          errmsg("operator family \"%s\" of access method %s contains invalid ORDER BY specification for operator %s",
     217             :                                 opfamilyname, "spgist",
     218             :                                 format_operator(oprform->amopopr))));
     219           0 :                 result = false;
     220             :             }
     221             :         }
     222             :         else
     223         268 :             op_rettype = BOOLOID;
     224             : 
     225             :         /* Check operator signature --- same for all spgist strategies */
     226         284 :         if (!check_amop_signature(oprform->amopopr, op_rettype,
     227             :                                   oprform->amoplefttype,
     228             :                                   oprform->amoprighttype))
     229             :         {
     230           0 :             ereport(INFO,
     231             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     232             :                      errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
     233             :                             opfamilyname, "spgist",
     234             :                             format_operator(oprform->amopopr))));
     235           0 :             result = false;
     236             :         }
     237             :     }
     238             : 
     239             :     /* Now check for inconsistent groups of operators/functions */
     240          28 :     opclassgroup = NULL;
     241          76 :     foreach(lc, grouplist)
     242             :     {
     243          48 :         OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
     244             : 
     245             :         /* Remember the group exactly matching the test opclass */
     246          48 :         if (thisgroup->lefttype == opcintype &&
     247          48 :             thisgroup->righttype == opcintype)
     248          28 :             opclassgroup = thisgroup;
     249             : 
     250             :         /*
     251             :          * Complain if there are any datatype pairs with functions but no
     252             :          * operators.  This is about the best we can do for now to detect
     253             :          * missing operators.
     254             :          */
     255          48 :         if (thisgroup->operatorset == 0)
     256             :         {
     257           0 :             ereport(INFO,
     258             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     259             :                      errmsg("operator family \"%s\" of access method %s is missing operator(s) for types %s and %s",
     260             :                             opfamilyname, "spgist",
     261             :                             format_type_be(thisgroup->lefttype),
     262             :                             format_type_be(thisgroup->righttype))));
     263           0 :             result = false;
     264             :         }
     265             : 
     266             :         /*
     267             :          * Complain if we're missing functions for any datatype, remembering
     268             :          * that SP-GiST doesn't use cross-type support functions.
     269             :          */
     270          48 :         if (thisgroup->lefttype != thisgroup->righttype)
     271          20 :             continue;
     272             : 
     273         224 :         for (i = 1; i <= SPGISTNProc; i++)
     274             :         {
     275         196 :             if ((thisgroup->functionset & (((uint64) 1) << i)) != 0)
     276         168 :                 continue;       /* got it */
     277          28 :             if (i == SPGIST_OPTIONS_PROC)
     278          28 :                 continue;       /* optional method */
     279           0 :             ereport(INFO,
     280             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     281             :                      errmsg("operator family \"%s\" of access method %s is missing support function %d for type %s",
     282             :                             opfamilyname, "spgist", i,
     283             :                             format_type_be(thisgroup->lefttype))));
     284           0 :             result = false;
     285             :         }
     286             :     }
     287             : 
     288             :     /* Check that the originally-named opclass is supported */
     289             :     /* (if group is there, we already checked it adequately above) */
     290          28 :     if (!opclassgroup)
     291             :     {
     292           0 :         ereport(INFO,
     293             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     294             :                  errmsg("operator class \"%s\" of access method %s is missing operator(s)",
     295             :                         opclassname, "spgist")));
     296           0 :         result = false;
     297             :     }
     298             : 
     299          28 :     ReleaseCatCacheList(proclist);
     300          28 :     ReleaseCatCacheList(oprlist);
     301          28 :     ReleaseSysCache(familytup);
     302          28 :     ReleaseSysCache(classtup);
     303             : 
     304          28 :     return result;
     305             : }

Generated by: LCOV version 1.13