LCOV - code coverage report
Current view: top level - src/backend/access/brin - brin_validate.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 65 86 75.6 %
Date: 2019-09-22 08:06:49 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * brin_validate.c
       4             :  *    Opclass validator for BRIN.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/access/brin/brin_validate.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "access/amvalidate.h"
      17             : #include "access/brin_internal.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/syscache.h"
      26             : #include "utils/regproc.h"
      27             : 
      28             : 
      29             : /*
      30             :  * Validator for a BRIN opclass.
      31             :  *
      32             :  * Some of the checks done here cover the whole opfamily, and therefore are
      33             :  * redundant when checking each opclass in a family.  But they don't run long
      34             :  * enough to be much of a problem, so we accept the duplication rather than
      35             :  * complicate the amvalidate API.
      36             :  */
      37             : bool
      38         116 : brinvalidate(Oid opclassoid)
      39             : {
      40         116 :     bool        result = true;
      41             :     HeapTuple   classtup;
      42             :     Form_pg_opclass classform;
      43             :     Oid         opfamilyoid;
      44             :     Oid         opcintype;
      45             :     char       *opclassname;
      46             :     HeapTuple   familytup;
      47             :     Form_pg_opfamily familyform;
      48             :     char       *opfamilyname;
      49             :     CatCList   *proclist,
      50             :                *oprlist;
      51         116 :     uint64      allfuncs = 0;
      52         116 :     uint64      allops = 0;
      53             :     List       *grouplist;
      54             :     OpFamilyOpFuncGroup *opclassgroup;
      55             :     int         i;
      56             :     ListCell   *lc;
      57             : 
      58             :     /* Fetch opclass information */
      59         116 :     classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
      60         116 :     if (!HeapTupleIsValid(classtup))
      61           0 :         elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
      62         116 :     classform = (Form_pg_opclass) GETSTRUCT(classtup);
      63             : 
      64         116 :     opfamilyoid = classform->opcfamily;
      65         116 :     opcintype = classform->opcintype;
      66         116 :     opclassname = NameStr(classform->opcname);
      67             : 
      68             :     /* Fetch opfamily information */
      69         116 :     familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
      70         116 :     if (!HeapTupleIsValid(familytup))
      71           0 :         elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
      72         116 :     familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
      73             : 
      74         116 :     opfamilyname = NameStr(familyform->opfname);
      75             : 
      76             :     /* Fetch all operators and support functions of the opfamily */
      77         116 :     oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
      78         116 :     proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
      79             : 
      80             :     /* Check individual support functions */
      81        1476 :     for (i = 0; i < proclist->n_members; i++)
      82             :     {
      83        1360 :         HeapTuple   proctup = &proclist->members[i]->tuple;
      84        1360 :         Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
      85             :         bool        ok;
      86             : 
      87             :         /* Check procedure numbers and function signatures */
      88        1360 :         switch (procform->amprocnum)
      89             :         {
      90             :             case BRIN_PROCNUM_OPCINFO:
      91         332 :                 ok = check_amproc_signature(procform->amproc, INTERNALOID, true,
      92             :                                             1, 1, INTERNALOID);
      93         332 :                 break;
      94             :             case BRIN_PROCNUM_ADDVALUE:
      95         332 :                 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
      96             :                                             4, 4, INTERNALOID, INTERNALOID,
      97             :                                             INTERNALOID, INTERNALOID);
      98         332 :                 break;
      99             :             case BRIN_PROCNUM_CONSISTENT:
     100         332 :                 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
     101             :                                             3, 3, INTERNALOID, INTERNALOID,
     102             :                                             INTERNALOID);
     103         332 :                 break;
     104             :             case BRIN_PROCNUM_UNION:
     105         332 :                 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
     106             :                                             3, 3, INTERNALOID, INTERNALOID,
     107             :                                             INTERNALOID);
     108         332 :                 break;
     109             :             default:
     110             :                 /* Complain if it's not a valid optional proc number */
     111          64 :                 if (procform->amprocnum < BRIN_FIRST_OPTIONAL_PROCNUM ||
     112          32 :                     procform->amprocnum > BRIN_LAST_OPTIONAL_PROCNUM)
     113             :                 {
     114           0 :                     ereport(INFO,
     115             :                             (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     116             :                              errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
     117             :                                     opfamilyname, "brin",
     118             :                                     format_procedure(procform->amproc),
     119             :                                     procform->amprocnum)));
     120           0 :                     result = false;
     121           0 :                     continue;   /* omit bad proc numbers from allfuncs */
     122             :                 }
     123             :                 /* Can't check signatures of optional procs, so assume OK */
     124          32 :                 ok = true;
     125          32 :                 break;
     126             :         }
     127             : 
     128        1360 :         if (!ok)
     129             :         {
     130           0 :             ereport(INFO,
     131             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     132             :                      errmsg("operator family \"%s\" of access method %s contains function %s with wrong signature for support number %d",
     133             :                             opfamilyname, "brin",
     134             :                             format_procedure(procform->amproc),
     135             :                             procform->amprocnum)));
     136           0 :             result = false;
     137             :         }
     138             : 
     139             :         /* Track all valid procedure numbers seen in opfamily */
     140        1360 :         allfuncs |= ((uint64) 1) << procform->amprocnum;
     141             :     }
     142             : 
     143             :     /* Check individual operators */
     144        1848 :     for (i = 0; i < oprlist->n_members; i++)
     145             :     {
     146        1732 :         HeapTuple   oprtup = &oprlist->members[i]->tuple;
     147        1732 :         Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
     148             : 
     149             :         /* Check that only allowed strategy numbers exist */
     150        1732 :         if (oprform->amopstrategy < 1 || oprform->amopstrategy > 63)
     151             :         {
     152           0 :             ereport(INFO,
     153             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     154             :                      errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
     155             :                             opfamilyname, "brin",
     156             :                             format_operator(oprform->amopopr),
     157             :                             oprform->amopstrategy)));
     158           0 :             result = false;
     159             :         }
     160             :         else
     161             :         {
     162             :             /*
     163             :              * The set of operators supplied varies across BRIN opfamilies.
     164             :              * Our plan is to identify all operator strategy numbers used in
     165             :              * the opfamily and then complain about datatype combinations that
     166             :              * are missing any operator(s).  However, consider only numbers
     167             :              * that appear in some non-cross-type case, since cross-type
     168             :              * operators may have unique strategies.  (This is not a great
     169             :              * heuristic, in particular an erroneous number used in a
     170             :              * cross-type operator will not get noticed; but the core BRIN
     171             :              * opfamilies are messy enough to make it necessary.)
     172             :              */
     173        1732 :             if (oprform->amoplefttype == oprform->amoprighttype)
     174         924 :                 allops |= ((uint64) 1) << oprform->amopstrategy;
     175             :         }
     176             : 
     177             :         /* brin doesn't support ORDER BY operators */
     178        3464 :         if (oprform->amoppurpose != AMOP_SEARCH ||
     179        1732 :             OidIsValid(oprform->amopsortfamily))
     180             :         {
     181           0 :             ereport(INFO,
     182             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     183             :                      errmsg("operator family \"%s\" of access method %s contains invalid ORDER BY specification for operator %s",
     184             :                             opfamilyname, "brin",
     185             :                             format_operator(oprform->amopopr))));
     186           0 :             result = false;
     187             :         }
     188             : 
     189             :         /* Check operator signature --- same for all brin strategies */
     190        1732 :         if (!check_amop_signature(oprform->amopopr, BOOLOID,
     191             :                                   oprform->amoplefttype,
     192             :                                   oprform->amoprighttype))
     193             :         {
     194           0 :             ereport(INFO,
     195             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     196             :                      errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
     197             :                             opfamilyname, "brin",
     198             :                             format_operator(oprform->amopopr))));
     199           0 :             result = false;
     200             :         }
     201             :     }
     202             : 
     203             :     /* Now check for inconsistent groups of operators/functions */
     204         116 :     grouplist = identify_opfamily_groups(oprlist, proclist);
     205         116 :     opclassgroup = NULL;
     206         456 :     foreach(lc, grouplist)
     207             :     {
     208         340 :         OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
     209             : 
     210             :         /* Remember the group exactly matching the test opclass */
     211         520 :         if (thisgroup->lefttype == opcintype &&
     212         180 :             thisgroup->righttype == opcintype)
     213         116 :             opclassgroup = thisgroup;
     214             : 
     215             :         /*
     216             :          * Some BRIN opfamilies expect cross-type support functions to exist,
     217             :          * and some don't.  We don't know exactly which are which, so if we
     218             :          * find a cross-type operator for which there are no support functions
     219             :          * at all, let it pass.  (Don't expect that all operators exist for
     220             :          * such cross-type cases, either.)
     221             :          */
     222         348 :         if (thisgroup->functionset == 0 &&
     223           8 :             thisgroup->lefttype != thisgroup->righttype)
     224           8 :             continue;
     225             : 
     226             :         /*
     227             :          * Else complain if there seems to be an incomplete set of either
     228             :          * operators or support functions for this datatype pair.
     229             :          */
     230         332 :         if (thisgroup->operatorset != allops)
     231             :         {
     232           0 :             ereport(INFO,
     233             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     234             :                      errmsg("operator family \"%s\" of access method %s is missing operator(s) for types %s and %s",
     235             :                             opfamilyname, "brin",
     236             :                             format_type_be(thisgroup->lefttype),
     237             :                             format_type_be(thisgroup->righttype))));
     238           0 :             result = false;
     239             :         }
     240         332 :         if (thisgroup->functionset != allfuncs)
     241             :         {
     242           0 :             ereport(INFO,
     243             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     244             :                      errmsg("operator family \"%s\" of access method %s is missing support function(s) for types %s and %s",
     245             :                             opfamilyname, "brin",
     246             :                             format_type_be(thisgroup->lefttype),
     247             :                             format_type_be(thisgroup->righttype))));
     248           0 :             result = false;
     249             :         }
     250             :     }
     251             : 
     252             :     /* Check that the originally-named opclass is complete */
     253         116 :     if (!opclassgroup || opclassgroup->operatorset != allops)
     254             :     {
     255           0 :         ereport(INFO,
     256             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     257             :                  errmsg("operator class \"%s\" of access method %s is missing operator(s)",
     258             :                         opclassname, "brin")));
     259           0 :         result = false;
     260             :     }
     261         580 :     for (i = 1; i <= BRIN_MANDATORY_NPROCS; i++)
     262             :     {
     263         928 :         if (opclassgroup &&
     264         464 :             (opclassgroup->functionset & (((int64) 1) << i)) != 0)
     265         464 :             continue;           /* got it */
     266           0 :         ereport(INFO,
     267             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     268             :                  errmsg("operator class \"%s\" of access method %s is missing support function %d",
     269             :                         opclassname, "brin", i)));
     270           0 :         result = false;
     271             :     }
     272             : 
     273         116 :     ReleaseCatCacheList(proclist);
     274         116 :     ReleaseCatCacheList(oprlist);
     275         116 :     ReleaseSysCache(familytup);
     276         116 :     ReleaseSysCache(classtup);
     277             : 
     278         116 :     return result;
     279             : }

Generated by: LCOV version 1.13