7
7
* Portions Copyright (c) 1994, Regents of the University of California
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.108 2002/03/26 19:15:45 tgl Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.109 2002/03/29 22:10:33 tgl Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
18
18
#include "catalog/catalog.h"
19
19
#include "catalog/catname.h"
20
20
#include "catalog/indexing.h"
21
+ #include "catalog/namespace.h"
21
22
#include "catalog/pg_language.h"
22
23
#include "catalog/pg_proc.h"
23
24
#include "catalog/pg_trigger.h"
29
30
#include "utils/builtins.h"
30
31
#include "utils/fmgroids.h"
31
32
#include "utils/inval.h"
33
+ #include "utils/lsyscache.h"
32
34
#include "utils/syscache.h"
33
35
34
36
@@ -96,20 +98,10 @@ CreateTrigger(CreateTrigStmt *stmt)
96
98
stmt -> trigname = constrtrigname ;
97
99
sprintf (constrtrigname , "RI_ConstraintTrigger_%u" , newoid ());
98
100
99
- if (stmt -> constrrel = = NULL )
100
- constrrelid = InvalidOid ;
101
+ if (stmt -> constrrel ! = NULL )
102
+ constrrelid = RangeVarGetRelid ( stmt -> constrrel , false) ;
101
103
else
102
- {
103
- /*
104
- * NoLock is probably sufficient here, since we're only
105
- * interested in getting the relation's OID...
106
- */
107
- Relation conrel ;
108
-
109
- conrel = heap_openrv (stmt -> constrrel , NoLock );
110
- constrrelid = conrel -> rd_id ;
111
- heap_close (conrel , NoLock );
112
- }
104
+ constrrelid = InvalidOid ;
113
105
}
114
106
115
107
TRIGGER_CLEAR_TYPE (tgtype );
@@ -310,8 +302,11 @@ CreateTrigger(CreateTrigStmt *stmt)
310
302
heap_close (rel , NoLock );
311
303
}
312
304
305
+ /*
306
+ * DropTrigger - drop an individual trigger by name
307
+ */
313
308
void
314
- DropTrigger (DropTrigStmt * stmt )
309
+ DropTrigger (Oid relid , const char * trigname )
315
310
{
316
311
Relation rel ;
317
312
Relation tgrel ;
@@ -320,21 +315,21 @@ DropTrigger(DropTrigStmt *stmt)
320
315
Relation pgrel ;
321
316
HeapTuple tuple ;
322
317
Relation ridescs [Num_pg_class_indices ];
318
+ int remaining = 0 ;
323
319
int found = 0 ;
324
- int tgfound = 0 ;
325
320
326
- rel = heap_openrv ( stmt -> relation , AccessExclusiveLock );
321
+ rel = heap_open ( relid , AccessExclusiveLock );
327
322
328
323
if (rel -> rd_rel -> relkind != RELKIND_RELATION )
329
324
elog (ERROR , "DropTrigger: relation \"%s\" is not a table" ,
330
- stmt -> relation -> relname );
325
+ RelationGetRelationName ( rel ) );
331
326
332
- if (!allowSystemTableMods && IsSystemRelationName (stmt -> relation -> relname ))
327
+ if (!allowSystemTableMods && IsSystemRelationName (RelationGetRelationName ( rel ) ))
333
328
elog (ERROR , "DropTrigger: can't drop trigger for system relation %s" ,
334
- stmt -> relation -> relname );
329
+ RelationGetRelationName ( rel ) );
335
330
336
- if (!pg_class_ownercheck (RelationGetRelid ( rel ) , GetUserId ()))
337
- elog (ERROR , "%s: %s" , stmt -> relation -> relname ,
331
+ if (!pg_class_ownercheck (relid , GetUserId ()))
332
+ elog (ERROR , "%s: %s" , RelationGetRelationName ( rel ) ,
338
333
aclcheck_error_strings [ACLCHECK_NOT_OWNER ]);
339
334
340
335
/*
@@ -346,33 +341,33 @@ DropTrigger(DropTrigStmt *stmt)
346
341
tgrel = heap_openr (TriggerRelationName , RowExclusiveLock );
347
342
ScanKeyEntryInitialize (& key , 0 , Anum_pg_trigger_tgrelid ,
348
343
F_OIDEQ ,
349
- ObjectIdGetDatum (RelationGetRelid ( rel ) ));
344
+ ObjectIdGetDatum (relid ));
350
345
tgscan = systable_beginscan (tgrel , TriggerRelidIndex , true,
351
346
SnapshotNow , 1 , & key );
352
347
while (HeapTupleIsValid (tuple = systable_getnext (tgscan )))
353
348
{
354
349
Form_pg_trigger pg_trigger = (Form_pg_trigger ) GETSTRUCT (tuple );
355
350
356
- if (namestrcmp (& (pg_trigger -> tgname ), stmt -> trigname ) == 0 )
351
+ if (namestrcmp (& (pg_trigger -> tgname ), trigname ) == 0 )
357
352
{
358
353
/* Delete any comments associated with this trigger */
359
354
DeleteComments (tuple -> t_data -> t_oid , RelationGetRelid (tgrel ));
360
355
361
356
simple_heap_delete (tgrel , & tuple -> t_self );
362
- tgfound ++ ;
357
+ found ++ ;
363
358
}
364
359
else
365
- found ++ ;
360
+ remaining ++ ;
366
361
}
367
362
systable_endscan (tgscan );
368
363
heap_close (tgrel , RowExclusiveLock );
369
364
370
- if (tgfound == 0 )
365
+ if (found == 0 )
371
366
elog (ERROR , "DropTrigger: there is no trigger %s on relation %s" ,
372
- stmt -> trigname , stmt -> relation -> relname );
373
- if (tgfound > 1 )
367
+ trigname , RelationGetRelationName ( rel ) );
368
+ if (found > 1 ) /* shouldn't happen */
374
369
elog (NOTICE , "DropTrigger: found (and deleted) %d triggers %s on relation %s" ,
375
- tgfound , stmt -> trigname , stmt -> relation -> relname );
370
+ found , trigname , RelationGetRelationName ( rel ) );
376
371
377
372
/*
378
373
* Update relation's pg_class entry. Crucial side-effect: other
@@ -381,26 +376,20 @@ DropTrigger(DropTrigStmt *stmt)
381
376
*/
382
377
pgrel = heap_openr (RelationRelationName , RowExclusiveLock );
383
378
tuple = SearchSysCacheCopy (RELOID ,
384
- ObjectIdGetDatum (RelationGetRelid ( rel ) ),
379
+ ObjectIdGetDatum (relid ),
385
380
0 , 0 , 0 );
386
381
if (!HeapTupleIsValid (tuple ))
387
382
elog (ERROR , "DropTrigger: relation %s not found in pg_class" ,
388
- stmt -> relation -> relname );
383
+ RelationGetRelationName ( rel ) );
389
384
390
- ((Form_pg_class ) GETSTRUCT (tuple ))-> reltriggers = found ;
385
+ ((Form_pg_class ) GETSTRUCT (tuple ))-> reltriggers = remaining ;
391
386
simple_heap_update (pgrel , & tuple -> t_self , tuple );
392
387
CatalogOpenIndices (Num_pg_class_indices , Name_pg_class_indices , ridescs );
393
388
CatalogIndexInsert (ridescs , Num_pg_class_indices , pgrel , tuple );
394
389
CatalogCloseIndices (Num_pg_class_indices , ridescs );
395
390
heap_freetuple (tuple );
396
391
heap_close (pgrel , RowExclusiveLock );
397
392
398
- /*
399
- * We used to try to update the rel's relcache entry here, but that's
400
- * fairly pointless since it will happen as a byproduct of the
401
- * upcoming CommandCounterIncrement...
402
- */
403
-
404
393
/* Keep lock on target rel until end of xact */
405
394
heap_close (rel , NoLock );
406
395
}
@@ -479,25 +468,12 @@ RelationRemoveTriggers(Relation rel)
479
468
480
469
while (HeapTupleIsValid (tup = systable_getnext (tgscan )))
481
470
{
482
- Form_pg_trigger pg_trigger ;
483
- Relation refrel ;
484
- DropTrigStmt * stmt = makeNode (DropTrigStmt );
485
-
486
- pg_trigger = (Form_pg_trigger ) GETSTRUCT (tup );
487
-
488
- stmt -> trigname = pstrdup (NameStr (pg_trigger -> tgname ));
489
-
490
- /* May as well grab AccessExclusiveLock, since DropTrigger will. */
491
- refrel = heap_open (pg_trigger -> tgrelid , AccessExclusiveLock );
492
- stmt -> relation = makeNode (RangeVar );
493
- /* XXX bogus: what about schema? */
494
- stmt -> relation -> relname = pstrdup (RelationGetRelationName (refrel ));
495
- heap_close (refrel , NoLock );
471
+ Form_pg_trigger pg_trigger = (Form_pg_trigger ) GETSTRUCT (tup );
496
472
497
473
elog (NOTICE , "DROP TABLE implicitly drops referential integrity trigger from table \"%s\"" ,
498
- stmt -> relation -> relname );
474
+ get_temp_rel_by_physicalname ( get_rel_name ( pg_trigger -> tgrelid )) );
499
475
500
- DropTrigger (stmt );
476
+ DropTrigger (pg_trigger -> tgrelid , NameStr ( pg_trigger -> tgname ) );
501
477
502
478
/*
503
479
* Need to do a command counter increment here to show up new
0 commit comments