LCOV - differential code coverage report
Current view: top level - src/backend/commands - operatorcmds.c (source / functions) Coverage Total Hit UBC CBC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 92.8 % 265 246 19 246
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 6 6 6
Baseline: lcov-20261128-baseline Branches: 75.8 % 244 185 59 185
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 3 3 3
(30,360] days: 57.1 % 14 8 6 8
(360..) days: 94.8 % 248 235 13 235
Function coverage date bins:
(360..) days: 100.0 % 6 6 6
Branch coverage date bins:
(30,360] days: 50.0 % 16 8 8 8
(360..) days: 77.6 % 228 177 51 177

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * operatorcmds.c
                                  4                 :                :  *
                                  5                 :                :  *    Routines for operator manipulation commands
                                  6                 :                :  *
                                  7                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  8                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  9                 :                :  *
                                 10                 :                :  *
                                 11                 :                :  * IDENTIFICATION
                                 12                 :                :  *    src/backend/commands/operatorcmds.c
                                 13                 :                :  *
                                 14                 :                :  * DESCRIPTION
                                 15                 :                :  *    The "DefineFoo" routines take the parse tree and pick out the
                                 16                 :                :  *    appropriate arguments/flags, passing the results to the
                                 17                 :                :  *    corresponding "FooCreate" routines (in src/backend/catalog) that do
                                 18                 :                :  *    the actual catalog-munging.  These routines also verify permission
                                 19                 :                :  *    of the user to execute the command.
                                 20                 :                :  *
                                 21                 :                :  * NOTES
                                 22                 :                :  *    These things must be defined and committed in the following order:
                                 23                 :                :  *      "create function":
                                 24                 :                :  *              input/output, recv/send functions
                                 25                 :                :  *      "create type":
                                 26                 :                :  *              type
                                 27                 :                :  *      "create operator":
                                 28                 :                :  *              operators
                                 29                 :                :  *
                                 30                 :                :  *-------------------------------------------------------------------------
                                 31                 :                :  */
                                 32                 :                : #include "postgres.h"
                                 33                 :                : 
                                 34                 :                : #include "access/htup_details.h"
                                 35                 :                : #include "access/table.h"
                                 36                 :                : #include "catalog/indexing.h"
                                 37                 :                : #include "catalog/objectaccess.h"
                                 38                 :                : #include "catalog/pg_namespace.h"
                                 39                 :                : #include "catalog/pg_operator.h"
                                 40                 :                : #include "catalog/pg_proc.h"
                                 41                 :                : #include "catalog/pg_type.h"
                                 42                 :                : #include "commands/defrem.h"
                                 43                 :                : #include "miscadmin.h"
                                 44                 :                : #include "parser/parse_func.h"
                                 45                 :                : #include "parser/parse_oper.h"
                                 46                 :                : #include "parser/parse_type.h"
                                 47                 :                : #include "utils/acl.h"
                                 48                 :                : #include "utils/lsyscache.h"
                                 49                 :                : #include "utils/rel.h"
                                 50                 :                : #include "utils/syscache.h"
                                 51                 :                : 
                                 52                 :                : static Oid  ValidateRestrictionEstimator(List *restrictionName);
                                 53                 :                : static Oid  ValidateJoinEstimator(List *joinName);
                                 54                 :                : static Oid  ValidateOperatorReference(List *name,
                                 55                 :                :                                       Oid leftTypeId,
                                 56                 :                :                                       Oid rightTypeId);
                                 57                 :                : 
                                 58                 :                : /*
                                 59                 :                :  * DefineOperator
                                 60                 :                :  *      this function extracts all the information from the
                                 61                 :                :  *      parameter list generated by the parser and then has
                                 62                 :                :  *      OperatorCreate() do all the actual work.
                                 63                 :                :  *
                                 64                 :                :  * 'parameters' is a list of DefElem
                                 65                 :                :  */
                                 66                 :                : ObjectAddress
 8993 tgl@sss.pgh.pa.us          67                 :CBC        1039 : DefineOperator(List *names, List *parameters)
                                 68                 :                : {
                                 69                 :                :     char       *oprName;
                                 70                 :                :     Oid         oprNamespace;
                                 71                 :                :     AclResult   aclresult;
 3447                            72                 :           1039 :     bool        canMerge = false;   /* operator merges */
 6953 bruce@momjian.us           73                 :           1039 :     bool        canHash = false;    /* operator hashes */
 3447 tgl@sss.pgh.pa.us          74                 :           1039 :     List       *functionName = NIL; /* function for operator */
                                 75                 :           1039 :     TypeName   *typeName1 = NULL;   /* first type name */
                                 76                 :           1039 :     TypeName   *typeName2 = NULL;   /* second type name */
 8993                            77                 :           1039 :     Oid         typeId1 = InvalidOid;   /* types converted to OID */
                                 78                 :           1039 :     Oid         typeId2 = InvalidOid;
                                 79                 :                :     Oid         rettype;
 7714 bruce@momjian.us           80                 :           1039 :     List       *commutatorName = NIL;   /* optional commutator operator name */
 3447 tgl@sss.pgh.pa.us          81                 :           1039 :     List       *negatorName = NIL;  /* optional negator operator name */
 3027 peter_e@gmx.net            82                 :           1039 :     List       *restrictionName = NIL;  /* optional restrict. sel. function */
                                 83                 :           1039 :     List       *joinName = NIL; /* optional join sel. function */
                                 84                 :                :     Oid         functionOid;    /* functions converted to OID */
                                 85                 :                :     Oid         restrictionOid;
                                 86                 :                :     Oid         joinOid;
                                 87                 :                :     Oid         typeId[2];      /* to hold left and right arg */
                                 88                 :                :     int         nargs;
                                 89                 :                :     ListCell   *pl;
                                 90                 :                : 
                                 91                 :                :     /* Convert list of names to a name and namespace */
 8993 tgl@sss.pgh.pa.us          92                 :           1039 :     oprNamespace = QualifiedNameGetCreationNamespace(names, &oprName);
                                 93                 :                : 
                                 94                 :                :     /* Check we have creation rights in target namespace */
 1476 peter@eisentraut.org       95                 :           1039 :     aclresult = object_aclcheck(NamespaceRelationId, oprNamespace, GetUserId(), ACL_CREATE);
 8981 tgl@sss.pgh.pa.us          96         [ +  + ]:           1039 :     if (aclresult != ACLCHECK_OK)
 3283 peter_e@gmx.net            97                 :              4 :         aclcheck_error(aclresult, OBJECT_SCHEMA,
 8520 tgl@sss.pgh.pa.us          98                 :              4 :                        get_namespace_name(oprNamespace));
                                 99                 :                : 
                                100                 :                :     /*
                                101                 :                :      * loop over the definition list and extract the information we need.
                                102                 :                :      */
 8993                           103   [ +  -  +  +  :           6382 :     foreach(pl, parameters)
                                              +  + ]
                                104                 :                :     {
                                105                 :           5355 :         DefElem    *defel = (DefElem *) lfirst(pl);
                                106                 :                : 
 3228                           107         [ +  + ]:           5355 :         if (strcmp(defel->defname, "leftarg") == 0)
                                108                 :                :         {
 8993                           109                 :            980 :             typeName1 = defGetTypeName(defel);
                                110         [ +  + ]:            980 :             if (typeName1->setof)
 8532                           111         [ +  - ]:              4 :                 ereport(ERROR,
                                112                 :                :                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                113                 :                :                          errmsg("SETOF type not allowed for operator argument")));
                                114                 :                :         }
 3228                           115         [ +  + ]:           4375 :         else if (strcmp(defel->defname, "rightarg") == 0)
                                116                 :                :         {
 8993                           117                 :           1019 :             typeName2 = defGetTypeName(defel);
                                118         [ +  + ]:           1019 :             if (typeName2->setof)
 8532                           119         [ +  - ]:              4 :                 ereport(ERROR,
                                120                 :                :                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                121                 :                :                          errmsg("SETOF type not allowed for operator argument")));
                                122                 :                :         }
                                123                 :                :         /* "function" and "procedure" are equivalent here */
 3027 peter_e@gmx.net           124         [ +  + ]:           3356 :         else if (strcmp(defel->defname, "function") == 0)
                                125                 :             73 :             functionName = defGetQualifiedName(defel);
 3228 tgl@sss.pgh.pa.us         126         [ +  + ]:           3283 :         else if (strcmp(defel->defname, "procedure") == 0)
 8992                           127                 :            946 :             functionName = defGetQualifiedName(defel);
 3228                           128         [ +  + ]:           2337 :         else if (strcmp(defel->defname, "commutator") == 0)
 8992                           129                 :            613 :             commutatorName = defGetQualifiedName(defel);
 3228                           130         [ +  + ]:           1724 :         else if (strcmp(defel->defname, "negator") == 0)
 8992                           131                 :            378 :             negatorName = defGetQualifiedName(defel);
 3228                           132         [ +  + ]:           1346 :         else if (strcmp(defel->defname, "restrict") == 0)
 8992                           133                 :            584 :             restrictionName = defGetQualifiedName(defel);
 3228                           134         [ +  + ]:            762 :         else if (strcmp(defel->defname, "join") == 0)
 8992                           135                 :            564 :             joinName = defGetQualifiedName(defel);
 3228                           136         [ +  + ]:            198 :         else if (strcmp(defel->defname, "hashes") == 0)
 8233                           137                 :             54 :             canHash = defGetBoolean(defel);
 3228                           138         [ +  + ]:            144 :         else if (strcmp(defel->defname, "merges") == 0)
 8233                           139                 :             80 :             canMerge = defGetBoolean(defel);
                                140                 :                :         /* These obsolete options are taken as meaning canMerge */
 3228                           141         [ +  + ]:             64 :         else if (strcmp(defel->defname, "sort1") == 0)
 7280                           142                 :              8 :             canMerge = true;
 3228                           143         [ +  + ]:             56 :         else if (strcmp(defel->defname, "sort2") == 0)
 7280                           144                 :              8 :             canMerge = true;
 3228                           145         [ +  + ]:             48 :         else if (strcmp(defel->defname, "ltcmp") == 0)
 7280                           146                 :              4 :             canMerge = true;
 3228                           147         [ +  + ]:             44 :         else if (strcmp(defel->defname, "gtcmp") == 0)
 7280                           148                 :              4 :             canMerge = true;
                                149                 :                :         else
                                150                 :                :         {
                                151                 :                :             /* WARNING, not ERROR, for historical backwards-compatibility */
 8532                           152         [ +  - ]:             40 :             ereport(WARNING,
                                153                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                154                 :                :                      errmsg("operator attribute \"%s\" not recognized",
                                155                 :                :                             defel->defname)));
                                156                 :                :         }
                                157                 :                :     }
                                158                 :                : 
                                159                 :                :     /*
                                160                 :                :      * make sure we have our required definitions
                                161                 :                :      */
 8992                           162         [ +  + ]:           1027 :     if (functionName == NIL)
 8532                           163         [ +  - ]:              8 :         ereport(ERROR,
                                164                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                165                 :                :                  errmsg("operator function must be specified")));
                                166                 :                : 
                                167                 :                :     /* Transform type names to type OIDs */
 8993                           168         [ +  + ]:           1019 :     if (typeName1)
 5878 peter_e@gmx.net           169                 :            976 :         typeId1 = typenameTypeId(NULL, typeName1);
 8993 tgl@sss.pgh.pa.us         170         [ +  + ]:           1019 :     if (typeName2)
 5878 peter_e@gmx.net           171                 :           1011 :         typeId2 = typenameTypeId(NULL, typeName2);
                                172                 :                : 
                                173                 :                :     /*
                                174                 :                :      * If only the right argument is missing, the user is likely trying to
                                175                 :                :      * create a postfix operator, so give them a hint about why that does not
                                176                 :                :      * work.  But if both arguments are missing, do not mention postfix
                                177                 :                :      * operators, as the user most likely simply neglected to mention the
                                178                 :                :      * arguments.
                                179                 :                :      */
 6678 tgl@sss.pgh.pa.us         180   [ +  +  +  + ]:           1019 :     if (!OidIsValid(typeId1) && !OidIsValid(typeId2))
                                181         [ +  - ]:              4 :         ereport(ERROR,
                                182                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                183                 :                :                  errmsg("operator argument types must be specified")));
 2263                           184         [ +  + ]:           1015 :     if (!OidIsValid(typeId2))
                                185         [ +  - ]:              4 :         ereport(ERROR,
                                186                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                187                 :                :                  errmsg("operator right argument type must be specified"),
                                188                 :                :                  errdetail("Postfix operators are not supported.")));
                                189                 :                : 
 5457 peter_e@gmx.net           190         [ +  + ]:           1011 :     if (typeName1)
                                191                 :                :     {
 1476 peter@eisentraut.org      192                 :            972 :         aclresult = object_aclcheck(TypeRelationId, typeId1, GetUserId(), ACL_USAGE);
 5457 peter_e@gmx.net           193         [ +  + ]:            972 :         if (aclresult != ACLCHECK_OK)
 5279                           194                 :              8 :             aclcheck_error_type(aclresult, typeId1);
                                195                 :                :     }
                                196                 :                : 
 5457                           197         [ +  - ]:           1003 :     if (typeName2)
                                198                 :                :     {
 1476 peter@eisentraut.org      199                 :           1003 :         aclresult = object_aclcheck(TypeRelationId, typeId2, GetUserId(), ACL_USAGE);
 5457 peter_e@gmx.net           200         [ +  + ]:           1003 :         if (aclresult != ACLCHECK_OK)
 5279                           201                 :              4 :             aclcheck_error_type(aclresult, typeId2);
                                202                 :                :     }
                                203                 :                : 
                                204                 :                :     /*
                                205                 :                :      * Look up the operator's underlying function.
                                206                 :                :      */
 6678 tgl@sss.pgh.pa.us         207         [ +  + ]:            999 :     if (!OidIsValid(typeId1))
                                208                 :                :     {
                                209                 :             39 :         typeId[0] = typeId2;
                                210                 :             39 :         nargs = 1;
                                211                 :                :     }
                                212         [ -  + ]:            960 :     else if (!OidIsValid(typeId2))
                                213                 :                :     {
 6678 tgl@sss.pgh.pa.us         214                 :UBC           0 :         typeId[0] = typeId1;
                                215                 :              0 :         nargs = 1;
                                216                 :                :     }
                                217                 :                :     else
                                218                 :                :     {
 6678 tgl@sss.pgh.pa.us         219                 :CBC         960 :         typeId[0] = typeId1;
                                220                 :            960 :         typeId[1] = typeId2;
                                221                 :            960 :         nargs = 2;
                                222                 :                :     }
                                223                 :            999 :     functionOid = LookupFuncName(functionName, nargs, typeId, false);
                                224                 :                : 
                                225                 :                :     /*
                                226                 :                :      * We require EXECUTE rights for the function.  This isn't strictly
                                227                 :                :      * necessary, since EXECUTE will be checked at any attempted use of the
                                228                 :                :      * operator, but it seems like a good idea anyway.
                                229                 :                :      */
 1476 peter@eisentraut.org      230                 :            999 :     aclresult = object_aclcheck(ProcedureRelationId, functionOid, GetUserId(), ACL_EXECUTE);
 6678 tgl@sss.pgh.pa.us         231         [ +  + ]:            999 :     if (aclresult != ACLCHECK_OK)
 3283 peter_e@gmx.net           232                 :              4 :         aclcheck_error(aclresult, OBJECT_FUNCTION,
 6678 tgl@sss.pgh.pa.us         233                 :              4 :                        NameListToString(functionName));
                                234                 :                : 
 5457 peter_e@gmx.net           235                 :            995 :     rettype = get_func_rettype(functionOid);
 1476 peter@eisentraut.org      236                 :            995 :     aclresult = object_aclcheck(TypeRelationId, rettype, GetUserId(), ACL_USAGE);
 5457 peter_e@gmx.net           237         [ +  + ]:            995 :     if (aclresult != ACLCHECK_OK)
 5279                           238                 :              4 :         aclcheck_error_type(aclresult, rettype);
                                239                 :                : 
                                240                 :                :     /*
                                241                 :                :      * Look up restriction and join estimators if specified
                                242                 :                :      */
 6678 tgl@sss.pgh.pa.us         243         [ +  + ]:            991 :     if (restrictionName)
 4155 heikki.linnakangas@i      244                 :            584 :         restrictionOid = ValidateRestrictionEstimator(restrictionName);
                                245                 :                :     else
 6678 tgl@sss.pgh.pa.us         246                 :            407 :         restrictionOid = InvalidOid;
                                247         [ +  + ]:            991 :     if (joinName)
 4155 heikki.linnakangas@i      248                 :            564 :         joinOid = ValidateJoinEstimator(joinName);
                                249                 :                :     else
 6678 tgl@sss.pgh.pa.us         250                 :            427 :         joinOid = InvalidOid;
                                251                 :                : 
                                252                 :                :     /*
                                253                 :                :      * now have OperatorCreate do all the work..
                                254                 :                :      */
                                255                 :                :     return
 4931 bruce@momjian.us          256                 :            991 :         OperatorCreate(oprName, /* operator name */
                                257                 :                :                        oprNamespace,    /* namespace */
                                258                 :                :                        typeId1, /* left type id */
                                259                 :                :                        typeId2, /* right type id */
                                260                 :                :                        functionOid, /* function for operator */
                                261                 :                :                        commutatorName,  /* optional commutator operator name */
                                262                 :                :                        negatorName, /* optional negator operator name */
                                263                 :                :                        restrictionOid,  /* optional restrict. sel. function */
                                264                 :                :                        joinOid, /* optional join sel. function name */
                                265                 :                :                        canMerge,    /* operator merges */
                                266                 :                :                        canHash);    /* operator hashes */
                                267                 :                : }
                                268                 :                : 
                                269                 :                : /*
                                270                 :                :  * Look up a restriction estimator function by name, and verify that it has
                                271                 :                :  * the correct signature and we have the permissions to attach it to an
                                272                 :                :  * operator.
                                273                 :                :  */
                                274                 :                : static Oid
 4155 heikki.linnakangas@i      275                 :            814 : ValidateRestrictionEstimator(List *restrictionName)
                                276                 :                : {
                                277                 :                :     Oid         typeId[4];
                                278                 :                :     Oid         restrictionOid;
                                279                 :                : 
                                280                 :            814 :     typeId[0] = INTERNALOID;    /* PlannerInfo */
                                281                 :            814 :     typeId[1] = OIDOID;         /* operator OID */
                                282                 :            814 :     typeId[2] = INTERNALOID;    /* args list */
                                283                 :            814 :     typeId[3] = INT4OID;        /* varRelid */
                                284                 :                : 
                                285                 :            814 :     restrictionOid = LookupFuncName(restrictionName, 4, typeId, false);
                                286                 :                : 
                                287                 :                :     /* estimators must return float8 */
                                288         [ -  + ]:            810 :     if (get_func_rettype(restrictionOid) != FLOAT8OID)
 4155 heikki.linnakangas@i      289         [ #  # ]:UBC           0 :         ereport(ERROR,
                                290                 :                :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                291                 :                :                  errmsg("restriction estimator function %s must return type %s",
                                292                 :                :                         NameListToString(restrictionName), "float8")));
                                293                 :                : 
                                294                 :                :     /*
                                295                 :                :      * If the estimator is not a built-in function, require superuser
                                296                 :                :      * privilege to install it.  This protects against using something that is
                                297                 :                :      * not a restriction estimator or has hard-wired assumptions about what
                                298                 :                :      * data types it is working with.  (Built-in estimators are required to
                                299                 :                :      * defend themselves adequately against unexpected data type choices, but
                                300                 :                :      * it seems impractical to expect that of extensions' estimators.)
                                301                 :                :      *
                                302                 :                :      * If it is built-in, only require EXECUTE rights.
                                303                 :                :      */
  292 tgl@sss.pgh.pa.us         304         [ +  + ]:CBC         810 :     if (restrictionOid >= FirstGenbkiObjectId)
                                305                 :                :     {
                                306         [ -  + ]:             21 :         if (!superuser())
  292 tgl@sss.pgh.pa.us         307         [ #  # ]:UBC           0 :             ereport(ERROR,
                                308                 :                :                     (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
                                309                 :                :                      errmsg("must be superuser to specify a non-built-in restriction estimator function")));
                                310                 :                :     }
                                311                 :                :     else
                                312                 :                :     {
                                313                 :                :         AclResult   aclresult;
                                314                 :                : 
  292 tgl@sss.pgh.pa.us         315                 :CBC         789 :         aclresult = object_aclcheck(ProcedureRelationId, restrictionOid,
                                316                 :                :                                     GetUserId(), ACL_EXECUTE);
                                317         [ -  + ]:            789 :         if (aclresult != ACLCHECK_OK)
  292 tgl@sss.pgh.pa.us         318                 :UBC           0 :             aclcheck_error(aclresult, OBJECT_FUNCTION,
                                319                 :              0 :                            NameListToString(restrictionName));
                                320                 :                :     }
                                321                 :                : 
 4155 heikki.linnakangas@i      322                 :CBC         810 :     return restrictionOid;
                                323                 :                : }
                                324                 :                : 
                                325                 :                : /*
                                326                 :                :  * Look up a join estimator function by name, and verify that it has the
                                327                 :                :  * correct signature and we have the permissions to attach it to an
                                328                 :                :  * operator.
                                329                 :                :  */
                                330                 :                : static Oid
                                331                 :            794 : ValidateJoinEstimator(List *joinName)
                                332                 :                : {
                                333                 :                :     Oid         typeId[5];
                                334                 :                :     Oid         joinOid;
                                335                 :                :     Oid         joinOid2;
                                336                 :                : 
                                337                 :            794 :     typeId[0] = INTERNALOID;    /* PlannerInfo */
                                338                 :            794 :     typeId[1] = OIDOID;         /* operator OID */
                                339                 :            794 :     typeId[2] = INTERNALOID;    /* args list */
                                340                 :            794 :     typeId[3] = INT2OID;        /* jointype */
                                341                 :            794 :     typeId[4] = INTERNALOID;    /* SpecialJoinInfo */
                                342                 :                : 
                                343                 :                :     /*
                                344                 :                :      * As of Postgres 8.4, the preferred signature for join estimators has 5
                                345                 :                :      * arguments, but we still allow the old 4-argument form.  Whine about
                                346                 :                :      * ambiguity if both forms exist.
                                347                 :                :      */
                                348                 :            794 :     joinOid = LookupFuncName(joinName, 5, typeId, true);
 2301 tgl@sss.pgh.pa.us         349                 :            794 :     joinOid2 = LookupFuncName(joinName, 4, typeId, true);
                                350         [ +  + ]:            794 :     if (OidIsValid(joinOid))
                                351                 :                :     {
                                352         [ -  + ]:            790 :         if (OidIsValid(joinOid2))
 2301 tgl@sss.pgh.pa.us         353         [ #  # ]:UBC           0 :             ereport(ERROR,
                                354                 :                :                     (errcode(ERRCODE_AMBIGUOUS_FUNCTION),
                                355                 :                :                      errmsg("join estimator function %s has multiple matches",
                                356                 :                :                             NameListToString(joinName))));
                                357                 :                :     }
                                358                 :                :     else
                                359                 :                :     {
 2301 tgl@sss.pgh.pa.us         360                 :CBC           4 :         joinOid = joinOid2;
                                361                 :                :         /* If not found, reference the 5-argument signature in error msg */
                                362         [ +  - ]:              4 :         if (!OidIsValid(joinOid))
                                363                 :              4 :             joinOid = LookupFuncName(joinName, 5, typeId, false);
                                364                 :                :     }
                                365                 :                : 
                                366                 :                :     /* estimators must return float8 */
 4155 heikki.linnakangas@i      367         [ -  + ]:            790 :     if (get_func_rettype(joinOid) != FLOAT8OID)
 4155 heikki.linnakangas@i      368         [ #  # ]:UBC           0 :         ereport(ERROR,
                                369                 :                :                 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
                                370                 :                :                  errmsg("join estimator function %s must return type %s",
                                371                 :                :                         NameListToString(joinName), "float8")));
                                372                 :                : 
                                373                 :                :     /* privilege checks are the same as in ValidateRestrictionEstimator */
  292 tgl@sss.pgh.pa.us         374         [ +  + ]:CBC         790 :     if (joinOid >= FirstGenbkiObjectId)
                                375                 :                :     {
                                376         [ -  + ]:              5 :         if (!superuser())
  292 tgl@sss.pgh.pa.us         377         [ #  # ]:UBC           0 :             ereport(ERROR,
                                378                 :                :                     (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
                                379                 :                :                      errmsg("must be superuser to specify a non-built-in join estimator function")));
                                380                 :                :     }
                                381                 :                :     else
                                382                 :                :     {
                                383                 :                :         AclResult   aclresult;
                                384                 :                : 
  292 tgl@sss.pgh.pa.us         385                 :CBC         785 :         aclresult = object_aclcheck(ProcedureRelationId, joinOid,
                                386                 :                :                                     GetUserId(), ACL_EXECUTE);
                                387         [ -  + ]:            785 :         if (aclresult != ACLCHECK_OK)
  292 tgl@sss.pgh.pa.us         388                 :UBC           0 :             aclcheck_error(aclresult, OBJECT_FUNCTION,
                                389                 :              0 :                            NameListToString(joinName));
                                390                 :                :     }
                                391                 :                : 
 4155 heikki.linnakangas@i      392                 :CBC         790 :     return joinOid;
                                393                 :                : }
                                394                 :                : 
                                395                 :                : /*
                                396                 :                :  * Look up and return the OID of an operator,
                                397                 :                :  * given a possibly-qualified name and left and right type IDs.
                                398                 :                :  *
                                399                 :                :  * Verifies that the operator is defined (not a shell) and owned by
                                400                 :                :  * the current user, so that we have permission to associate it with
                                401                 :                :  * the operator being altered.  Rejecting shell operators is a policy
                                402                 :                :  * choice to help catch mistakes, rather than something essential.
                                403                 :                :  */
                                404                 :                : static Oid
 1135 tgl@sss.pgh.pa.us         405                 :             40 : ValidateOperatorReference(List *name,
                                406                 :                :                           Oid leftTypeId,
                                407                 :                :                           Oid rightTypeId)
                                408                 :                : {
                                409                 :                :     Oid         oid;
                                410                 :                :     bool        defined;
                                411                 :                : 
                                412                 :             40 :     oid = OperatorLookup(name,
                                413                 :                :                          leftTypeId,
                                414                 :                :                          rightTypeId,
                                415                 :                :                          &defined);
                                416                 :                : 
                                417                 :                :     /* These message strings are chosen to match parse_oper.c */
                                418         [ -  + ]:             40 :     if (!OidIsValid(oid))
 1135 tgl@sss.pgh.pa.us         419         [ #  # ]:UBC           0 :         ereport(ERROR,
                                420                 :                :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                421                 :                :                  errmsg("operator does not exist: %s",
                                422                 :                :                         op_signature_string(name,
                                423                 :                :                                             leftTypeId,
                                424                 :                :                                             rightTypeId))));
                                425                 :                : 
 1135 tgl@sss.pgh.pa.us         426         [ -  + ]:CBC          40 :     if (!defined)
 1135 tgl@sss.pgh.pa.us         427         [ #  # ]:UBC           0 :         ereport(ERROR,
                                428                 :                :                 (errcode(ERRCODE_UNDEFINED_FUNCTION),
                                429                 :                :                  errmsg("operator is only a shell: %s",
                                430                 :                :                         op_signature_string(name,
                                431                 :                :                                             leftTypeId,
                                432                 :                :                                             rightTypeId))));
                                433                 :                : 
 1135 tgl@sss.pgh.pa.us         434         [ -  + ]:CBC          40 :     if (!object_ownercheck(OperatorRelationId, oid, GetUserId()))
 1135 tgl@sss.pgh.pa.us         435                 :UBC           0 :         aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_OPERATOR,
                                436                 :              0 :                        NameListToString(name));
                                437                 :                : 
 1135 tgl@sss.pgh.pa.us         438                 :CBC          40 :     return oid;
                                439                 :                : }
                                440                 :                : 
                                441                 :                : 
                                442                 :                : /*
                                443                 :                :  * Guts of operator deletion.
                                444                 :                :  */
                                445                 :                : void
 8905                           446                 :            490 : RemoveOperatorById(Oid operOid)
                                447                 :                : {
                                448                 :                :     Relation    relation;
                                449                 :                :     HeapTuple   tup;
                                450                 :                :     Form_pg_operator op;
                                451                 :                : 
 2868 andres@anarazel.de        452                 :            490 :     relation = table_open(OperatorRelationId, RowExclusiveLock);
                                453                 :                : 
 6131 rhaas@postgresql.org      454                 :            490 :     tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operOid));
 8851 bruce@momjian.us          455         [ -  + ]:            490 :     if (!HeapTupleIsValid(tup)) /* should not happen */
 8532 tgl@sss.pgh.pa.us         456         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for operator %u", operOid);
 3900 tgl@sss.pgh.pa.us         457                 :CBC         490 :     op = (Form_pg_operator) GETSTRUCT(tup);
                                458                 :                : 
                                459                 :                :     /*
                                460                 :                :      * Reset links from commutator and negator, if any.  In case of a
                                461                 :                :      * self-commutator or self-negator, this means we have to re-fetch the
                                462                 :                :      * updated tuple.  (We could optimize away updates on the tuple we're
                                463                 :                :      * about to drop, but it doesn't seem worth convoluting the logic for.)
                                464                 :                :      */
                                465   [ +  +  -  + ]:            490 :     if (OidIsValid(op->oprcom) || OidIsValid(op->oprnegate))
                                466                 :                :     {
   19                           467                 :            224 :         UpdateOperatorLinks(operOid, op->oprcom, op->oprnegate,
                                468                 :                :                             op->oprresult, true);
 3900                           469   [ +  +  -  + ]:            224 :         if (operOid == op->oprcom || operOid == op->oprnegate)
                                470                 :                :         {
                                471                 :             83 :             ReleaseSysCache(tup);
                                472                 :             83 :             tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operOid));
                                473         [ -  + ]:             83 :             if (!HeapTupleIsValid(tup)) /* should not happen */
 3900 tgl@sss.pgh.pa.us         474         [ #  # ]:UBC           0 :                 elog(ERROR, "cache lookup failed for operator %u", operOid);
                                475                 :                :         }
                                476                 :                :     }
                                477                 :                : 
 3587 tgl@sss.pgh.pa.us         478                 :CBC         490 :     CatalogTupleDelete(relation, &tup->t_self);
                                479                 :                : 
 8905                           480                 :            490 :     ReleaseSysCache(tup);
                                481                 :                : 
 2868 andres@anarazel.de        482                 :            490 :     table_close(relation, RowExclusiveLock);
 8993 tgl@sss.pgh.pa.us         483                 :            490 : }
                                484                 :                : 
                                485                 :                : /*
                                486                 :                :  * AlterOperator
                                487                 :                :  *      routine implementing ALTER OPERATOR <operator> SET (option = ...).
                                488                 :                :  *
                                489                 :                :  * Currently, only RESTRICT and JOIN estimator functions can be changed.
                                490                 :                :  * COMMUTATOR, NEGATOR, MERGES, and HASHES attributes can be set if they
                                491                 :                :  * have not been set previously.  (Changing or removing one of these
                                492                 :                :  * attributes could invalidate existing plans, which seems more trouble
                                493                 :                :  * than it's worth.)
                                494                 :                :  */
                                495                 :                : ObjectAddress
 4155 heikki.linnakangas@i      496                 :            336 : AlterOperator(AlterOperatorStmt *stmt)
                                497                 :                : {
                                498                 :                :     ObjectAddress address;
                                499                 :                :     Oid         oprId;
                                500                 :                :     Relation    catalog;
                                501                 :                :     HeapTuple   tup;
                                502                 :                :     Form_pg_operator oprForm;
                                503                 :                :     Oid         oprResultId;
                                504                 :                :     int         i;
                                505                 :                :     ListCell   *pl;
                                506                 :                :     Datum       values[Natts_pg_operator];
                                507                 :                :     bool        nulls[Natts_pg_operator];
                                508                 :                :     bool        replaces[Natts_pg_operator];
 3027 peter_e@gmx.net           509                 :            336 :     List       *restrictionName = NIL;  /* optional restrict. sel. function */
 4155 heikki.linnakangas@i      510                 :            336 :     bool        updateRestriction = false;
                                511                 :                :     Oid         restrictionOid;
 3027 peter_e@gmx.net           512                 :            336 :     List       *joinName = NIL; /* optional join sel. function */
 4155 heikki.linnakangas@i      513                 :            336 :     bool        updateJoin = false;
                                514                 :                :     Oid         joinOid;
 1135 tgl@sss.pgh.pa.us         515                 :            336 :     List       *commutatorName = NIL;   /* optional commutator operator name */
                                516                 :                :     Oid         commutatorOid;
                                517                 :            336 :     List       *negatorName = NIL;  /* optional negator operator name */
                                518                 :                :     Oid         negatorOid;
                                519                 :            336 :     bool        canMerge = false;
                                520                 :            336 :     bool        updateMerges = false;
                                521                 :            336 :     bool        canHash = false;
                                522                 :            336 :     bool        updateHashes = false;
                                523                 :                : 
                                524                 :                :     /* Look up the operator */
 3622 peter_e@gmx.net           525                 :            336 :     oprId = LookupOperWithArgs(stmt->opername, false);
 2868 andres@anarazel.de        526                 :            336 :     catalog = table_open(OperatorRelationId, RowExclusiveLock);
 4155 heikki.linnakangas@i      527                 :            336 :     tup = SearchSysCacheCopy1(OPEROID, ObjectIdGetDatum(oprId));
 2764 tgl@sss.pgh.pa.us         528         [ -  + ]:            336 :     if (!HeapTupleIsValid(tup))
 4155 heikki.linnakangas@i      529         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for operator %u", oprId);
 4155 heikki.linnakangas@i      530                 :CBC         336 :     oprForm = (Form_pg_operator) GETSTRUCT(tup);
   19 tgl@sss.pgh.pa.us         531                 :            336 :     oprResultId = oprForm->oprresult;
                                532                 :                : 
                                533                 :                :     /* Process options */
 4155 heikki.linnakangas@i      534   [ +  -  +  +  :            894 :     foreach(pl, stmt->options)
                                              +  + ]
                                535                 :                :     {
                                536                 :            562 :         DefElem    *defel = (DefElem *) lfirst(pl);
                                537                 :                :         List       *param;
                                538                 :                : 
                                539         [ +  + ]:            562 :         if (defel->arg == NULL)
                                540                 :             42 :             param = NIL;        /* NONE, removes the function */
                                541                 :                :         else
                                542                 :            520 :             param = defGetQualifiedName(defel);
                                543                 :                : 
 3228 tgl@sss.pgh.pa.us         544         [ +  + ]:            562 :         if (strcmp(defel->defname, "restrict") == 0)
                                545                 :                :         {
 4155 heikki.linnakangas@i      546                 :            242 :             restrictionName = param;
                                547                 :            242 :             updateRestriction = true;
                                548                 :                :         }
 3228 tgl@sss.pgh.pa.us         549         [ +  + ]:            320 :         else if (strcmp(defel->defname, "join") == 0)
                                550                 :                :         {
 4155 heikki.linnakangas@i      551                 :            238 :             joinName = param;
                                552                 :            238 :             updateJoin = true;
                                553                 :                :         }
 1135 tgl@sss.pgh.pa.us         554         [ +  + ]:             82 :         else if (strcmp(defel->defname, "commutator") == 0)
                                555                 :                :         {
                                556                 :             20 :             commutatorName = defGetQualifiedName(defel);
                                557                 :                :         }
                                558         [ +  + ]:             62 :         else if (strcmp(defel->defname, "negator") == 0)
                                559                 :                :         {
                                560                 :             20 :             negatorName = defGetQualifiedName(defel);
                                561                 :                :         }
                                562         [ +  + ]:             42 :         else if (strcmp(defel->defname, "merges") == 0)
                                563                 :                :         {
                                564                 :             16 :             canMerge = defGetBoolean(defel);
                                565                 :             16 :             updateMerges = true;
                                566                 :                :         }
                                567         [ +  + ]:             26 :         else if (strcmp(defel->defname, "hashes") == 0)
                                568                 :                :         {
                                569                 :             22 :             canHash = defGetBoolean(defel);
                                570                 :             22 :             updateHashes = true;
                                571                 :                :         }
                                572                 :                : 
                                573                 :                :         /*
                                574                 :                :          * The rest of the options that CREATE accepts cannot be changed.
                                575                 :                :          * Check for them so that we can give a meaningful error message.
                                576                 :                :          */
 3228                           577         [ +  - ]:              4 :         else if (strcmp(defel->defname, "leftarg") == 0 ||
                                578         [ +  - ]:              4 :                  strcmp(defel->defname, "rightarg") == 0 ||
 3027 peter_e@gmx.net           579         [ +  - ]:              4 :                  strcmp(defel->defname, "function") == 0 ||
 1135 tgl@sss.pgh.pa.us         580         [ -  + ]:              4 :                  strcmp(defel->defname, "procedure") == 0)
                                581                 :                :         {
 4155 heikki.linnakangas@i      582         [ #  # ]:UBC           0 :             ereport(ERROR,
                                583                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                584                 :                :                      errmsg("operator attribute \"%s\" cannot be changed",
                                585                 :                :                             defel->defname)));
                                586                 :                :         }
                                587                 :                :         else
 4155 heikki.linnakangas@i      588         [ +  - ]:CBC           4 :             ereport(ERROR,
                                589                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                590                 :                :                      errmsg("operator attribute \"%s\" not recognized",
                                591                 :                :                             defel->defname)));
                                592                 :                :     }
                                593                 :                : 
                                594                 :                :     /* Check permissions. Must be owner. */
 1476 peter@eisentraut.org      595         [ +  + ]:            332 :     if (!object_ownercheck(OperatorRelationId, oprId, GetUserId()))
 3283 peter_e@gmx.net           596                 :              4 :         aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_OPERATOR,
 4155 heikki.linnakangas@i      597                 :              4 :                        NameStr(oprForm->oprname));
                                598                 :                : 
                                599                 :                :     /*
                                600                 :                :      * Look up OIDs for any parameters specified
                                601                 :                :      */
                                602         [ +  + ]:            328 :     if (restrictionName)
                                603                 :            230 :         restrictionOid = ValidateRestrictionEstimator(restrictionName);
                                604                 :                :     else
                                605                 :             98 :         restrictionOid = InvalidOid;
                                606         [ +  + ]:            324 :     if (joinName)
                                607                 :            230 :         joinOid = ValidateJoinEstimator(joinName);
                                608                 :                :     else
                                609                 :             94 :         joinOid = InvalidOid;
                                610                 :                : 
 1135 tgl@sss.pgh.pa.us         611         [ +  + ]:            320 :     if (commutatorName)
                                612                 :                :     {
                                613                 :                :         /* commutator has reversed arg types */
                                614                 :             20 :         commutatorOid = ValidateOperatorReference(commutatorName,
                                615                 :                :                                                   oprForm->oprright,
                                616                 :                :                                                   oprForm->oprleft);
                                617                 :                : 
                                618                 :                :         /*
                                619                 :                :          * We don't need to do anything extra for a self commutator as in
                                620                 :                :          * OperatorCreate, since the operator surely exists already.
                                621                 :                :          */
                                622                 :                :     }
                                623                 :                :     else
                                624                 :            300 :         commutatorOid = InvalidOid;
                                625                 :                : 
                                626         [ +  + ]:            320 :     if (negatorName)
                                627                 :                :     {
                                628                 :             20 :         negatorOid = ValidateOperatorReference(negatorName,
                                629                 :                :                                                oprForm->oprleft,
                                630                 :                :                                                oprForm->oprright);
                                631                 :                : 
                                632                 :                :         /* Must reject self-negation */
                                633         [ +  + ]:             20 :         if (negatorOid == oprForm->oid)
 4155 heikki.linnakangas@i      634         [ +  - ]:              4 :             ereport(ERROR,
                                635                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                636                 :                :                      errmsg("operator cannot be its own negator")));
                                637                 :                :     }
                                638                 :                :     else
                                639                 :                :     {
 1135 tgl@sss.pgh.pa.us         640                 :            300 :         negatorOid = InvalidOid;
                                641                 :                :     }
                                642                 :                : 
                                643                 :                :     /*
                                644                 :                :      * Check that we're not changing any attributes that might be depended on
                                645                 :                :      * by plans, while allowing no-op updates.
                                646                 :                :      */
                                647   [ +  +  +  + ]:            316 :     if (OidIsValid(commutatorOid) && OidIsValid(oprForm->oprcom) &&
                                648         [ +  + ]:              8 :         commutatorOid != oprForm->oprcom)
                                649         [ +  - ]:              4 :         ereport(ERROR,
                                650                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                651                 :                :                  errmsg("operator attribute \"%s\" cannot be changed if it has already been set",
                                652                 :                :                         "commutator")));
                                653                 :                : 
                                654   [ +  +  +  + ]:            312 :     if (OidIsValid(negatorOid) && OidIsValid(oprForm->oprnegate) &&
                                655         [ +  + ]:              8 :         negatorOid != oprForm->oprnegate)
                                656         [ +  - ]:              4 :         ereport(ERROR,
                                657                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                658                 :                :                  errmsg("operator attribute \"%s\" cannot be changed if it has already been set",
                                659                 :                :                         "negator")));
                                660                 :                : 
                                661   [ +  +  +  +  :            308 :     if (updateMerges && oprForm->oprcanmerge && !canMerge)
                                              +  + ]
                                662         [ +  - ]:              4 :         ereport(ERROR,
                                663                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                664                 :                :                  errmsg("operator attribute \"%s\" cannot be changed if it has already been set",
                                665                 :                :                         "merges")));
                                666                 :                : 
                                667   [ +  +  +  +  :            304 :     if (updateHashes && oprForm->oprcanhash && !canHash)
                                              +  + ]
                                668         [ +  - ]:              4 :         ereport(ERROR,
                                669                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                670                 :                :                  errmsg("operator attribute \"%s\" cannot be changed if it has already been set",
                                671                 :                :                         "hashes")));
                                672                 :                : 
                                673                 :                :     /* Perform additional checks, like OperatorCreate does */
                                674                 :            300 :     OperatorValidateParams(oprForm->oprleft,
                                675                 :                :                            oprForm->oprright,
                                676                 :                :                            oprForm->oprresult,
                                677                 :                :                            OidIsValid(commutatorOid),
                                678                 :                :                            OidIsValid(negatorOid),
                                679                 :                :                            OidIsValid(restrictionOid),
                                680                 :                :                            OidIsValid(joinOid),
                                681                 :                :                            canMerge,
                                682                 :                :                            canHash);
                                683                 :                : 
                                684                 :                :     /* Update the tuple */
 4155 heikki.linnakangas@i      685         [ +  + ]:           4800 :     for (i = 0; i < Natts_pg_operator; ++i)
                                686                 :                :     {
                                687                 :           4500 :         values[i] = (Datum) 0;
                                688                 :           4500 :         replaces[i] = false;
                                689                 :           4500 :         nulls[i] = false;
                                690                 :                :     }
                                691         [ +  + ]:            300 :     if (updateRestriction)
                                692                 :                :     {
                                693                 :            234 :         replaces[Anum_pg_operator_oprrest - 1] = true;
 1135 tgl@sss.pgh.pa.us         694                 :            234 :         values[Anum_pg_operator_oprrest - 1] = ObjectIdGetDatum(restrictionOid);
                                695                 :                :     }
 4155 heikki.linnakangas@i      696         [ +  + ]:            300 :     if (updateJoin)
                                697                 :                :     {
                                698                 :            234 :         replaces[Anum_pg_operator_oprjoin - 1] = true;
 1135 tgl@sss.pgh.pa.us         699                 :            234 :         values[Anum_pg_operator_oprjoin - 1] = ObjectIdGetDatum(joinOid);
                                700                 :                :     }
                                701         [ +  + ]:            300 :     if (OidIsValid(commutatorOid))
                                702                 :                :     {
                                703                 :             16 :         replaces[Anum_pg_operator_oprcom - 1] = true;
                                704                 :             16 :         values[Anum_pg_operator_oprcom - 1] = ObjectIdGetDatum(commutatorOid);
                                705                 :                :     }
                                706         [ +  + ]:            300 :     if (OidIsValid(negatorOid))
                                707                 :                :     {
                                708                 :             12 :         replaces[Anum_pg_operator_oprnegate - 1] = true;
                                709                 :             12 :         values[Anum_pg_operator_oprnegate - 1] = ObjectIdGetDatum(negatorOid);
                                710                 :                :     }
                                711         [ +  + ]:            300 :     if (updateMerges)
                                712                 :                :     {
                                713                 :             12 :         replaces[Anum_pg_operator_oprcanmerge - 1] = true;
                                714                 :             12 :         values[Anum_pg_operator_oprcanmerge - 1] = BoolGetDatum(canMerge);
                                715                 :                :     }
                                716         [ +  + ]:            300 :     if (updateHashes)
                                717                 :                :     {
                                718                 :             18 :         replaces[Anum_pg_operator_oprcanhash - 1] = true;
                                719                 :             18 :         values[Anum_pg_operator_oprcanhash - 1] = BoolGetDatum(canHash);
                                720                 :                :     }
                                721                 :                : 
 4155 heikki.linnakangas@i      722                 :            300 :     tup = heap_modify_tuple(tup, RelationGetDescr(catalog),
                                723                 :                :                             values, nulls, replaces);
                                724                 :                : 
 3588 alvherre@alvh.no-ip.      725                 :            300 :     CatalogTupleUpdate(catalog, &tup->t_self, tup);
                                726                 :                : 
 1929 tgl@sss.pgh.pa.us         727                 :            300 :     address = makeOperatorDependencies(tup, false, true);
                                728                 :                : 
                                729                 :                :     /*
                                730                 :                :      * We only worry about checking/changing newly-installed commutator or
                                731                 :                :      * negator values.  This function cannot change a shell operator into a
                                732                 :                :      * non-shell, so there's no need to recheck pre-existing links.
                                733                 :                :      */
 1135                           734   [ +  +  +  + ]:            300 :     if (OidIsValid(commutatorOid) || OidIsValid(negatorOid))
   19                           735                 :             28 :         UpdateOperatorLinks(oprId, commutatorOid, negatorOid,
                                736                 :                :                             oprResultId, false);
                                737                 :                : 
 3985                           738         [ -  + ]:            288 :     InvokeObjectPostAlterHook(OperatorRelationId, oprId, 0);
                                739                 :                : 
 2868 andres@anarazel.de        740                 :            288 :     table_close(catalog, NoLock);
                                741                 :                : 
 4155 heikki.linnakangas@i      742                 :            288 :     return address;
                                743                 :                : }
        

Generated by: LCOV version 2.0-1