Skip to content

Commit 4c25a06

Browse files
committed
Code review for ALTER TRIGGER RENAME patch: make better use of index,
don't scribble on tuple returned by table scan.
1 parent 857661b commit 4c25a06

File tree

2 files changed

+44
-48
lines changed

2 files changed

+44
-48
lines changed

src/backend/commands/trigger.c

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.116 2002/04/27 03:45:02 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.117 2002/04/30 01:24:57 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -514,8 +514,7 @@ RelationRemoveTriggers(Relation rel)
514514
* for name conflict (within rel)
515515
* for original trigger (if not arg)
516516
* modify tgname in trigger tuple
517-
* insert modified trigger in trigger catalog
518-
* delete original trigger from trigger catalog
517+
* update row in catalog
519518
*/
520519
void
521520
renametrig(Oid relid,
@@ -526,8 +525,7 @@ renametrig(Oid relid,
526525
Relation tgrel;
527526
HeapTuple tuple;
528527
SysScanDesc tgscan;
529-
ScanKeyData key;
530-
bool found = FALSE;
528+
ScanKeyData key[2];
531529
Relation idescs[Num_pg_trigger_indices];
532530

533531
/*
@@ -550,70 +548,69 @@ renametrig(Oid relid,
550548
/*
551549
* First pass -- look for name conflict
552550
*/
553-
ScanKeyEntryInitialize(&key, 0,
551+
ScanKeyEntryInitialize(&key[0], 0,
554552
Anum_pg_trigger_tgrelid,
555553
F_OIDEQ,
556554
ObjectIdGetDatum(relid));
555+
ScanKeyEntryInitialize(&key[1], 0,
556+
Anum_pg_trigger_tgname,
557+
F_NAMEEQ,
558+
PointerGetDatum(newname));
557559
tgscan = systable_beginscan(tgrel, TriggerRelidNameIndex, true,
558-
SnapshotNow, 1, &key);
559-
while (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
560-
{
561-
Form_pg_trigger pg_trigger = (Form_pg_trigger) GETSTRUCT(tuple);
562-
563-
if (namestrcmp(&(pg_trigger->tgname), newname) == 0)
564-
elog(ERROR, "renametrig: trigger %s already defined on relation %s",
565-
newname, RelationGetRelationName(targetrel));
566-
}
560+
SnapshotNow, 2, key);
561+
if (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
562+
elog(ERROR, "renametrig: trigger %s already defined on relation %s",
563+
newname, RelationGetRelationName(targetrel));
567564
systable_endscan(tgscan);
568565

569566
/*
570567
* Second pass -- look for trigger existing with oldname and update
571568
*/
572-
ScanKeyEntryInitialize(&key, 0,
569+
ScanKeyEntryInitialize(&key[0], 0,
573570
Anum_pg_trigger_tgrelid,
574571
F_OIDEQ,
575572
ObjectIdGetDatum(relid));
573+
ScanKeyEntryInitialize(&key[1], 0,
574+
Anum_pg_trigger_tgname,
575+
F_NAMEEQ,
576+
PointerGetDatum(oldname));
576577
tgscan = systable_beginscan(tgrel, TriggerRelidNameIndex, true,
577-
SnapshotNow, 1, &key);
578-
while (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
578+
SnapshotNow, 2, key);
579+
if (HeapTupleIsValid(tuple = systable_getnext(tgscan)))
579580
{
580-
Form_pg_trigger pg_trigger = (Form_pg_trigger) GETSTRUCT(tuple);
581+
/*
582+
* Update pg_trigger tuple with new tgname.
583+
*/
584+
tuple = heap_copytuple(tuple); /* need a modifiable copy */
581585

582-
if (namestrcmp(&(pg_trigger->tgname), oldname) == 0)
583-
{
584-
/*
585-
* Update pg_trigger tuple with new tgname.
586-
* (Scribbling on tuple is OK because it's a copy...)
587-
*/
588-
namestrcpy(&(pg_trigger->tgname), newname);
589-
simple_heap_update(tgrel, &tuple->t_self, tuple);
586+
namestrcpy(&((Form_pg_trigger) GETSTRUCT(tuple))->tgname, newname);
590587

591-
/*
592-
* keep system catalog indices current
593-
*/
594-
CatalogOpenIndices(Num_pg_trigger_indices, Name_pg_trigger_indices, idescs);
595-
CatalogIndexInsert(idescs, Num_pg_trigger_indices, tgrel, tuple);
596-
CatalogCloseIndices(Num_pg_trigger_indices, idescs);
588+
simple_heap_update(tgrel, &tuple->t_self, tuple);
597589

598-
/*
599-
* Invalidate relation's relcache entry so that other
600-
* backends (and this one too!) are sent SI message to make them
601-
* rebuild relcache entries.
602-
*/
603-
CacheInvalidateRelcache(relid);
590+
/*
591+
* keep system catalog indices current
592+
*/
593+
CatalogOpenIndices(Num_pg_trigger_indices, Name_pg_trigger_indices, idescs);
594+
CatalogIndexInsert(idescs, Num_pg_trigger_indices, tgrel, tuple);
595+
CatalogCloseIndices(Num_pg_trigger_indices, idescs);
604596

605-
found = TRUE;
606-
break;
607-
}
597+
/*
598+
* Invalidate relation's relcache entry so that other backends (and
599+
* this one too!) are sent SI message to make them rebuild relcache
600+
* entries. (Ideally this should happen automatically...)
601+
*/
602+
CacheInvalidateRelcache(relid);
608603
}
604+
else
605+
{
606+
elog(ERROR, "renametrig: trigger %s not defined on relation %s",
607+
oldname, RelationGetRelationName(targetrel));
608+
}
609+
609610
systable_endscan(tgscan);
610611

611612
heap_close(tgrel, RowExclusiveLock);
612613

613-
if (!found)
614-
elog(ERROR, "renametrig: trigger %s not defined on relation %s",
615-
oldname, RelationGetRelationName(targetrel));
616-
617614
/*
618615
* Close rel, but keep exclusive lock!
619616
*/

src/include/commands/tablecmds.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: tablecmds.h,v 1.3 2002/04/26 19:29:47 tgl Exp $
10+
* $Id: tablecmds.h,v 1.4 2002/04/30 01:24:52 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
1414
#ifndef TABLECMDS_H
1515
#define TABLECMDS_H
1616

1717
#include "nodes/parsenodes.h"
18-
#include "utils/inval.h"
1918

2019
extern void AlterTableAddColumn(Oid myrelid, bool inherits,
2120
ColumnDef *colDef);

0 commit comments

Comments
 (0)