LCOV - code coverage report
Current view: top level - src/backend/access/nbtree - nbtvalidate.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 91 114 79.8 %
Date: 2024-04-26 11:11:00 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * nbtvalidate.c
       4             :  *    Opclass validator for btree.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/access/nbtree/nbtvalidate.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "access/amvalidate.h"
      17             : #include "access/htup_details.h"
      18             : #include "access/nbtree.h"
      19             : #include "access/xact.h"
      20             : #include "catalog/pg_am.h"
      21             : #include "catalog/pg_amop.h"
      22             : #include "catalog/pg_amproc.h"
      23             : #include "catalog/pg_opclass.h"
      24             : #include "catalog/pg_opfamily.h"
      25             : #include "catalog/pg_type.h"
      26             : #include "utils/builtins.h"
      27             : #include "utils/lsyscache.h"
      28             : #include "utils/regproc.h"
      29             : #include "utils/syscache.h"
      30             : 
      31             : 
      32             : /*
      33             :  * Validator for a btree opclass.
      34             :  *
      35             :  * Some of the checks done here cover the whole opfamily, and therefore are
      36             :  * redundant when checking each opclass in a family.  But they don't run long
      37             :  * enough to be much of a problem, so we accept the duplication rather than
      38             :  * complicate the amvalidate API.
      39             :  */
      40             : bool
      41         292 : btvalidate(Oid opclassoid)
      42             : {
      43         292 :     bool        result = true;
      44             :     HeapTuple   classtup;
      45             :     Form_pg_opclass classform;
      46             :     Oid         opfamilyoid;
      47             :     Oid         opcintype;
      48             :     char       *opclassname;
      49             :     HeapTuple   familytup;
      50             :     Form_pg_opfamily familyform;
      51             :     char       *opfamilyname;
      52             :     CatCList   *proclist,
      53             :                *oprlist;
      54             :     List       *grouplist;
      55             :     OpFamilyOpFuncGroup *opclassgroup;
      56             :     List       *familytypes;
      57             :     int         usefulgroups;
      58             :     int         i;
      59             :     ListCell   *lc;
      60             : 
      61             :     /* Fetch opclass information */
      62         292 :     classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
      63         292 :     if (!HeapTupleIsValid(classtup))
      64           0 :         elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
      65         292 :     classform = (Form_pg_opclass) GETSTRUCT(classtup);
      66             : 
      67         292 :     opfamilyoid = classform->opcfamily;
      68         292 :     opcintype = classform->opcintype;
      69         292 :     opclassname = NameStr(classform->opcname);
      70             : 
      71             :     /* Fetch opfamily information */
      72         292 :     familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
      73         292 :     if (!HeapTupleIsValid(familytup))
      74           0 :         elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
      75         292 :     familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
      76             : 
      77         292 :     opfamilyname = NameStr(familyform->opfname);
      78             : 
      79             :     /* Fetch all operators and support functions of the opfamily */
      80         292 :     oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
      81         292 :     proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
      82             : 
      83             :     /* Check individual support functions */
      84        2138 :     for (i = 0; i < proclist->n_members; i++)
      85             :     {
      86        1846 :         HeapTuple   proctup = &proclist->members[i]->tuple;
      87        1846 :         Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
      88             :         bool        ok;
      89             : 
      90             :         /* Check procedure numbers and function signatures */
      91        1846 :         switch (procform->amprocnum)
      92             :         {
      93        1102 :             case BTORDER_PROC:
      94        1102 :                 ok = check_amproc_signature(procform->amproc, INT4OID, true,
      95             :                                             2, 2, procform->amproclefttype,
      96             :                                             procform->amprocrighttype);
      97        1102 :                 break;
      98         234 :             case BTSORTSUPPORT_PROC:
      99         234 :                 ok = check_amproc_signature(procform->amproc, VOIDOID, true,
     100             :                                             1, 1, INTERNALOID);
     101         234 :                 break;
     102         228 :             case BTINRANGE_PROC:
     103         228 :                 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
     104             :                                             5, 5,
     105             :                                             procform->amproclefttype,
     106             :                                             procform->amproclefttype,
     107             :                                             procform->amprocrighttype,
     108             :                                             BOOLOID, BOOLOID);
     109         228 :                 break;
     110         282 :             case BTEQUALIMAGE_PROC:
     111         282 :                 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
     112             :                                             1, 1, OIDOID);
     113         282 :                 break;
     114           0 :             case BTOPTIONS_PROC:
     115           0 :                 ok = check_amoptsproc_signature(procform->amproc);
     116           0 :                 break;
     117           0 :             default:
     118           0 :                 ereport(INFO,
     119             :                         (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     120             :                          errmsg("operator family \"%s\" of access method %s contains function %s with invalid support number %d",
     121             :                                 opfamilyname, "btree",
     122             :                                 format_procedure(procform->amproc),
     123             :                                 procform->amprocnum)));
     124           0 :                 result = false;
     125           0 :                 continue;       /* don't want additional message */
     126             :         }
     127             : 
     128        1846 :         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, "btree",
     134             :                             format_procedure(procform->amproc),
     135             :                             procform->amprocnum)));
     136           0 :             result = false;
     137             :         }
     138             :     }
     139             : 
     140             :     /* Check individual operators */
     141        5802 :     for (i = 0; i < oprlist->n_members; i++)
     142             :     {
     143        5510 :         HeapTuple   oprtup = &oprlist->members[i]->tuple;
     144        5510 :         Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
     145             : 
     146             :         /* Check that only allowed strategy numbers exist */
     147        5510 :         if (oprform->amopstrategy < 1 ||
     148        5510 :             oprform->amopstrategy > BTMaxStrategyNumber)
     149             :         {
     150           0 :             ereport(INFO,
     151             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     152             :                      errmsg("operator family \"%s\" of access method %s contains operator %s with invalid strategy number %d",
     153             :                             opfamilyname, "btree",
     154             :                             format_operator(oprform->amopopr),
     155             :                             oprform->amopstrategy)));
     156           0 :             result = false;
     157             :         }
     158             : 
     159             :         /* btree doesn't support ORDER BY operators */
     160        5510 :         if (oprform->amoppurpose != AMOP_SEARCH ||
     161        5510 :             OidIsValid(oprform->amopsortfamily))
     162             :         {
     163           0 :             ereport(INFO,
     164             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     165             :                      errmsg("operator family \"%s\" of access method %s contains invalid ORDER BY specification for operator %s",
     166             :                             opfamilyname, "btree",
     167             :                             format_operator(oprform->amopopr))));
     168           0 :             result = false;
     169             :         }
     170             : 
     171             :         /* Check operator signature --- same for all btree strategies */
     172        5510 :         if (!check_amop_signature(oprform->amopopr, BOOLOID,
     173             :                                   oprform->amoplefttype,
     174             :                                   oprform->amoprighttype))
     175             :         {
     176           0 :             ereport(INFO,
     177             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     178             :                      errmsg("operator family \"%s\" of access method %s contains operator %s with wrong signature",
     179             :                             opfamilyname, "btree",
     180             :                             format_operator(oprform->amopopr))));
     181           0 :             result = false;
     182             :         }
     183             :     }
     184             : 
     185             :     /* Now check for inconsistent groups of operators/functions */
     186         292 :     grouplist = identify_opfamily_groups(oprlist, proclist);
     187         292 :     usefulgroups = 0;
     188         292 :     opclassgroup = NULL;
     189         292 :     familytypes = NIL;
     190        1460 :     foreach(lc, grouplist)
     191             :     {
     192        1168 :         OpFamilyOpFuncGroup *thisgroup = (OpFamilyOpFuncGroup *) lfirst(lc);
     193             : 
     194             :         /*
     195             :          * It is possible for an in_range support function to have a RHS type
     196             :          * that is otherwise irrelevant to the opfamily --- for instance, SQL
     197             :          * requires the datetime_ops opclass to have range support with an
     198             :          * interval offset.  So, if this group appears to contain only an
     199             :          * in_range function, ignore it: it doesn't represent a pair of
     200             :          * supported types.
     201             :          */
     202        1168 :         if (thisgroup->operatorset == 0 &&
     203          66 :             thisgroup->functionset == (1 << BTINRANGE_PROC))
     204          66 :             continue;
     205             : 
     206             :         /* Else count it as a relevant group */
     207        1102 :         usefulgroups++;
     208             : 
     209             :         /* Remember the group exactly matching the test opclass */
     210        1102 :         if (thisgroup->lefttype == opcintype &&
     211         434 :             thisgroup->righttype == opcintype)
     212         292 :             opclassgroup = thisgroup;
     213             : 
     214             :         /*
     215             :          * Identify all distinct data types handled in this opfamily.  This
     216             :          * implementation is O(N^2), but there aren't likely to be enough
     217             :          * types in the family for it to matter.
     218             :          */
     219        1102 :         familytypes = list_append_unique_oid(familytypes, thisgroup->lefttype);
     220        1102 :         familytypes = list_append_unique_oid(familytypes, thisgroup->righttype);
     221             : 
     222             :         /*
     223             :          * Complain if there seems to be an incomplete set of either operators
     224             :          * or support functions for this datatype pair.  The sortsupport,
     225             :          * in_range, and equalimage functions are considered optional.
     226             :          */
     227        1102 :         if (thisgroup->operatorset !=
     228             :             ((1 << BTLessStrategyNumber) |
     229             :              (1 << BTLessEqualStrategyNumber) |
     230             :              (1 << BTEqualStrategyNumber) |
     231             :              (1 << BTGreaterEqualStrategyNumber) |
     232             :              (1 << BTGreaterStrategyNumber)))
     233             :         {
     234           0 :             ereport(INFO,
     235             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     236             :                      errmsg("operator family \"%s\" of access method %s is missing operator(s) for types %s and %s",
     237             :                             opfamilyname, "btree",
     238             :                             format_type_be(thisgroup->lefttype),
     239             :                             format_type_be(thisgroup->righttype))));
     240           0 :             result = false;
     241             :         }
     242        1102 :         if ((thisgroup->functionset & (1 << BTORDER_PROC)) == 0)
     243             :         {
     244           0 :             ereport(INFO,
     245             :                     (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     246             :                      errmsg("operator family \"%s\" of access method %s is missing support function for types %s and %s",
     247             :                             opfamilyname, "btree",
     248             :                             format_type_be(thisgroup->lefttype),
     249             :                             format_type_be(thisgroup->righttype))));
     250           0 :             result = false;
     251             :         }
     252             :     }
     253             : 
     254             :     /* Check that the originally-named opclass is supported */
     255             :     /* (if group is there, we already checked it adequately above) */
     256         292 :     if (!opclassgroup)
     257             :     {
     258           0 :         ereport(INFO,
     259             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     260             :                  errmsg("operator class \"%s\" of access method %s is missing operator(s)",
     261             :                         opclassname, "btree")));
     262           0 :         result = false;
     263             :     }
     264             : 
     265             :     /*
     266             :      * Complain if the opfamily doesn't have entries for all possible
     267             :      * combinations of its supported datatypes.  While missing cross-type
     268             :      * operators are not fatal, they do limit the planner's ability to derive
     269             :      * additional qual clauses from equivalence classes, so it seems
     270             :      * reasonable to insist that all built-in btree opfamilies be complete.
     271             :      */
     272         292 :     if (usefulgroups != (list_length(familytypes) * list_length(familytypes)))
     273             :     {
     274          16 :         ereport(INFO,
     275             :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
     276             :                  errmsg("operator family \"%s\" of access method %s is missing cross-type operator(s)",
     277             :                         opfamilyname, "btree")));
     278          16 :         result = false;
     279             :     }
     280             : 
     281         292 :     ReleaseCatCacheList(proclist);
     282         292 :     ReleaseCatCacheList(oprlist);
     283         292 :     ReleaseSysCache(familytup);
     284         292 :     ReleaseSysCache(classtup);
     285             : 
     286         292 :     return result;
     287             : }
     288             : 
     289             : /*
     290             :  * Prechecking function for adding operators/functions to a btree opfamily.
     291             :  */
     292             : void
     293         202 : btadjustmembers(Oid opfamilyoid,
     294             :                 Oid opclassoid,
     295             :                 List *operators,
     296             :                 List *functions)
     297             : {
     298             :     Oid         opcintype;
     299             :     ListCell   *lc;
     300             : 
     301             :     /*
     302             :      * Btree operators and comparison support functions are always "loose"
     303             :      * members of the opfamily if they are cross-type.  If they are not
     304             :      * cross-type, we prefer to tie them to the appropriate opclass ... but if
     305             :      * the user hasn't created one, we can't do that, and must fall back to
     306             :      * using the opfamily dependency.  (We mustn't force creation of an
     307             :      * opclass in such a case, as leaving an incomplete opclass laying about
     308             :      * would be bad.  Throwing an error is another undesirable alternative.)
     309             :      *
     310             :      * This behavior results in a bit of a dump/reload hazard, in that the
     311             :      * order of restoring objects could affect what dependencies we end up
     312             :      * with.  pg_dump's existing behavior will preserve the dependency choices
     313             :      * in most cases, but not if a cross-type operator has been bound tightly
     314             :      * into an opclass.  That's a mistake anyway, so silently "fixing" it
     315             :      * isn't awful.
     316             :      *
     317             :      * Optional support functions are always "loose" family members.
     318             :      *
     319             :      * To avoid repeated lookups, we remember the most recently used opclass's
     320             :      * input type.
     321             :      */
     322         202 :     if (OidIsValid(opclassoid))
     323             :     {
     324             :         /* During CREATE OPERATOR CLASS, need CCI to see the pg_opclass row */
     325          92 :         CommandCounterIncrement();
     326          92 :         opcintype = get_opclass_input_type(opclassoid);
     327             :     }
     328             :     else
     329         110 :         opcintype = InvalidOid;
     330             : 
     331             :     /*
     332             :      * We handle operators and support functions almost identically, so rather
     333             :      * than duplicate this code block, just join the lists.
     334             :      */
     335        1152 :     foreach(lc, list_concat_copy(operators, functions))
     336             :     {
     337         950 :         OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
     338             : 
     339         950 :         if (op->is_func && op->number != BTORDER_PROC)
     340             :         {
     341             :             /* Optional support proc, so always a soft family dependency */
     342          14 :             op->ref_is_hard = false;
     343          14 :             op->ref_is_family = true;
     344          14 :             op->refobjid = opfamilyoid;
     345             :         }
     346         936 :         else if (op->lefttype != op->righttype)
     347             :         {
     348             :             /* Cross-type, so always a soft family dependency */
     349         418 :             op->ref_is_hard = false;
     350         418 :             op->ref_is_family = true;
     351         418 :             op->refobjid = opfamilyoid;
     352             :         }
     353             :         else
     354             :         {
     355             :             /* Not cross-type; is there a suitable opclass? */
     356         518 :             if (op->lefttype != opcintype)
     357             :             {
     358             :                 /* Avoid repeating this expensive lookup, even if it fails */
     359          40 :                 opcintype = op->lefttype;
     360          40 :                 opclassoid = opclass_for_family_datatype(BTREE_AM_OID,
     361             :                                                          opfamilyoid,
     362             :                                                          opcintype);
     363             :             }
     364         518 :             if (OidIsValid(opclassoid))
     365             :             {
     366             :                 /* Hard dependency on opclass */
     367         478 :                 op->ref_is_hard = true;
     368         478 :                 op->ref_is_family = false;
     369         478 :                 op->refobjid = opclassoid;
     370             :             }
     371             :             else
     372             :             {
     373             :                 /* We're stuck, so make a soft dependency on the opfamily */
     374          40 :                 op->ref_is_hard = false;
     375          40 :                 op->ref_is_family = true;
     376          40 :                 op->refobjid = opfamilyoid;
     377             :             }
     378             :         }
     379             :     }
     380         202 : }

Generated by: LCOV version 1.14