@@ -67,10 +67,13 @@ chunk_constraints_expand(ChunkConstraints *ccs, int16 new_capacity)
67
67
ccs -> constraints = repalloc (ccs -> constraints , CHUNK_CONSTRAINTS_SIZE (new_capacity ));
68
68
}
69
69
70
-
71
70
static
72
71
void
73
- chunk_constraint_choose_name (Name dst , bool is_dimension , int32 dimension_slice_id , const char * hypertable_constraint_name , int32 chunk_id )
72
+ chunk_constraint_choose_name (Name dst ,
73
+ bool is_dimension ,
74
+ int32 dimension_slice_id ,
75
+ const char * hypertable_constraint_name ,
76
+ int32 chunk_id )
74
77
{
75
78
if (is_dimension )
76
79
{
@@ -216,7 +219,8 @@ chunk_constraints_add_from_tuple(ChunkConstraints *ccs, TupleInfo *ti)
216
219
heap_deform_tuple (ti -> tuple , ti -> desc , values , nulls );
217
220
218
221
constraint_name = DatumGetName (values [Anum_chunk_constraint_constraint_name - 1 ]);
219
- if (heap_attisnull (ti -> tuple , Anum_chunk_constraint_dimension_slice_id ))
222
+
223
+ if (nulls [Anum_chunk_constraint_dimension_slice_id - 1 ])
220
224
{
221
225
dimension_slice_id = 0 ;
222
226
hypertable_constraint_name = DatumGetName (values [Anum_chunk_constraint_hypertable_constraint_name - 1 ]);
@@ -332,6 +336,9 @@ chunk_constraints_create(ChunkConstraints *ccs,
332
336
hypertable_id );
333
337
}
334
338
339
+ /*
340
+ * Scan filter function for only getting dimension constraints.
341
+ */
335
342
static bool
336
343
chunk_constraint_for_dimension_slice (TupleInfo * ti , void * data )
337
344
{
@@ -343,7 +350,8 @@ chunk_constraint_tuple_found(TupleInfo *ti, void *data)
343
350
{
344
351
ChunkConstraints * ccs = data ;
345
352
346
- chunk_constraints_add_from_tuple (ccs , ti );
353
+ if (NULL != ccs )
354
+ chunk_constraints_add_from_tuple (ccs , ti );
347
355
348
356
return true;
349
357
}
@@ -469,34 +477,58 @@ chunk_constraint_dimension_slice_id_tuple_found(TupleInfo *ti, void *data)
469
477
return true;
470
478
}
471
479
472
- /*
473
- * Scan for all chunk constraints that match the given slice ID. The chunk
474
- * constraints are saved in the chunk scan context.
475
- */
476
- int
477
- chunk_constraint_scan_by_dimension_slice_id (DimensionSlice * slice , ChunkScanCtx * ctx )
480
+ static int
481
+ scan_by_dimension_slice_id (int32 dimension_slice_id ,
482
+ tuple_found_func tuple_found ,
483
+ void * data )
478
484
{
479
485
ScanKeyData scankey [1 ];
480
- ChunkConstraintScanData data = {
481
- .scanctx = ctx ,
482
- .slice = slice ,
483
- };
484
486
485
487
ScanKeyInit (& scankey [0 ],
486
488
Anum_chunk_constraint_chunk_id_dimension_slice_id_idx_dimension_slice_id ,
487
489
BTEqualStrategyNumber ,
488
490
F_INT4EQ ,
489
- Int32GetDatum (slice -> fd . id ));
491
+ Int32GetDatum (dimension_slice_id ));
490
492
491
493
return chunk_constraint_scan_internal (CHUNK_CONSTRAINT_CHUNK_ID_DIMENSION_SLICE_ID_IDX ,
492
494
scankey ,
493
495
1 ,
494
- chunk_constraint_dimension_slice_id_tuple_found ,
496
+ tuple_found ,
495
497
chunk_constraint_for_dimension_slice ,
496
- & data ,
498
+ data ,
497
499
AccessShareLock );
498
500
}
499
501
502
+ /*
503
+ * Scan for all chunk constraints that match the given slice ID. The chunk
504
+ * constraints are saved in the chunk scan context.
505
+ */
506
+ int
507
+ chunk_constraint_scan_by_dimension_slice (DimensionSlice * slice , ChunkScanCtx * ctx )
508
+ {
509
+ ChunkConstraintScanData data = {
510
+ .scanctx = ctx ,
511
+ .slice = slice ,
512
+ };
513
+
514
+ return scan_by_dimension_slice_id (slice -> fd .id ,
515
+ chunk_constraint_dimension_slice_id_tuple_found ,
516
+ & data );
517
+ }
518
+
519
+ /*
520
+ * Scan for chunk constraints given a dimension slice ID.
521
+ *
522
+ * Optionally, collect all chunk constraints if ChunkConstraints is non-NULL.
523
+ */
524
+ int
525
+ chunk_constraint_scan_by_dimension_slice_id (int32 dimension_slice_id , ChunkConstraints * ccs )
526
+ {
527
+ return scan_by_dimension_slice_id (dimension_slice_id ,
528
+ chunk_constraint_tuple_found ,
529
+ ccs );
530
+ }
531
+
500
532
static bool
501
533
chunk_constraint_need_on_chunk (Form_pg_constraint conform )
502
534
{
@@ -592,6 +624,7 @@ chunk_constraint_create_on_chunk(Chunk *chunk, Oid constraint_oid)
592
624
typedef struct ConstraintInfo
593
625
{
594
626
const char * hypertable_constraint_name ;
627
+ ChunkConstraints * ccs ;
595
628
} ConstraintInfo ;
596
629
597
630
typedef struct RenameHypertableConstraintInfo
@@ -601,9 +634,15 @@ typedef struct RenameHypertableConstraintInfo
601
634
} RenameHypertableConstraintInfo ;
602
635
603
636
637
+ /*
638
+ * Delete a chunk constraint tuple.
639
+ *
640
+ * Optionally, the data argument is a ConstraintInfo.
641
+ */
604
642
static bool
605
643
chunk_constraint_delete_tuple (TupleInfo * ti , void * data )
606
644
{
645
+ ConstraintInfo * info = data ;
607
646
bool isnull ;
608
647
Datum constrname = heap_getattr (ti -> tuple , Anum_chunk_constraint_constraint_name ,
609
648
ti -> desc , & isnull );
@@ -617,6 +656,10 @@ chunk_constraint_delete_tuple(TupleInfo *ti, void *data)
617
656
};
618
657
Oid index_relid = get_constraint_index (constrobj .objectId );
619
658
659
+ /* Collect the deleted constraints */
660
+ if (NULL != info && NULL != info -> ccs )
661
+ chunk_constraint_tuple_found (ti , info -> ccs );
662
+
620
663
/*
621
664
* If this is an index constraint, we need to cleanup the index metadata.
622
665
* Don't drop the index though, since that will happend when the
@@ -665,13 +708,20 @@ chunk_constraint_delete_by_hypertable_constraint_name(int32 chunk_id,
665
708
RowExclusiveLock );
666
709
}
667
710
711
+ /*
712
+ * Delete all constraints for a chunk. Optionally, collect the deleted constraints.
713
+ */
668
714
int
669
- chunk_constraint_delete_by_chunk_id (int32 chunk_id )
715
+ chunk_constraint_delete_by_chunk_id (int32 chunk_id , ChunkConstraints * ccs )
670
716
{
717
+ ConstraintInfo info = {
718
+ .ccs = ccs ,
719
+ };
720
+
671
721
return chunk_constraint_scan_by_chunk_id_internal (chunk_id ,
672
722
chunk_constraint_delete_tuple ,
673
723
NULL ,
674
- NULL ,
724
+ & info ,
675
725
RowExclusiveLock );
676
726
}
677
727
0 commit comments