]> granicus.if.org Git - postgresql/blobdiff - src/backend/catalog/pg_operator.c
Rename heap_replace to heap_update.
[postgresql] / src / backend / catalog / pg_operator.c
index 96fb9ae35f6909377e9c2da6a4d2fb93c9e6908d..5ce7655edd47010bd9c30f8e19ff37885d15c7ca 100644 (file)
@@ -1,13 +1,13 @@
 /*-------------------------------------------------------------------------
  *
- * pg_operator.c--
+ * pg_operator.c
  *       routines to support manipulation of the pg_operator relation
  *
  * Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/catalog/pg_operator.c,v 1.30 1998/11/27 19:51:50 vadim Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/catalog/pg_operator.c,v 1.44 1999/11/24 00:44:29 momjian Exp $
  *
  * NOTES
  *       these routines moved here from commands/define.c and somewhat cleaned up.
 
 #include "access/heapam.h"
 #include "catalog/catname.h"
+#include "catalog/indexing.h"
 #include "catalog/pg_operator.h"
 #include "catalog/pg_proc.h"
 #include "catalog/pg_type.h"
-#include "fmgr.h"
 #include "miscadmin.h"
-#include "parser/parse_oper.h"
-#include "storage/bufmgr.h"
+#include "parser/parse_func.h"
 #include "utils/builtins.h"
 #include "utils/syscache.h"
-#include "utils/tqual.h"
 
-#ifndef HAVE_MEMMOVE
-#include <regex/utils.h>
-#else
-#include <string.h>
-#endif
 
 static Oid OperatorGetWithOpenRelation(Relation pg_operator_desc,
                                                        const char *operatorName,
                                                        Oid leftObjectId,
-                                                       Oid rightObjectId);
+                                                       Oid rightObjectId,
+                                                       bool *defined);
+
 static Oid OperatorGet(char *operatorName,
                        char *leftTypeName,
-                       char *rightTypeName);
+                       char *rightTypeName,
+                       bool *defined);
 
 static Oid OperatorShellMakeWithOpenRelation(Relation pg_operator_desc,
                                                                  char *operatorName,
                                                                  Oid leftObjectId,
                                                                  Oid rightObjectId);
+
 static Oid OperatorShellMake(char *operatorName,
                                  char *leftTypeName,
                                  char *rightTypeName);
 
 static void OperatorDef(char *operatorName,
-                       int definedOK,
                        char *leftTypeName,
                        char *rightTypeName,
                        char *procedureName,
@@ -65,6 +61,7 @@ static void OperatorDef(char *operatorName,
                        bool canHash,
                        char *leftSortName,
                        char *rightSortName);
+
 static void OperatorUpd(Oid baseId, Oid commId, Oid negId);
 
 /* ----------------------------------------------------------------
@@ -75,14 +72,16 @@ static void OperatorUpd(Oid baseId, Oid commId, Oid negId);
  * ----------------------------------------------------------------
  *       pg_operator_desc      -- reldesc for pg_operator
  *       operatorName          -- name of operator to fetch
- *       leftObjectId          -- left oid of operator to fetch
- *       rightObjectId         -- right oid of operator to fetch
+ *       leftObjectId          -- left data type oid of operator to fetch
+ *       rightObjectId         -- right data type oid of operator to fetch
+ *       defined                       -- set TRUE if defined (not a shell)
  */
 static Oid
 OperatorGetWithOpenRelation(Relation pg_operator_desc,
                                                        const char *operatorName,
                                                        Oid leftObjectId,
-                                                       Oid rightObjectId)
+                                                       Oid rightObjectId,
+                                                       bool *defined)
 {
        HeapScanDesc pg_operator_scan;
        Oid                     operatorObjectId;
@@ -125,7 +124,19 @@ OperatorGetWithOpenRelation(Relation pg_operator_desc,
         * ----------------
         */
        tup = heap_getnext(pg_operator_scan, 0);
-       operatorObjectId = HeapTupleIsValid(tup) ? tup->t_data->t_oid : InvalidOid;
+
+       if (HeapTupleIsValid(tup))
+       {
+               regproc         oprcode = ((Form_pg_operator) GETSTRUCT(tup))->oprcode;
+
+               operatorObjectId = tup->t_data->t_oid;
+               *defined = RegProcedureIsValid(oprcode);
+       }
+       else
+       {
+               operatorObjectId = InvalidOid;
+               *defined = false;
+       }
 
        /* ----------------
         *      close the scan and return the oid.
@@ -146,7 +157,8 @@ OperatorGetWithOpenRelation(Relation pg_operator_desc,
 static Oid
 OperatorGet(char *operatorName,
                        char *leftTypeName,
-                       char *rightTypeName)
+                       char *rightTypeName,
+                       bool *defined)
 {
        Relation        pg_operator_desc;
 
@@ -157,7 +169,7 @@ OperatorGet(char *operatorName,
        bool            rightDefined = false;
 
        /* ----------------
-        *      look up the operator types.
+        *      look up the operator data types.
         *
         *      Note: types must be defined before operators
         * ----------------
@@ -167,7 +179,8 @@ OperatorGet(char *operatorName,
                leftObjectId = TypeGet(leftTypeName, &leftDefined);
 
                if (!OidIsValid(leftObjectId) || !leftDefined)
-                       elog(ERROR, "OperatorGet: left type '%s' nonexistent", leftTypeName);
+                       elog(ERROR, "OperatorGet: left type '%s' nonexistent",
+                                leftTypeName);
        }
 
        if (rightTypeName)
@@ -181,13 +194,13 @@ OperatorGet(char *operatorName,
 
        if (!((OidIsValid(leftObjectId) && leftDefined) ||
                  (OidIsValid(rightObjectId) && rightDefined)))
-               elog(ERROR, "OperatorGet: no argument types??");
+               elog(ERROR, "OperatorGet: must have at least one argument type");
 
        /* ----------------
         *      open the pg_operator relation
         * ----------------
         */
-       pg_operator_desc = heap_openr(OperatorRelationName);
+       pg_operator_desc = heap_openr(OperatorRelationName, AccessShareLock);
 
        /* ----------------
         *      get the oid for the operator with the appropriate name
@@ -197,16 +210,16 @@ OperatorGet(char *operatorName,
        operatorObjectId = OperatorGetWithOpenRelation(pg_operator_desc,
                                                                                                   operatorName,
                                                                                                   leftObjectId,
-                                                                                                  rightObjectId);
+                                                                                                  rightObjectId,
+                                                                                                  defined);
 
        /* ----------------
         *      close the relation and return the operator oid.
         * ----------------
         */
-       heap_close(pg_operator_desc);
+       heap_close(pg_operator_desc, AccessShareLock);
 
-       return
-               operatorObjectId;
+       return operatorObjectId;
 }
 
 /* ----------------------------------------------------------------
@@ -239,7 +252,8 @@ OperatorShellMakeWithOpenRelation(Relation pg_operator_desc,
        }
 
        /* ----------------
-        *      initialize *values with the type name and
+        *      initialize *values with the operator name and input data types.
+        *      Note that oprcode is set to InvalidOid, indicating it's a shell.
         * ----------------
         */
        i = 0;
@@ -247,9 +261,7 @@ OperatorShellMakeWithOpenRelation(Relation pg_operator_desc,
        values[i++] = NameGetDatum(&oname);
        values[i++] = Int32GetDatum(GetUserId());
        values[i++] = (Datum) (uint16) 0;
-
-       values[i++] = (Datum) 'b';      /* fill oprkind with a bogus value */
-
+       values[i++] = (Datum) 'b';      /* assume it's binary */
        values[i++] = (Datum) (bool) 0;
        values[i++] = (Datum) (bool) 0;
        values[i++] = ObjectIdGetDatum(leftObjectId);           /* <-- left oid */
@@ -281,14 +293,22 @@ OperatorShellMakeWithOpenRelation(Relation pg_operator_desc,
        heap_insert(pg_operator_desc, tup);
        operatorObjectId = tup->t_data->t_oid;
 
+       if (RelationGetForm(pg_operator_desc)->relhasindex)
+       {
+               Relation        idescs[Num_pg_operator_indices];
+
+               CatalogOpenIndices(Num_pg_operator_indices, Name_pg_operator_indices, idescs);
+               CatalogIndexInsert(idescs, Num_pg_operator_indices, pg_operator_desc, tup);
+               CatalogCloseIndices(Num_pg_operator_indices, idescs);
+       }
+
        /* ----------------
         *      free the tuple and return the operator oid
         * ----------------
         */
        pfree(tup);
 
-       return
-               operatorObjectId;
+       return operatorObjectId;
 }
 
 /* ----------------------------------------------------------------
@@ -331,26 +351,24 @@ OperatorShellMake(char *operatorName,
         *      open pg_operator
         * ----------------
         */
-       pg_operator_desc = heap_openr(OperatorRelationName);
+       pg_operator_desc = heap_openr(OperatorRelationName, RowExclusiveLock);
 
        /* ----------------
         *      add a "shell" operator tuple to the operator relation
         *      and recover the shell tuple's oid.
         * ----------------
         */
-       operatorObjectId =
-               OperatorShellMakeWithOpenRelation(pg_operator_desc,
-                                                                                 operatorName,
-                                                                                 leftObjectId,
-                                                                                 rightObjectId);
+       operatorObjectId = OperatorShellMakeWithOpenRelation(pg_operator_desc,
+                                                                                                                operatorName,
+                                                                                                                leftObjectId,
+                                                                                                                rightObjectId);
        /* ----------------
         *      close the operator relation and return the oid.
         * ----------------
         */
-       heap_close(pg_operator_desc);
+       heap_close(pg_operator_desc, RowExclusiveLock);
 
-       return
-               operatorObjectId;
+       return operatorObjectId;
 }
 
 /* --------------------------------
@@ -373,8 +391,8 @@ OperatorShellMake(char *operatorName,
  * Algorithm:
  *
  * check if operator already defined
- *       if so issue error if not definedOk, this is a duplicate
- *       but if definedOk, save the Oid -- filling in a shell
+ *       if so, but oprcode is null, save the Oid -- we are filling in a shell
+ *       otherwise error
  * get the attribute types from relation descriptor for pg_operator
  * assign values to the fields of the operator:
  *      operatorName
@@ -393,12 +411,9 @@ OperatorShellMake(char *operatorName,
  *                                             the same as the main operatorName, then create
  *                                             a shell and enter the new ObjectId
  *                                       else if this does not exist but IS the same
- *                                             name as the main operator, set the ObjectId=0.
- *                                             Later OperatorCreate will make another call
- *                                             to OperatorDef which will cause this field
- *                                             to be filled in (because even though the names
- *                                             will be switched, they are the same name and
- *                                             at this point this ObjectId will then be defined)
+ *                                             name & types as the main operator, set the ObjectId=0.
+ *                                             (We are creating a self-commutating operator.)
+ *                                             The link will be fixed later by OperatorUpd.
  *      negatorObjectId   -- same as for commutatorObjectId
  *      leftSortObjectId  -- same as for commutatorObjectId
  *      rightSortObjectId -- same as for commutatorObjectId
@@ -420,23 +435,21 @@ OperatorShellMake(char *operatorName,
  * --------------------------------
  *             "X" indicates an optional argument (i.e. one that can be NULL)
  *             operatorName;                   -- operator name
- *             definedOK;                              -- operator can already have an oid?
  *             leftTypeName;                   -- X left type name
  *             rightTypeName;                  -- X right type name
- *             procedureName;                  -- procedure oid for operator code
+ *             procedureName;                  -- procedure name for operator code
  *             precedence;                             -- operator precedence
  *             isLeftAssociative;              -- operator is left associative?
  *             commutatorName;                 -- X commutator operator name
  *             negatorName;                    -- X negator operator name
  *             restrictionName;                -- X restriction sel. procedure name
  *             joinName;                               -- X join sel. procedure name
- *             canHash;                                -- possible hash operator?
- *             leftSortName;                   -- X left sort operator
- *             rightSortName;                  -- X right sort operator
+ *             canHash;                                -- can hash join be used with operator?
+ *             leftSortName;                   -- X left sort operator (for merge join)
+ *             rightSortName;                  -- X right sort operator (for merge join)
  */
 static void
 OperatorDef(char *operatorName,
-                       int definedOK,
                        char *leftTypeName,
                        char *rightTypeName,
                        char *procedureName,
@@ -459,14 +472,15 @@ OperatorDef(char *operatorName,
        char            nulls[Natts_pg_operator];
        char            replaces[Natts_pg_operator];
        Datum           values[Natts_pg_operator];
-       Oid                     other_oid = 0;
        Oid                     operatorObjectId;
+       bool            operatorAlreadyDefined;
        Oid                     leftTypeId = InvalidOid;
        Oid                     rightTypeId = InvalidOid;
        Oid                     commutatorId = InvalidOid;
        Oid                     negatorId = InvalidOid;
        bool            leftDefined = false;
        bool            rightDefined = false;
+       bool            selfCommutator = false;
        char       *name[4];
        Oid                     typeId[8];
        int                     nargs;
@@ -488,21 +502,45 @@ OperatorDef(char *operatorName,
 
        operatorObjectId = OperatorGet(operatorName,
                                                                   leftTypeName,
-                                                                  rightTypeName);
+                                                                  rightTypeName,
+                                                                  &operatorAlreadyDefined);
 
-       if (OidIsValid(operatorObjectId) && !definedOK)
+       if (operatorAlreadyDefined)
                elog(ERROR, "OperatorDef: operator \"%s\" already defined",
                         operatorName);
 
+       /*
+        * At this point, if operatorObjectId is not InvalidOid then we are
+        * filling in a previously-created shell.
+        */
+
+       /* ----------------
+        *      look up the operator data types.
+        *
+        *      Note: types must be defined before operators
+        * ----------------
+        */
        if (leftTypeName)
+       {
                leftTypeId = TypeGet(leftTypeName, &leftDefined);
 
+               if (!OidIsValid(leftTypeId) || !leftDefined)
+                       elog(ERROR, "OperatorDef: left type '%s' nonexistent",
+                                leftTypeName);
+       }
+
        if (rightTypeName)
+       {
                rightTypeId = TypeGet(rightTypeName, &rightDefined);
 
-       if (!((OidIsValid(leftTypeId && leftDefined)) ||
-                 (OidIsValid(rightTypeId && rightDefined))))
-               elog(ERROR, "OperatorGet: no argument types??");
+               if (!OidIsValid(rightTypeId) || !rightDefined)
+                       elog(ERROR, "OperatorDef: right type '%s' nonexistent",
+                                rightTypeName);
+       }
+
+       if (!((OidIsValid(leftTypeId) && leftDefined) ||
+                 (OidIsValid(rightTypeId) && rightDefined)))
+               elog(ERROR, "OperatorDef: must have at least one argument type");
 
        for (i = 0; i < Natts_pg_operator; ++i)
        {
@@ -535,7 +573,7 @@ OperatorDef(char *operatorName,
                typeId[1] = rightTypeId;
                nargs = 2;
        }
-       tup = SearchSysCacheTuple(PRONAME,
+       tup = SearchSysCacheTuple(PROCNAME,
                                                          PointerGetDatum(procedureName),
                                                          Int32GetDatum(nargs),
                                                          PointerGetDatum(typeId),
@@ -545,9 +583,8 @@ OperatorDef(char *operatorName,
                func_error("OperatorDef", procedureName, nargs, typeId, NULL);
 
        values[Anum_pg_operator_oprcode - 1] = ObjectIdGetDatum(tup->t_data->t_oid);
-       values[Anum_pg_operator_oprresult - 1] =
-               ObjectIdGetDatum(((Form_pg_proc)
-                                                 GETSTRUCT(tup))->prorettype);
+       values[Anum_pg_operator_oprresult - 1] = ObjectIdGetDatum(((Form_pg_proc)
+                                                                                       GETSTRUCT(tup))->prorettype);
 
        /* ----------------
         *      find restriction
@@ -561,7 +598,7 @@ OperatorDef(char *operatorName,
                typeId[2] = INT2OID;    /* attribute number */
                typeId[3] = 0;                  /* value - can be any type      */
                typeId[4] = INT4OID;    /* flags - left or right selectivity */
-               tup = SearchSysCacheTuple(PRONAME,
+               tup = SearchSysCacheTuple(PROCNAME,
                                                                  PointerGetDatum(restrictionName),
                                                                  Int32GetDatum(5),
                                                                  PointerGetDatum(typeId),
@@ -587,7 +624,7 @@ OperatorDef(char *operatorName,
                typeId[3] = OIDOID;             /* relation OID 2 */
                typeId[4] = INT2OID;    /* attribute number 2 */
 
-               tup = SearchSysCacheTuple(PRONAME,
+               tup = SearchSysCacheTuple(PROCNAME,
                                                                  PointerGetDatum(joinName),
                                                                  Int32GetDatum(5),
                                                                  PointerGetDatum(typeId),
@@ -615,12 +652,12 @@ OperatorDef(char *operatorName,
        values[i++] = ObjectIdGetDatum(leftTypeId);
        values[i++] = ObjectIdGetDatum(rightTypeId);
 
-       ++i;                                            /* Skip "prorettype", this was done above */
+       ++i;                                            /* Skip "oprresult", it was filled in
+                                                                * above */
 
        /*
-        * Set up the other operators.  If they do not currently exist, set up
-        * shells in order to get ObjectId's and call OperatorDef again later
-        * to fill in the shells.
+        * Set up the other operators.  If they do not currently exist, create
+        * shells in order to get ObjectId's.
         */
        name[0] = commutatorName;
        name[1] = negatorName;
@@ -631,64 +668,107 @@ OperatorDef(char *operatorName,
        {
                if (name[j])
                {
-
-                       /* for the commutator, switch order of arguments */
-                       if (j == 0)
-                       {
-                               other_oid = OperatorGet(name[j], rightTypeName, leftTypeName);
-                               commutatorId = other_oid;
-                       }
-                       else
+                       char       *otherLeftTypeName = NULL;
+                       char       *otherRightTypeName = NULL;
+                       Oid                     otherLeftTypeId = InvalidOid;
+                       Oid                     otherRightTypeId = InvalidOid;
+                       Oid                     other_oid = InvalidOid;
+                       bool            otherDefined = false;
+
+                       switch (j)
                        {
-                               other_oid = OperatorGet(name[j], leftTypeName, rightTypeName);
-                               if (j == 1)
+                               case 0: /* commutator has reversed arg types */
+                                       otherLeftTypeName = rightTypeName;
+                                       otherRightTypeName = leftTypeName;
+                                       otherLeftTypeId = rightTypeId;
+                                       otherRightTypeId = leftTypeId;
+                                       other_oid = OperatorGet(name[j],
+                                                                                       otherLeftTypeName,
+                                                                                       otherRightTypeName,
+                                                                                       &otherDefined);
+                                       commutatorId = other_oid;
+                                       break;
+                               case 1: /* negator has same arg types */
+                                       otherLeftTypeName = leftTypeName;
+                                       otherRightTypeName = rightTypeName;
+                                       otherLeftTypeId = leftTypeId;
+                                       otherRightTypeId = rightTypeId;
+                                       other_oid = OperatorGet(name[j],
+                                                                                       otherLeftTypeName,
+                                                                                       otherRightTypeName,
+                                                                                       &otherDefined);
                                        negatorId = other_oid;
+                                       break;
+                               case 2: /* left sort op takes left-side data type */
+                                       otherLeftTypeName = leftTypeName;
+                                       otherRightTypeName = leftTypeName;
+                                       otherLeftTypeId = leftTypeId;
+                                       otherRightTypeId = leftTypeId;
+                                       other_oid = OperatorGet(name[j],
+                                                                                       otherLeftTypeName,
+                                                                                       otherRightTypeName,
+                                                                                       &otherDefined);
+                                       break;
+                               case 3: /* right sort op takes right-side data
+                                                                * type */
+                                       otherLeftTypeName = rightTypeName;
+                                       otherRightTypeName = rightTypeName;
+                                       otherLeftTypeId = rightTypeId;
+                                       otherRightTypeId = rightTypeId;
+                                       other_oid = OperatorGet(name[j],
+                                                                                       otherLeftTypeName,
+                                                                                       otherRightTypeName,
+                                                                                       &otherDefined);
+                                       break;
                        }
 
-                       if (OidIsValid(other_oid))      /* already in catalogs */
+                       if (OidIsValid(other_oid))
+                       {
+                               /* other op already in catalogs */
                                values[i++] = ObjectIdGetDatum(other_oid);
-                       else if (strcmp(operatorName, name[j]) != 0)
+                       }
+                       else if (strcmp(operatorName, name[j]) != 0 ||
+                                        otherLeftTypeId != leftTypeId ||
+                                        otherRightTypeId != rightTypeId)
                        {
                                /* not in catalogs, different from operator */
-
-                               /* for the commutator, switch order of arguments */
-                               if (j == 0)
-                               {
-                                       other_oid = OperatorShellMake(name[j],
-                                                                                                 rightTypeName,
-                                                                                                 leftTypeName);
-                               }
-                               else
-                               {
-                                       other_oid = OperatorShellMake(name[j],
-                                                                                                 leftTypeName,
-                                                                                                 rightTypeName);
-                               }
-
+                               other_oid = OperatorShellMake(name[j],
+                                                                                         otherLeftTypeName,
+                                                                                         otherRightTypeName);
                                if (!OidIsValid(other_oid))
                                        elog(ERROR,
-                                                "OperatorDef: can't create operator '%s'",
+                                                "OperatorDef: can't create operator shell '%s'",
                                                 name[j]);
                                values[i++] = ObjectIdGetDatum(other_oid);
-
                        }
                        else
-/* not in catalogs, same as operator ??? */
-                               values[i++] = ObjectIdGetDatum(InvalidOid);
+                       {
 
+                               /*
+                                * self-linkage to this operator; will fix below. Note
+                                * that only self-linkage for commutation makes sense.
+                                */
+                               if (j != 0)
+                                       elog(ERROR,
+                                                "OperatorDef: operator can't be its own negator or sort op");
+                               selfCommutator = true;
+                               values[i++] = ObjectIdGetDatum(InvalidOid);
+                       }
                }
                else
-/* new operator is optional */
+               {
+                       /* other operator is omitted */
                        values[i++] = ObjectIdGetDatum(InvalidOid);
+               }
        }
 
-       /* last three fields were filled in first */
+       /* last three fields were filled in above */
+
+       pg_operator_desc = heap_openr(OperatorRelationName, RowExclusiveLock);
 
        /*
         * If we are adding to an operator shell, get its t_self
         */
-       pg_operator_desc = heap_openr(OperatorRelationName);
-
        if (operatorObjectId)
        {
                opKey[0].sk_argument = PointerGetDatum(operatorName);
@@ -711,11 +791,11 @@ OperatorDef(char *operatorName,
                                                                   replaces);
 
                        setheapoverride(true);
-                       heap_replace(pg_operator_desc, &tup->t_self, tup);
+                       heap_update(pg_operator_desc, &tup->t_self, tup, NULL);
                        setheapoverride(false);
                }
                else
-                       elog(ERROR, "OperatorDef: no operator %d", other_oid);
+                       elog(ERROR, "OperatorDef: no operator %u", operatorObjectId);
 
                heap_endscan(pg_operator_scan);
        }
@@ -726,26 +806,35 @@ OperatorDef(char *operatorName,
 
                heap_insert(pg_operator_desc, tup);
                operatorObjectId = tup->t_data->t_oid;
+
+       }
+
+       if (RelationGetForm(pg_operator_desc)->relhasindex)
+       {
+               Relation        idescs[Num_pg_operator_indices];
+
+               CatalogOpenIndices(Num_pg_operator_indices, Name_pg_operator_indices, idescs);
+               CatalogIndexInsert(idescs, Num_pg_operator_indices, pg_operator_desc, tup);
+               CatalogCloseIndices(Num_pg_operator_indices, idescs);
        }
 
-       heap_close(pg_operator_desc);
+       heap_close(pg_operator_desc, RowExclusiveLock);
 
        /*
-        * It's possible that we're creating a skeleton operator here for the
-        * commute or negate attributes of a real operator.  If we are, then
-        * we're done.  If not, we may need to update the negator and
-        * commutator for this attribute.  The reason for this is that the
-        * user may want to create two operators (say < and >=).  When he
-        * defines <, if he uses >= as the negator or commutator, he won't be
-        * able to insert it later, since (for some reason) define operator
-        * defines it for him.  So what he does is to define > without a
-        * negator or commutator.  Then he defines >= with < as the negator
-        * and commutator.      As a side effect, this will update the > tuple if
-        * it has no commutator or negator defined.
-        *
-        * Alstublieft, Tom Vijlbrief.
+        * If a commutator and/or negator link is provided, update the other
+        * operator(s) to point at this one, if they don't already have a
+        * link. This supports an alternate style of operator definition
+        * wherein the user first defines one operator without giving negator
+        * or commutator, then defines the other operator of the pair with the
+        * proper commutator or negator attribute.      That style doesn't require
+        * creation of a shell, and it's the only style that worked right
+        * before Postgres version 6.5. This code also takes care of the
+        * situation where the new operator is its own commutator.
         */
-       if (!definedOK)
+       if (selfCommutator)
+               commutatorId = operatorObjectId;
+
+       if (OidIsValid(commutatorId) || OidIsValid(negatorId))
                OperatorUpd(operatorObjectId, commutatorId, negatorId);
 }
 
@@ -753,8 +842,8 @@ OperatorDef(char *operatorName,
  * OperatorUpd
  *
  *     For a given operator, look up its negator and commutator operators.
- *     If they are defined, but their negator and commutator operators
- *     (respectively) are not, then use the new operator for neg and comm.
+ *     If they are defined, but their negator and commutator fields
+ *     (respectively) are empty, then use the new operator for neg or comm.
  *     This solves a problem for users who need to insert two new operators
  *     which are the negator or commutator of each other.
  * ----------------------------------------------------------------
@@ -784,7 +873,7 @@ OperatorUpd(Oid baseId, Oid commId, Oid negId)
                nulls[i] = ' ';
        }
 
-       pg_operator_desc = heap_openr(OperatorRelationName);
+       pg_operator_desc = heap_openr(OperatorRelationName, RowExclusiveLock);
 
        /* check and update the commutator, if necessary */
        opKey[0].sk_argument = ObjectIdGetDatum(commId);
@@ -797,7 +886,11 @@ OperatorUpd(Oid baseId, Oid commId, Oid negId)
 
        tup = heap_getnext(pg_operator_scan, 0);
 
-       /* if the commutator and negator are the same operator, do one update */
+       /*
+        * if the commutator and negator are the same operator, do one update.
+        * XXX this is probably useless code --- I doubt it ever makes sense
+        * for commutator and negator to be the same thing...
+        */
        if (commId == negId)
        {
                if (HeapTupleIsValid(tup))
@@ -811,15 +904,13 @@ OperatorUpd(Oid baseId, Oid commId, Oid negId)
 
                                if (!OidIsValid(t->oprnegate))
                                {
-                                       values[Anum_pg_operator_oprnegate - 1] =
-                                               ObjectIdGetDatum(baseId);
+                                       values[Anum_pg_operator_oprnegate - 1] = ObjectIdGetDatum(baseId);
                                        replaces[Anum_pg_operator_oprnegate - 1] = 'r';
                                }
 
                                if (!OidIsValid(t->oprcom))
                                {
-                                       values[Anum_pg_operator_oprcom - 1] =
-                                               ObjectIdGetDatum(baseId);
+                                       values[Anum_pg_operator_oprcom - 1] = ObjectIdGetDatum(baseId);
                                        replaces[Anum_pg_operator_oprcom - 1] = 'r';
                                }
 
@@ -830,19 +921,28 @@ OperatorUpd(Oid baseId, Oid commId, Oid negId)
                                                                           replaces);
 
                                setheapoverride(true);
-                               heap_replace(pg_operator_desc, &tup->t_self, tup);
+                               heap_update(pg_operator_desc, &tup->t_self, tup, NULL);
                                setheapoverride(false);
-
+               
+                               if (RelationGetForm(pg_operator_desc)->relhasindex)
+                               {
+                                       Relation        idescs[Num_pg_operator_indices];
+                       
+                                       CatalogOpenIndices(Num_pg_operator_indices, Name_pg_operator_indices, idescs);
+                                       CatalogIndexInsert(idescs, Num_pg_operator_indices, pg_operator_desc, tup);
+                                       CatalogCloseIndices(Num_pg_operator_indices, idescs);
+                               }
                        }
                }
                heap_endscan(pg_operator_scan);
 
-               heap_close(pg_operator_desc);
+               heap_close(pg_operator_desc, RowExclusiveLock);
 
                return;
        }
 
        /* if commutator and negator are different, do two updates */
+
        if (HeapTupleIsValid(tup) &&
                !(OidIsValid(((Form_pg_operator) GETSTRUCT(tup))->oprcom)))
        {
@@ -855,13 +955,24 @@ OperatorUpd(Oid baseId, Oid commId, Oid negId)
                                                           replaces);
 
                setheapoverride(true);
-               heap_replace(pg_operator_desc, &tup->t_self, tup);
+               heap_update(pg_operator_desc, &tup->t_self, tup, NULL);
                setheapoverride(false);
 
+               if (RelationGetForm(pg_operator_desc)->relhasindex)
+               {
+                       Relation        idescs[Num_pg_operator_indices];
+       
+                       CatalogOpenIndices(Num_pg_operator_indices, Name_pg_operator_indices, idescs);
+                       CatalogIndexInsert(idescs, Num_pg_operator_indices, pg_operator_desc, tup);
+                       CatalogCloseIndices(Num_pg_operator_indices, idescs);
+               }
+
                values[Anum_pg_operator_oprcom - 1] = (Datum) NULL;
                replaces[Anum_pg_operator_oprcom - 1] = ' ';
        }
 
+       heap_endscan(pg_operator_scan);
+
        /* check and update the negator, if necessary */
        opKey[0].sk_argument = ObjectIdGetDatum(negId);
 
@@ -884,49 +995,30 @@ OperatorUpd(Oid baseId, Oid commId, Oid negId)
                                                           replaces);
 
                setheapoverride(true);
-               heap_replace(pg_operator_desc, &tup->t_self, tup);
+               heap_update(pg_operator_desc, &tup->t_self, tup, NULL);
                setheapoverride(false);
+
+               if (RelationGetForm(pg_operator_desc)->relhasindex)
+               {
+                       Relation        idescs[Num_pg_operator_indices];
+       
+                       CatalogOpenIndices(Num_pg_operator_indices, Name_pg_operator_indices, idescs);
+                       CatalogIndexInsert(idescs, Num_pg_operator_indices, pg_operator_desc, tup);
+                       CatalogCloseIndices(Num_pg_operator_indices, idescs);
+               }
        }
 
        heap_endscan(pg_operator_scan);
 
-       heap_close(pg_operator_desc);
+
+       heap_close(pg_operator_desc, RowExclusiveLock);
 }
 
 
 /* ----------------------------------------------------------------
  * OperatorCreate
  *
- * Algorithm:
- *
- *     Since the commutator, negator, leftsortoperator, and rightsortoperator
- *     can be defined implicitly through OperatorCreate, must check before
- *     the main operator is added to see if they already exist.  If they
- *     do not already exist, OperatorDef makes a "shell" for each undefined
- *     one, and then OperatorCreate must call OperatorDef again to fill in
- *     each shell.  All this is necessary in order to get the right ObjectId's
- *     filled into the right fields.
- *
- *     The "definedOk" flag indicates that OperatorDef can be called on
- *     the operator even though it already has an entry in the PG_OPERATOR
- *     relation.  This allows shells to be filled in.  The user cannot
- *     forward declare operators, this is strictly an internal capability.
- *
- *     When the shells are filled in by subsequent calls to OperatorDef,
- *     all the fields are the same as the definition of the original operator
- *     except that the target operator name and the original operatorName
- *     are switched.  In the case of commutator and negator, special flags
- *     are set to indicate their status, telling the executor(?) that
- *     the operands are to be switched, or the outcome of the procedure
- *     negated.
- *
- * ************************* NOTE NOTE NOTE ******************************
- *
- *     If the execution of this utility is interrupted, the pg_operator
- *     catalog may be left in an inconsistent state.  Similarly, if
- *     something is removed from the pg_operator, pg_type, or pg_procedure
- *     catalog while this is executing, the results may be inconsistent.
- * ----------------------------------------------------------------
+ * This is now just an interface procedure for OperatorDef ...
  *
  * "X" indicates an optional argument (i.e. one that can be NULL)
  *             operatorName;                   -- operator name
@@ -938,11 +1030,10 @@ OperatorUpd(Oid baseId, Oid commId, Oid negId)
  *             commutatorName;                 -- X commutator operator name
  *             negatorName;                    -- X negator operator name
  *             restrictionName;                -- X restriction sel. procedure
- *             joinName;                               -- X join sel. procedure name
- *             canHash;                                -- operator hashes
- *             leftSortName;                   -- X left sort operator
- *             rightSortName;                  -- X right sort operator
- *
+ *             joinName;                               -- X join sel. procedure
+ *             canHash;                                -- hash join can be used with this operator
+ *             leftSortName;                   -- X left sort operator (for merge join)
+ *             rightSortName;                  -- X right sort operator (for merge join)
  */
 void
 OperatorCreate(char *operatorName,
@@ -959,59 +1050,31 @@ OperatorCreate(char *operatorName,
                           char *leftSortName,
                           char *rightSortName)
 {
-       Oid                     commObjectId,
-                               negObjectId;
-       Oid                     leftSortObjectId,
-                               rightSortObjectId;
-       int                     definedOK;
-
        if (!leftTypeName && !rightTypeName)
-               elog(ERROR, "OperatorCreate : at least one of leftarg or rightarg must be defined");
-
-       /* ----------------
-        *      get the oid's of the operator's associated operators, if possible.
-        * ----------------
-        */
-       if (commutatorName)
-               commObjectId = OperatorGet(commutatorName,              /* commute type order */
-                                                                  rightTypeName,
-                                                                  leftTypeName);
-       else
-               commObjectId = 0;
+               elog(ERROR, "OperatorCreate: at least one of leftarg or rightarg must be defined");
 
-       if (negatorName)
-               negObjectId = OperatorGet(negatorName,
-                                                                 leftTypeName,
-                                                                 rightTypeName);
-       else
-               negObjectId = 0;
-
-       if (leftSortName)
-               leftSortObjectId = OperatorGet(leftSortName,
-                                                                          leftTypeName,
-                                                                          rightTypeName);
-       else
-               leftSortObjectId = 0;
-
-       if (rightSortName)
-               rightSortObjectId = OperatorGet(rightSortName,
-                                                                               rightTypeName,
-                                                                               leftTypeName);
-       else
-               rightSortObjectId = 0;
+       if (!(leftTypeName && rightTypeName))
+       {
+               /* If it's not a binary op, these things mustn't be set: */
+               if (commutatorName)
+                       elog(ERROR, "OperatorCreate: only binary operators can have commutators");
+               if (negatorName)
+                       elog(ERROR, "OperatorCreate: only binary operators can have negators");
+               if (restrictionName || joinName)
+                       elog(ERROR, "OperatorCreate: only binary operators can have selectivity");
+               if (canHash)
+                       elog(ERROR, "OperatorCreate: only binary operators can hash");
+               if (leftSortName || rightSortName)
+                       elog(ERROR, "OperatorCreate: only binary operators can have sort links");
+       }
 
        /* ----------------
         *      Use OperatorDef() to define the specified operator and
         *      also create shells for the operator's associated operators
         *      if they don't already exist.
-        *
-        *      This operator should not be defined yet.
         * ----------------
         */
-       definedOK = 0;
-
        OperatorDef(operatorName,
-                               definedOK,
                                leftTypeName,
                                rightTypeName,
                                procedureName,
@@ -1024,77 +1087,4 @@ OperatorCreate(char *operatorName,
                                canHash,
                                leftSortName,
                                rightSortName);
-
-       /* ----------------
-        *      Now fill in information in the operator's associated
-        *      operators.
-        *
-        *      These operators should be defined or have shells defined.
-        * ----------------
-        */
-       definedOK = 1;
-
-       if (!OidIsValid(commObjectId) && commutatorName)
-               OperatorDef(commutatorName,
-                                       definedOK,
-                                       leftTypeName,           /* should eventually */
-                                       rightTypeName,          /* commute order */
-                                       procedureName,
-                                       precedence,
-                                       isLeftAssociative,
-                                       operatorName,           /* commutator */
-                                       negatorName,
-                                       restrictionName,
-                                       joinName,
-                                       canHash,
-                                       rightSortName,
-                                       leftSortName);
-
-       if (negatorName && !OidIsValid(negObjectId))
-               OperatorDef(negatorName,
-                                       definedOK,
-                                       leftTypeName,
-                                       rightTypeName,
-                                       procedureName,
-                                       precedence,
-                                       isLeftAssociative,
-                                       commutatorName,
-                                       operatorName,           /* negator */
-                                       restrictionName,
-                                       joinName,
-                                       canHash,
-                                       leftSortName,
-                                       rightSortName);
-
-       if (leftSortName && !OidIsValid(leftSortObjectId))
-               OperatorDef(leftSortName,
-                                       definedOK,
-                                       leftTypeName,
-                                       rightTypeName,
-                                       procedureName,
-                                       precedence,
-                                       isLeftAssociative,
-                                       commutatorName,
-                                       negatorName,
-                                       restrictionName,
-                                       joinName,
-                                       canHash,
-                                       operatorName,           /* left sort */
-                                       rightSortName);
-
-       if (rightSortName && !OidIsValid(rightSortObjectId))
-               OperatorDef(rightSortName,
-                                       definedOK,
-                                       leftTypeName,
-                                       rightTypeName,
-                                       procedureName,
-                                       precedence,
-                                       isLeftAssociative,
-                                       commutatorName,
-                                       negatorName,
-                                       restrictionName,
-                                       joinName,
-                                       canHash,
-                                       leftSortName,
-                                       operatorName);          /* right sort */
 }