LCOV - code coverage report
Current view: top level - src/backend/access/gist - gistvalidate.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 77 98 78.6 %
Date: 2020-05-25 05:06:35 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * gistvalidate.c
       4             :  *    Opclass validator for 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/gist/gistvalidate.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "access/amvalidate.h"
      17             : #include "access/gist_private.h"
      18             : #include "access/htup_details.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 a GiST opclass.
      32             :  */
      33             : bool
      34          98 : gistvalidate(Oid opclassoid)
      35             : {
      36          98 :     bool        result = true;
      37             :     HeapTuple   classtup;
      38             :     Form_pg_opclass classform;
      39             :     Oid         opfamilyoid;
      40             :     Oid         opcintype;
      41             :     Oid         opckeytype;
      42             :     char       *opclassname;
      43             :     HeapTuple   familytup;
      44             :     Form_pg_opfamily familyform;
      45             :     char       *opfamilyname;
      46             :     CatCList   *proclist,
      47             :                *oprlist;
      48             :     List       *grouplist;
      49             :     OpFamilyOpFuncGroup *opclassgroup;
      50             :     int         i;
      51             :     ListCell   *lc;
      52             : 
      53             :     /* Fetch opclass information */
      54          98 :     classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
      55          98 :     if (!HeapTupleIsValid(classtup))
      56           0 :         elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
      57          98 :     classform = (Form_pg_opclass) GETSTRUCT(classtup);
      58             : 
      59          98 :     opfamilyoid = classform->opcfamily;
      60          98 :     opcintype = classform->opcintype;
      61          98 :     opckeytype = classform->opckeytype;
      62          98 :     if (!OidIsValid(opckeytype))
      63          18 :         opckeytype = opcintype;
      64          98 :     opclassname = NameStr(classform->opcname);
      65             : 
      66             :     /* Fetch opfamily information */
      67          98 :     familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
      68          98 :     if (!HeapTupleIsValid(familytup))
      69           0 :         elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
      70          98 :     familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
      71             : 
      72          98 :     opfamilyname = NameStr(familyform->opfname);
      73             : 
      74             :     /* Fetch all operators and support functions of the opfamily */
      75          98 :     oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
      76          98 :     proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
      77             : 
      78             :     /* Check individual support functions */
      79         852 :     for (i = 0; i < proclist->n_members; i++)
      80             :     {
      81         754 :         HeapTuple   proctup = &proclist->members[i]->tuple;
      82         754 :         Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
      83             :         bool        ok;
      84             : 
      85             :         /*
      86             :          * All GiST support functions should be registered with matching
      87             :          * left/right types
      88             :          */
      89         754 :         if (procform->amproclefttype != procform->amprocrighttype)
      90             :         {
      91           0 :             ereport(INFO,
      92             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
      93             :                      errmsg("operator family \"%s\" of access method %s contains support function %s with different left and right input types",
      94             :                             opfamilyname, "gist",
      95             :                             format_procedure(procform->amproc))));
      96           0 :             result = false;
      97             :         }
      98             : 
      99             :         /*
     100             :          * We can't check signatures except within the specific opclass, since
     101             :          * we need to know the associated opckeytype in many cases.
     102             :          */
     103         754 :         if (procform->amproclefttype != opcintype)
     104           0 :             continue;
     105             : 
     106             :         /* Check procedure numbers and function signatures */
     107         754 :         switch (procform->amprocnum)
     108             :         {
     109          98 :             case GIST_CONSISTENT_PROC:
     110          98 :                 ok = check_amproc_signature(procform->amproc, BOOLOID, false,
     111             :                                             5, 5, INTERNALOID, opcintype,
     112             :                                             INT2OID, OIDOID, INTERNALOID);
     113          98 :                 break;
     114          98 :             case GIST_UNION_PROC:
     115          98 :                 ok = check_amproc_signature(procform->amproc, opckeytype, false,
     116             :                                             2, 2, INTERNALOID, INTERNALOID);
     117          98 :                 break;
     118         204 :             case GIST_COMPRESS_PROC:
     119             :             case GIST_DECOMPRESS_PROC:
     120             :             case GIST_FETCH_PROC:
     121         204 :                 ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
     122             :                                             1, 1, INTERNALOID);
     123         204 :                 break;
     124          98 :             case GIST_PENALTY_PROC:
     125          98 :                 ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
     126             :                                             3, 3, INTERNALOID,
     127             :                                             INTERNALOID, INTERNALOID);
     128          98 :                 break;
     129          98 :             case GIST_PICKSPLIT_PROC:
     130          98 :                 ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
     131             :                                             2, 2, INTERNALOID, INTERNALOID);
     132          98 :                 break;
     133          98 :             case GIST_EQUAL_PROC:
     134          98 :                 ok = check_amproc_signature(procform->amproc, INTERNALOID, false,
     135             :                                             3, 3, opckeytype, opckeytype,
     136             :                                             INTERNALOID);
     137          98 :                 break;
     138          44 :             case GIST_DISTANCE_PROC:
     139          44 :                 ok = check_amproc_signature(procform->amproc, FLOAT8OID, false,
     140             :                                             5, 5, INTERNALOID, opcintype,
     141             :                                             INT2OID, OIDOID, INTERNALOID);
     142          44 :                 break;
     143          16 :             case GIST_OPTIONS_PROC:
     144          16 :                 ok = check_amoptsproc_signature(procform->amproc);
     145          16 :                 break;
     146           0 :             default:
     147           0 :                 ereport(INFO,
     148             :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     149             :                          errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
     150             :                                 opfamilyname, "gist",
     151             :                                 format_procedure(procform->amproc),
     152             :                                 procform->amprocnum)));
     153           0 :                 result = false;
     154           0 :                 continue;       /* don't want additional message */
     155             :         }
     156             : 
     157         754 :         if (!ok)
     158             :         {
     159           0 :             ereport(INFO,
     160             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     161             :                      errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
     162             :                             opfamilyname, "gist",
     163             :                             format_procedure(procform->amproc),
     164             :                             procform->amprocnum)));
     165           0 :             result = false;
     166             :         }
     167             :     }
     168             : 
     169             :     /* Check individual operators */
     170         874 :     for (i = 0; i < oprlist->n_members; i++)
     171             :     {
     172         776 :         HeapTuple   oprtup = &oprlist->members[i]->tuple;
     173         776 :         Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
     174             :         Oid         op_rettype;
     175             : 
     176             :         /* TODO: Check that only allowed strategy numbers exist */
     177         776 :         if (oprform->amopstrategy < 1)
     178             :         {
     179           0 :             ereport(INFO,
     180             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     181             :                      errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
     182             :                             opfamilyname, "gist",
     183             :                             format_operator(oprform->amopopr),
     184             :                             oprform->amopstrategy)));
     185           0 :             result = false;
     186             :         }
     187             : 
     188             :         /* GiST supports ORDER BY operators */
     189         776 :         if (oprform->amoppurpose != AMOP_SEARCH)
     190             :         {
     191             :             /* ... but must have matching distance proc */
     192          54 :             if (!OidIsValid(get_opfamily_proc(opfamilyoid,
     193             :                                               oprform->amoplefttype,
     194             :                                               oprform->amoplefttype,
     195             :                                               GIST_DISTANCE_PROC)))
     196             :             {
     197           0 :                 ereport(INFO,
     198             :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     199             :                          errmsg("operator family \"%s\" of access method %s contains unsupported ORDER BY specification for operator %s",
     200             :                                 opfamilyname, "gist",
     201             :                                 format_operator(oprform->amopopr))));
     202           0 :                 result = false;
     203             :             }
     204             :             /* ... and operator result must match the claimed btree opfamily */
     205          54 :             op_rettype = get_op_rettype(oprform->amopopr);
     206          54 :             if (!opfamily_can_sort_type(oprform->amopsortfamily, op_rettype))
     207             :             {
     208           0 :                 ereport(INFO,
     209             :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     210             :                          errmsg("operator family \"%s\" of access method %s contains incorrect ORDER BY opfamily specification for operator %s",
     211             :                                 opfamilyname, "gist",
     212             :                                 format_operator(oprform->amopopr))));
     213           0 :                 result = false;
     214             :             }
     215             :         }
     216             :         else
     217             :         {
     218             :             /* Search operators must always return bool */
     219         722 :             op_rettype = BOOLOID;
     220             :         }
     221             : 
     222             :         /* Check operator signature */
     223         776 :         if (!check_amop_signature(oprform->amopopr, op_rettype,
     224             :                                   oprform->amoplefttype,
     225             :                                   oprform->amoprighttype))
     226             :         {
     227           0 :             ereport(INFO,
     228             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     229             :                      errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
     230             :                             opfamilyname, "gist",
     231             :                             format_operator(oprform->amopopr))));
     232           0 :             result = false;
     233             :         }
     234             :     }
     235             : 
     236             :     /* Now check for inconsistent groups of operators/functions */
     237          98 :     grouplist = identify_opfamily_groups(oprlist, proclist);
     238          98 :     opclassgroup = NULL;
     239         272 :     foreach(lc, grouplist)
     240             :     {
     241         174 :         OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
     242             : 
     243             :         /* Remember the group exactly matching the test opclass */
     244         174 :         if (thisgroup->lefttype == opcintype &&
     245         154 :             thisgroup->righttype == opcintype)
     246          98 :             opclassgroup = thisgroup;
     247             : 
     248             :         /*
     249             :          * There is not a lot we can do to check the operator sets, since each
     250             :          * GiST opclass is more or less a law unto itself, and some contain
     251             :          * only operators that are binary-compatible with the opclass datatype
     252             :          * (meaning that empty operator sets can be OK).  That case also means
     253             :          * that we shouldn't insist on nonempty function sets except for the
     254             :          * opclass's own group.
     255             :          */
     256             :     }
     257             : 
     258             :     /* Check that the originally-named opclass is complete */
     259        1078 :     for (i = 1; i <= GISTNProcs; i++)
     260             :     {
     261         980 :         if (opclassgroup &&
     262         980 :             (opclassgroup->functionset & (((uint64) 1) << i)) != 0)
     263         754 :             continue;           /* got it */
     264         226 :         if (i == GIST_DISTANCE_PROC || i == GIST_FETCH_PROC ||
     265         114 :             i == GIST_COMPRESS_PROC || i == GIST_DECOMPRESS_PROC ||
     266             :             i == GIST_OPTIONS_PROC)
     267         226 :             continue;           /* optional methods */
     268           0 :         ereport(INFO,
     269             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     270             :                  errmsg("operator class \"%s\" of access method %s is missing support function %d",
     271             :                         opclassname, "gist", i)));
     272           0 :         result = false;
     273             :     }
     274             : 
     275          98 :     ReleaseCatCacheList(proclist);
     276          98 :     ReleaseCatCacheList(oprlist);
     277          98 :     ReleaseSysCache(familytup);
     278          98 :     ReleaseSysCache(classtup);
     279             : 
     280          98 :     return result;
     281             : }

Generated by: LCOV version 1.13