@@ -667,6 +667,12 @@ validate_interval_value(PG_FUNCTION_ARGS)
667
667
}
668
668
669
669
670
+ /*
671
+ * ------------------
672
+ * Helper functions
673
+ * ------------------
674
+ */
675
+
670
676
/*
671
677
* Check if interval is insignificant to avoid infinite loops while adding
672
678
* new partitions
@@ -678,17 +684,19 @@ validate_interval_value(PG_FUNCTION_ARGS)
678
684
static bool
679
685
interval_is_trivial (Oid atttype , Datum interval , Oid interval_type )
680
686
{
687
+ Oid plus_op_func ;
688
+ Datum plus_op_result ;
689
+ Oid plus_op_result_type ;
690
+
681
691
Datum default_value ;
682
- Datum op_result ;
683
- Oid op_result_type ;
684
- Operator op ;
685
- Oid op_func ;
692
+
686
693
FmgrInfo cmp_func ;
687
694
int32 cmp_result ;
688
695
689
696
/*
690
- * Generate default value. For float4 and float8 values we also check
691
- * that they aren't NaN or INF
697
+ * Generate default value.
698
+ *
699
+ * For float4 and float8 values we also check that they aren't NaN or INF.
692
700
*/
693
701
switch (atttype )
694
702
{
@@ -697,69 +705,70 @@ interval_is_trivial(Oid atttype, Datum interval, Oid interval_type)
697
705
case INT8OID :
698
706
default_value = Int16GetDatum (0 );
699
707
break ;
708
+
700
709
case FLOAT4OID :
701
710
{
702
- float4 f = DatumGetFloat4 (interval );
711
+ float4 f = DatumGetFloat4 (interval );
703
712
704
713
if (isnan (f ) || is_infinite (f ))
705
714
elog (ERROR , "invalid floating point interval" );
706
715
default_value = Float4GetDatum (0 );
707
- break ;
708
716
}
717
+ break ;
718
+
709
719
case FLOAT8OID :
710
720
{
711
- float8 f = DatumGetFloat8 (interval );
721
+ float8 f = DatumGetFloat8 (interval );
712
722
713
723
if (isnan (f ) || is_infinite (f ))
714
724
elog (ERROR , "invalid floating point interval" );
715
725
default_value = Float8GetDatum (0 );
716
- break ;
717
726
}
727
+ break ;
728
+
718
729
case NUMERICOID :
719
730
{
720
- Numeric ni = DatumGetNumeric (interval );
721
- Numeric numeric ;
731
+ Numeric ni = DatumGetNumeric (interval ),
732
+ numeric ;
722
733
723
734
/* Test for NaN */
724
735
if (numeric_is_nan (ni ))
725
736
elog (ERROR , "invalid numeric interval" );
726
737
727
738
/* Building default value */
728
- numeric = DatumGetNumeric (DirectFunctionCall3 (numeric_in ,
729
- CStringGetDatum ("0" ),
730
- ObjectIdGetDatum (InvalidOid ),
731
- Int32GetDatum (-1 )));
739
+ numeric = DatumGetNumeric (
740
+ DirectFunctionCall3 (numeric_in ,
741
+ CStringGetDatum ("0" ),
742
+ ObjectIdGetDatum (InvalidOid ),
743
+ Int32GetDatum (-1 )));
732
744
default_value = NumericGetDatum (numeric );
733
- break ;
734
745
}
746
+ break ;
747
+
735
748
case TIMESTAMPOID :
736
749
case TIMESTAMPTZOID :
737
750
default_value = TimestampGetDatum (GetCurrentTimestamp ());
738
751
break ;
752
+
739
753
case DATEOID :
740
754
{
741
- Datum ts = TimestampGetDatum (GetCurrentTimestamp ());
755
+ Datum ts = TimestampGetDatum (GetCurrentTimestamp ());
742
756
743
757
default_value = perform_type_cast (ts , TIMESTAMPTZOID , DATEOID , NULL );
744
- break ;
745
758
}
759
+ break ;
760
+
746
761
default :
747
762
return false;
748
763
}
749
764
750
765
/* Find suitable addition operator for default value and interval */
751
- op = get_binary_operator ("+" , atttype , interval_type );
752
- if (!op )
753
- elog (ERROR , "missing \"+\" operator for types %s and %s" ,
754
- format_type_be (atttype ),
755
- format_type_be (interval_type ));
766
+ extract_op_func_and_ret_type ("+" , atttype , interval_type ,
767
+ & plus_op_func ,
768
+ & plus_op_result_type );
756
769
757
- op_func = oprfuncid (op );
758
- op_result_type = get_operator_ret_type (op );
759
- ReleaseSysCache (op );
760
-
761
- /* Invoke addition operator and get a result*/
762
- op_result = OidFunctionCall2 (op_func , default_value , interval );
770
+ /* Invoke addition operator and get a result */
771
+ plus_op_result = OidFunctionCall2 (plus_op_func , default_value , interval );
763
772
764
773
/*
765
774
* If operator result type isn't the same as original value then
@@ -768,40 +777,38 @@ interval_is_trivial(Oid atttype, Datum interval, Oid interval_type)
768
777
* to a date then we'll get a timestamp which is one second later than
769
778
* original date (obviously). But when we convert it back to a date we will
770
779
* get the same original value meaning that one second interval wouldn't
771
- * change original value anyhow. We should consider such interval
772
- * as trivial
780
+ * change original value anyhow. We should consider such interval as trivial
773
781
*/
774
- if (op_result_type != atttype )
782
+ if (plus_op_result_type != atttype )
775
783
{
776
- op_result = perform_type_cast (op_result , op_result_type , atttype , NULL );
777
- op_result_type = atttype ;
784
+ plus_op_result = perform_type_cast (plus_op_result ,
785
+ plus_op_result_type ,
786
+ atttype , NULL );
787
+ plus_op_result_type = atttype ;
778
788
}
779
789
780
790
/*
781
- * Compare it to the default_value. If they are the same then obviously
782
- * interval is trivial
791
+ * Compare it to the default_value.
792
+ *
793
+ * If they are the same then obviously interval is trivial.
783
794
*/
784
795
fill_type_cmp_fmgr_info (& cmp_func ,
785
796
getBaseType (atttype ),
786
- getBaseType (op_result_type ));
797
+ getBaseType (plus_op_result_type ));
798
+
787
799
cmp_result = DatumGetInt32 (FunctionCall2 (& cmp_func ,
788
800
default_value ,
789
- op_result ));
801
+ plus_op_result ));
790
802
if (cmp_result == 0 )
791
803
return true;
792
- else if (cmp_result > 0 ) /* Negative interval? */
804
+
805
+ else if (cmp_result > 0 ) /* Negative interval? */
793
806
elog (ERROR , "interval must not be negative" );
794
807
808
+ /* Everything is OK */
795
809
return false;
796
810
}
797
811
798
-
799
- /*
800
- * ------------------
801
- * Helper functions
802
- * ------------------
803
- */
804
-
805
812
/*
806
813
* Drop old partition constraint and create
807
814
* a new one with specified boundaries
0 commit comments