LCOV - differential code coverage report
Current view: top level - src/backend/catalog - pg_operator.c (source / functions) Coverage Total Hit UBC CBC DUB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 90.2 % 297 268 29 268 3
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 10 10 10 1
Baseline: lcov-20261128-baseline Branches: 67.2 % 256 172 84 172
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: 91.3 % 46 42 4 42
(360..) days: 90.0 % 251 226 25 226
Function coverage date bins:
(7,30] days: 100.0 % 2 2 2
(360..) days: 100.0 % 8 8 8
Branch coverage date bins:
(7,30] days: 69.2 % 52 36 16 36
(360..) days: 66.7 % 204 136 68 136

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pg_operator.c
                                  4                 :                :  *    routines to support manipulation of the pg_operator relation
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/catalog/pg_operator.c
                                 12                 :                :  *
                                 13                 :                :  * NOTES
                                 14                 :                :  *    these routines moved here from commands/define.c and somewhat cleaned up.
                                 15                 :                :  *
                                 16                 :                :  *-------------------------------------------------------------------------
                                 17                 :                :  */
                                 18                 :                : #include "postgres.h"
                                 19                 :                : 
                                 20                 :                : #include "access/htup_details.h"
                                 21                 :                : #include "access/table.h"
                                 22                 :                : #include "access/xact.h"
                                 23                 :                : #include "catalog/catalog.h"
                                 24                 :                : #include "catalog/dependency.h"
                                 25                 :                : #include "catalog/indexing.h"
                                 26                 :                : #include "catalog/namespace.h"
                                 27                 :                : #include "catalog/objectaccess.h"
                                 28                 :                : #include "catalog/pg_namespace.h"
                                 29                 :                : #include "catalog/pg_operator.h"
                                 30                 :                : #include "catalog/pg_proc.h"
                                 31                 :                : #include "catalog/pg_type.h"
                                 32                 :                : #include "miscadmin.h"
                                 33                 :                : #include "parser/parse_oper.h"
                                 34                 :                : #include "utils/acl.h"
                                 35                 :                : #include "utils/builtins.h"
                                 36                 :                : #include "utils/lsyscache.h"
                                 37                 :                : #include "utils/rel.h"
                                 38                 :                : #include "utils/syscache.h"
                                 39                 :                : 
                                 40                 :                : 
                                 41                 :                : static Oid  OperatorGet(const char *operatorName,
                                 42                 :                :                         Oid operatorNamespace,
                                 43                 :                :                         Oid leftObjectId,
                                 44                 :                :                         Oid rightObjectId,
                                 45                 :                :                         bool *defined);
                                 46                 :                : 
                                 47                 :                : static Oid  OperatorShellMake(const char *operatorName,
                                 48                 :                :                               Oid operatorNamespace,
                                 49                 :                :                               Oid leftTypeId,
                                 50                 :                :                               Oid rightTypeId);
                                 51                 :                : 
                                 52                 :                : static Oid  get_other_operator(List *otherOp,
                                 53                 :                :                                Oid otherLeftTypeId, Oid otherRightTypeId,
                                 54                 :                :                                const char *operatorName, Oid operatorNamespace,
                                 55                 :                :                                Oid leftTypeId, Oid rightTypeId);
                                 56                 :                : 
                                 57                 :                : static Oid  get_opowner(Oid opno);
                                 58                 :                : 
                                 59                 :                : 
                                 60                 :                : /*
                                 61                 :                :  * Check whether a proposed operator name is legal
                                 62                 :                :  *
                                 63                 :                :  * This had better match the behavior of parser/scan.l!
                                 64                 :                :  *
                                 65                 :                :  * We need this because the parser is not smart enough to check that
                                 66                 :                :  * the arguments of CREATE OPERATOR's COMMUTATOR, NEGATOR, etc clauses
                                 67                 :                :  * are operator names rather than some other lexical entity.
                                 68                 :                :  */
                                 69                 :                : static bool
 9168 tgl@sss.pgh.pa.us          70                 :CBC        1315 : validOperatorName(const char *name)
                                 71                 :                : {
 9165 bruce@momjian.us           72                 :           1315 :     size_t      len = strlen(name);
                                 73                 :                : 
                                 74                 :                :     /* Can't be empty or too long */
 9168 tgl@sss.pgh.pa.us          75   [ +  -  -  + ]:           1315 :     if (len == 0 || len >= NAMEDATALEN)
 9168 tgl@sss.pgh.pa.us          76                 :UBC           0 :         return false;
                                 77                 :                : 
                                 78                 :                :     /* Can't contain any invalid characters */
                                 79                 :                :     /* Test string here should match op_chars in scan.l */
 8531 tgl@sss.pgh.pa.us          80         [ -  + ]:CBC        1315 :     if (strspn(name, "~!@#^&|`?+-*/%<>=") != len)
 9168 tgl@sss.pgh.pa.us          81                 :UBC           0 :         return false;
                                 82                 :                : 
                                 83                 :                :     /* Can't contain slash-star or dash-dash (comment starts) */
 9168 tgl@sss.pgh.pa.us          84   [ +  -  -  + ]:CBC        1315 :     if (strstr(name, "/*") || strstr(name, "--"))
 9168 tgl@sss.pgh.pa.us          85                 :UBC           0 :         return false;
                                 86                 :                : 
                                 87                 :                :     /*
                                 88                 :                :      * For SQL standard compatibility, '+' and '-' cannot be the last char of
                                 89                 :                :      * a multi-char operator unless the operator contains chars that are not
                                 90                 :                :      * in SQL operators. The idea is to lex '=-' as two operators, but not to
                                 91                 :                :      * forbid operator names like '?-' that could not be sequences of standard
                                 92                 :                :      * SQL operators.
                                 93                 :                :      */
 9168 tgl@sss.pgh.pa.us          94         [ +  + ]:CBC        1315 :     if (len > 1 &&
 9165 bruce@momjian.us           95         [ +  - ]:            943 :         (name[len - 1] == '+' ||
                                 96         [ +  + ]:            943 :          name[len - 1] == '-'))
                                 97                 :                :     {
                                 98                 :                :         int         ic;
                                 99                 :                : 
                                100         [ +  - ]:             10 :         for (ic = len - 2; ic >= 0; ic--)
                                101                 :                :         {
 8531 tgl@sss.pgh.pa.us         102         [ +  + ]:             10 :             if (strchr("~!@#^&|`?%", name[ic]))
 9168                           103                 :              5 :                 break;
                                104                 :                :         }
                                105         [ -  + ]:              5 :         if (ic < 0)
 9168 tgl@sss.pgh.pa.us         106                 :UBC           0 :             return false;       /* nope, not valid */
                                107                 :                :     }
                                108                 :                : 
                                109                 :                :     /* != isn't valid either, because parser will convert it to <> */
 9168 tgl@sss.pgh.pa.us         110         [ -  + ]:CBC        1315 :     if (strcmp(name, "!=") == 0)
 9168 tgl@sss.pgh.pa.us         111                 :UBC           0 :         return false;
                                112                 :                : 
 9168 tgl@sss.pgh.pa.us         113                 :CBC        1315 :     return true;
                                114                 :                : }
                                115                 :                : 
                                116                 :                : 
                                117                 :                : /*
                                118                 :                :  * OperatorGet
                                119                 :                :  *
                                120                 :                :  *      finds an operator given an exact specification (name, namespace,
                                121                 :                :  *      left and right type IDs).
                                122                 :                :  *
                                123                 :                :  *      *defined is set true if defined (not a shell)
                                124                 :                :  */
                                125                 :                : static Oid
 9010                           126                 :            991 : OperatorGet(const char *operatorName,
                                127                 :                :             Oid operatorNamespace,
                                128                 :                :             Oid leftObjectId,
                                129                 :                :             Oid rightObjectId,
                                130                 :                :             bool *defined)
                                131                 :                : {
                                132                 :                :     HeapTuple   tup;
                                133                 :                :     Oid         operatorObjectId;
                                134                 :                : 
 6131 rhaas@postgresql.org      135                 :            991 :     tup = SearchSysCache4(OPERNAMENSP,
                                136                 :                :                           PointerGetDatum(operatorName),
                                137                 :                :                           ObjectIdGetDatum(leftObjectId),
                                138                 :                :                           ObjectIdGetDatum(rightObjectId),
                                139                 :                :                           ObjectIdGetDatum(operatorNamespace));
10093 tgl@sss.pgh.pa.us         140         [ +  + ]:            991 :     if (HeapTupleIsValid(tup))
                                141                 :                :     {
 2930 andres@anarazel.de        142                 :            309 :         Form_pg_operator oprform = (Form_pg_operator) GETSTRUCT(tup);
                                143                 :                : 
                                144                 :            309 :         operatorObjectId = oprform->oid;
                                145                 :            309 :         *defined = RegProcedureIsValid(oprform->oprcode);
 8992 tgl@sss.pgh.pa.us         146                 :            309 :         ReleaseSysCache(tup);
                                147                 :                :     }
                                148                 :                :     else
                                149                 :                :     {
10093                           150                 :            682 :         operatorObjectId = InvalidOid;
                                151                 :            682 :         *defined = false;
                                152                 :                :     }
                                153                 :                : 
 8992                           154                 :            991 :     return operatorObjectId;
                                155                 :                : }
                                156                 :                : 
                                157                 :                : /*
                                158                 :                :  * OperatorLookup
                                159                 :                :  *
                                160                 :                :  *      looks up an operator given a possibly-qualified name and
                                161                 :                :  *      left and right type IDs.
                                162                 :                :  *
                                163                 :                :  *      *defined is set true if defined (not a shell)
                                164                 :                :  */
                                165                 :                : Oid
                                166                 :           1031 : OperatorLookup(List *operatorName,
                                167                 :                :                Oid leftObjectId,
                                168                 :                :                Oid rightObjectId,
                                169                 :                :                bool *defined)
                                170                 :                : {
                                171                 :                :     Oid         operatorObjectId;
                                172                 :                :     RegProcedure oprcode;
                                173                 :                : 
 7564                           174                 :           1031 :     operatorObjectId = LookupOperName(NULL, operatorName,
                                175                 :                :                                       leftObjectId, rightObjectId,
                                176                 :                :                                       true, -1);
 8992                           177         [ +  + ]:           1031 :     if (!OidIsValid(operatorObjectId))
                                178                 :                :     {
                                179                 :            463 :         *defined = false;
                                180                 :            463 :         return InvalidOid;
                                181                 :                :     }
                                182                 :                : 
                                183                 :            568 :     oprcode = get_opcode(operatorObjectId);
                                184                 :            568 :     *defined = RegProcedureIsValid(oprcode);
                                185                 :                : 
10160 bruce@momjian.us          186                 :            568 :     return operatorObjectId;
                                187                 :                : }
                                188                 :                : 
                                189                 :                : 
                                190                 :                : /*
                                191                 :                :  * OperatorShellMake
                                192                 :                :  *      Make a "shell" entry for a not-yet-existing operator.
                                193                 :                :  */
                                194                 :                : static Oid
 9010 tgl@sss.pgh.pa.us         195                 :            324 : OperatorShellMake(const char *operatorName,
                                196                 :                :                   Oid operatorNamespace,
                                197                 :                :                   Oid leftTypeId,
                                198                 :                :                   Oid rightTypeId)
                                199                 :                : {
                                200                 :                :     Relation    pg_operator_desc;
                                201                 :                :     Oid         operatorObjectId;
                                202                 :                :     int         i;
                                203                 :                :     HeapTuple   tup;
                                204                 :                :     Datum       values[Natts_pg_operator];
                                205                 :                :     bool        nulls[Natts_pg_operator];
                                206                 :                :     NameData    oname;
                                207                 :                :     TupleDesc   tupDesc;
                                208                 :                : 
                                209                 :                :     /*
                                210                 :                :      * validate operator name
                                211                 :                :      */
 9168                           212         [ -  + ]:            324 :     if (!validOperatorName(operatorName))
 8531 tgl@sss.pgh.pa.us         213         [ #  # ]:UBC           0 :         ereport(ERROR,
                                214                 :                :                 (errcode(ERRCODE_INVALID_NAME),
                                215                 :                :                  errmsg("\"%s\" is not a valid operator name",
                                216                 :                :                         operatorName)));
                                217                 :                : 
                                218                 :                :     /*
                                219                 :                :      * open pg_operator
                                220                 :                :      */
 2868 andres@anarazel.de        221                 :CBC         324 :     pg_operator_desc = table_open(OperatorRelationId, RowExclusiveLock);
 2930                           222                 :            324 :     tupDesc = pg_operator_desc->rd_att;
                                223                 :                : 
                                224                 :                :     /*
                                225                 :                :      * initialize our *nulls and *values arrays
                                226                 :                :      */
10674 bruce@momjian.us          227         [ +  + ]:           5184 :     for (i = 0; i < Natts_pg_operator; ++i)
                                228                 :                :     {
 6600 tgl@sss.pgh.pa.us         229                 :           4860 :         nulls[i] = false;
  477                           230                 :           4860 :         values[i] = (Datum) 0;  /* redundant, but safe */
                                231                 :                :     }
                                232                 :                : 
                                233                 :                :     /*
                                234                 :                :      * initialize values[] with the operator name and input data types. Note
                                235                 :                :      * that oprcode is set to InvalidOid, indicating it's a shell.
                                236                 :                :      */
 2930 andres@anarazel.de        237                 :            324 :     operatorObjectId = GetNewOidWithIndex(pg_operator_desc, OperatorOidIndexId,
                                238                 :                :                                           Anum_pg_operator_oid);
                                239                 :            324 :     values[Anum_pg_operator_oid - 1] = ObjectIdGetDatum(operatorObjectId);
10468 scrappy@hub.org           240                 :            324 :     namestrcpy(&oname, operatorName);
 5644 tgl@sss.pgh.pa.us         241                 :            324 :     values[Anum_pg_operator_oprname - 1] = NameGetDatum(&oname);
                                242                 :            324 :     values[Anum_pg_operator_oprnamespace - 1] = ObjectIdGetDatum(operatorNamespace);
                                243                 :            324 :     values[Anum_pg_operator_oprowner - 1] = ObjectIdGetDatum(GetUserId());
 2263                           244         [ +  - ]:            324 :     values[Anum_pg_operator_oprkind - 1] = CharGetDatum(leftTypeId ? 'b' : 'l');
 5644                           245                 :            324 :     values[Anum_pg_operator_oprcanmerge - 1] = BoolGetDatum(false);
                                246                 :            324 :     values[Anum_pg_operator_oprcanhash - 1] = BoolGetDatum(false);
                                247                 :            324 :     values[Anum_pg_operator_oprleft - 1] = ObjectIdGetDatum(leftTypeId);
                                248                 :            324 :     values[Anum_pg_operator_oprright - 1] = ObjectIdGetDatum(rightTypeId);
                                249                 :            324 :     values[Anum_pg_operator_oprresult - 1] = ObjectIdGetDatum(InvalidOid);
                                250                 :            324 :     values[Anum_pg_operator_oprcom - 1] = ObjectIdGetDatum(InvalidOid);
                                251                 :            324 :     values[Anum_pg_operator_oprnegate - 1] = ObjectIdGetDatum(InvalidOid);
                                252                 :            324 :     values[Anum_pg_operator_oprcode - 1] = ObjectIdGetDatum(InvalidOid);
                                253                 :            324 :     values[Anum_pg_operator_oprrest - 1] = ObjectIdGetDatum(InvalidOid);
                                254                 :            324 :     values[Anum_pg_operator_oprjoin - 1] = ObjectIdGetDatum(InvalidOid);
                                255                 :                : 
                                256                 :                :     /*
                                257                 :                :      * create a new operator tuple
                                258                 :                :      */
 6600                           259                 :            324 :     tup = heap_form_tuple(tupDesc, values, nulls);
                                260                 :                : 
                                261                 :                :     /*
                                262                 :                :      * insert our "shell" operator tuple
                                263                 :                :      */
 2930 andres@anarazel.de        264                 :            324 :     CatalogTupleInsert(pg_operator_desc, tup);
                                265                 :                : 
                                266                 :                :     /* Add dependencies for the entry */
 1929 tgl@sss.pgh.pa.us         267                 :            324 :     makeOperatorDependencies(tup, true, false);
                                268                 :                : 
 9844 JanWieck@Yahoo.com        269                 :            324 :     heap_freetuple(tup);
                                270                 :                : 
                                271                 :                :     /* Post creation hook for new shell operator */
 5015 rhaas@postgresql.org      272         [ -  + ]:            324 :     InvokeObjectPostCreateHook(OperatorRelationId, operatorObjectId, 0);
                                273                 :                : 
                                274                 :                :     /*
                                275                 :                :      * Make sure the tuple is visible for subsequent lookups/updates.
                                276                 :                :      */
 6678 tgl@sss.pgh.pa.us         277                 :            324 :     CommandCounterIncrement();
                                278                 :                : 
                                279                 :                :     /*
                                280                 :                :      * close the operator relation and return the oid.
                                281                 :                :      */
 2868 andres@anarazel.de        282                 :            324 :     table_close(pg_operator_desc, RowExclusiveLock);
                                283                 :                : 
10160 bruce@momjian.us          284                 :            324 :     return operatorObjectId;
                                285                 :                : }
                                286                 :                : 
                                287                 :                : /*
                                288                 :                :  * OperatorCreate
                                289                 :                :  *
                                290                 :                :  * "X" indicates an optional argument (i.e. one that can be NULL or 0)
                                291                 :                :  *      operatorName            name for new operator
                                292                 :                :  *      operatorNamespace       namespace for new operator
                                293                 :                :  *      leftTypeId              X left type ID
                                294                 :                :  *      rightTypeId             right type ID
                                295                 :                :  *      procedureId             procedure ID for operator
                                296                 :                :  *      commutatorName          X commutator operator
                                297                 :                :  *      negatorName             X negator operator
                                298                 :                :  *      restrictionId           X restriction selectivity procedure ID
                                299                 :                :  *      joinId                  X join selectivity procedure ID
                                300                 :                :  *      canMerge                merge join can be used with this operator
                                301                 :                :  *      canHash                 hash join can be used with this operator
                                302                 :                :  *
                                303                 :                :  * The caller should have validated properties and permissions for the
                                304                 :                :  * objects passed as OID references.  We must handle the commutator and
                                305                 :                :  * negator operator references specially, however, since those need not
                                306                 :                :  * exist beforehand.
                                307                 :                :  *
                                308                 :                :  * This routine gets complicated because it allows the user to
                                309                 :                :  * specify operators that do not exist.  For example, if operator
                                310                 :                :  * "op" is being defined, the negator operator "negop" and the
                                311                 :                :  * commutator "commop" can also be defined without specifying
                                312                 :                :  * any information other than their names.  Since in order to
                                313                 :                :  * add "op" to the PG_OPERATOR catalog, all the Oid's for these
                                314                 :                :  * operators must be placed in the fields of "op", a forward
                                315                 :                :  * declaration is done on the commutator and negator operators.
                                316                 :                :  * This is called creating a shell, and its main effect is to
                                317                 :                :  * create a tuple in the PG_OPERATOR catalog with minimal
                                318                 :                :  * information about the operator (just its name and types).
                                319                 :                :  * Forward declaration is used only for this purpose, it is
                                320                 :                :  * not available to the user as it is for type definition.
                                321                 :                :  */
                                322                 :                : ObjectAddress
 8992 tgl@sss.pgh.pa.us         323                 :            991 : OperatorCreate(const char *operatorName,
                                324                 :                :                Oid operatorNamespace,
                                325                 :                :                Oid leftTypeId,
                                326                 :                :                Oid rightTypeId,
                                327                 :                :                Oid procedureId,
                                328                 :                :                List *commutatorName,
                                329                 :                :                List *negatorName,
                                330                 :                :                Oid restrictionId,
                                331                 :                :                Oid joinId,
                                332                 :                :                bool canMerge,
                                333                 :                :                bool canHash)
                                334                 :                : {
                                335                 :                :     Relation    pg_operator_desc;
                                336                 :                :     HeapTuple   tup;
                                337                 :                :     bool        isUpdate;
                                338                 :                :     bool        nulls[Natts_pg_operator];
                                339                 :                :     bool        replaces[Natts_pg_operator];
                                340                 :                :     Datum       values[Natts_pg_operator];
                                341                 :                :     Oid         operatorObjectId;
                                342                 :                :     bool        operatorAlreadyDefined;
                                343                 :                :     Oid         operResultType;
                                344                 :                :     Oid         commutatorId,
                                345                 :                :                 negatorId,
                                346                 :                :                 oldCommutatorId,
                                347                 :                :                 oldNegatorId;
10093                           348                 :            991 :     bool        selfCommutator = false;
                                349                 :                :     NameData    oname;
                                350                 :                :     int         i;
                                351                 :                :     ObjectAddress address;
                                352                 :                : 
   19                           353                 :            991 :     pg_operator_desc = table_open(OperatorRelationId, RowExclusiveLock);
                                354                 :                : 
                                355                 :                :     /*
                                356                 :                :      * Sanity checks
                                357                 :                :      */
 9010                           358         [ -  + ]:            991 :     if (!validOperatorName(operatorName))
 8531 tgl@sss.pgh.pa.us         359         [ #  # ]:UBC           0 :         ereport(ERROR,
                                360                 :                :                 (errcode(ERRCODE_INVALID_NAME),
                                361                 :                :                  errmsg("\"%s\" is not a valid operator name",
                                362                 :                :                         operatorName)));
                                363                 :                : 
 6678 tgl@sss.pgh.pa.us         364                 :CBC         991 :     operResultType = get_func_rettype(procedureId);
                                365                 :                : 
10674 bruce@momjian.us          366                 :            991 :     operatorObjectId = OperatorGet(operatorName,
                                367                 :                :                                    operatorNamespace,
                                368                 :                :                                    leftTypeId,
                                369                 :                :                                    rightTypeId,
                                370                 :                :                                    &operatorAlreadyDefined);
                                371                 :                : 
10093 tgl@sss.pgh.pa.us         372         [ -  + ]:            991 :     if (operatorAlreadyDefined)
 8531 tgl@sss.pgh.pa.us         373         [ #  # ]:UBC           0 :         ereport(ERROR,
                                374                 :                :                 (errcode(ERRCODE_DUPLICATE_FUNCTION),
                                375                 :                :                  errmsg("operator %s already exists",
                                376                 :                :                         operatorName)));
                                377                 :                : 
                                378                 :                :     /*
                                379                 :                :      * At this point, if operatorObjectId is not InvalidOid then we are
                                380                 :                :      * filling in a previously-created shell.  Insist that the user own any
                                381                 :                :      * such shell.
                                382                 :                :      *
                                383                 :                :      * We used to test object_ownercheck() here, allowing a superuser to
                                384                 :                :      * overwrite shells created by ordinary users, and also allowing those
                                385                 :                :      * users to mount trojan-horse attacks against the superuser by installing
                                386                 :                :      * unexpected commutator/negator links.  Now we insist that both sides of
                                387                 :                :      * a commutator/negator pair be created by exactly the same role.  They
                                388                 :                :      * can be reassigned to other owners later, however, so the tests below
                                389                 :                :      * are still object_ownercheck().  This is important to avoid breaking
                                390                 :                :      * pg_dump.
                                391                 :                :      */
 6678 tgl@sss.pgh.pa.us         392   [ +  +  -  + ]:CBC        1300 :     if (OidIsValid(operatorObjectId) &&
   19                           393                 :            309 :         get_opowner(operatorObjectId) != GetUserId())
 3283 peter_e@gmx.net           394                 :UBC           0 :         aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_OPERATOR,
                                395                 :                :                        operatorName);
                                396                 :                : 
                                397                 :                :     /*
                                398                 :                :      * If we are replacing an operator shell, it's highly likely that it has
                                399                 :                :      * oprcom and/or oprnegate filled in already (else there would have been
                                400                 :                :      * no reason to make the shell).  Fetch a copy of the shell tuple, which
                                401                 :                :      * we'll need below anyway to do heap_modify_tuple, and grab the old
                                402                 :                :      * commutator/negator values out of it.
                                403                 :                :      */
   19 tgl@sss.pgh.pa.us         404         [ +  + ]:CBC         991 :     if (OidIsValid(operatorObjectId))
                                405                 :                :     {
                                406                 :                :         Form_pg_operator t;
                                407                 :                : 
                                408                 :            309 :         tup = SearchSysCacheCopy1(OPEROID,
                                409                 :                :                                   ObjectIdGetDatum(operatorObjectId));
                                410         [ -  + ]:            309 :         if (!HeapTupleIsValid(tup))
   19 tgl@sss.pgh.pa.us         411         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for operator %u",
                                412                 :                :                  operatorObjectId);
   19 tgl@sss.pgh.pa.us         413                 :CBC         309 :         t = (Form_pg_operator) GETSTRUCT(tup);
                                414                 :            309 :         oldCommutatorId = t->oprcom;
                                415                 :            309 :         oldNegatorId = t->oprnegate;
                                416                 :                :     }
                                417                 :                :     else
                                418                 :                :     {
                                419                 :            682 :         tup = NULL;
                                420                 :            682 :         oldCommutatorId = oldNegatorId = InvalidOid;
                                421                 :                :     }
                                422                 :                : 
                                423                 :                :     /*
                                424                 :                :      * Set up the other operators.  If they do not currently exist, create
                                425                 :                :      * shells in order to get ObjectId's.
                                426                 :                :      */
                                427                 :                : 
 8992                           428         [ +  + ]:            991 :     if (commutatorName)
                                429                 :                :     {
                                430                 :                :         /* commutator has reversed arg types */
                                431                 :            613 :         commutatorId = get_other_operator(commutatorName,
                                432                 :                :                                           rightTypeId, leftTypeId,
                                433                 :                :                                           operatorName, operatorNamespace,
                                434                 :                :                                           leftTypeId, rightTypeId);
                                435                 :                : 
                                436                 :                :         /* Permission check: must own other operator */
 6678                           437         [ +  + ]:            613 :         if (OidIsValid(commutatorId) &&
 1476 peter@eisentraut.org      438         [ -  + ]:            478 :             !object_ownercheck(OperatorRelationId, commutatorId, GetUserId()))
 3283 peter_e@gmx.net           439                 :UBC           0 :             aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_OPERATOR,
 6678 tgl@sss.pgh.pa.us         440                 :              0 :                            NameListToString(commutatorName));
                                441                 :                : 
                                442                 :                :         /*
                                443                 :                :          * If self-linkage to the new operator is requested, we'll fix it
                                444                 :                :          * below.  (In case of self-linkage to an existing shell operator, we
                                445                 :                :          * need do nothing special.)
                                446                 :                :          */
 8992 tgl@sss.pgh.pa.us         447         [ +  + ]:CBC         613 :         if (!OidIsValid(commutatorId))
                                448                 :            135 :             selfCommutator = true;
                                449                 :                :     }
                                450                 :                :     else
                                451                 :            378 :         commutatorId = InvalidOid;
                                452                 :                : 
                                453         [ +  + ]:            991 :     if (negatorName)
                                454                 :                :     {
                                455                 :                :         /* negator has same arg types */
                                456                 :            378 :         negatorId = get_other_operator(negatorName,
                                457                 :                :                                        leftTypeId, rightTypeId,
                                458                 :                :                                        operatorName, operatorNamespace,
                                459                 :                :                                        leftTypeId, rightTypeId);
                                460                 :                : 
                                461                 :                :         /* Permission check: must own other operator */
 6678                           462         [ +  + ]:            378 :         if (OidIsValid(negatorId) &&
 1476 peter@eisentraut.org      463         [ -  + ]:            374 :             !object_ownercheck(OperatorRelationId, negatorId, GetUserId()))
 3283 peter_e@gmx.net           464                 :UBC           0 :             aclcheck_error(ACLCHECK_NOT_OWNER, OBJECT_OPERATOR,
 6678 tgl@sss.pgh.pa.us         465                 :              0 :                            NameListToString(negatorName));
                                466                 :                : 
                                467                 :                :         /*
                                468                 :                :          * Prevent self negation, as it doesn't make sense.  It's self
                                469                 :                :          * negation if result is InvalidOid (negator would be the same
                                470                 :                :          * operator but it doesn't exist yet) or operatorObjectId (we are
                                471                 :                :          * replacing a shell that would need to be its own negator).
                                472                 :                :          */
 1135 tgl@sss.pgh.pa.us         473   [ +  +  +  + ]:CBC         378 :         if (!OidIsValid(negatorId) || negatorId == operatorObjectId)
                                474         [ +  - ]:              8 :             ereport(ERROR,
                                475                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                476                 :                :                      errmsg("operator cannot be its own negator")));
                                477                 :                :     }
                                478                 :                :     else
 8992                           479                 :            613 :         negatorId = InvalidOid;
                                480                 :                : 
                                481                 :                :     /*
                                482                 :                :      * If we had a pre-existing oprcom setting, we must not change it, else
                                483                 :                :      * we'd break the reciprocal linkage to whatever oprcom points at.
                                484                 :                :      */
   19                           485         [ +  + ]:            983 :     if (OidIsValid(oldCommutatorId))
                                486                 :                :     {
                                487         [ +  + ]:            214 :         if (!OidIsValid(commutatorId))
                                488                 :              5 :             commutatorId = oldCommutatorId;
                                489         [ +  + ]:            209 :         else if (oldCommutatorId != commutatorId)
                                490                 :                :         {
                                491                 :                :             /* Be paranoid about the possibility that it's a dangling link */
                                492                 :              4 :             char       *otherop = get_opname(oldCommutatorId);
                                493                 :                : 
                                494         [ +  - ]:              4 :             if (otherop != NULL)
                                495         [ +  - ]:              4 :                 ereport(ERROR,
                                496                 :                :                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                497                 :                :                          errmsg("commutator operator %s is already the commutator of operator %s",
                                498                 :                :                                 otherop, operatorName)));
                                499                 :                :             else
   19 tgl@sss.pgh.pa.us         500         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                501                 :                :                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                502                 :                :                          errmsg("commutator operator %u is already the commutator of operator %s",
                                503                 :                :                                 oldCommutatorId, operatorName)));
                                504                 :                :         }
                                505                 :                :     }
                                506                 :                : 
                                507                 :                :     /*
                                508                 :                :      * If we had a pre-existing oprnegate setting, we must not change it, else
                                509                 :                :      * we'd break the reciprocal linkage to whatever oprnegate points at.
                                510                 :                :      */
   19 tgl@sss.pgh.pa.us         511         [ +  + ]:CBC         979 :     if (OidIsValid(oldNegatorId))
                                512                 :                :     {
                                513         [ +  + ]:            176 :         if (!OidIsValid(negatorId))
                                514                 :              8 :             negatorId = oldNegatorId;
                                515         [ +  + ]:            168 :         else if (oldNegatorId != negatorId)
                                516                 :                :         {
                                517                 :                :             /* Be paranoid about the possibility that it's a dangling link */
                                518                 :              4 :             char       *otherop = get_opname(oldNegatorId);
                                519                 :                : 
                                520         [ +  - ]:              4 :             if (otherop != NULL)
                                521         [ +  - ]:              4 :                 ereport(ERROR,
                                522                 :                :                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                523                 :                :                          errmsg("negator operator %s is already the negator of operator %s",
                                524                 :                :                                 otherop, operatorName)));
                                525                 :                :             else
   19 tgl@sss.pgh.pa.us         526         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                527                 :                :                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                528                 :                :                          errmsg("negator operator %u is already the negator of operator %s",
                                529                 :                :                                 oldNegatorId, operatorName)));
                                530                 :                :         }
                                531                 :                :     }
                                532                 :                : 
                                533                 :                :     /*
                                534                 :                :      * Now that we know commutatorId and negatorId for sure, we can finish our
                                535                 :                :      * sanity checks.
                                536                 :                :      */
   19 tgl@sss.pgh.pa.us         537                 :CBC         975 :     OperatorValidateParams(leftTypeId,
                                538                 :                :                            rightTypeId,
                                539                 :                :                            operResultType,
                                540                 :                :                            OidIsValid(commutatorId),
                                541                 :                :                            OidIsValid(negatorId),
                                542                 :                :                            OidIsValid(restrictionId),
                                543                 :                :                            OidIsValid(joinId),
                                544                 :                :                            canMerge,
                                545                 :                :                            canHash);
                                546                 :                : 
                                547                 :                :     /*
                                548                 :                :      * set up values in the operator tuple
                                549                 :                :      */
                                550                 :                : 
 6678                           551         [ +  + ]:          15536 :     for (i = 0; i < Natts_pg_operator; ++i)
                                552                 :                :     {
  477                           553                 :          14565 :         values[i] = (Datum) 0;
 6600                           554                 :          14565 :         replaces[i] = true;
                                555                 :          14565 :         nulls[i] = false;
                                556                 :                :     }
                                557                 :                : 
 6678                           558                 :            971 :     namestrcpy(&oname, operatorName);
 5644                           559                 :            971 :     values[Anum_pg_operator_oprname - 1] = NameGetDatum(&oname);
                                560                 :            971 :     values[Anum_pg_operator_oprnamespace - 1] = ObjectIdGetDatum(operatorNamespace);
                                561                 :            971 :     values[Anum_pg_operator_oprowner - 1] = ObjectIdGetDatum(GetUserId());
 2263                           562         [ +  + ]:            971 :     values[Anum_pg_operator_oprkind - 1] = CharGetDatum(leftTypeId ? 'b' : 'l');
 5644                           563                 :            971 :     values[Anum_pg_operator_oprcanmerge - 1] = BoolGetDatum(canMerge);
                                564                 :            971 :     values[Anum_pg_operator_oprcanhash - 1] = BoolGetDatum(canHash);
                                565                 :            971 :     values[Anum_pg_operator_oprleft - 1] = ObjectIdGetDatum(leftTypeId);
                                566                 :            971 :     values[Anum_pg_operator_oprright - 1] = ObjectIdGetDatum(rightTypeId);
                                567                 :            971 :     values[Anum_pg_operator_oprresult - 1] = ObjectIdGetDatum(operResultType);
                                568                 :            971 :     values[Anum_pg_operator_oprcom - 1] = ObjectIdGetDatum(commutatorId);
                                569                 :            971 :     values[Anum_pg_operator_oprnegate - 1] = ObjectIdGetDatum(negatorId);
                                570                 :            971 :     values[Anum_pg_operator_oprcode - 1] = ObjectIdGetDatum(procedureId);
                                571                 :            971 :     values[Anum_pg_operator_oprrest - 1] = ObjectIdGetDatum(restrictionId);
                                572                 :            971 :     values[Anum_pg_operator_oprjoin - 1] = ObjectIdGetDatum(joinId);
                                573                 :                : 
                                574                 :                :     /*
                                575                 :                :      * If we are replacing an operator shell, update; else insert
                                576                 :                :      */
10674 bruce@momjian.us          577         [ +  + ]:            971 :     if (operatorObjectId)
                                578                 :                :     {
 3985 tgl@sss.pgh.pa.us         579                 :            293 :         isUpdate = true;
                                580                 :                : 
                                581                 :                :         /* tup was fetched above */
 2930 andres@anarazel.de        582                 :            293 :         replaces[Anum_pg_operator_oid - 1] = false;
 6600 tgl@sss.pgh.pa.us         583                 :            293 :         tup = heap_modify_tuple(tup,
                                584                 :                :                                 RelationGetDescr(pg_operator_desc),
                                585                 :                :                                 values,
                                586                 :                :                                 nulls,
                                587                 :                :                                 replaces);
                                588                 :                : 
 3588 alvherre@alvh.no-ip.      589                 :            293 :         CatalogTupleUpdate(pg_operator_desc, &tup->t_self, tup);
                                590                 :                :     }
                                591                 :                :     else
                                592                 :                :     {
 3985 tgl@sss.pgh.pa.us         593                 :            678 :         isUpdate = false;
                                594                 :                : 
 2930 andres@anarazel.de        595                 :            678 :         operatorObjectId = GetNewOidWithIndex(pg_operator_desc,
                                596                 :                :                                               OperatorOidIndexId,
                                597                 :                :                                               Anum_pg_operator_oid);
                                598                 :            678 :         values[Anum_pg_operator_oid - 1] = ObjectIdGetDatum(operatorObjectId);
                                599                 :                : 
 3985 tgl@sss.pgh.pa.us         600                 :            678 :         tup = heap_form_tuple(RelationGetDescr(pg_operator_desc),
                                601                 :                :                               values, nulls);
                                602                 :                : 
 2930 andres@anarazel.de        603                 :            678 :         CatalogTupleInsert(pg_operator_desc, tup);
                                604                 :                :     }
                                605                 :                : 
                                606                 :                :     /* Add dependencies for the entry */
 1929 tgl@sss.pgh.pa.us         607                 :            971 :     address = makeOperatorDependencies(tup, true, isUpdate);
                                608                 :                : 
                                609                 :                :     /*
                                610                 :                :      * If a commutator and/or negator link is provided, update the other
                                611                 :                :      * operator(s) to point at this one, if they don't already have a link.
                                612                 :                :      * This supports an alternative style of operator definition wherein the
                                613                 :                :      * user first defines one operator without giving negator or commutator,
                                614                 :                :      * then defines the other operator of the pair with the proper commutator
                                615                 :                :      * or negator attribute.  That style doesn't require creation of a shell,
                                616                 :                :      * and it's the only style that worked right before Postgres version 6.5.
                                617                 :                :      * This code also takes care of the situation where the new operator is
                                618                 :                :      * its own commutator.
                                619                 :                :      */
10093                           620         [ +  + ]:            970 :     if (selfCommutator)
                                621                 :            135 :         commutatorId = operatorObjectId;
                                622                 :                : 
                                623   [ +  +  +  + ]:            970 :     if (OidIsValid(commutatorId) || OidIsValid(negatorId))
   19                           624                 :            661 :         UpdateOperatorLinks(operatorObjectId, commutatorId, negatorId,
                                625                 :                :                             operResultType, false);
                                626                 :                : 
                                627                 :                :     /* Post creation hook for new operator */
 1135                           628         [ -  + ]:            950 :     InvokeObjectPostCreateHook(OperatorRelationId, operatorObjectId, 0);
                                629                 :                : 
                                630                 :            950 :     table_close(pg_operator_desc, RowExclusiveLock);
                                631                 :                : 
 4288 alvherre@alvh.no-ip.      632                 :            950 :     return address;
                                633                 :                : }
                                634                 :                : 
                                635                 :                : /*
                                636                 :                :  * OperatorValidateParams
                                637                 :                :  *
                                638                 :                :  * Check that an operator with argument types leftTypeId and rightTypeId,
                                639                 :                :  * returning operResultType, can have the attributes that are set to true.
                                640                 :                :  * Raise an error for any disallowed attribute.
                                641                 :                :  *
                                642                 :                :  * Note: in ALTER OPERATOR, we only bother to pass "true" for attributes
                                643                 :                :  * the command is trying to set, not those that may already be set.
                                644                 :                :  * This is OK as long as the attribute checks are independent.
                                645                 :                :  */
                                646                 :                : void
 1135 tgl@sss.pgh.pa.us         647                 :           1275 : OperatorValidateParams(Oid leftTypeId,
                                648                 :                :                        Oid rightTypeId,
                                649                 :                :                        Oid operResultType,
                                650                 :                :                        bool hasCommutator,
                                651                 :                :                        bool hasNegator,
                                652                 :                :                        bool hasRestrictionSelectivity,
                                653                 :                :                        bool hasJoinSelectivity,
                                654                 :                :                        bool canMerge,
                                655                 :                :                        bool canHash)
                                656                 :                : {
                                657   [ +  +  -  + ]:           1275 :     if (!(OidIsValid(leftTypeId) && OidIsValid(rightTypeId)))
                                658                 :                :     {
                                659                 :                :         /* If it's not a binary op, these things mustn't be set: */
                                660         [ -  + ]:             39 :         if (hasCommutator)
 1135 tgl@sss.pgh.pa.us         661         [ #  # ]:UBC           0 :             ereport(ERROR,
                                662                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                663                 :                :                      errmsg("only binary operators can have commutators")));
 1135 tgl@sss.pgh.pa.us         664         [ -  + ]:CBC          39 :         if (hasJoinSelectivity)
 1135 tgl@sss.pgh.pa.us         665         [ #  # ]:UBC           0 :             ereport(ERROR,
                                666                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                667                 :                :                      errmsg("only binary operators can have join selectivity")));
 1135 tgl@sss.pgh.pa.us         668         [ -  + ]:CBC          39 :         if (canMerge)
 1135 tgl@sss.pgh.pa.us         669         [ #  # ]:UBC           0 :             ereport(ERROR,
                                670                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                671                 :                :                      errmsg("only binary operators can merge join")));
 1135 tgl@sss.pgh.pa.us         672         [ -  + ]:CBC          39 :         if (canHash)
 1135 tgl@sss.pgh.pa.us         673         [ #  # ]:UBC           0 :             ereport(ERROR,
                                674                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                675                 :                :                      errmsg("only binary operators can hash")));
                                676                 :                :     }
                                677                 :                : 
 1135 tgl@sss.pgh.pa.us         678         [ +  + ]:CBC        1275 :     if (operResultType != BOOLOID)
                                679                 :                :     {
                                680                 :                :         /* If it's not a boolean op, these things mustn't be set: */
                                681         [ +  + ]:            210 :         if (hasNegator)
                                682         [ +  - ]:              4 :             ereport(ERROR,
                                683                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                684                 :                :                      errmsg("only boolean operators can have negators")));
                                685         [ -  + ]:            206 :         if (hasRestrictionSelectivity)
 1135 tgl@sss.pgh.pa.us         686         [ #  # ]:UBC           0 :             ereport(ERROR,
                                687                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                688                 :                :                      errmsg("only boolean operators can have restriction selectivity")));
 1135 tgl@sss.pgh.pa.us         689         [ -  + ]:CBC         206 :         if (hasJoinSelectivity)
 1135 tgl@sss.pgh.pa.us         690         [ #  # ]:UBC           0 :             ereport(ERROR,
                                691                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                692                 :                :                      errmsg("only boolean operators can have join selectivity")));
 1135 tgl@sss.pgh.pa.us         693         [ -  + ]:CBC         206 :         if (canMerge)
 1135 tgl@sss.pgh.pa.us         694         [ #  # ]:UBC           0 :             ereport(ERROR,
                                695                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                696                 :                :                      errmsg("only boolean operators can merge join")));
 1135 tgl@sss.pgh.pa.us         697         [ -  + ]:CBC         206 :         if (canHash)
 1135 tgl@sss.pgh.pa.us         698         [ #  # ]:UBC           0 :             ereport(ERROR,
                                699                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                700                 :                :                      errmsg("only boolean operators can hash")));
                                701                 :                :     }
 1135 tgl@sss.pgh.pa.us         702                 :CBC        1271 : }
                                703                 :                : 
                                704                 :                : /*
                                705                 :                :  * Try to lookup another operator (commutator, etc); return its OID
                                706                 :                :  *
                                707                 :                :  * If not found, check to see if it would be the same operator we are trying
                                708                 :                :  * to define; if so, return InvalidOid.  (Caller must decide whether
                                709                 :                :  * that is sensible.)  If it is not the same operator, create a shell
                                710                 :                :  * operator.
                                711                 :                :  */
                                712                 :                : static Oid
 8992                           713                 :            991 : get_other_operator(List *otherOp, Oid otherLeftTypeId, Oid otherRightTypeId,
                                714                 :                :                    const char *operatorName, Oid operatorNamespace,
                                715                 :                :                    Oid leftTypeId, Oid rightTypeId)
                                716                 :                : {
                                717                 :                :     Oid         other_oid;
                                718                 :                :     bool        otherDefined;
                                719                 :                :     char       *otherName;
                                720                 :                :     Oid         otherNamespace;
                                721                 :                :     AclResult   aclresult;
                                722                 :                : 
                                723                 :            991 :     other_oid = OperatorLookup(otherOp,
                                724                 :                :                                otherLeftTypeId,
                                725                 :                :                                otherRightTypeId,
                                726                 :                :                                &otherDefined);
                                727                 :                : 
                                728         [ +  + ]:            991 :     if (OidIsValid(other_oid))
                                729                 :                :     {
                                730                 :                :         /* other op already in catalogs */
                                731                 :            528 :         return other_oid;
                                732                 :                :     }
                                733                 :                : 
                                734                 :            463 :     otherNamespace = QualifiedNameGetCreationNamespace(otherOp,
                                735                 :                :                                                        &otherName);
                                736                 :                : 
                                737   [ +  +  +  - ]:            463 :     if (strcmp(otherName, operatorName) == 0 &&
                                738         [ +  + ]:            195 :         otherNamespace == operatorNamespace &&
                                739         [ +  - ]:            139 :         otherLeftTypeId == leftTypeId &&
                                740                 :                :         otherRightTypeId == rightTypeId)
                                741                 :                :     {
                                742                 :                :         /* self-linkage to new operator; caller must handle this */
                                743                 :            139 :         return InvalidOid;
                                744                 :                :     }
                                745                 :                : 
                                746                 :                :     /* not in catalogs, different from operator, so make shell */
                                747                 :                : 
 1476 peter@eisentraut.org      748                 :            324 :     aclresult = object_aclcheck(NamespaceRelationId, otherNamespace, GetUserId(),
                                749                 :                :                                 ACL_CREATE);
 8981 tgl@sss.pgh.pa.us         750         [ -  + ]:            324 :     if (aclresult != ACLCHECK_OK)
 3283 peter_e@gmx.net           751                 :UBC           0 :         aclcheck_error(aclresult, OBJECT_SCHEMA,
 8520 tgl@sss.pgh.pa.us         752                 :              0 :                        get_namespace_name(otherNamespace));
                                753                 :                : 
 8992 tgl@sss.pgh.pa.us         754                 :CBC         324 :     other_oid = OperatorShellMake(otherName,
                                755                 :                :                                   otherNamespace,
                                756                 :                :                                   otherLeftTypeId,
                                757                 :                :                                   otherRightTypeId);
                                758                 :            324 :     return other_oid;
                                759                 :                : }
                                760                 :                : 
                                761                 :                : /*
                                762                 :                :  * UpdateOperatorLinks
                                763                 :                :  *  Update the commutator and negator back-links for an operator being
                                764                 :                :  *  created/altered/deleted.
                                765                 :                :  *
                                766                 :                :  *  baseId: OID of the operator that is the direct target of the change
                                767                 :                :  *  commId: its oprcom value (possibly InvalidOid)
                                768                 :                :  *  negId: its oprnegate value (possibly InvalidOid)
                                769                 :                :  *  oprResultId: its oprresult value (InvalidOid if a shell)
                                770                 :                :  *  isDelete: true if we are deleting the baseId operator
                                771                 :                :  *
                                772                 :                :  *  When isDelete is false, we update the referenced operators to link
                                773                 :                :  *  back to the given operator, after checking that they return the
                                774                 :                :  *  same data type as the given operator.
                                775                 :                :  *  When isDelete is true, update those operators' links to be InvalidOid.
                                776                 :                :  *
                                777                 :                :  *  The !isDelete case solves a problem for users who need to insert two new
                                778                 :                :  *  operators that are the negator or commutator of each other, while the
                                779                 :                :  *  isDelete case is needed so as not to leave dangling OID links behind
                                780                 :                :  *  after dropping an operator.
                                781                 :                :  */
                                782                 :                : void
   19                           783                 :            913 : UpdateOperatorLinks(Oid baseId, Oid commId, Oid negId,
                                784                 :                :                     Oid oprResultId, bool isDelete)
                                785                 :                : {
                                786                 :                :     Relation    pg_operator_desc;
                                787                 :                :     HeapTuple   tup;
                                788                 :                : 
                                789                 :                :     /*
                                790                 :                :      * If we're making an operator into its own commutator, then we need a
                                791                 :                :      * command-counter increment here, since we've just inserted the tuple
                                792                 :                :      * we're about to update.  But when we're dropping an operator, we can
                                793                 :                :      * skip this because we're at the beginning of the command.
                                794                 :                :      */
 3900                           795         [ +  + ]:            913 :     if (!isDelete)
                                796                 :            689 :         CommandCounterIncrement();
                                797                 :                : 
                                798                 :                :     /* Open the relation. */
 2868 andres@anarazel.de        799                 :            913 :     pg_operator_desc = table_open(OperatorRelationId, RowExclusiveLock);
                                800                 :                : 
                                801                 :                :     /* Get a writable copy of the commutator's tuple. */
 3900 tgl@sss.pgh.pa.us         802         [ +  + ]:            913 :     if (OidIsValid(commId))
                                803                 :            853 :         tup = SearchSysCacheCopy1(OPEROID, ObjectIdGetDatum(commId));
                                804                 :                :     else
                                805                 :             60 :         tup = NULL;
                                806                 :                : 
                                807                 :                :     /* Check and update the commutator's tuple if need be. */
                                808         [ +  + ]:            913 :     if (HeapTupleIsValid(tup))
                                809                 :                :     {
                                810                 :            853 :         Form_pg_operator t = (Form_pg_operator) GETSTRUCT(tup);
                                811                 :            853 :         bool        update_commutator = false;
                                812                 :                : 
                                813                 :                :         /*
                                814                 :                :          * We can skip doing an update if the commutator's oprcom field is
                                815                 :                :          * already what we want.  While that's not expected in the isDelete
                                816                 :                :          * case, it's perfectly possible when filling in a shell operator.
                                817                 :                :          */
 1135                           818   [ +  +  +  - ]:            853 :         if (isDelete && OidIsValid(t->oprcom))
                                819                 :                :         {
 3900                           820                 :            224 :             t->oprcom = InvalidOid;
                                821                 :            224 :             update_commutator = true;
                                822                 :                :         }
 1135                           823   [ +  -  +  + ]:            629 :         else if (!isDelete && t->oprcom != baseId)
                                824                 :                :         {
                                825                 :                :             /*
                                826                 :                :              * If commutator's oprcom field is already set to point to some
                                827                 :                :              * third operator, it's an error.  Changing its link would be
                                828                 :                :              * unsafe, and letting the inconsistency stand would not be good
                                829                 :                :              * either.  This might be indicative of catalog corruption, so
                                830                 :                :              * don't assume t->oprcom is necessarily a valid operator.
                                831                 :                :              */
                                832         [ +  + ]:            382 :             if (OidIsValid(t->oprcom))
                                833                 :                :             {
                                834                 :              8 :                 char       *thirdop = get_opname(t->oprcom);
                                835                 :                : 
                                836         [ +  - ]:              8 :                 if (thirdop != NULL)
                                837         [ +  - ]:              8 :                     ereport(ERROR,
                                838                 :                :                             (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                839                 :                :                              errmsg("commutator operator %s is already the commutator of operator %s",
                                840                 :                :                                     NameStr(t->oprname), thirdop)));
                                841                 :                :                 else
 1135 tgl@sss.pgh.pa.us         842         [ #  # ]:UBC           0 :                     ereport(ERROR,
                                843                 :                :                             (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                844                 :                :                              errmsg("commutator operator %s is already the commutator of operator %u",
                                845                 :                :                                     NameStr(t->oprname), t->oprcom)));
                                846                 :                :             }
                                847                 :                : 
 3900 tgl@sss.pgh.pa.us         848                 :CBC         374 :             t->oprcom = baseId;
                                849                 :            374 :             update_commutator = true;
                                850                 :                :         }
                                851                 :                : 
                                852                 :                :         /*
                                853                 :                :          * If we are installing an oprcom link, or one is already present,
                                854                 :                :          * verify that the operators return the same data type; unless one or
                                855                 :                :          * both is a shell, in which case we can't check yet.  (We'll check in
                                856                 :                :          * some future pass through this code when both become defined.  Since
                                857                 :                :          * this might indeed be that call, we must check even if not changing
                                858                 :                :          * the tuple.)
                                859                 :                :          */
   19                           860   [ +  +  +  - ]:            845 :         if (t->oprcom == baseId &&
                                861         [ +  + ]:            621 :             OidIsValid(oprResultId) && OidIsValid(t->oprresult) &&
                                862         [ +  + ]:            398 :             oprResultId != t->oprresult)
                                863         [ +  - ]:             12 :             ereport(ERROR,
                                864                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                865                 :                :                      errmsg("commutator operator %s does not return the same data type as operator %s",
                                866                 :                :                             NameStr(t->oprname), get_opname(baseId))));
                                867                 :                : 
                                868                 :                :         /* If any columns were found to need modification, update tuple. */
 3900                           869         [ +  + ]:            833 :         if (update_commutator)
                                870                 :                :         {
 3588 alvherre@alvh.no-ip.      871                 :            590 :             CatalogTupleUpdate(pg_operator_desc, &tup->t_self, tup);
                                872                 :                : 
                                873                 :                :             /*
                                874                 :                :              * Do CCI to make the updated tuple visible.  We must do this in
                                875                 :                :              * case the commutator is also the negator.  (Which would be a
                                876                 :                :              * logic error on the operator definer's part, but that's not a
                                877                 :                :              * good reason to fail here.)  We would need a CCI anyway in the
                                878                 :                :              * deletion case for a self-commutator with no negator.
                                879                 :                :              */
 3900 tgl@sss.pgh.pa.us         880                 :            590 :             CommandCounterIncrement();
                                881                 :                :         }
                                882                 :                :     }
                                883                 :                : 
                                884                 :                :     /*
                                885                 :                :      * Similarly find, check, and update the negator, if any.
                                886                 :                :      */
                                887         [ +  + ]:            893 :     if (OidIsValid(negId))
                                888                 :            509 :         tup = SearchSysCacheCopy1(OPEROID, ObjectIdGetDatum(negId));
                                889                 :                :     else
                                890                 :            384 :         tup = NULL;
                                891                 :                : 
                                892         [ +  + ]:            893 :     if (HeapTupleIsValid(tup))
                                893                 :                :     {
                                894                 :            509 :         Form_pg_operator t = (Form_pg_operator) GETSTRUCT(tup);
                                895                 :            509 :         bool        update_negator = false;
                                896                 :                : 
                                897                 :                :         /*
                                898                 :                :          * We can skip doing an update if the negator's oprnegate field is
                                899                 :                :          * already what we want.  While that's not expected in the isDelete
                                900                 :                :          * case, it's perfectly possible when filling in a shell operator.
                                901                 :                :          */
 1135                           902   [ +  +  +  - ]:            509 :         if (isDelete && OidIsValid(t->oprnegate))
                                903                 :                :         {
 3900                           904                 :            127 :             t->oprnegate = InvalidOid;
                                905                 :            127 :             update_negator = true;
                                906                 :                :         }
 1135                           907   [ +  -  +  + ]:            382 :         else if (!isDelete && t->oprnegate != baseId)
                                908                 :                :         {
                                909                 :                :             /*
                                910                 :                :              * If negator's oprnegate field is already set to point to some
                                911                 :                :              * third operator, it's an error.  Changing its link would be
                                912                 :                :              * unsafe, and letting the inconsistency stand would not be good
                                913                 :                :              * either.  This might be indicative of catalog corruption, so
                                914                 :                :              * don't assume t->oprnegate is necessarily a valid operator.
                                915                 :                :              */
                                916         [ +  + ]:            210 :             if (OidIsValid(t->oprnegate))
                                917                 :                :             {
                                918                 :              8 :                 char       *thirdop = get_opname(t->oprnegate);
                                919                 :                : 
                                920         [ +  - ]:              8 :                 if (thirdop != NULL)
                                921         [ +  - ]:              8 :                     ereport(ERROR,
                                922                 :                :                             (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                923                 :                :                              errmsg("negator operator %s is already the negator of operator %s",
                                924                 :                :                                     NameStr(t->oprname), thirdop)));
                                925                 :                :                 else
 1135 tgl@sss.pgh.pa.us         926         [ #  # ]:UBC           0 :                     ereport(ERROR,
                                927                 :                :                             (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                928                 :                :                              errmsg("negator operator %s is already the negator of operator %u",
                                929                 :                :                                     NameStr(t->oprname), t->oprnegate)));
                                930                 :                :             }
                                931                 :                : 
 3900 tgl@sss.pgh.pa.us         932                 :CBC         202 :             t->oprnegate = baseId;
                                933                 :            202 :             update_negator = true;
                                934                 :                :         }
                                935                 :                : 
                                936                 :                :         /*
                                937                 :                :          * If we are installing an oprnegate link, or one is already present,
                                938                 :                :          * verify that the operators return the same data type; unless one or
                                939                 :                :          * both is a shell, in which case we can't check yet.  (We'll check in
                                940                 :                :          * some future pass through this code when both become defined.  Since
                                941                 :                :          * this might indeed be that call, we must check even if not changing
                                942                 :                :          * the tuple.)
                                943                 :                :          */
   19                           944   [ +  +  +  - ]:            501 :         if (t->oprnegate == baseId &&
                                945         [ +  + ]:            374 :             OidIsValid(oprResultId) && OidIsValid(t->oprresult) &&
                                946         [ +  + ]:            188 :             oprResultId != t->oprresult)
                                947         [ +  - ]:              4 :             ereport(ERROR,
                                948                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                                949                 :                :                      errmsg("negator operator %s does not return the same data type as operator %s",
                                950                 :                :                             NameStr(t->oprname), get_opname(baseId))));
                                951                 :                : 
                                952                 :                :         /* If any columns were found to need modification, update tuple. */
 3900                           953         [ +  + ]:            497 :         if (update_negator)
                                954                 :                :         {
 3588 alvherre@alvh.no-ip.      955                 :            325 :             CatalogTupleUpdate(pg_operator_desc, &tup->t_self, tup);
                                956                 :                : 
                                957                 :                :             /*
                                958                 :                :              * In the deletion case, do CCI to make the updated tuple visible.
                                959                 :                :              * We must do this in case the operator is its own negator. (Which
                                960                 :                :              * would be a logic error on the operator definer's part, but
                                961                 :                :              * that's not a good reason to fail here.)
                                962                 :                :              */
 3900 tgl@sss.pgh.pa.us         963         [ +  + ]:            325 :             if (isDelete)
                                964                 :            127 :                 CommandCounterIncrement();
                                965                 :                :         }
                                966                 :                :     }
                                967                 :                : 
                                968                 :                :     /* Close relation and release catalog lock. */
 2868 andres@anarazel.de        969                 :            881 :     table_close(pg_operator_desc, RowExclusiveLock);
11099 scrappy@hub.org           970                 :            881 : }
                                971                 :                : 
                                972                 :                : /*
                                973                 :                :  * Create dependencies for an operator (either a freshly inserted
                                974                 :                :  * complete operator, a new shell operator, a just-updated shell,
                                975                 :                :  * or an operator that's being modified by ALTER OPERATOR).
                                976                 :                :  *
                                977                 :                :  * makeExtensionDep should be true when making a new operator or
                                978                 :                :  * replacing a shell, false for ALTER OPERATOR.  Passing false
                                979                 :                :  * will prevent any change in the operator's extension membership.
                                980                 :                :  *
                                981                 :                :  * NB: the OidIsValid tests in this routine are necessary, in case
                                982                 :                :  * the given operator is a shell.
                                983                 :                :  */
                                984                 :                : ObjectAddress
 1929 tgl@sss.pgh.pa.us         985                 :           1595 : makeOperatorDependencies(HeapTuple tuple,
                                986                 :                :                          bool makeExtensionDep,
                                987                 :                :                          bool isUpdate)
                                988                 :                : {
 8851 bruce@momjian.us          989                 :           1595 :     Form_pg_operator oper = (Form_pg_operator) GETSTRUCT(tuple);
                                990                 :                :     ObjectAddress myself,
                                991                 :                :                 referenced;
                                992                 :                :     ObjectAddresses *addrs;
                                993                 :                : 
 2341 michael@paquier.xyz       994                 :           1595 :     ObjectAddressSet(myself, OperatorRelationId, oper->oid);
                                995                 :                : 
                                996                 :                :     /*
                                997                 :                :      * If we are updating the operator, delete any existing entries, except
                                998                 :                :      * for extension membership which should remain the same.
                                999                 :                :      */
 3985 tgl@sss.pgh.pa.us        1000         [ +  + ]:           1595 :     if (isUpdate)
                               1001                 :                :     {
                               1002                 :            593 :         deleteDependencyRecordsFor(myself.classId, myself.objectId, true);
                               1003                 :            593 :         deleteSharedDependencyRecordsFor(myself.classId, myself.objectId, 0);
                               1004                 :                :     }
                               1005                 :                : 
 2275 michael@paquier.xyz      1006                 :           1595 :     addrs = new_object_addresses();
                               1007                 :                : 
                               1008                 :                :     /* Dependency on namespace */
 8899 tgl@sss.pgh.pa.us        1009         [ +  - ]:           1595 :     if (OidIsValid(oper->oprnamespace))
                               1010                 :                :     {
 2341 michael@paquier.xyz      1011                 :           1595 :         ObjectAddressSet(referenced, NamespaceRelationId, oper->oprnamespace);
 2275                          1012                 :           1595 :         add_exact_object_address(&referenced, addrs);
                               1013                 :                :     }
                               1014                 :                : 
                               1015                 :                :     /* Dependency on left type */
 8901 tgl@sss.pgh.pa.us        1016         [ +  + ]:           1595 :     if (OidIsValid(oper->oprleft))
                               1017                 :                :     {
 2341 michael@paquier.xyz      1018                 :           1556 :         ObjectAddressSet(referenced, TypeRelationId, oper->oprleft);
 2275                          1019                 :           1556 :         add_exact_object_address(&referenced, addrs);
                               1020                 :                :     }
                               1021                 :                : 
                               1022                 :                :     /* Dependency on right type */
 8901 tgl@sss.pgh.pa.us        1023         [ +  - ]:           1595 :     if (OidIsValid(oper->oprright))
                               1024                 :                :     {
 2341 michael@paquier.xyz      1025                 :           1595 :         ObjectAddressSet(referenced, TypeRelationId, oper->oprright);
 2275                          1026                 :           1595 :         add_exact_object_address(&referenced, addrs);
                               1027                 :                :     }
                               1028                 :                : 
                               1029                 :                :     /* Dependency on result type */
 8901 tgl@sss.pgh.pa.us        1030         [ +  + ]:           1595 :     if (OidIsValid(oper->oprresult))
                               1031                 :                :     {
 2341 michael@paquier.xyz      1032                 :           1271 :         ObjectAddressSet(referenced, TypeRelationId, oper->oprresult);
 2275                          1033                 :           1271 :         add_exact_object_address(&referenced, addrs);
                               1034                 :                :     }
                               1035                 :                : 
                               1036                 :                :     /*
                               1037                 :                :      * NOTE: we do not consider the operator to depend on the associated
                               1038                 :                :      * operators oprcom and oprnegate.  We do not want to delete this operator
                               1039                 :                :      * if those go away, but only reset the link fields; which is not a
                               1040                 :                :      * function that the dependency logic can handle.  (It's taken care of
                               1041                 :                :      * manually within RemoveOperatorById, instead.)
                               1042                 :                :      */
                               1043                 :                : 
                               1044                 :                :     /* Dependency on implementation function */
 8901 tgl@sss.pgh.pa.us        1045         [ +  + ]:           1595 :     if (OidIsValid(oper->oprcode))
                               1046                 :                :     {
 2341 michael@paquier.xyz      1047                 :           1271 :         ObjectAddressSet(referenced, ProcedureRelationId, oper->oprcode);
 2275                          1048                 :           1271 :         add_exact_object_address(&referenced, addrs);
                               1049                 :                :     }
                               1050                 :                : 
                               1051                 :                :     /* Dependency on restriction selectivity function */
 8901 tgl@sss.pgh.pa.us        1052         [ +  + ]:           1595 :     if (OidIsValid(oper->oprrest))
                               1053                 :                :     {
 2341 michael@paquier.xyz      1054                 :            820 :         ObjectAddressSet(referenced, ProcedureRelationId, oper->oprrest);
 2275                          1055                 :            820 :         add_exact_object_address(&referenced, addrs);
                               1056                 :                :     }
                               1057                 :                : 
                               1058                 :                :     /* Dependency on join selectivity function */
 8901 tgl@sss.pgh.pa.us        1059         [ +  + ]:           1595 :     if (OidIsValid(oper->oprjoin))
                               1060                 :                :     {
 2341 michael@paquier.xyz      1061                 :            800 :         ObjectAddressSet(referenced, ProcedureRelationId, oper->oprjoin);
 2275                          1062                 :            800 :         add_exact_object_address(&referenced, addrs);
                               1063                 :                :     }
                               1064                 :                : 
                               1065                 :           1595 :     record_object_address_dependencies(&myself, addrs, DEPENDENCY_NORMAL);
                               1066                 :           1595 :     free_object_addresses(addrs);
                               1067                 :                : 
                               1068                 :                :     /* Dependency on owner */
 2930 andres@anarazel.de       1069                 :           1595 :     recordDependencyOnOwner(OperatorRelationId, oper->oid,
                               1070                 :                :                             oper->oprowner);
                               1071                 :                : 
                               1072                 :                :     /* Dependency on extension */
 1929 tgl@sss.pgh.pa.us        1073         [ +  + ]:           1595 :     if (makeExtensionDep)
 1573                          1074                 :           1295 :         recordDependencyOnCurrentExtension(&myself, isUpdate);
                               1075                 :                : 
 4288 alvherre@alvh.no-ip.     1076                 :           1594 :     return myself;
                               1077                 :                : }
                               1078                 :                : 
                               1079                 :                : /*
                               1080                 :                :  * get_opowner
                               1081                 :                :  *
                               1082                 :                :  *      Returns the oid of the operator's owner role, given the operator oid.
                               1083                 :                :  *      Returns InvalidOid if no such operator.
                               1084                 :                :  *
                               1085                 :                :  * If this is ever needed anywhere else, it should move to lsyscache.c.
                               1086                 :                :  */
                               1087                 :                : static Oid
   19 tgl@sss.pgh.pa.us        1088                 :            309 : get_opowner(Oid opno)
                               1089                 :                : {
                               1090                 :                :     HeapTuple   tp;
                               1091                 :                : 
                               1092                 :            309 :     tp = SearchSysCache1(OPEROID, ObjectIdGetDatum(opno));
                               1093         [ +  - ]:            309 :     if (HeapTupleIsValid(tp))
                               1094                 :                :     {
                               1095                 :            309 :         Form_pg_operator optup = (Form_pg_operator) GETSTRUCT(tp);
                               1096                 :                :         Oid         result;
                               1097                 :                : 
                               1098                 :            309 :         result = optup->oprowner;
                               1099                 :            309 :         ReleaseSysCache(tp);
                               1100                 :            309 :         return result;
                               1101                 :                :     }
                               1102                 :                :     else
   19 tgl@sss.pgh.pa.us        1103                 :UBC           0 :         return InvalidOid;
                               1104                 :                : }
        

Generated by: LCOV version 2.0-1