11
11
#include <catalog/pg_constraint.h>
12
12
#include <catalog/pg_constraint_fn.h>
13
13
#include <catalog/objectaddress.h>
14
+ #include <commands/tablecmds.h>
14
15
#include <catalog/dependency.h>
15
16
#include <funcapi.h>
17
+ #include <nodes/makefuncs.h>
16
18
17
19
#include "scanner.h"
18
20
#include "chunk_constraint.h"
@@ -65,6 +67,36 @@ chunk_constraints_expand(ChunkConstraints *ccs, int16 new_capacity)
65
67
ccs -> constraints = repalloc (ccs -> constraints , CHUNK_CONSTRAINTS_SIZE (new_capacity ));
66
68
}
67
69
70
+
71
+ static
72
+ void
73
+ chunk_constraint_choose_name (Name dst , bool is_dimension , int32 dimension_slice_id , const char * hypertable_constraint_name , int32 chunk_id )
74
+ {
75
+ if (is_dimension )
76
+ {
77
+ snprintf (NameStr (* dst ), NAMEDATALEN , "constraint_%d" ,
78
+ dimension_slice_id );
79
+ }
80
+ else
81
+ {
82
+ char constrname [100 ];
83
+ CatalogSecurityContext sec_ctx ;
84
+
85
+ Assert (hypertable_constraint_name != NULL );
86
+
87
+ catalog_become_owner (catalog_get (), & sec_ctx );
88
+ snprintf (constrname ,
89
+ 100 ,
90
+ "%d_" INT64_FORMAT "_%s" ,
91
+ chunk_id ,
92
+ catalog_table_next_seq_id (catalog_get (), CHUNK_CONSTRAINT ),
93
+ hypertable_constraint_name );
94
+ catalog_restore_user (& sec_ctx );
95
+
96
+ namestrcpy (dst , constrname );
97
+ }
98
+ }
99
+
68
100
static ChunkConstraint *
69
101
chunk_constraints_add (ChunkConstraints * ccs ,
70
102
int32 chunk_id ,
@@ -81,30 +113,14 @@ chunk_constraints_add(ChunkConstraints *ccs,
81
113
82
114
if (NULL == constraint_name )
83
115
{
116
+ chunk_constraint_choose_name (& cc -> fd .constraint_name ,
117
+ is_dimension_constraint (cc ),
118
+ cc -> fd .dimension_slice_id ,
119
+ hypertable_constraint_name ,
120
+ cc -> fd .chunk_id );
121
+
84
122
if (is_dimension_constraint (cc ))
85
- {
86
- snprintf (NameStr (cc -> fd .constraint_name ),
87
- NAMEDATALEN ,
88
- "constraint_%d" ,
89
- cc -> fd .dimension_slice_id );
90
123
namestrcpy (& cc -> fd .hypertable_constraint_name , "" );
91
- }
92
- else if (NULL != hypertable_constraint_name )
93
- {
94
- CatalogSecurityContext sec_ctx ;
95
- char constrname [100 ];
96
-
97
- catalog_become_owner (catalog_get (), & sec_ctx );
98
- snprintf (constrname ,
99
- 100 ,
100
- "%d_" INT64_FORMAT "_%s" ,
101
- cc -> fd .chunk_id ,
102
- catalog_table_next_seq_id (catalog_get (), CHUNK_CONSTRAINT ),
103
- hypertable_constraint_name );
104
- catalog_restore_user (& sec_ctx );
105
-
106
- namestrcpy (& cc -> fd .constraint_name , constrname );
107
- }
108
124
}
109
125
else
110
126
namestrcpy (& cc -> fd .constraint_name , constraint_name );
@@ -189,22 +205,33 @@ chunk_constraint_insert(ChunkConstraint *constraint)
189
205
190
206
191
207
static ChunkConstraint *
192
- chunk_constraints_add_from_tuple (ChunkConstraints * ccs , HeapTuple tuple )
208
+ chunk_constraints_add_from_tuple (ChunkConstraints * ccs , TupleInfo * ti )
193
209
{
194
- FormData_chunk_constraint * form = (FormData_chunk_constraint * ) GETSTRUCT (tuple );
195
- int32 dimension_slice_id = form -> dimension_slice_id ;
196
- const char * hypertable_constraint_name = NameStr (form -> hypertable_constraint_name );
210
+ bool nulls [Natts_chunk_constraint ];
211
+ Datum values [Natts_chunk_constraint ];
212
+ int32 dimension_slice_id ;
213
+ Name constraint_name ;
214
+ Name hypertable_constraint_name ;
197
215
198
- if (heap_attisnull (tuple , Anum_chunk_constraint_dimension_slice_id ))
216
+ heap_deform_tuple (ti -> tuple , ti -> desc , values , nulls );
217
+
218
+ constraint_name = DatumGetName (values [Anum_chunk_constraint_constraint_name - 1 ]);
219
+ if (heap_attisnull (ti -> tuple , Anum_chunk_constraint_dimension_slice_id ))
220
+ {
199
221
dimension_slice_id = 0 ;
222
+ hypertable_constraint_name = DatumGetName (values [Anum_chunk_constraint_hypertable_constraint_name - 1 ]);
223
+ }
200
224
else
201
- hypertable_constraint_name = "" ;
225
+ {
226
+ dimension_slice_id = DatumGetInt32 (values [Anum_chunk_constraint_dimension_slice_id - 1 ]);
227
+ hypertable_constraint_name = DatumGetName (DirectFunctionCall1 (namein , CStringGetDatum ("" )));
228
+ }
202
229
203
230
return chunk_constraints_add (ccs ,
204
- form -> chunk_id ,
231
+ DatumGetInt32 ( values [ Anum_chunk_constraint_chunk_id - 1 ]) ,
205
232
dimension_slice_id ,
206
- NameStr (form -> constraint_name ),
207
- hypertable_constraint_name );
233
+ NameStr (* constraint_name ),
234
+ NameStr ( * hypertable_constraint_name ) );
208
235
}
209
236
210
237
/*
@@ -316,7 +343,7 @@ chunk_constraint_tuple_found(TupleInfo *ti, void *data)
316
343
{
317
344
ChunkConstraints * ccs = data ;
318
345
319
- chunk_constraints_add_from_tuple (ccs , ti -> tuple );
346
+ chunk_constraints_add_from_tuple (ccs , ti );
320
347
321
348
return true;
322
349
}
@@ -408,7 +435,7 @@ chunk_constraint_dimension_slice_id_tuple_found(TupleInfo *ti, void *data)
408
435
else
409
436
chunk = entry -> chunk ;
410
437
411
- chunk_constraints_add_from_tuple (chunk -> constraints , ti -> tuple );
438
+ chunk_constraints_add_from_tuple (chunk -> constraints , ti );
412
439
413
440
hypercube_add_slice (chunk -> cube , ccsd -> slice );
414
441
@@ -546,23 +573,31 @@ chunk_constraint_create_on_chunk(Chunk *chunk, Oid constraint_oid)
546
573
chunk -> fd .hypertable_id );
547
574
}
548
575
549
- typedef struct DeleteConstraintInfo
576
+ typedef struct ConstraintInfo
550
577
{
551
- int32 chunk_id ;
552
- Oid chunk_oid ;
553
- char * hypertable_constraint_name ;
554
- } DeleteConstraintInfo ;
578
+ const char * hypertable_constraint_name ;
579
+ } ConstraintInfo ;
580
+
581
+ typedef struct RenameHypertableConstraintInfo
582
+ {
583
+ ConstraintInfo base ;
584
+ const char * newname ;
585
+ } RenameHypertableConstraintInfo ;
586
+
555
587
556
588
static bool
557
589
chunk_constraint_delete_tuple (TupleInfo * ti , void * data )
558
590
{
559
- DeleteConstraintInfo * info = data ;
560
591
bool isnull ;
561
592
Datum constrname = heap_getattr (ti -> tuple , Anum_chunk_constraint_constraint_name ,
562
593
ti -> desc , & isnull );
594
+ int32 chunk_id = DatumGetInt32 (heap_getattr (ti -> tuple , Anum_chunk_constraint_chunk_id ,
595
+ ti -> desc , & isnull ));
596
+ Chunk * chunk = chunk_get_by_id (chunk_id , 0 , true);
597
+
563
598
ObjectAddress constrobj = {
564
599
.classId = ConstraintRelationId ,
565
- .objectId = get_relation_constraint_oid (info -> chunk_oid ,
600
+ .objectId = get_relation_constraint_oid (chunk -> table_id ,
566
601
NameStr (* DatumGetName (constrname )), false),
567
602
};
568
603
@@ -575,22 +610,19 @@ chunk_constraint_delete_tuple(TupleInfo *ti, void *data)
575
610
static bool
576
611
hypertable_constraint_tuple_filter (TupleInfo * ti , void * data )
577
612
{
578
- DeleteConstraintInfo * info = data ;
613
+ ConstraintInfo * info = data ;
579
614
bool nulls [Natts_chunk_constraint ];
580
615
Datum values [Natts_chunk_constraint ];
581
- int32 chunk_id ;
582
616
const char * constrname ;
583
617
584
618
heap_deform_tuple (ti -> tuple , ti -> desc , values , nulls );
585
619
586
620
if (nulls [Anum_chunk_constraint_hypertable_constraint_name - 1 ])
587
621
return false;
588
622
589
- chunk_id = DatumGetInt32 (values [Anum_chunk_constraint_chunk_id - 1 ]);
590
623
constrname = NameStr (* DatumGetName (values [Anum_chunk_constraint_hypertable_constraint_name - 1 ]));
591
624
592
- return info -> chunk_id == chunk_id &&
593
- NULL != info -> hypertable_constraint_name &&
625
+ return NULL != info -> hypertable_constraint_name &&
594
626
strcmp (info -> hypertable_constraint_name , constrname ) == 0 ;
595
627
}
596
628
@@ -599,9 +631,7 @@ chunk_constraint_delete_by_hypertable_constraint_name(int32 chunk_id,
599
631
Oid chunk_oid ,
600
632
char * hypertable_constraint_name )
601
633
{
602
- DeleteConstraintInfo info = {
603
- .chunk_id = chunk_id ,
604
- .chunk_oid = chunk_oid ,
634
+ ConstraintInfo info = {
605
635
.hypertable_constraint_name = hypertable_constraint_name ,
606
636
};
607
637
@@ -615,16 +645,10 @@ chunk_constraint_delete_by_hypertable_constraint_name(int32 chunk_id,
615
645
int
616
646
chunk_constraint_delete_by_chunk_id (int32 chunk_id , Oid chunk_oid )
617
647
{
618
- DeleteConstraintInfo info = {
619
- .chunk_id = chunk_id ,
620
- .chunk_oid = chunk_oid ,
621
- .hypertable_constraint_name = NULL ,
622
- };
623
-
624
648
return chunk_constraint_scan_by_chunk_id_internal (chunk_id ,
625
649
chunk_constraint_delete_tuple ,
626
650
NULL ,
627
- & info ,
651
+ NULL ,
628
652
RowExclusiveLock );
629
653
}
630
654
@@ -640,3 +664,74 @@ chunk_constraint_recreate(ChunkConstraint *cc, Oid chunk_oid)
640
664
performDeletion (& constrobj , DROP_RESTRICT , 0 );
641
665
chunk_constraint_create_on_table (cc , chunk_oid );
642
666
}
667
+
668
+ static void
669
+ chunk_constraint_rename_on_chunk_table (int32 chunk_id , char * old_name , char * new_name )
670
+ {
671
+ Chunk * chunk = chunk_get_by_id (chunk_id , 0 , true);
672
+ RenameStmt rename = {
673
+ .renameType = OBJECT_TABCONSTRAINT ,
674
+ .relation = makeRangeVar (NameStr (chunk -> fd .schema_name ), NameStr (chunk -> fd .table_name ), 0 ),
675
+ .subname = old_name ,
676
+ .newname = new_name ,
677
+ };
678
+
679
+ RenameConstraint (& rename );
680
+ }
681
+
682
+ static bool
683
+ chunk_constraint_rename_hypertable_tuple (TupleInfo * ti , void * data )
684
+ {
685
+ RenameHypertableConstraintInfo * info = data ;
686
+
687
+ bool nulls [Natts_chunk_constraint ];
688
+ Datum values [Natts_chunk_constraint ];
689
+ bool repl [Natts_chunk_constraint ] = {false};
690
+
691
+ HeapTuple tuple ;
692
+ NameData new_hypertable_constraint_name ;
693
+ NameData new_chunk_constraint_name ;
694
+ Name old_chunk_constraint_name ;
695
+ int32 chunk_id ;
696
+
697
+ heap_deform_tuple (ti -> tuple , ti -> desc , values , nulls );
698
+
699
+ chunk_id = DatumGetInt32 (values [Anum_chunk_constraint_chunk_id - 1 ]);
700
+ namestrcpy (& new_hypertable_constraint_name , info -> newname );
701
+ chunk_constraint_choose_name (& new_chunk_constraint_name ,
702
+ false,
703
+ 0 ,
704
+ info -> newname ,
705
+ chunk_id );
706
+
707
+ values [Anum_chunk_constraint_hypertable_constraint_name - 1 ] = NameGetDatum (& new_hypertable_constraint_name );
708
+ repl [Anum_chunk_constraint_hypertable_constraint_name - 1 ] = true;
709
+ old_chunk_constraint_name = DatumGetName (values [Anum_chunk_constraint_constraint_name - 1 ]);
710
+ values [Anum_chunk_constraint_constraint_name - 1 ] = NameGetDatum (& new_chunk_constraint_name );
711
+ repl [Anum_chunk_constraint_constraint_name - 1 ] = true;
712
+
713
+ chunk_constraint_rename_on_chunk_table (chunk_id , NameStr (* old_chunk_constraint_name ), NameStr (new_chunk_constraint_name ));
714
+
715
+ tuple = heap_modify_tuple (ti -> tuple , ti -> desc , values , nulls , repl );
716
+ catalog_update (ti -> scanrel , tuple );
717
+ heap_freetuple (tuple );
718
+
719
+ return true;
720
+ }
721
+
722
+ int
723
+ chunk_constraint_rename_hypertable_constraint (int32 chunk_id , const char * oldname , const char * newname )
724
+ {
725
+ RenameHypertableConstraintInfo info = {
726
+ .base = {
727
+ .hypertable_constraint_name = oldname ,
728
+ },
729
+ .newname = newname ,
730
+ };
731
+
732
+ return chunk_constraint_scan_by_chunk_id_internal (chunk_id ,
733
+ chunk_constraint_rename_hypertable_tuple ,
734
+ hypertable_constraint_tuple_filter ,
735
+ & info ,
736
+ RowExclusiveLock );
737
+ }
0 commit comments