@@ -495,9 +495,9 @@ CreateStatistics(CreateStatsStmt *stmt)
495
495
values [Anum_pg_statistic_ext_stxrelid - 1 ] = ObjectIdGetDatum (relid );
496
496
values [Anum_pg_statistic_ext_stxname - 1 ] = NameGetDatum (& stxname );
497
497
values [Anum_pg_statistic_ext_stxnamespace - 1 ] = ObjectIdGetDatum (namespaceId );
498
- values [Anum_pg_statistic_ext_stxstattarget - 1 ] = Int16GetDatum (-1 );
499
498
values [Anum_pg_statistic_ext_stxowner - 1 ] = ObjectIdGetDatum (stxowner );
500
499
values [Anum_pg_statistic_ext_stxkeys - 1 ] = PointerGetDatum (stxkeys );
500
+ nulls [Anum_pg_statistic_ext_stxstattarget - 1 ] = true;
501
501
values [Anum_pg_statistic_ext_stxkind - 1 ] = PointerGetDatum (stxkind );
502
502
503
503
values [Anum_pg_statistic_ext_stxexprs - 1 ] = exprsDatum ;
@@ -606,23 +606,36 @@ AlterStatistics(AlterStatsStmt *stmt)
606
606
bool repl_null [Natts_pg_statistic_ext ];
607
607
bool repl_repl [Natts_pg_statistic_ext ];
608
608
ObjectAddress address ;
609
- int newtarget = stmt -> stxstattarget ;
609
+ int newtarget ;
610
+ bool newtarget_default ;
610
611
611
- /* Limit statistics target to a sane range */
612
- if (newtarget < -1 )
612
+ /* -1 was used in previous versions for the default setting */
613
+ if (stmt -> stxstattarget && intVal ( stmt -> stxstattarget ) != -1 )
613
614
{
614
- ereport (ERROR ,
615
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
616
- errmsg ("statistics target %d is too low" ,
617
- newtarget )));
615
+ newtarget = intVal (stmt -> stxstattarget );
616
+ newtarget_default = false;
618
617
}
619
- else if (newtarget > MAX_STATISTICS_TARGET )
618
+ else
619
+ newtarget_default = true;
620
+
621
+ if (!newtarget_default )
620
622
{
621
- newtarget = MAX_STATISTICS_TARGET ;
622
- ereport (WARNING ,
623
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
624
- errmsg ("lowering statistics target to %d" ,
625
- newtarget )));
623
+ /* Limit statistics target to a sane range */
624
+ if (newtarget < 0 )
625
+ {
626
+ ereport (ERROR ,
627
+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
628
+ errmsg ("statistics target %d is too low" ,
629
+ newtarget )));
630
+ }
631
+ else if (newtarget > MAX_STATISTICS_TARGET )
632
+ {
633
+ newtarget = MAX_STATISTICS_TARGET ;
634
+ ereport (WARNING ,
635
+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
636
+ errmsg ("lowering statistics target to %d" ,
637
+ newtarget )));
638
+ }
626
639
}
627
640
628
641
/* lookup OID of the statistics object */
@@ -673,7 +686,10 @@ AlterStatistics(AlterStatsStmt *stmt)
673
686
674
687
/* replace the stxstattarget column */
675
688
repl_repl [Anum_pg_statistic_ext_stxstattarget - 1 ] = true;
676
- repl_val [Anum_pg_statistic_ext_stxstattarget - 1 ] = Int16GetDatum (newtarget );
689
+ if (!newtarget_default )
690
+ repl_val [Anum_pg_statistic_ext_stxstattarget - 1 ] = Int16GetDatum (newtarget );
691
+ else
692
+ repl_null [Anum_pg_statistic_ext_stxstattarget - 1 ] = true;
677
693
678
694
newtup = heap_modify_tuple (oldtup , RelationGetDescr (rel ),
679
695
repl_val , repl_null , repl_repl );
0 commit comments