@@ -356,8 +356,6 @@ chunk_constraint_tuple_found(TupleInfo *ti, void *data)
356
356
return true;
357
357
}
358
358
359
- #define NO_INDEXSCAN -1
360
-
361
359
static int
362
360
chunk_constraint_scan_internal (int indexid ,
363
361
ScanKeyData * scankey ,
@@ -370,7 +368,7 @@ chunk_constraint_scan_internal(int indexid,
370
368
Catalog * catalog = catalog_get ();
371
369
ScannerCtx scanctx = {
372
370
.table = catalog -> tables [CHUNK_CONSTRAINT ].id ,
373
- .scantype = ( indexid == NO_INDEXSCAN ) ? ScannerTypeHeap : ScannerTypeIndex ,
371
+ .index = CATALOG_INDEX ( catalog , CHUNK_CONSTRAINT , indexid ) ,
374
372
.nkeys = nkeys ,
375
373
.scankey = scankey ,
376
374
.data = data ,
@@ -380,9 +378,6 @@ chunk_constraint_scan_internal(int indexid,
380
378
.scandirection = ForwardScanDirection ,
381
379
};
382
380
383
- if (indexid != NO_INDEXSCAN )
384
- scanctx .index = catalog -> tables [CHUNK_CONSTRAINT ].index_ids [indexid ];
385
-
386
381
return scanner_scan (& scanctx );
387
382
}
388
383
@@ -413,6 +408,38 @@ chunk_constraint_scan_by_chunk_id_internal(int32 chunk_id,
413
408
lockmode );
414
409
}
415
410
411
+ static int
412
+ chunk_constraint_scan_by_chunk_id_constraint_name_internal (int32 chunk_id ,
413
+ const char * constraint_name ,
414
+ tuple_found_func tuple_found ,
415
+ tuple_found_func tuple_filter ,
416
+ void * data ,
417
+ LOCKMODE lockmode )
418
+ {
419
+ ScanKeyData scankey [2 ];
420
+
421
+
422
+ ScanKeyInit (& scankey [0 ],
423
+ Anum_chunk_constraint_chunk_id_constraint_name_idx_chunk_id ,
424
+ BTEqualStrategyNumber ,
425
+ F_INT4EQ ,
426
+ Int32GetDatum (chunk_id ));
427
+
428
+ ScanKeyInit (& scankey [1 ],
429
+ Anum_chunk_constraint_chunk_id_constraint_name_idx_constraint_name ,
430
+ BTEqualStrategyNumber ,
431
+ F_NAMEEQ ,
432
+ DirectFunctionCall1 (namein , CStringGetDatum (constraint_name )));
433
+
434
+ return chunk_constraint_scan_internal (CHUNK_CONSTRAINT_CHUNK_ID_CONSTRAINT_NAME_IDX ,
435
+ scankey ,
436
+ 2 ,
437
+ tuple_found ,
438
+ tuple_filter ,
439
+ data ,
440
+ lockmode );
441
+ }
442
+
416
443
/*
417
444
* Scan all the chunk's constraints given its chunk ID.
418
445
*
@@ -625,6 +652,8 @@ typedef struct ConstraintInfo
625
652
{
626
653
const char * hypertable_constraint_name ;
627
654
ChunkConstraints * ccs ;
655
+ bool delete_metadata ;
656
+ bool drop_constraint ;
628
657
} ConstraintInfo ;
629
658
630
659
typedef struct RenameHypertableConstraintInfo
@@ -652,24 +681,29 @@ chunk_constraint_delete_tuple(TupleInfo *ti, void *data)
652
681
ObjectAddress constrobj = {
653
682
.classId = ConstraintRelationId ,
654
683
.objectId = get_relation_constraint_oid (chunk -> table_id ,
655
- NameStr (* DatumGetName (constrname )), false ),
684
+ NameStr (* DatumGetName (constrname )), true ),
656
685
};
657
686
Oid index_relid = get_constraint_index (constrobj .objectId );
658
687
659
688
/* Collect the deleted constraints */
660
- if (NULL != info && NULL != info -> ccs )
689
+ if (NULL != info -> ccs )
661
690
chunk_constraint_tuple_found (ti , info -> ccs );
662
691
663
- /*
664
- * If this is an index constraint, we need to cleanup the index metadata.
665
- * Don't drop the index though, since that will happend when the
666
- * constraint is dropped.
667
- */
668
- if (OidIsValid (index_relid ))
669
- chunk_index_delete (chunk , index_relid , false);
692
+ if (info -> delete_metadata )
693
+ {
694
+ /*
695
+ * If this is an index constraint, we need to cleanup the index
696
+ * metadata. Don't drop the index though, since that will happend when
697
+ * the constraint is dropped.
698
+ */
699
+ if (OidIsValid (index_relid ))
700
+ chunk_index_delete (chunk , index_relid , false);
670
701
671
- catalog_delete (ti -> scanrel , ti -> tuple );
672
- performDeletion (& constrobj , DROP_RESTRICT , 0 );
702
+ catalog_delete (ti -> scanrel , ti -> tuple );
703
+ }
704
+
705
+ if (info -> drop_constraint && OidIsValid (constrobj .objectId ))
706
+ performDeletion (& constrobj , DROP_RESTRICT , 0 );
673
707
674
708
return true;
675
709
}
@@ -695,10 +729,13 @@ hypertable_constraint_tuple_filter(TupleInfo *ti, void *data)
695
729
696
730
int
697
731
chunk_constraint_delete_by_hypertable_constraint_name (int32 chunk_id ,
698
- char * hypertable_constraint_name )
732
+ char * hypertable_constraint_name ,
733
+ bool delete_metadata , bool drop_constraint )
699
734
{
700
735
ConstraintInfo info = {
701
736
.hypertable_constraint_name = hypertable_constraint_name ,
737
+ .delete_metadata = delete_metadata ,
738
+ .drop_constraint = drop_constraint
702
739
};
703
740
704
741
return chunk_constraint_scan_by_chunk_id_internal (chunk_id ,
@@ -708,6 +745,23 @@ chunk_constraint_delete_by_hypertable_constraint_name(int32 chunk_id,
708
745
RowExclusiveLock );
709
746
}
710
747
748
+ int
749
+ chunk_constraint_delete_by_constraint_name (int32 chunk_id , const char * constraint_name ,
750
+ bool delete_metadata , bool drop_constraint )
751
+ {
752
+ ConstraintInfo info = {
753
+ .delete_metadata = delete_metadata ,
754
+ .drop_constraint = drop_constraint
755
+ };
756
+
757
+ return chunk_constraint_scan_by_chunk_id_constraint_name_internal (chunk_id ,
758
+ constraint_name ,
759
+ chunk_constraint_delete_tuple ,
760
+ NULL ,
761
+ & info ,
762
+ RowExclusiveLock );
763
+ }
764
+
711
765
/*
712
766
* Delete all constraints for a chunk. Optionally, collect the deleted constraints.
713
767
*/
@@ -716,6 +770,8 @@ chunk_constraint_delete_by_chunk_id(int32 chunk_id, ChunkConstraints *ccs)
716
770
{
717
771
ConstraintInfo info = {
718
772
.ccs = ccs ,
773
+ .delete_metadata = true,
774
+ .drop_constraint = true,
719
775
};
720
776
721
777
return chunk_constraint_scan_by_chunk_id_internal (chunk_id ,
@@ -728,6 +784,11 @@ chunk_constraint_delete_by_chunk_id(int32 chunk_id, ChunkConstraints *ccs)
728
784
int
729
785
chunk_constraint_delete_by_dimension_slice_id (int32 dimension_slice_id )
730
786
{
787
+ ConstraintInfo info = {
788
+ .delete_metadata = true,
789
+ .drop_constraint = true,
790
+ };
791
+
731
792
ScanKeyData scankey [1 ];
732
793
733
794
ScanKeyInit (& scankey [0 ],
@@ -736,12 +797,12 @@ chunk_constraint_delete_by_dimension_slice_id(int32 dimension_slice_id)
736
797
F_INT4EQ ,
737
798
Int32GetDatum (dimension_slice_id ));
738
799
739
- return chunk_constraint_scan_internal (NO_INDEXSCAN ,
800
+ return chunk_constraint_scan_internal (INVALID_INDEXID ,
740
801
scankey ,
741
802
1 ,
742
803
chunk_constraint_delete_tuple ,
743
804
NULL ,
744
- NULL ,
805
+ & info ,
745
806
RowExclusiveLock );
746
807
}
747
808
0 commit comments