LCOV - differential code coverage report
Current view: top level - src/backend/access/nbtree - nbtvalidate.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 79.8 % 114 91 23 91
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 2 2 2
Baseline: 16@8cea358b128 Branches: 59.1 % 88 52 36 52
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (240..) days: 79.8 % 114 91 23 91
Function coverage date bins:
(240..) days: 100.0 % 2 2 2
Branch coverage date bins:
(240..) days: 59.1 % 88 52 36 52

 Age         Owner                    Branch data    TLA  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
 3010 tgl@sss.pgh.pa.us          41                 :CBC         146 : btvalidate(Oid opclassoid)
                                 42                 :                : {
 3006                            43                 :            146 :     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 */
 3010                            62                 :            146 :     classtup = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclassoid));
                                 63         [ -  + ]:            146 :     if (!HeapTupleIsValid(classtup))
 3010 tgl@sss.pgh.pa.us          64         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for operator class %u", opclassoid);
 3010 tgl@sss.pgh.pa.us          65                 :CBC         146 :     classform = (Form_pg_opclass) GETSTRUCT(classtup);
                                 66                 :                : 
                                 67                 :            146 :     opfamilyoid = classform->opcfamily;
                                 68                 :            146 :     opcintype = classform->opcintype;
 3006                            69                 :            146 :     opclassname = NameStr(classform->opcname);
                                 70                 :                : 
                                 71                 :                :     /* Fetch opfamily information */
                                 72                 :            146 :     familytup = SearchSysCache1(OPFAMILYOID, ObjectIdGetDatum(opfamilyoid));
                                 73         [ -  + ]:            146 :     if (!HeapTupleIsValid(familytup))
 3006 tgl@sss.pgh.pa.us          74         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for operator family %u", opfamilyoid);
 3006 tgl@sss.pgh.pa.us          75                 :CBC         146 :     familyform = (Form_pg_opfamily) GETSTRUCT(familytup);
                                 76                 :                : 
                                 77                 :            146 :     opfamilyname = NameStr(familyform->opfname);
                                 78                 :                : 
                                 79                 :                :     /* Fetch all operators and support functions of the opfamily */
 3010                            80                 :            146 :     oprlist = SearchSysCacheList1(AMOPSTRATEGY, ObjectIdGetDatum(opfamilyoid));
                                 81                 :            146 :     proclist = SearchSysCacheList1(AMPROCNUM, ObjectIdGetDatum(opfamilyoid));
                                 82                 :                : 
                                 83                 :                :     /* Check individual support functions */
                                 84         [ +  + ]:           1069 :     for (i = 0; i < proclist->n_members; i++)
                                 85                 :                :     {
                                 86                 :            923 :         HeapTuple   proctup = &proclist->members[i]->tuple;
                                 87                 :            923 :         Form_pg_amproc procform = (Form_pg_amproc) GETSTRUCT(proctup);
                                 88                 :                :         bool        ok;
                                 89                 :                : 
                                 90                 :                :         /* Check procedure numbers and function signatures */
 3006                            91   [ +  +  +  +  :            923 :         switch (procform->amprocnum)
                                              -  - ]
                                 92                 :                :         {
                                 93                 :            551 :             case BTORDER_PROC:
                                 94                 :            551 :                 ok = check_amproc_signature(procform->amproc, INT4OID, true,
                                 95                 :                :                                             2, 2, procform->amproclefttype,
                                 96                 :                :                                             procform->amprocrighttype);
                                 97                 :            551 :                 break;
                                 98                 :            117 :             case BTSORTSUPPORT_PROC:
                                 99                 :            117 :                 ok = check_amproc_signature(procform->amproc, VOIDOID, true,
                                100                 :                :                                             1, 1, INTERNALOID);
                                101                 :            117 :                 break;
 2258                           102                 :            114 :             case BTINRANGE_PROC:
                                103                 :            114 :                 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
                                104                 :                :                                             5, 5,
                                105                 :                :                                             procform->amproclefttype,
                                106                 :                :                                             procform->amproclefttype,
                                107                 :                :                                             procform->amprocrighttype,
                                108                 :                :                                             BOOLOID, BOOLOID);
                                109                 :            114 :                 break;
 1509 pg@bowt.ie                110                 :            141 :             case BTEQUALIMAGE_PROC:
                                111                 :            141 :                 ok = check_amproc_signature(procform->amproc, BOOLOID, true,
                                112                 :                :                                             1, 1, OIDOID);
                                113                 :            141 :                 break;
 1476 akorotkov@postgresql      114                 :UBC           0 :             case BTOPTIONS_PROC:
                                115                 :              0 :                 ok = check_amoptsproc_signature(procform->amproc);
                                116                 :              0 :                 break;
 3006 tgl@sss.pgh.pa.us         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                 :                : 
 3006 tgl@sss.pgh.pa.us         128         [ -  + ]:CBC         923 :         if (!ok)
                                129                 :                :         {
 3006 tgl@sss.pgh.pa.us         130         [ #  # ]:UBC           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 */
 3010 tgl@sss.pgh.pa.us         141         [ +  + ]:CBC        2901 :     for (i = 0; i < oprlist->n_members; i++)
                                142                 :                :     {
                                143                 :           2755 :         HeapTuple   oprtup = &oprlist->members[i]->tuple;
                                144                 :           2755 :         Form_pg_amop oprform = (Form_pg_amop) GETSTRUCT(oprtup);
                                145                 :                : 
                                146                 :                :         /* Check that only allowed strategy numbers exist */
                                147         [ +  - ]:           2755 :         if (oprform->amopstrategy < 1 ||
                                148         [ -  + ]:           2755 :             oprform->amopstrategy > BTMaxStrategyNumber)
                                149                 :                :         {
 3006 tgl@sss.pgh.pa.us         150         [ #  # ]:UBC           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 */
 3006 tgl@sss.pgh.pa.us         160         [ +  - ]:CBC        2755 :         if (oprform->amoppurpose != AMOP_SEARCH ||
                                161         [ -  + ]:           2755 :             OidIsValid(oprform->amopsortfamily))
                                162                 :                :         {
 3006 tgl@sss.pgh.pa.us         163         [ #  # ]:UBC           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 */
 3006 tgl@sss.pgh.pa.us         172         [ -  + ]:CBC        2755 :         if (!check_amop_signature(oprform->amopopr, BOOLOID,
                                173                 :                :                                   oprform->amoplefttype,
                                174                 :                :                                   oprform->amoprighttype))
                                175                 :                :         {
 3006 tgl@sss.pgh.pa.us         176         [ #  # ]:UBC           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 */
 3006 tgl@sss.pgh.pa.us         186                 :CBC         146 :     grouplist = identify_opfamily_groups(oprlist, proclist);
 2258                           187                 :            146 :     usefulgroups = 0;
 3006                           188                 :            146 :     opclassgroup = NULL;
                                189                 :            146 :     familytypes = NIL;
                                190   [ +  -  +  +  :            730 :     foreach(lc, grouplist)
                                              +  + ]
                                191                 :                :     {
                                192                 :            584 :         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                 :                :          */
 2258                           202         [ +  + ]:            584 :         if (thisgroup->operatorset == 0 &&
                                203         [ +  - ]:             33 :             thisgroup->functionset == (1 << BTINRANGE_PROC))
                                204                 :             33 :             continue;
                                205                 :                : 
                                206                 :                :         /* Else count it as a relevant group */
                                207                 :            551 :         usefulgroups++;
                                208                 :                : 
                                209                 :                :         /* Remember the group exactly matching the test opclass */
 3006                           210         [ +  + ]:            551 :         if (thisgroup->lefttype == opcintype &&
                                211         [ +  + ]:            217 :             thisgroup->righttype == opcintype)
                                212                 :            146 :             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                 :            551 :         familytypes = list_append_unique_oid(familytypes, thisgroup->lefttype);
                                220                 :            551 :         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         [ -  + ]:            551 :         if (thisgroup->operatorset !=
                                228                 :                :             ((1 << BTLessStrategyNumber) |
                                229                 :                :              (1 << BTLessEqualStrategyNumber) |
                                230                 :                :              (1 << BTEqualStrategyNumber) |
                                231                 :                :              (1 << BTGreaterEqualStrategyNumber) |
                                232                 :                :              (1 << BTGreaterStrategyNumber)))
                                233                 :                :         {
 3006 tgl@sss.pgh.pa.us         234         [ #  # ]:UBC           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                 :                :         }
 3006 tgl@sss.pgh.pa.us         242         [ -  + ]:CBC         551 :         if ((thisgroup->functionset & (1 << BTORDER_PROC)) == 0)
                                243                 :                :         {
 3006 tgl@sss.pgh.pa.us         244         [ #  # ]:UBC           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) */
 3006 tgl@sss.pgh.pa.us         256         [ -  + ]:CBC         146 :     if (!opclassgroup)
                                257                 :                :     {
 3006 tgl@sss.pgh.pa.us         258         [ #  # ]:UBC           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                 :                :      */
 2258 tgl@sss.pgh.pa.us         272         [ +  + ]:CBC         146 :     if (usefulgroups != (list_length(familytypes) * list_length(familytypes)))
                                273                 :                :     {
 3006                           274         [ +  - ]:              8 :         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                 :              8 :         result = false;
                                279                 :                :     }
                                280                 :                : 
 3010                           281                 :            146 :     ReleaseCatCacheList(proclist);
                                282                 :            146 :     ReleaseCatCacheList(oprlist);
 3006                           283                 :            146 :     ReleaseSysCache(familytup);
                                284                 :            146 :     ReleaseSysCache(classtup);
                                285                 :                : 
                                286                 :            146 :     return result;
                                287                 :                : }
                                288                 :                : 
                                289                 :                : /*
                                290                 :                :  * Prechecking function for adding operators/functions to a btree opfamily.
                                291                 :                :  */
                                292                 :                : void
 1352                           293                 :            101 : 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         [ +  + ]:            101 :     if (OidIsValid(opclassoid))
                                323                 :                :     {
                                324                 :                :         /* During CREATE OPERATOR CLASS, need CCI to see the pg_opclass row */
                                325                 :             46 :         CommandCounterIncrement();
                                326                 :             46 :         opcintype = get_opclass_input_type(opclassoid);
                                327                 :                :     }
                                328                 :                :     else
                                329                 :             55 :         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   [ +  +  +  +  :            576 :     foreach(lc, list_concat_copy(operators, functions))
                                              +  + ]
                                336                 :                :     {
                                337                 :            475 :         OpFamilyMember *op = (OpFamilyMember *) lfirst(lc);
                                338                 :                : 
                                339   [ +  +  +  + ]:            475 :         if (op->is_func && op->number != BTORDER_PROC)
                                340                 :                :         {
                                341                 :                :             /* Optional support proc, so always a soft family dependency */
                                342                 :              7 :             op->ref_is_hard = false;
                                343                 :              7 :             op->ref_is_family = true;
                                344                 :              7 :             op->refobjid = opfamilyoid;
                                345                 :                :         }
                                346         [ +  + ]:            468 :         else if (op->lefttype != op->righttype)
                                347                 :                :         {
                                348                 :                :             /* Cross-type, so always a soft family dependency */
                                349                 :            209 :             op->ref_is_hard = false;
                                350                 :            209 :             op->ref_is_family = true;
                                351                 :            209 :             op->refobjid = opfamilyoid;
                                352                 :                :         }
                                353                 :                :         else
                                354                 :                :         {
                                355                 :                :             /* Not cross-type; is there a suitable opclass? */
                                356         [ +  + ]:            259 :             if (op->lefttype != opcintype)
                                357                 :                :             {
                                358                 :                :                 /* Avoid repeating this expensive lookup, even if it fails */
                                359                 :             20 :                 opcintype = op->lefttype;
                                360                 :             20 :                 opclassoid = opclass_for_family_datatype(BTREE_AM_OID,
                                361                 :                :                                                          opfamilyoid,
                                362                 :                :                                                          opcintype);
                                363                 :                :             }
                                364         [ +  + ]:            259 :             if (OidIsValid(opclassoid))
                                365                 :                :             {
                                366                 :                :                 /* Hard dependency on opclass */
                                367                 :            239 :                 op->ref_is_hard = true;
                                368                 :            239 :                 op->ref_is_family = false;
                                369                 :            239 :                 op->refobjid = opclassoid;
                                370                 :                :             }
                                371                 :                :             else
                                372                 :                :             {
                                373                 :                :                 /* We're stuck, so make a soft dependency on the opfamily */
                                374                 :             20 :                 op->ref_is_hard = false;
                                375                 :             20 :                 op->ref_is_family = true;
                                376                 :             20 :                 op->refobjid = opfamilyoid;
                                377                 :                :             }
                                378                 :                :         }
                                379                 :                :     }
                                380                 :            101 : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622