From 093f080d545594b6d3ef902695054954f0af397c Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@gmail.com>
Date: Fri, 23 Jan 2026 00:48:44 -0500
Subject: [PATCH v30 5/6] Add tests for multirange stats

---
 src/test/regress/expected/stats_import.out | 270 ++++++++++++++-------
 src/test/regress/sql/stats_import.sql      |  73 +++++-
 2 files changed, 248 insertions(+), 95 deletions(-)

diff --git a/src/test/regress/expected/stats_import.out b/src/test/regress/expected/stats_import.out
index 954812d2dee..bafbdba7651 100644
--- a/src/test/regress/expected/stats_import.out
+++ b/src/test/regress/expected/stats_import.out
@@ -126,6 +126,63 @@ WHERE relation = 'stats_import.test_i'::regclass AND
 (1 row)
 
 COMMIT;
+-- test multiranges and extended stats therein
+CREATE TABLE stats_import.test_mr(
+    id INTEGER PRIMARY KEY,
+    name text,
+    mrange int4multirange
+) WITH (autovacuum_enabled = false);
+INSERT INTO stats_import.test_mr
+VALUES
+  (1, 'red', '{[1,3),[5,9),[20,30)}'::int4multirange),
+  (2, 'red', '{[11,13),[15,19),[20,30)}'::int4multirange),
+  (3, 'red', '{[21,23),[25,29),[120,130)}'::int4multirange);
+SELECT pg_catalog.pg_restore_attribute_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test_mr',
+  'attname', 'mrange',
+  'inherited', false,
+  'range_length_histogram', '{19,29,109}'::text,
+  'range_empty_frac', '0'::real,
+  'range_bounds_histogram', '{"[1,30)","[11,30)","[21,130)"}'::text
+);
+ pg_restore_attribute_stats 
+----------------------------
+ t
+(1 row)
+
+CREATE STATISTICS stats_import.test_mr_stat
+  ON name, mrange, ( mrange + '{[10000,10200)}'::int4multirange)
+  FROM stats_import.test_mr;
+-- ok
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test_mr',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_mr_stat',
+  'inherited', false,
+  'n_distinct', '[{"attributes": [2, 3], "ndistinct": 3},
+                  {"attributes": [2, -1], "ndistinct": 3},
+                  {"attributes": [3, -1], "ndistinct": 3},
+                  {"attributes": [2, 3, -1], "ndistinct": 3}]'::pg_catalog.pg_ndistinct,
+  'dependencies', '[{"attributes": [3], "dependency": 2, "degree": 1.000000},
+                    {"attributes": [3], "dependency": -1, "degree": 1.000000},
+                    {"attributes": [-1], "dependency": 2, "degree": 1.000000},
+                    {"attributes": [-1], "dependency": 3, "degree": 1.000000},
+                    {"attributes": [2, 3], "dependency": -1, "degree": 1.000000},
+                    {"attributes": [2, -1], "dependency": 3, "degree": 1.000000},
+                    {"attributes": [3, -1], "dependency": 2, "degree": 1.000000}]'::pg_catalog.pg_dependencies,
+  'most_common_vals', '{{red,"{[1,3),[5,9),[20,30)}","{[1,3),[5,9),[20,30),[10000,10200)}"},{red,"{[11,13),[15,19),[20,30)}","{[11,13),[15,19),[20,30),[10000,10200)}"},{red,"{[21,23),[25,29),[120,130)}","{[21,23),[25,29),[120,130),[10000,10200)}"}}'::text[],
+  'most_common_val_nulls', '{{f,f,f},{f,f,f},{f,f,f}}'::boolean[],
+  'most_common_freqs', '{0.3333333333333333,0.3333333333333333,0.3333333333333333}'::double precision[],
+  'most_common_base_freqs', '{0.1111111111111111,0.1111111111111111,0.1111111111111111}'::double precision[],
+  'exprs', '{{0,60,-1,NULL,NULL,NULL,NULL,NULL,NULL,NULL}}'::text[]
+);
+ pg_restore_extended_stats 
+---------------------------
+ t
+(1 row)
+
 --  relpages may be -1 for partitioned tables
 CREATE TABLE stats_import.part_parent ( i integer ) PARTITION BY RANGE(i);
 CREATE TABLE stats_import.part_child_1
@@ -2155,6 +2212,22 @@ WARNING:  Could not parse MCV element "(POOHHA,1.1,ONE,01-01-2001,"{""xkey"": ""
  f
 (1 row)
 
+-- ok
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test_clone',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_clone',
+  'inherited', false,
+  'most_common_vals', '{{four,NULL,0,NULL},{one,"(4,1.1,ONE,01-01-2001,\"{\"\"xkey\"\": \"\"xval\"\"}\")",1,2},{tre,"(3,3.3,TRE,03-03-2003,)",-1,3},{two,"(2,2.2,TWO,02-02-2002,\"[true, 4, \"\"six\"\"]\")",1,2}}'::text[],
+  'most_common_val_nulls', '{{f,t,f,t},{f,f,f,f},{f,f,f,f},{f,f,f,f}}'::boolean[],
+  'most_common_freqs', '{0.25,0.25,0.25,0.25}'::double precision[],
+  'most_common_base_freqs', '{0.00390625,0.015625,0.00390625,0.015625}'::double precision[]);
+ pg_restore_extended_stats 
+---------------------------
+ t
+(1 row)
+
 -- Set n_distinct using at attnum (1) that is not in the statistics
 -- object
 SELECT pg_catalog.pg_restore_extended_stats(
@@ -2277,28 +2350,32 @@ REVOKE ALL ON SCHEMA stats_import FROM regress_test_extstat_restore;
 DROP ROLE regress_test_extstat_restore;
 SELECT replace(e.n_distinct,   '}, ', E'},\n') AS n_distinct,
   e.dependencies,
-  e.most_common_vals, e.most_common_val_nulls,
+  replace(e.most_common_vals::text, '},{', E'},\n{ ') AS most_common_vals,
+  e.most_common_val_nulls,
   e.most_common_freqs, e.most_common_base_freqs
 FROM pg_stats_ext AS e
 WHERE e.statistics_schemaname = 'stats_import'
 AND e.statistics_name = 'test_stat_clone'
 AND e.inherited = false
 \gx
--[ RECORD 1 ]----------+------------------------------------------------
-n_distinct             | [{"attributes": [2, 3], "ndistinct": 4},       +
-                       | {"attributes": [2, -1], "ndistinct": 4},       +
-                       | {"attributes": [3, -1], "ndistinct": 4},       +
-                       | {"attributes": [3, -2], "ndistinct": 4},       +
-                       | {"attributes": [-1, -2], "ndistinct": 3},      +
-                       | {"attributes": [2, 3, -1], "ndistinct": 4},    +
-                       | {"attributes": [2, 3, -2], "ndistinct": 4},    +
-                       | {"attributes": [2, -1, -2], "ndistinct": 4},   +
+-[ RECORD 1 ]----------+----------------------------------------------------------------------
+n_distinct             | [{"attributes": [2, 3], "ndistinct": 4},                             +
+                       | {"attributes": [2, -1], "ndistinct": 4},                             +
+                       | {"attributes": [3, -1], "ndistinct": 4},                             +
+                       | {"attributes": [3, -2], "ndistinct": 4},                             +
+                       | {"attributes": [-1, -2], "ndistinct": 3},                            +
+                       | {"attributes": [2, 3, -1], "ndistinct": 4},                          +
+                       | {"attributes": [2, 3, -2], "ndistinct": 4},                          +
+                       | {"attributes": [2, -1, -2], "ndistinct": 4},                         +
                        | {"attributes": [2, 3, -1, -2], "ndistinct": 4}]
 dependencies           | 
-most_common_vals       | 
-most_common_val_nulls  | 
-most_common_freqs      | 
-most_common_base_freqs | 
+most_common_vals       | {{four,NULL,0,NULL},                                                 +
+                       | { one,"(4,1.1,ONE,01-01-2001,\"{\"\"xkey\"\": \"\"xval\"\"}\")",1,2},+
+                       | { tre,"(3,3.3,TRE,03-03-2003,)",-1,3},                               +
+                       | { two,"(2,2.2,TWO,02-02-2002,\"[true, 4, \"\"six\"\"]\")",1,2}}
+most_common_val_nulls  | {{f,t,f,t},{f,f,f,f},{f,f,f,f},{f,f,f,f}}
+most_common_freqs      | {0.25,0.25,0.25,0.25}
+most_common_base_freqs | {0.00390625,0.015625,0.00390625,0.015625}
 
 -- set dependencies using at attnum (1) that is not in the statistics object
 SELECT pg_catalog.pg_restore_extended_stats(
@@ -2382,54 +2459,58 @@ SELECT pg_catalog.pg_restore_extended_stats(
 SELECT
     replace(e.n_distinct,   '}, ', E'},\n') AS n_distinct,
     replace(e.dependencies, '}, ', E'},\n') AS dependencies,
-    e.most_common_vals, e.most_common_val_nulls,
+    replace(e.most_common_vals::text, '},{', E'},\n{ ') AS most_common_vals,
+    e.most_common_val_nulls,
     e.most_common_freqs, e.most_common_base_freqs
 FROM pg_stats_ext AS e
 WHERE e.statistics_schemaname = 'stats_import'
 AND e.statistics_name = 'test_stat_clone'
 AND e.inherited = false
 \gx
--[ RECORD 1 ]----------+------------------------------------------------------------------
-n_distinct             | [{"attributes": [2, 3], "ndistinct": 4},                         +
-                       | {"attributes": [2, -1], "ndistinct": 4},                         +
-                       | {"attributes": [3, -1], "ndistinct": 4},                         +
-                       | {"attributes": [3, -2], "ndistinct": 4},                         +
-                       | {"attributes": [-1, -2], "ndistinct": 3},                        +
-                       | {"attributes": [2, 3, -1], "ndistinct": 4},                      +
-                       | {"attributes": [2, 3, -2], "ndistinct": 4},                      +
-                       | {"attributes": [2, -1, -2], "ndistinct": 4},                     +
+-[ RECORD 1 ]----------+----------------------------------------------------------------------
+n_distinct             | [{"attributes": [2, 3], "ndistinct": 4},                             +
+                       | {"attributes": [2, -1], "ndistinct": 4},                             +
+                       | {"attributes": [3, -1], "ndistinct": 4},                             +
+                       | {"attributes": [3, -2], "ndistinct": 4},                             +
+                       | {"attributes": [-1, -2], "ndistinct": 3},                            +
+                       | {"attributes": [2, 3, -1], "ndistinct": 4},                          +
+                       | {"attributes": [2, 3, -2], "ndistinct": 4},                          +
+                       | {"attributes": [2, -1, -2], "ndistinct": 4},                         +
                        | {"attributes": [2, 3, -1, -2], "ndistinct": 4}]
-dependencies           | [{"attributes": [2], "dependency": 3, "degree": 1.000000},       +
-                       | {"attributes": [2], "dependency": -1, "degree": 1.000000},       +
-                       | {"attributes": [2], "dependency": -2, "degree": 1.000000},       +
-                       | {"attributes": [3], "dependency": 2, "degree": 1.000000},        +
-                       | {"attributes": [3], "dependency": -1, "degree": 1.000000},       +
-                       | {"attributes": [3], "dependency": -2, "degree": 1.000000},       +
-                       | {"attributes": [-1], "dependency": 2, "degree": 0.500000},       +
-                       | {"attributes": [-1], "dependency": 3, "degree": 0.500000},       +
-                       | {"attributes": [-1], "dependency": -2, "degree": 1.000000},      +
-                       | {"attributes": [-2], "dependency": 2, "degree": 0.500000},       +
-                       | {"attributes": [-2], "dependency": 3, "degree": 0.500000},       +
-                       | {"attributes": [-2], "dependency": -1, "degree": 1.000000},      +
-                       | {"attributes": [2, 3], "dependency": -1, "degree": 1.000000},    +
-                       | {"attributes": [2, 3], "dependency": -2, "degree": 1.000000},    +
-                       | {"attributes": [2, -1], "dependency": 3, "degree": 1.000000},    +
-                       | {"attributes": [2, -1], "dependency": -2, "degree": 1.000000},   +
-                       | {"attributes": [2, -2], "dependency": 3, "degree": 1.000000},    +
-                       | {"attributes": [2, -2], "dependency": -1, "degree": 1.000000},   +
-                       | {"attributes": [3, -1], "dependency": 2, "degree": 1.000000},    +
-                       | {"attributes": [3, -1], "dependency": -2, "degree": 1.000000},   +
-                       | {"attributes": [3, -2], "dependency": 2, "degree": 1.000000},    +
-                       | {"attributes": [3, -2], "dependency": -1, "degree": 1.000000},   +
-                       | {"attributes": [-1, -2], "dependency": 2, "degree": 0.500000},   +
-                       | {"attributes": [-1, -2], "dependency": 3, "degree": 0.500000},   +
-                       | {"attributes": [2, 3, -2], "dependency": -1, "degree": 1.000000},+
-                       | {"attributes": [2, -1, -2], "dependency": 3, "degree": 1.000000},+
+dependencies           | [{"attributes": [2], "dependency": 3, "degree": 1.000000},           +
+                       | {"attributes": [2], "dependency": -1, "degree": 1.000000},           +
+                       | {"attributes": [2], "dependency": -2, "degree": 1.000000},           +
+                       | {"attributes": [3], "dependency": 2, "degree": 1.000000},            +
+                       | {"attributes": [3], "dependency": -1, "degree": 1.000000},           +
+                       | {"attributes": [3], "dependency": -2, "degree": 1.000000},           +
+                       | {"attributes": [-1], "dependency": 2, "degree": 0.500000},           +
+                       | {"attributes": [-1], "dependency": 3, "degree": 0.500000},           +
+                       | {"attributes": [-1], "dependency": -2, "degree": 1.000000},          +
+                       | {"attributes": [-2], "dependency": 2, "degree": 0.500000},           +
+                       | {"attributes": [-2], "dependency": 3, "degree": 0.500000},           +
+                       | {"attributes": [-2], "dependency": -1, "degree": 1.000000},          +
+                       | {"attributes": [2, 3], "dependency": -1, "degree": 1.000000},        +
+                       | {"attributes": [2, 3], "dependency": -2, "degree": 1.000000},        +
+                       | {"attributes": [2, -1], "dependency": 3, "degree": 1.000000},        +
+                       | {"attributes": [2, -1], "dependency": -2, "degree": 1.000000},       +
+                       | {"attributes": [2, -2], "dependency": 3, "degree": 1.000000},        +
+                       | {"attributes": [2, -2], "dependency": -1, "degree": 1.000000},       +
+                       | {"attributes": [3, -1], "dependency": 2, "degree": 1.000000},        +
+                       | {"attributes": [3, -1], "dependency": -2, "degree": 1.000000},       +
+                       | {"attributes": [3, -2], "dependency": 2, "degree": 1.000000},        +
+                       | {"attributes": [3, -2], "dependency": -1, "degree": 1.000000},       +
+                       | {"attributes": [-1, -2], "dependency": 2, "degree": 0.500000},       +
+                       | {"attributes": [-1, -2], "dependency": 3, "degree": 0.500000},       +
+                       | {"attributes": [2, 3, -2], "dependency": -1, "degree": 1.000000},    +
+                       | {"attributes": [2, -1, -2], "dependency": 3, "degree": 1.000000},    +
                        | {"attributes": [3, -1, -2], "dependency": 2, "degree": 1.000000}]
-most_common_vals       | 
-most_common_val_nulls  | 
-most_common_freqs      | 
-most_common_base_freqs | 
+most_common_vals       | {{four,NULL,0,NULL},                                                 +
+                       | { one,"(4,1.1,ONE,01-01-2001,\"{\"\"xkey\"\": \"\"xval\"\"}\")",1,2},+
+                       | { tre,"(3,3.3,TRE,03-03-2003,)",-1,3},                               +
+                       | { two,"(2,2.2,TWO,02-02-2002,\"[true, 4, \"\"six\"\"]\")",1,2}}
+most_common_val_nulls  | {{f,t,f,t},{f,f,f,f},{f,f,f,f},{f,f,f,f}}
+most_common_freqs      | {0.25,0.25,0.25,0.25}
+most_common_base_freqs | {0.00390625,0.015625,0.00390625,0.015625}
 
 -- if any one mcv param specified, all four must be specified (part 1)
 SELECT pg_catalog.pg_restore_extended_stats(
@@ -2505,51 +2586,55 @@ SELECT pg_catalog.pg_restore_extended_stats(
 
 SELECT replace(e.n_distinct,   '}, ', E'},\n') AS n_distinct,
     replace(e.dependencies, '}, ', E'},\n') AS dependencies,
-    e.most_common_vals, e.most_common_val_nulls,
+    replace(e.most_common_vals::text, '},{', E'},\n{ ') AS most_common_vals,
+    e.most_common_val_nulls,
     e.most_common_freqs, e.most_common_base_freqs
   FROM pg_stats_ext AS e
   WHERE e.statistics_schemaname = 'stats_import' AND
     e.statistics_name = 'test_stat_clone' AND
     e.inherited = false
 \gx
--[ RECORD 1 ]----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-n_distinct             | [{"attributes": [2, 3], "ndistinct": 4},                                                                                                                                                   +
-                       | {"attributes": [2, -1], "ndistinct": 4},                                                                                                                                                   +
-                       | {"attributes": [3, -1], "ndistinct": 4},                                                                                                                                                   +
-                       | {"attributes": [3, -2], "ndistinct": 4},                                                                                                                                                   +
-                       | {"attributes": [-1, -2], "ndistinct": 3},                                                                                                                                                  +
-                       | {"attributes": [2, 3, -1], "ndistinct": 4},                                                                                                                                                +
-                       | {"attributes": [2, 3, -2], "ndistinct": 4},                                                                                                                                                +
-                       | {"attributes": [2, -1, -2], "ndistinct": 4},                                                                                                                                               +
+-[ RECORD 1 ]----------+----------------------------------------------------------------------
+n_distinct             | [{"attributes": [2, 3], "ndistinct": 4},                             +
+                       | {"attributes": [2, -1], "ndistinct": 4},                             +
+                       | {"attributes": [3, -1], "ndistinct": 4},                             +
+                       | {"attributes": [3, -2], "ndistinct": 4},                             +
+                       | {"attributes": [-1, -2], "ndistinct": 3},                            +
+                       | {"attributes": [2, 3, -1], "ndistinct": 4},                          +
+                       | {"attributes": [2, 3, -2], "ndistinct": 4},                          +
+                       | {"attributes": [2, -1, -2], "ndistinct": 4},                         +
                        | {"attributes": [2, 3, -1, -2], "ndistinct": 4}]
-dependencies           | [{"attributes": [2], "dependency": 3, "degree": 1.000000},                                                                                                                                 +
-                       | {"attributes": [2], "dependency": -1, "degree": 1.000000},                                                                                                                                 +
-                       | {"attributes": [2], "dependency": -2, "degree": 1.000000},                                                                                                                                 +
-                       | {"attributes": [3], "dependency": 2, "degree": 1.000000},                                                                                                                                  +
-                       | {"attributes": [3], "dependency": -1, "degree": 1.000000},                                                                                                                                 +
-                       | {"attributes": [3], "dependency": -2, "degree": 1.000000},                                                                                                                                 +
-                       | {"attributes": [-1], "dependency": 2, "degree": 0.500000},                                                                                                                                 +
-                       | {"attributes": [-1], "dependency": 3, "degree": 0.500000},                                                                                                                                 +
-                       | {"attributes": [-1], "dependency": -2, "degree": 1.000000},                                                                                                                                +
-                       | {"attributes": [-2], "dependency": 2, "degree": 0.500000},                                                                                                                                 +
-                       | {"attributes": [-2], "dependency": 3, "degree": 0.500000},                                                                                                                                 +
-                       | {"attributes": [-2], "dependency": -1, "degree": 1.000000},                                                                                                                                +
-                       | {"attributes": [2, 3], "dependency": -1, "degree": 1.000000},                                                                                                                              +
-                       | {"attributes": [2, 3], "dependency": -2, "degree": 1.000000},                                                                                                                              +
-                       | {"attributes": [2, -1], "dependency": 3, "degree": 1.000000},                                                                                                                              +
-                       | {"attributes": [2, -1], "dependency": -2, "degree": 1.000000},                                                                                                                             +
-                       | {"attributes": [2, -2], "dependency": 3, "degree": 1.000000},                                                                                                                              +
-                       | {"attributes": [2, -2], "dependency": -1, "degree": 1.000000},                                                                                                                             +
-                       | {"attributes": [3, -1], "dependency": 2, "degree": 1.000000},                                                                                                                              +
-                       | {"attributes": [3, -1], "dependency": -2, "degree": 1.000000},                                                                                                                             +
-                       | {"attributes": [3, -2], "dependency": 2, "degree": 1.000000},                                                                                                                              +
-                       | {"attributes": [3, -2], "dependency": -1, "degree": 1.000000},                                                                                                                             +
-                       | {"attributes": [-1, -2], "dependency": 2, "degree": 0.500000},                                                                                                                             +
-                       | {"attributes": [-1, -2], "dependency": 3, "degree": 0.500000},                                                                                                                             +
-                       | {"attributes": [2, 3, -2], "dependency": -1, "degree": 1.000000},                                                                                                                          +
-                       | {"attributes": [2, -1, -2], "dependency": 3, "degree": 1.000000},                                                                                                                          +
+dependencies           | [{"attributes": [2], "dependency": 3, "degree": 1.000000},           +
+                       | {"attributes": [2], "dependency": -1, "degree": 1.000000},           +
+                       | {"attributes": [2], "dependency": -2, "degree": 1.000000},           +
+                       | {"attributes": [3], "dependency": 2, "degree": 1.000000},            +
+                       | {"attributes": [3], "dependency": -1, "degree": 1.000000},           +
+                       | {"attributes": [3], "dependency": -2, "degree": 1.000000},           +
+                       | {"attributes": [-1], "dependency": 2, "degree": 0.500000},           +
+                       | {"attributes": [-1], "dependency": 3, "degree": 0.500000},           +
+                       | {"attributes": [-1], "dependency": -2, "degree": 1.000000},          +
+                       | {"attributes": [-2], "dependency": 2, "degree": 0.500000},           +
+                       | {"attributes": [-2], "dependency": 3, "degree": 0.500000},           +
+                       | {"attributes": [-2], "dependency": -1, "degree": 1.000000},          +
+                       | {"attributes": [2, 3], "dependency": -1, "degree": 1.000000},        +
+                       | {"attributes": [2, 3], "dependency": -2, "degree": 1.000000},        +
+                       | {"attributes": [2, -1], "dependency": 3, "degree": 1.000000},        +
+                       | {"attributes": [2, -1], "dependency": -2, "degree": 1.000000},       +
+                       | {"attributes": [2, -2], "dependency": 3, "degree": 1.000000},        +
+                       | {"attributes": [2, -2], "dependency": -1, "degree": 1.000000},       +
+                       | {"attributes": [3, -1], "dependency": 2, "degree": 1.000000},        +
+                       | {"attributes": [3, -1], "dependency": -2, "degree": 1.000000},       +
+                       | {"attributes": [3, -2], "dependency": 2, "degree": 1.000000},        +
+                       | {"attributes": [3, -2], "dependency": -1, "degree": 1.000000},       +
+                       | {"attributes": [-1, -2], "dependency": 2, "degree": 0.500000},       +
+                       | {"attributes": [-1, -2], "dependency": 3, "degree": 0.500000},       +
+                       | {"attributes": [2, 3, -2], "dependency": -1, "degree": 1.000000},    +
+                       | {"attributes": [2, -1, -2], "dependency": 3, "degree": 1.000000},    +
                        | {"attributes": [3, -1, -2], "dependency": 2, "degree": 1.000000}]
-most_common_vals       | {{four,NULL,0,NULL},{one,"(1,1.1,ONE,01-01-2001,\"{\"\"xkey\"\": \"\"xval\"\"}\")",1,2},{tre,"(3,3.3,TRE,03-03-2003,)",-1,3},{two,"(2,2.2,TWO,02-02-2002,\"[true, 4, \"\"six\"\"]\")",1,2}}
+most_common_vals       | {{four,NULL,0,NULL},                                                 +
+                       | { one,"(1,1.1,ONE,01-01-2001,\"{\"\"xkey\"\": \"\"xval\"\"}\")",1,2},+
+                       | { tre,"(3,3.3,TRE,03-03-2003,)",-1,3},                               +
+                       | { two,"(2,2.2,TWO,02-02-2002,\"[true, 4, \"\"six\"\"]\")",1,2}}
 most_common_val_nulls  | {{f,t,f,t},{f,f,f,f},{f,f,f,f},{f,f,f,f}}
 most_common_freqs      | {0.25,0.25,0.25,0.25}
 most_common_base_freqs | {0.00390625,0.015625,0.00390625,0.015625}
@@ -2740,9 +2825,10 @@ SELECT o.inherited,
 (0 rows)
 
 DROP SCHEMA stats_import CASCADE;
-NOTICE:  drop cascades to 6 other objects
+NOTICE:  drop cascades to 7 other objects
 DETAIL:  drop cascades to type stats_import.complex_type
 drop cascades to table stats_import.test
+drop cascades to table stats_import.test_mr
 drop cascades to table stats_import.part_parent
 drop cascades to sequence stats_import.testseq
 drop cascades to view stats_import.testview
diff --git a/src/test/regress/sql/stats_import.sql b/src/test/regress/sql/stats_import.sql
index 13285f0371f..ca92953f6b3 100644
--- a/src/test/regress/sql/stats_import.sql
+++ b/src/test/regress/sql/stats_import.sql
@@ -101,6 +101,58 @@ WHERE relation = 'stats_import.test_i'::regclass AND
 
 COMMIT;
 
+-- test multiranges and extended stats therein
+CREATE TABLE stats_import.test_mr(
+    id INTEGER PRIMARY KEY,
+    name text,
+    mrange int4multirange
+) WITH (autovacuum_enabled = false);
+
+INSERT INTO stats_import.test_mr
+VALUES
+  (1, 'red', '{[1,3),[5,9),[20,30)}'::int4multirange),
+  (2, 'red', '{[11,13),[15,19),[20,30)}'::int4multirange),
+  (3, 'red', '{[21,23),[25,29),[120,130)}'::int4multirange);
+
+SELECT pg_catalog.pg_restore_attribute_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test_mr',
+  'attname', 'mrange',
+  'inherited', false,
+  'range_length_histogram', '{19,29,109}'::text,
+  'range_empty_frac', '0'::real,
+  'range_bounds_histogram', '{"[1,30)","[11,30)","[21,130)"}'::text
+);
+
+CREATE STATISTICS stats_import.test_mr_stat
+  ON name, mrange, ( mrange + '{[10000,10200)}'::int4multirange)
+  FROM stats_import.test_mr;
+
+-- ok
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test_mr',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_mr_stat',
+  'inherited', false,
+  'n_distinct', '[{"attributes": [2, 3], "ndistinct": 3},
+                  {"attributes": [2, -1], "ndistinct": 3},
+                  {"attributes": [3, -1], "ndistinct": 3},
+                  {"attributes": [2, 3, -1], "ndistinct": 3}]'::pg_catalog.pg_ndistinct,
+  'dependencies', '[{"attributes": [3], "dependency": 2, "degree": 1.000000},
+                    {"attributes": [3], "dependency": -1, "degree": 1.000000},
+                    {"attributes": [-1], "dependency": 2, "degree": 1.000000},
+                    {"attributes": [-1], "dependency": 3, "degree": 1.000000},
+                    {"attributes": [2, 3], "dependency": -1, "degree": 1.000000},
+                    {"attributes": [2, -1], "dependency": 3, "degree": 1.000000},
+                    {"attributes": [3, -1], "dependency": 2, "degree": 1.000000}]'::pg_catalog.pg_dependencies,
+  'most_common_vals', '{{red,"{[1,3),[5,9),[20,30)}","{[1,3),[5,9),[20,30),[10000,10200)}"},{red,"{[11,13),[15,19),[20,30)}","{[11,13),[15,19),[20,30),[10000,10200)}"},{red,"{[21,23),[25,29),[120,130)}","{[21,23),[25,29),[120,130),[10000,10200)}"}}'::text[],
+  'most_common_val_nulls', '{{f,f,f},{f,f,f},{f,f,f}}'::boolean[],
+  'most_common_freqs', '{0.3333333333333333,0.3333333333333333,0.3333333333333333}'::double precision[],
+  'most_common_base_freqs', '{0.1111111111111111,0.1111111111111111,0.1111111111111111}'::double precision[],
+  'exprs', '{{0,60,-1,NULL,NULL,NULL,NULL,NULL,NULL,NULL}}'::text[]
+);
+
 --  relpages may be -1 for partitioned tables
 CREATE TABLE stats_import.part_parent ( i integer ) PARTITION BY RANGE(i);
 CREATE TABLE stats_import.part_child_1
@@ -1475,6 +1527,18 @@ SELECT pg_catalog.pg_restore_extended_stats(
   'most_common_freqs', '{0.25,0.25,0.25,0.25}'::double precision[],
   'most_common_base_freqs', '{0.00390625,0.015625,0.00390625,0.015625}'::double precision[]);
 
+-- ok
+SELECT pg_catalog.pg_restore_extended_stats(
+  'schemaname', 'stats_import',
+  'relname', 'test_clone',
+  'statistics_schemaname', 'stats_import',
+  'statistics_name', 'test_stat_clone',
+  'inherited', false,
+  'most_common_vals', '{{four,NULL,0,NULL},{one,"(4,1.1,ONE,01-01-2001,\"{\"\"xkey\"\": \"\"xval\"\"}\")",1,2},{tre,"(3,3.3,TRE,03-03-2003,)",-1,3},{two,"(2,2.2,TWO,02-02-2002,\"[true, 4, \"\"six\"\"]\")",1,2}}'::text[],
+  'most_common_val_nulls', '{{f,t,f,t},{f,f,f,f},{f,f,f,f},{f,f,f,f}}'::boolean[],
+  'most_common_freqs', '{0.25,0.25,0.25,0.25}'::double precision[],
+  'most_common_base_freqs', '{0.00390625,0.015625,0.00390625,0.015625}'::double precision[]);
+
 -- Set n_distinct using at attnum (1) that is not in the statistics
 -- object
 SELECT pg_catalog.pg_restore_extended_stats(
@@ -1566,7 +1630,8 @@ DROP ROLE regress_test_extstat_restore;
 
 SELECT replace(e.n_distinct,   '}, ', E'},\n') AS n_distinct,
   e.dependencies,
-  e.most_common_vals, e.most_common_val_nulls,
+  replace(e.most_common_vals::text, '},{', E'},\n{ ') AS most_common_vals,
+  e.most_common_val_nulls,
   e.most_common_freqs, e.most_common_base_freqs
 FROM pg_stats_ext AS e
 WHERE e.statistics_schemaname = 'stats_import'
@@ -1639,7 +1704,8 @@ SELECT pg_catalog.pg_restore_extended_stats(
 SELECT
     replace(e.n_distinct,   '}, ', E'},\n') AS n_distinct,
     replace(e.dependencies, '}, ', E'},\n') AS dependencies,
-    e.most_common_vals, e.most_common_val_nulls,
+    replace(e.most_common_vals::text, '},{', E'},\n{ ') AS most_common_vals,
+    e.most_common_val_nulls,
     e.most_common_freqs, e.most_common_base_freqs
 FROM pg_stats_ext AS e
 WHERE e.statistics_schemaname = 'stats_import'
@@ -1697,7 +1763,8 @@ SELECT pg_catalog.pg_restore_extended_stats(
 
 SELECT replace(e.n_distinct,   '}, ', E'},\n') AS n_distinct,
     replace(e.dependencies, '}, ', E'},\n') AS dependencies,
-    e.most_common_vals, e.most_common_val_nulls,
+    replace(e.most_common_vals::text, '},{', E'},\n{ ') AS most_common_vals,
+    e.most_common_val_nulls,
     e.most_common_freqs, e.most_common_base_freqs
   FROM pg_stats_ext AS e
   WHERE e.statistics_schemaname = 'stats_import' AND
-- 
2.52.0

