@@ -495,9 +495,9 @@ CreateStatistics(CreateStatsStmt *stmt)
495495 values [Anum_pg_statistic_ext_stxrelid - 1 ] = ObjectIdGetDatum (relid );
496496 values [Anum_pg_statistic_ext_stxname - 1 ] = NameGetDatum (& stxname );
497497 values [Anum_pg_statistic_ext_stxnamespace - 1 ] = ObjectIdGetDatum (namespaceId );
498- values [Anum_pg_statistic_ext_stxstattarget - 1 ] = Int16GetDatum (-1 );
499498 values [Anum_pg_statistic_ext_stxowner - 1 ] = ObjectIdGetDatum (stxowner );
500499 values [Anum_pg_statistic_ext_stxkeys - 1 ] = PointerGetDatum (stxkeys );
500+ nulls [Anum_pg_statistic_ext_stxstattarget - 1 ] = true;
501501 values [Anum_pg_statistic_ext_stxkind - 1 ] = PointerGetDatum (stxkind );
502502
503503 values [Anum_pg_statistic_ext_stxexprs - 1 ] = exprsDatum ;
@@ -606,23 +606,36 @@ AlterStatistics(AlterStatsStmt *stmt)
606606 bool repl_null [Natts_pg_statistic_ext ];
607607 bool repl_repl [Natts_pg_statistic_ext ];
608608 ObjectAddress address ;
609- int newtarget = stmt -> stxstattarget ;
609+ int newtarget ;
610+ bool newtarget_default ;
610611
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 )
613614 {
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;
618617 }
619- else if (newtarget > MAX_STATISTICS_TARGET )
618+ else
619+ newtarget_default = true;
620+
621+ if (!newtarget_default )
620622 {
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+ }
626639 }
627640
628641 /* lookup OID of the statistics object */
@@ -673,7 +686,10 @@ AlterStatistics(AlterStatsStmt *stmt)
673686
674687 /* replace the stxstattarget column */
675688 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;
677693
678694 newtup = heap_modify_tuple (oldtup , RelationGetDescr (rel ),
679695 repl_val , repl_null , repl_repl );
0 commit comments