Thread: Correct way to do deletes?

Correct way to do deletes?

From
Bill Studenmund
Date:
I'm working on DROP OPERATOR CLASS, and have a question about how to
actually do the deletes. I ask as my main method has been to copy other
bits of the backend, assuming they do things right.

But I've found two different examples, and don't know which is right? Or
are they both?

Specifically I'm at the step of cleaning out the entries in pg_amop and
pg_amproc.

Example 1 from backend/commands/remove.c for aggregates:
   relation = heap_openr(AggregateRelationName, RowExclusiveLock);
   tup = SearchSysCache(AGGNAME,                        PointerGetDatum(aggName),
ObjectIdGetDatum(basetypeID),                       0, 0);
 
   if (!HeapTupleIsValid(tup))       agg_error("RemoveAggregate", aggName, basetypeID);
   /* Remove any comments related to this aggregate */   DeleteComments(tup->t_data->t_oid,
RelationGetRelid(relation));
   simple_heap_delete(relation, &tup->t_self);
   ReleaseSysCache(tup);
   heap_close(relation, RowExclusiveLock);

Another apparently contrary example from backend/catalog/heap.c:
   pg_class_desc = heap_openr(RelationRelationName, RowExclusiveLock);
   tup = SearchSysCacheCopy(RELOID,                            ObjectIdGetDatum(rel->rd_id),
0,0, 0);   if (!HeapTupleIsValid(tup))       elog(ERROR, "Relation \"%s\" does not exist",
RelationGetRelationName(rel));
   /*    * delete the relation tuple from pg_class, and finish up.    */   simple_heap_delete(pg_class_desc,
&tup->t_self);  heap_freetuple(tup);
 
   heap_close(pg_class_desc, RowExclusiveLock);

In one we SearchSysCache(), simple_heap_delete(), and then
ReleaseSysCache(). In the other we SearchSysCacheCopy(),
simple_heap_delete, and heap_freetuple().

Are they two parallel ways, or is one better?

Take care,

Bill



Re: Correct way to do deletes?

From
Tom Lane
Date:
Bill Studenmund <wrstuden@netbsd.org> writes:
> In one we SearchSysCache(), simple_heap_delete(), and then
> ReleaseSysCache(). In the other we SearchSysCacheCopy(),
> simple_heap_delete, and heap_freetuple().

> Are they two parallel ways, or is one better?

I don't think it matters much anymore.  The copy approach takes a few
more cycles, though, to no good purpose since you have no need to modify
the tuple struct obtained from cache.  (When updating a tuple, copying
the cache entry and scribbling directly on the copy is sometimes more
convenient than calling heap_modifytuple to build a new tuple.)
        regards, tom lane