Thread: contrib/ gist cleanup

contrib/ gist cleanup

From
Neil Conway
Date:
This patch removes a bunch of no-longer-necessary manual pfree() calls
from the implementation of GiST methods in contrib/ modules.

Barring any objections, I'll apply this tomorrow.

-Neil
Index: contrib/btree_gist/btree_bit.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/btree_gist/btree_bit.c,v
retrieving revision 1.4
diff -c -r1.4 btree_bit.c
*** contrib/btree_gist/btree_bit.c    12 May 2005 00:39:37 -0000    1.4
--- contrib/btree_gist/btree_bit.c    20 May 2005 02:40:35 -0000
***************
*** 127,133 ****
  {
      GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
-     void       *qtst = (void *) PG_GETARG_POINTER(1);
      void       *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
      StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
      bool        retval = FALSE;
--- 127,132 ----
***************
*** 140,150 ****
          bytea       *q = gbt_bit_xfrm((bytea *) query);

          retval = gbt_var_consistent(&r, (void *) q, &strategy, FALSE, &tinfo);
-         pfree(q);
      }
-
-     if (qtst != query)
-         pfree(query);
      PG_RETURN_BOOL(retval);
  }

--- 139,145 ----
Index: contrib/btree_gist/btree_bytea.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/btree_gist/btree_bytea.c,v
retrieving revision 1.4
diff -c -r1.4 btree_bytea.c
*** contrib/btree_gist/btree_bytea.c    12 May 2005 00:39:37 -0000    1.4
--- contrib/btree_gist/btree_bytea.c    20 May 2005 02:40:45 -0000
***************
*** 97,112 ****
  {
      GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
-     void       *qtst = (void *) PG_GETARG_POINTER(1);
      void       *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
      StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
!     bool        retval = FALSE;
      GBT_VARKEY_R r = gbt_var_key_readable(key);

      retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
-
-     if (qtst != query)
-         pfree(query);
      PG_RETURN_BOOL(retval);
  }

--- 97,108 ----
  {
      GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
      void       *query = (void *) DatumGetByteaP(PG_GETARG_DATUM(1));
      StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
!     bool        retval;
      GBT_VARKEY_R r = gbt_var_key_readable(key);

      retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
      PG_RETURN_BOOL(retval);
  }

Index: contrib/btree_gist/btree_numeric.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/btree_gist/btree_numeric.c,v
retrieving revision 1.4
diff -c -r1.4 btree_numeric.c
*** contrib/btree_gist/btree_numeric.c    12 May 2005 00:39:37 -0000    1.4
--- contrib/btree_gist/btree_numeric.c    20 May 2005 02:40:03 -0000
***************
*** 98,113 ****

      GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
-     void       *qtst = (void *) PG_GETARG_POINTER(1);
      void       *query = (void *) DatumGetNumeric(PG_GETARG_DATUM(1));
      StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
!     bool        retval = FALSE;
      GBT_VARKEY_R r = gbt_var_key_readable(key);

      retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
-
-     if (qtst != query)
-         pfree(query);
      PG_RETURN_BOOL(retval);
  }

--- 98,109 ----

      GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
      void       *query = (void *) DatumGetNumeric(PG_GETARG_DATUM(1));
      StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
!     bool        retval;
      GBT_VARKEY_R r = gbt_var_key_readable(key);

      retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);
      PG_RETURN_BOOL(retval);
  }

***************
*** 164,171 ****
                                               PointerGetDatum(uk.lower)
                                               ));

-     pfree(DatumGetPointer(uni));
-
      os = DatumGetNumeric(DirectFunctionCall2(
                                               numeric_sub,
                                               PointerGetDatum(ok.upper),
--- 160,165 ----
***************
*** 178,204 ****
                                               NumericGetDatum(os)
                                               ));

-     pfree(os);
-
      if (NUMERIC_IS_NAN(us))
      {
-
          if (NUMERIC_IS_NAN(os))
              *result = 0.0;
          else
              *result = 1.0;
-
      }
      else
      {
-
          Numeric        nul = DatumGetNumeric(DirectFunctionCall1(int4_numeric, Int32GetDatum(0)));

          *result = 0.0;

          if (DirectFunctionCall2(numeric_gt, NumericGetDatum(ds), NumericGetDatum(nul)))
          {
-
              *result += FLT_MIN;
              os = DatumGetNumeric(DirectFunctionCall2(
                                                       numeric_div,
--- 172,192 ----
***************
*** 206,224 ****
                                                       NumericGetDatum(us)
                                                       ));
              *result += (float4) DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow, NumericGetDatum(os)));
-             pfree(os);
-
          }
-
-         pfree(nul);
      }

      if (*result > 0)
          *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));

-     pfree(us);
-     pfree(ds);
-
      PG_RETURN_POINTER(result);
  }

--- 194,205 ----
Index: contrib/btree_gist/btree_text.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/btree_gist/btree_text.c,v
retrieving revision 1.5
diff -c -r1.5 btree_text.c
*** contrib/btree_gist/btree_text.c    12 May 2005 00:39:37 -0000    1.5
--- contrib/btree_gist/btree_text.c    20 May 2005 02:41:01 -0000
***************
*** 108,115 ****
                        entry->rel, entry->page,
                        entry->offset, VARSIZE(DatumGetPointer(d)), TRUE);
          retval = gbt_var_compress(&trim, &tinfo);
-
-         pfree(DatumGetPointer(d));
      }
      else
          retval = entry;
--- 108,113 ----
***************
*** 124,130 ****
  {
      GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
-     void       *qtst = (void *) PG_GETARG_POINTER(1);
      void       *query = (void *) DatumGetTextP(PG_GETARG_DATUM(1));
      StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
      bool        retval = FALSE;
--- 122,127 ----
***************
*** 132,140 ****

      retval = gbt_var_consistent(&r, query, &strategy, GIST_LEAF(entry), &tinfo);

-     if (qtst != query)
-         pfree(query);
-
      PG_RETURN_BOOL(retval);
  }

--- 129,134 ----
***************
*** 144,168 ****
  {
      GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
-     void       *qtst = (void *) PG_GETARG_POINTER(1);
      void       *query = (void *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
      void       *trim = (void *) DatumGetPointer(DirectFunctionCall1(rtrim1, PointerGetDatum(query)));
      StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
!     bool        retval = FALSE;
      GBT_VARKEY_R r = gbt_var_key_readable(key);

      retval = gbt_var_consistent(&r, trim, &strategy, GIST_LEAF(entry), &tinfo);
-
-     pfree(trim);
-
-     if (qtst != query)
-         pfree(query);
      PG_RETURN_BOOL(retval);
  }


-
-
  Datum
  gbt_text_union(PG_FUNCTION_ARGS)
  {
--- 138,154 ----
  {
      GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
      GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
      void       *query = (void *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(1)));
      void       *trim = (void *) DatumGetPointer(DirectFunctionCall1(rtrim1, PointerGetDatum(query)));
      StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
!     bool        retval;
      GBT_VARKEY_R r = gbt_var_key_readable(key);

      retval = gbt_var_consistent(&r, trim, &strategy, GIST_LEAF(entry), &tinfo);
      PG_RETURN_BOOL(retval);
  }


  Datum
  gbt_text_union(PG_FUNCTION_ARGS)
  {
Index: contrib/btree_gist/btree_time.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/btree_gist/btree_time.c,v
retrieving revision 1.6
diff -c -r1.6 btree_time.c
*** contrib/btree_gist/btree_time.c    25 Apr 2005 07:00:32 -0000    1.6
--- contrib/btree_gist/btree_time.c    20 May 2005 02:12:57 -0000
***************
*** 222,228 ****

      /* see interval_larger */
      res = Max(intr->time + intr->month * (30 * 86400), 0);
-     pfree(intr);

      intr = DatumGetIntervalP(DirectFunctionCall2(
                                                   time_mi_time,
--- 222,227 ----
***************
*** 231,237 ****

      /* see interval_larger */
      res += Max(intr->time + intr->month * (30 * 86400), 0);
-     pfree(intr);

      *result = 0.0;

--- 230,235 ----
***************
*** 244,250 ****
          *result += FLT_MIN;
          *result += (float) (res / ((double) (res + intr->time + intr->month * (30 * 86400))));
          *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
-         pfree(intr);
      }

      PG_RETURN_POINTER(result);
--- 242,247 ----
Index: contrib/btree_gist/btree_ts.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/btree_gist/btree_ts.c,v
retrieving revision 1.7
diff -c -r1.7 btree_ts.c
*** contrib/btree_gist/btree_ts.c    21 Feb 2005 10:03:57 -0000    1.7
--- contrib/btree_gist/btree_ts.c    20 May 2005 02:12:35 -0000
***************
*** 226,232 ****

  #ifdef HAVE_INT64_TIMESTAMP
      int64        res;
-
  #else
      double        res;
  #endif
--- 226,231 ----
***************
*** 240,246 ****
      /* see interval_larger */

      res = Max(intr->time + intr->month * (30 * 86400), 0);
-     pfree(intr);

      intr = DatumGetIntervalP(DirectFunctionCall2(
                                                   timestamp_mi,
--- 239,244 ----
***************
*** 250,256 ****

      /* see interval_larger */
      res += Max(intr->time + intr->month * (30 * 86400), 0);
-     pfree(intr);

      *result = 0.0;

--- 248,253 ----
***************
*** 264,274 ****
          *result += FLT_MIN;
          *result += (float) (res / ((double) (res + intr->time + intr->month * (30 * 86400))));
          *result *= (FLT_MAX / (((GISTENTRY *) PG_GETARG_POINTER(0))->rel->rd_att->natts + 1));
-         pfree(intr);
      }

      PG_RETURN_POINTER(result);
-
  }


--- 261,269 ----
Index: contrib/btree_gist/btree_utils_num.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/btree_gist/btree_utils_num.c,v
retrieving revision 1.6
diff -c -r1.6 btree_utils_num.c
*** contrib/btree_gist/btree_utils_num.c    12 May 2005 00:39:37 -0000    1.6
--- contrib/btree_gist/btree_utils_num.c    20 May 2005 02:12:02 -0000
***************
*** 246,252 ****
          }
      }

-     pfree(arr);
-
      return v;
  }
--- 246,250 ----
Index: contrib/btree_gist/btree_utils_var.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/btree_gist/btree_utils_var.c,v
retrieving revision 1.8
diff -c -r1.8 btree_utils_var.c
*** contrib/btree_gist/btree_utils_var.c    16 May 2005 06:45:57 -0000    1.8
--- contrib/btree_gist/btree_utils_var.c    20 May 2005 02:58:28 -0000
***************
*** 9,29 ****
  Datum
  gbt_var_decompress(PG_FUNCTION_ARGS)
  {
!         GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
!         GBT_VARKEY   *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));

!         if (key != (GBT_VARKEY *) DatumGetPointer(entry->key))
!         {
!                 GISTENTRY  *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));

!                 gistentryinit(*retval, PointerGetDatum(key),
!                                           entry->rel, entry->page,
!                                           entry->offset, VARSIZE(key), FALSE);

!                 PG_RETURN_POINTER(retval);
!         }

!         PG_RETURN_POINTER(entry);
  }

  /* Returns a better readable representaion of variable key ( sets pointer ) */
--- 9,29 ----
  Datum
  gbt_var_decompress(PG_FUNCTION_ARGS)
  {
!     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
!     GBT_VARKEY   *key = (GBT_VARKEY *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));

!     if (key != (GBT_VARKEY *) DatumGetPointer(entry->key))
!     {
!         GISTENTRY  *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));

!         gistentryinit(*retval, PointerGetDatum(key),
!                       entry->rel, entry->page,
!                       entry->offset, VARSIZE(key), FALSE);

!         PG_RETURN_POINTER(retval);
!     }

!     PG_RETURN_POINTER(entry);
  }

  /* Returns a better readable representaion of variable key ( sets pointer ) */
***************
*** 216,222 ****
      GBT_VARKEY_R nr;
      GBT_VARKEY_R eo = gbt_var_key_readable(e);

-
      if (eo.lower == eo.upper)    /* leaf */
      {
          tmp = gbt_var_leaf2node(e, tinfo);
--- 216,221 ----
***************
*** 235,254 ****
              nr.upper = ro.upper;
              nk = gbt_var_key_copy(&nr, TRUE);
          }
          if ((*tinfo->f_cmp) ((bytea *) ro.upper, (bytea *) eo.upper) < 0)
          {
              nr.upper = eo.upper;
              nr.lower = ro.lower;
              nk = gbt_var_key_copy(&nr, TRUE);
          }
          if (nk)
-         {
-             pfree(DatumGetPointer(*u));
              *u = PointerGetDatum(nk);
-         }
-
-
-
      }
      else
      {
--- 234,249 ----
              nr.upper = ro.upper;
              nk = gbt_var_key_copy(&nr, TRUE);
          }
+
          if ((*tinfo->f_cmp) ((bytea *) ro.upper, (bytea *) eo.upper) < 0)
          {
              nr.upper = eo.upper;
              nr.lower = ro.lower;
              nk = gbt_var_key_copy(&nr, TRUE);
          }
+
          if (nk)
              *u = PointerGetDatum(nk);
      }
      else
      {
***************
*** 256,265 ****
          nr.upper = eo.upper;
          *u = PointerGetDatum(gbt_var_key_copy(&nr, TRUE));
      }
-
-     if (tmp && tmp != e)
-         pfree(tmp);
-
  }


--- 251,256 ----
***************
*** 273,287 ****
      if (entry->leafkey)
      {
          GBT_VARKEY *r = NULL;
!         bytea       *tstd = (bytea *) DatumGetPointer(entry->key);        /* toasted     */
!         bytea       *leaf = (bytea *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));        /* untoasted */
          GBT_VARKEY_R u;

          u.lower = u.upper = leaf;
          r = gbt_var_key_copy(&u, FALSE);

-         if (tstd != leaf)
-             pfree(leaf);
          retval = palloc(sizeof(GISTENTRY));
          gistentryinit(*retval, PointerGetDatum(r),
                        entry->rel, entry->page,
--- 264,275 ----
      if (entry->leafkey)
      {
          GBT_VARKEY *r = NULL;
!         bytea       *leaf = (bytea *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
          GBT_VARKEY_R u;

          u.lower = u.upper = leaf;
          r = gbt_var_key_copy(&u, FALSE);

          retval = palloc(sizeof(GISTENTRY));
          gistentryinit(*retval, PointerGetDatum(r),
                        entry->rel, entry->page,
***************
*** 319,325 ****


      /* Truncate (=compress) key */
-
      if (tinfo->trnc)
      {
          int32        plen;
--- 307,312 ----
***************
*** 328,334 ****
          plen = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(out), tinfo);
          trc = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(out), plen + 1, tinfo);

-         pfree(DatumGetPointer(out));
          out = PointerGetDatum(trc);
      }

--- 315,320 ----
***************
*** 428,444 ****
              }
              dres /= 256.0;
          }
-         pfree(DatumGetPointer(d));

          *res += FLT_MIN;
          *res += (float) (dres / ((double) (ol + 1)));
          *res *= (FLT_MAX / (o->rel->rd_att->natts + 1));
-
      }

-     if (tmp && tmp != newe)
-         pfree(tmp);
-
      return res;
  }

--- 414,425 ----
***************
*** 524,541 ****
          }
      }

-     /* Free strxfrm'ed leafs */
-     for (i = 0; i < svcntr; i++)
-         pfree(sv[i]);
-
-     if (sv)
-         pfree(sv);
-
      /* Truncate (=compress) key */
-
      if (tinfo->trnc)
      {
-
          int32        ll = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), tinfo);
          int32        lr = gbt_var_node_cp_len((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), tinfo);
          GBT_VARKEY *dl;
--- 505,513 ----
***************
*** 546,560 ****

          dl = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_ldatum), ll, tinfo);
          dr = gbt_var_node_truncate((GBT_VARKEY *) DatumGetPointer(v->spl_rdatum), ll, tinfo);
-         pfree(DatumGetPointer(v->spl_ldatum));
-         pfree(DatumGetPointer(v->spl_rdatum));
          v->spl_ldatum = PointerGetDatum(dl);
          v->spl_rdatum = PointerGetDatum(dr);
-
      }

-     pfree(arr);
-
      return v;
  }

--- 518,527 ----
Index: contrib/cube/cube.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/cube/cube.c,v
retrieving revision 1.19
diff -c -r1.19 cube.c
*** contrib/cube/cube.c    17 May 2005 03:34:17 -0000    1.19
--- contrib/cube/cube.c    20 May 2005 02:33:19 -0000
***************
*** 229,236 ****
          out = g_cube_binary_union(tmp, (NDBOX *)
                                  DatumGetPointer(entryvec->vector[i].key),
                                    sizep);
-         if (i > 1)
-             pfree(tmp);
          tmp = out;
      }

--- 229,234 ----
***************
*** 269,275 ****
      rt_cube_size(ud, &tmp1);
      rt_cube_size((NDBOX *) DatumGetPointer(origentry->key), &tmp2);
      *result = (float) (tmp1 - tmp2);
-     pfree(ud);

      /*
       * fprintf(stderr, "penalty\n"); fprintf(stderr, "\t%g\n", *result);
--- 267,272 ----
***************
*** 339,349 ****
              rt_cube_size(inter_d, &size_inter);
              size_waste = size_union - size_inter;

-             pfree(union_d);
-
-             if (inter_d != (NDBOX *) NULL)
-                 pfree(inter_d);
-
              /*
               * are these a more promising split than what we've already
               * seen?
--- 336,341 ----
***************
*** 415,422 ****
          /* pick which page to add it to */
          if (size_alpha - size_l < size_beta - size_r)
          {
-             pfree(datum_l);
-             pfree(union_dr);
              datum_l = union_dl;
              size_l = size_alpha;
              *left++ = i;
--- 407,412 ----
***************
*** 424,431 ****
          }
          else
          {
-             pfree(datum_r);
-             pfree(union_dl);
              datum_r = union_dr;
              size_r = size_alpha;
              *right++ = i;
--- 414,419 ----
Index: contrib/ltree/_ltree_gist.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/ltree/_ltree_gist.c,v
retrieving revision 1.10
diff -c -r1.10 _ltree_gist.c
*** contrib/ltree/_ltree_gist.c    21 Oct 2004 19:28:33 -0000    1.10
--- contrib/ltree/_ltree_gist.c    20 May 2005 02:18:21 -0000
***************
*** 90,98 ****
              item = NEXTVAL(item);
          }

-         if (PointerGetDatum(val) != entry->key)
-             pfree(val);
-
          retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
          gistentryinit(*retval, PointerGetDatum(key),
                        entry->rel, entry->page,
--- 90,95 ----
***************
*** 424,430 ****
      }

      *right = *left = FirstOffsetNumber;
-     pfree(costvector);

      v->spl_ldatum = PointerGetDatum(datum_l);
      v->spl_rdatum = PointerGetDatum(datum_r);
--- 421,426 ----
Index: contrib/ltree/ltree_gist.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/ltree/ltree_gist.c,v
retrieving revision 1.8
diff -c -r1.8 ltree_gist.c
*** contrib/ltree/ltree_gist.c    21 Oct 2004 19:28:33 -0000    1.8
--- contrib/ltree/ltree_gist.c    20 May 2005 02:17:29 -0000
***************
*** 77,85 ****
          key->flag = LTG_ONENODE;
          memcpy((void *) LTG_NODE(key), (void *) val, val->len);

-         if (PointerGetDatum(val) != entry->key)
-             pfree(val);
-
          retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
          gistentryinit(*retval, PointerGetDatum(key),
                        entry->rel, entry->page,
--- 77,82 ----
***************
*** 432,438 ****
      else
          memcpy((void *) LTG_RNODE(ru), (void *) ru_r, ru_r->len);

-     pfree(array);
      v->spl_ldatum = PointerGetDatum(lu);
      v->spl_rdatum = PointerGetDatum(ru);

--- 429,434 ----
Index: contrib/pg_trgm/trgm_gist.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/pg_trgm/trgm_gist.c,v
retrieving revision 1.2
diff -c -r1.2 trgm_gist.c
*** contrib/pg_trgm/trgm_gist.c    29 Aug 2004 05:06:36 -0000    1.2
--- contrib/pg_trgm/trgm_gist.c    20 May 2005 02:39:47 -0000
***************
*** 91,103 ****
      if (entry->leafkey)
      {                            /* trgm */
          TRGM       *res;
-         text       *toastedval = (text *) DatumGetPointer(entry->key);
          text       *val = (text *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));

          res = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
-         if (val != toastedval)
-             pfree(val);
-
          retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
          gistentryinit(*retval, PointerGetDatum(res),
                        entry->rel, entry->page,
--- 91,99 ----
***************
*** 175,183 ****
  #endif
      }

-     PG_FREE_IF_COPY(query, 1);
-     pfree(qtrg);
-
      PG_RETURN_BOOL(res);
  }

--- 171,176 ----
***************
*** 603,610 ****
      }

      *right = *left = FirstOffsetNumber;
-     pfree(costvector);
-     pfree(cache);
      v->spl_ldatum = PointerGetDatum(datum_l);
      v->spl_rdatum = PointerGetDatum(datum_r);

--- 596,601 ----
Index: contrib/rtree_gist/rtree_gist.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/rtree_gist/rtree_gist.c,v
retrieving revision 1.10
diff -c -r1.10 rtree_gist.c
*** contrib/rtree_gist/rtree_gist.c    29 Aug 2004 05:06:37 -0000    1.10
--- contrib/rtree_gist/rtree_gist.c    20 May 2005 02:35:44 -0000
***************
*** 152,160 ****

      ud = DirectFunctionCall2(rt_box_union, origentry->key, newentry->key);
      tmp1 = size_box(ud);
-     if (DatumGetPointer(ud) != NULL)
-         pfree(DatumGetPointer(ud));
-
      *result = tmp1 - size_box(origentry->key);
      PG_RETURN_POINTER(result);
  }
--- 152,157 ----
***************
*** 342,348 ****
              else
                  ADDLIST(listT, unionT, posT, arr[i - 1].pos);
          }
-         pfree(arr);
      }

      /* which split more optimal? */
--- 339,344 ----
***************
*** 372,382 ****

      if (direction == 'x')
      {
-         pfree(unionB);
-         pfree(listB);
-         pfree(unionT);
-         pfree(listT);
-
          v->spl_left = listL;
          v->spl_right = listR;
          v->spl_nleft = posL;
--- 368,373 ----
***************
*** 386,396 ****
      }
      else
      {
-         pfree(unionR);
-         pfree(listR);
-         pfree(unionL);
-         pfree(listL);
-
          v->spl_left = listB;
          v->spl_right = listT;
          v->spl_nleft = posB;
--- 377,382 ----
***************
*** 497,505 ****
              in = (POLYGON *) PG_DETOAST_DATUM(entry->key);
              r = (BOX *) palloc(sizeof(BOX));
              memcpy((void *) r, (void *) &(in->boundbox), sizeof(BOX));
-             if (in != (POLYGON *) DatumGetPointer(entry->key))
-                 pfree(in);
-
              gistentryinit(*retval, PointerGetDatum(r),
                            entry->rel, entry->page,
                            entry->offset, sizeof(BOX), FALSE);
--- 483,488 ----
Index: contrib/seg/seg.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/seg/seg.c,v
retrieving revision 1.12
diff -c -r1.12 seg.c
*** contrib/seg/seg.c    21 Oct 2004 19:28:34 -0000    1.12
--- contrib/seg/seg.c    20 May 2005 02:31:25 -0000
***************
*** 238,245 ****
          out = gseg_binary_union(tmp, (SEG *)
                                  DatumGetPointer(entryvec->vector[i].key),
                                  sizep);
-         if (i > 1)
-             pfree(tmp);
          tmp = out;
      }

--- 238,243 ----
***************
*** 278,284 ****
      rt_seg_size(ud, &tmp1);
      rt_seg_size((SEG *) DatumGetPointer(origentry->key), &tmp2);
      *result = tmp1 - tmp2;
-     pfree(ud);

  #ifdef GIST_DEBUG
      fprintf(stderr, "penalty\n");
--- 276,281 ----
***************
*** 351,366 ****
              rt_seg_size(inter_d, &size_inter);
              size_waste = size_union - size_inter;

-             pfree(union_d);
-
-             if (inter_d != (SEG *) NULL)
-                 pfree(inter_d);
-
              /*
               * are these a more promising split that what we've already
               * seen?
               */
-
              if (size_waste > waste || firsttime)
              {
                  waste = size_waste;
--- 348,357 ----
***************
*** 427,434 ****
          /* pick which page to add it to */
          if (size_alpha - size_l < size_beta - size_r)
          {
-             pfree(datum_l);
-             pfree(union_dr);
              datum_l = union_dl;
              size_l = size_alpha;
              *left++ = i;
--- 418,423 ----
***************
*** 436,443 ****
          }
          else
          {
-             pfree(datum_r);
-             pfree(union_dl);
              datum_r = union_dr;
              size_r = size_alpha;
              *right++ = i;
--- 425,430 ----
Index: contrib/tsearch/gistidx.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/tsearch/gistidx.c,v
retrieving revision 1.8
diff -c -r1.8 gistidx.c
*** contrib/tsearch/gistidx.c    21 Oct 2004 19:28:35 -0000    1.8
--- contrib/tsearch/gistidx.c    20 May 2005 02:39:30 -0000
***************
*** 123,129 ****
      if (entry->leafkey)
      {                            /* txtidx */
          GISTTYPE   *res;
-         txtidx       *toastedval = (txtidx *) DatumGetPointer(entry->key);
          txtidx       *val = (txtidx *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
          int4        len;
          int4       *arr;
--- 123,128 ----
***************
*** 154,161 ****
              res = (GISTTYPE *) repalloc((void *) res, len);
              res->len = len;
          }
-         if (val != toastedval)
-             pfree(val);

          /* make signature, if array is too long */
          if (res->len > TOAST_INDEX_TARGET)
--- 153,158 ----
***************
*** 167,173 ****
              ressign->len = len;
              ressign->flag = SIGNKEY;
              makesign(GETSIGN(ressign), res);
-             pfree(res);
              res = ressign;
          }

--- 164,169 ----
***************
*** 780,787 ****
      }

      *right = *left = FirstOffsetNumber;
-     pfree(costvector);
-     pfree(cache);
      v->spl_ldatum = PointerGetDatum(datum_l);
      v->spl_rdatum = PointerGetDatum(datum_r);

--- 776,781 ----
Index: contrib/tsearch2/gistidx.c
===================================================================
RCS file: /var/lib/cvs/pgsql/contrib/tsearch2/gistidx.c,v
retrieving revision 1.6
diff -c -r1.6 gistidx.c
*** contrib/tsearch2/gistidx.c    31 Mar 2005 15:08:08 -0000    1.6
--- contrib/tsearch2/gistidx.c    20 May 2005 02:39:18 -0000
***************
*** 142,148 ****
      if (entry->leafkey)
      {                            /* tsvector */
          GISTTYPE   *res;
-         tsvector   *toastedval = (tsvector *) DatumGetPointer(entry->key);
          tsvector   *val = (tsvector *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
          int4        len;
          int4       *arr;
--- 142,147 ----
***************
*** 173,180 ****
              res = (GISTTYPE *) repalloc((void *) res, len);
              res->len = len;
          }
-         if (val != toastedval)
-             pfree(val);

          /* make signature, if array is too long */
          if (res->len > TOAST_INDEX_TARGET)
--- 172,177 ----
***************
*** 186,192 ****
              ressign->len = len;
              ressign->flag = SIGNKEY;
              makesign(GETSIGN(ressign), res);
-             pfree(res);
              res = ressign;
          }

--- 183,188 ----
***************
*** 734,741 ****
      }

      *right = *left = FirstOffsetNumber;
-     pfree(costvector);
-     pfree(cache);
      v->spl_ldatum = PointerGetDatum(datum_l);
      v->spl_rdatum = PointerGetDatum(datum_r);

--- 730,735 ----

Re: contrib/ gist cleanup

From
Neil Conway
Date:
Neil Conway wrote:
> This patch removes a bunch of no-longer-necessary manual pfree() calls
> from the implementation of GiST methods in contrib/ modules.

Applied.

-Neil