LCOV - code coverage report
Current view: top level - src/backend/access/brin - brin_validate.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 74.3 % 109 81
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 1 1
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 43.3 % 104 45

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

Generated by: LCOV version 2.3.2-1