8
8
* Portions Copyright (c) 1994, Regents of the University of California
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/catalog/dependency.c,v 1.1 2002/07/12 18:43:13 tgl Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/catalog/dependency.c,v 1.2 2002/07/15 16:33:31 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
21
21
#include "catalog/heap.h"
22
22
#include "catalog/index.h"
23
23
#include "catalog/indexing.h"
24
+ #include "catalog/pg_attrdef.h"
24
25
#include "catalog/pg_constraint.h"
25
26
#include "catalog/pg_depend.h"
26
27
#include "catalog/pg_language.h"
@@ -46,6 +47,7 @@ typedef enum ObjectClasses
46
47
OCLASS_PROC ,/* pg_proc */
47
48
OCLASS_TYPE ,/* pg_type */
48
49
OCLASS_CONSTRAINT ,/* pg_constraint */
50
+ OCLASS_DEFAULT ,/* pg_attrdef */
49
51
OCLASS_LANGUAGE ,/* pg_language */
50
52
OCLASS_OPERATOR ,/* pg_operator */
51
53
OCLASS_REWRITE ,/* pg_rewrite */
@@ -59,6 +61,7 @@ static bool recursiveDeletion(const ObjectAddress *object,
59
61
static void doDeletion (const ObjectAddress * object );
60
62
static ObjectClasses getObjectClass (const ObjectAddress * object );
61
63
static char * getObjectDescription (const ObjectAddress * object );
64
+ static void getRelationDescription (StringInfo buffer ,Oid relid );
62
65
63
66
64
67
/*
@@ -285,7 +288,7 @@ recursiveDeletion(const ObjectAddress *object,
285
288
* RESTRICT case. (However, normal dependencies on the
286
289
* component object could still cause failure.)
287
290
*/
288
- elog (DEBUG1 ,"Dropinternally cascades to %s" ,
291
+ elog (DEBUG1 ,"Dropauto- cascades to %s" ,
289
292
getObjectDescription (& otherObject ));
290
293
291
294
if (!recursiveDeletion (& otherObject ,behavior ,
@@ -392,6 +395,10 @@ doDeletion(const ObjectAddress *object)
392
395
RemoveConstraintById (object -> objectId );
393
396
break ;
394
397
398
+ case OCLASS_DEFAULT :
399
+ RemoveAttrDefaultById (object -> objectId );
400
+ break ;
401
+
395
402
case OCLASS_LANGUAGE :
396
403
DropProceduralLanguageById (object -> objectId );
397
404
break ;
@@ -425,6 +432,7 @@ getObjectClass(const ObjectAddress *object)
425
432
{
426
433
static bool reloids_initialized = false;
427
434
static Oid reloid_pg_constraint ;
435
+ static Oid reloid_pg_attrdef ;
428
436
static Oid reloid_pg_language ;
429
437
static Oid reloid_pg_operator ;
430
438
static Oid reloid_pg_rewrite ;
@@ -456,6 +464,7 @@ getObjectClass(const ObjectAddress *object)
456
464
if (!reloids_initialized )
457
465
{
458
466
reloid_pg_constraint = get_system_catalog_relid (ConstraintRelationName );
467
+ reloid_pg_attrdef = get_system_catalog_relid (AttrDefaultRelationName );
459
468
reloid_pg_language = get_system_catalog_relid (LanguageRelationName );
460
469
reloid_pg_operator = get_system_catalog_relid (OperatorRelationName );
461
470
reloid_pg_rewrite = get_system_catalog_relid (RewriteRelationName );
@@ -468,6 +477,11 @@ getObjectClass(const ObjectAddress *object)
468
477
Assert (object -> objectSubId == 0 );
469
478
return OCLASS_CONSTRAINT ;
470
479
}
480
+ if (object -> classId == reloid_pg_attrdef )
481
+ {
482
+ Assert (object -> objectSubId == 0 );
483
+ return OCLASS_DEFAULT ;
484
+ }
471
485
if (object -> classId == reloid_pg_language )
472
486
{
473
487
Assert (object -> objectSubId == 0 );
@@ -509,63 +523,12 @@ getObjectDescription(const ObjectAddress *object)
509
523
switch (getObjectClass (object ))
510
524
{
511
525
case OCLASS_CLASS :
512
- {
513
- HeapTuple relTup ;
514
- Form_pg_class relForm ;
515
-
516
- relTup = SearchSysCache (RELOID ,
517
- ObjectIdGetDatum (object -> objectId ),
518
- 0 ,0 ,0 );
519
- if (!HeapTupleIsValid (relTup ))
520
- elog (ERROR ,"getObjectDescription: Relation %u does not exist" ,
521
- object -> objectId );
522
- relForm = (Form_pg_class )GETSTRUCT (relTup );
523
-
524
- switch (relForm -> relkind )
525
- {
526
- case RELKIND_RELATION :
527
- appendStringInfo (& buffer ,"table %s" ,
528
- NameStr (relForm -> relname ));
529
- break ;
530
- case RELKIND_INDEX :
531
- appendStringInfo (& buffer ,"index %s" ,
532
- NameStr (relForm -> relname ));
533
- break ;
534
- case RELKIND_SPECIAL :
535
- appendStringInfo (& buffer ,"special system relation %s" ,
536
- NameStr (relForm -> relname ));
537
- break ;
538
- case RELKIND_SEQUENCE :
539
- appendStringInfo (& buffer ,"sequence %s" ,
540
- NameStr (relForm -> relname ));
541
- break ;
542
- case RELKIND_UNCATALOGED :
543
- appendStringInfo (& buffer ,"uncataloged table %s" ,
544
- NameStr (relForm -> relname ));
545
- break ;
546
- case RELKIND_TOASTVALUE :
547
- appendStringInfo (& buffer ,"toast table %s" ,
548
- NameStr (relForm -> relname ));
549
- break ;
550
- case RELKIND_VIEW :
551
- appendStringInfo (& buffer ,"view %s" ,
552
- NameStr (relForm -> relname ));
553
- break ;
554
- default :
555
- /* shouldn't get here */
556
- appendStringInfo (& buffer ,"relation %s" ,
557
- NameStr (relForm -> relname ));
558
- break ;
559
- }
560
-
526
+ getRelationDescription (& buffer ,object -> objectId );
561
527
if (object -> objectSubId != 0 )
562
528
appendStringInfo (& buffer ," column %s" ,
563
529
get_attname (object -> objectId ,
564
530
object -> objectSubId ));
565
-
566
- ReleaseSysCache (relTup );
567
531
break ;
568
- }
569
532
570
533
case OCLASS_PROC :
571
534
/* XXX could improve on this */
@@ -614,17 +577,61 @@ getObjectDescription(const ObjectAddress *object)
614
577
615
578
con = (Form_pg_constraint )GETSTRUCT (tup );
616
579
617
- appendStringInfo (& buffer ,"constraint %s" ,
618
- NameStr (con -> conname ));
619
580
if (OidIsValid (con -> conrelid ))
620
- appendStringInfo (& buffer ," on table %s" ,
621
- get_rel_name (con -> conrelid ));
581
+ {
582
+ appendStringInfo (& buffer ,"constraint %s on " ,
583
+ NameStr (con -> conname ));
584
+ getRelationDescription (& buffer ,con -> conrelid );
585
+ }
586
+ else
587
+ {
588
+ appendStringInfo (& buffer ,"constraint %s" ,
589
+ NameStr (con -> conname ));
590
+ }
622
591
623
592
systable_endscan (rcscan );
624
593
heap_close (conDesc ,AccessShareLock );
625
594
break ;
626
595
}
627
596
597
+ case OCLASS_DEFAULT :
598
+ {
599
+ Relation attrdefDesc ;
600
+ ScanKeyData skey [1 ];
601
+ SysScanDesc adscan ;
602
+ HeapTuple tup ;
603
+ Form_pg_attrdef attrdef ;
604
+ ObjectAddress colobject ;
605
+
606
+ attrdefDesc = heap_openr (AttrDefaultRelationName ,AccessShareLock );
607
+
608
+ ScanKeyEntryInitialize (& skey [0 ],0x0 ,
609
+ ObjectIdAttributeNumber ,F_OIDEQ ,
610
+ ObjectIdGetDatum (object -> objectId ));
611
+
612
+ adscan = systable_beginscan (attrdefDesc ,AttrDefaultOidIndex , true,
613
+ SnapshotNow ,1 ,skey );
614
+
615
+ tup = systable_getnext (adscan );
616
+
617
+ if (!HeapTupleIsValid (tup ))
618
+ elog (ERROR ,"getObjectDescription: Default %u does not exist" ,
619
+ object -> objectId );
620
+
621
+ attrdef = (Form_pg_attrdef )GETSTRUCT (tup );
622
+
623
+ colobject .classId = RelOid_pg_class ;
624
+ colobject .objectId = attrdef -> adrelid ;
625
+ colobject .objectSubId = attrdef -> adnum ;
626
+
627
+ appendStringInfo (& buffer ,"default for %s" ,
628
+ getObjectDescription (& colobject ));
629
+
630
+ systable_endscan (adscan );
631
+ heap_close (attrdefDesc ,AccessShareLock );
632
+ break ;
633
+ }
634
+
628
635
case OCLASS_LANGUAGE :
629
636
{
630
637
HeapTuple langTup ;
@@ -672,11 +679,9 @@ getObjectDescription(const ObjectAddress *object)
672
679
673
680
rule = (Form_pg_rewrite )GETSTRUCT (tup );
674
681
675
- appendStringInfo (& buffer ,"rule %s" ,
682
+ appendStringInfo (& buffer ,"rule %s on " ,
676
683
NameStr (rule -> rulename ));
677
- if (OidIsValid (rule -> ev_class ))
678
- appendStringInfo (& buffer ," on table %s" ,
679
- get_rel_name (rule -> ev_class ));
684
+ getRelationDescription (& buffer ,rule -> ev_class );
680
685
681
686
systable_endscan (rcscan );
682
687
heap_close (ruleDesc ,AccessShareLock );
@@ -708,11 +713,9 @@ getObjectDescription(const ObjectAddress *object)
708
713
709
714
trig = (Form_pg_trigger )GETSTRUCT (tup );
710
715
711
- appendStringInfo (& buffer ,"trigger %s" ,
716
+ appendStringInfo (& buffer ,"trigger %s on " ,
712
717
NameStr (trig -> tgname ));
713
- if (OidIsValid (trig -> tgrelid ))
714
- appendStringInfo (& buffer ," on table %s" ,
715
- get_rel_name (trig -> tgrelid ));
718
+ getRelationDescription (& buffer ,trig -> tgrelid );
716
719
717
720
systable_endscan (tgscan );
718
721
heap_close (trigDesc ,AccessShareLock );
@@ -729,3 +732,60 @@ getObjectDescription(const ObjectAddress *object)
729
732
730
733
return buffer .data ;
731
734
}
735
+
736
+ /*
737
+ * subroutine for getObjectDescription: describe a relation
738
+ */
739
+ static void
740
+ getRelationDescription (StringInfo buffer ,Oid relid )
741
+ {
742
+ HeapTuple relTup ;
743
+ Form_pg_class relForm ;
744
+
745
+ relTup = SearchSysCache (RELOID ,
746
+ ObjectIdGetDatum (relid ),
747
+ 0 ,0 ,0 );
748
+ if (!HeapTupleIsValid (relTup ))
749
+ elog (ERROR ,"getObjectDescription: Relation %u does not exist" ,
750
+ relid );
751
+ relForm = (Form_pg_class )GETSTRUCT (relTup );
752
+
753
+ switch (relForm -> relkind )
754
+ {
755
+ case RELKIND_RELATION :
756
+ appendStringInfo (buffer ,"table %s" ,
757
+ NameStr (relForm -> relname ));
758
+ break ;
759
+ case RELKIND_INDEX :
760
+ appendStringInfo (buffer ,"index %s" ,
761
+ NameStr (relForm -> relname ));
762
+ break ;
763
+ case RELKIND_SPECIAL :
764
+ appendStringInfo (buffer ,"special system relation %s" ,
765
+ NameStr (relForm -> relname ));
766
+ break ;
767
+ case RELKIND_SEQUENCE :
768
+ appendStringInfo (buffer ,"sequence %s" ,
769
+ NameStr (relForm -> relname ));
770
+ break ;
771
+ case RELKIND_UNCATALOGED :
772
+ appendStringInfo (buffer ,"uncataloged table %s" ,
773
+ NameStr (relForm -> relname ));
774
+ break ;
775
+ case RELKIND_TOASTVALUE :
776
+ appendStringInfo (buffer ,"toast table %s" ,
777
+ NameStr (relForm -> relname ));
778
+ break ;
779
+ case RELKIND_VIEW :
780
+ appendStringInfo (buffer ,"view %s" ,
781
+ NameStr (relForm -> relname ));
782
+ break ;
783
+ default :
784
+ /* shouldn't get here */
785
+ appendStringInfo (buffer ,"relation %s" ,
786
+ NameStr (relForm -> relname ));
787
+ break ;
788
+ }
789
+
790
+ ReleaseSysCache (relTup );
791
+ }