codekingpro/portable-devtools
114k
1/*2 * fix-CVE-2024-4317.sql3 *4 * Copyright (c) 2024, PostgreSQL Global Development Group5 *6 * src/backend/catalog/fix-CVE-2024-4317.sql7 *8 * This file should be run in every database in the cluster to address9 * CVE-2024-4317.10 */11 12SET search_path = pg_catalog;13 14CREATE OR REPLACE VIEW pg_stats_ext WITH (security_barrier) AS15 SELECT cn.nspname AS schemaname,16 c.relname AS tablename,17 sn.nspname AS statistics_schemaname,18 s.stxname AS statistics_name,19 pg_get_userbyid(s.stxowner) AS statistics_owner,20 ( SELECT array_agg(a.attname ORDER BY a.attnum)21 FROM unnest(s.stxkeys) k22 JOIN pg_attribute a23 ON (a.attrelid = s.stxrelid AND a.attnum = k)24 ) AS attnames,25 pg_get_statisticsobjdef_expressions(s.oid) as exprs,26 s.stxkind AS kinds,27 sd.stxdinherit AS inherited,28 sd.stxdndistinct AS n_distinct,29 sd.stxddependencies AS dependencies,30 m.most_common_vals,31 m.most_common_val_nulls,32 m.most_common_freqs,33 m.most_common_base_freqs34 FROM pg_statistic_ext s JOIN pg_class c ON (c.oid = s.stxrelid)35 JOIN pg_statistic_ext_data sd ON (s.oid = sd.stxoid)36 LEFT JOIN pg_namespace cn ON (cn.oid = c.relnamespace)37 LEFT JOIN pg_namespace sn ON (sn.oid = s.stxnamespace)38 LEFT JOIN LATERAL39 ( SELECT array_agg(values) AS most_common_vals,40 array_agg(nulls) AS most_common_val_nulls,41 array_agg(frequency) AS most_common_freqs,42 array_agg(base_frequency) AS most_common_base_freqs43 FROM pg_mcv_list_items(sd.stxdmcv)44 ) m ON sd.stxdmcv IS NOT NULL45 WHERE pg_has_role(c.relowner, 'USAGE')46 AND (c.relrowsecurity = false OR NOT row_security_active(c.oid));47 48CREATE OR REPLACE VIEW pg_stats_ext_exprs WITH (security_barrier) AS49 SELECT cn.nspname AS schemaname,50 c.relname AS tablename,51 sn.nspname AS statistics_schemaname,52 s.stxname AS statistics_name,53 pg_get_userbyid(s.stxowner) AS statistics_owner,54 stat.expr,55 sd.stxdinherit AS inherited,56 (stat.a).stanullfrac AS null_frac,57 (stat.a).stawidth AS avg_width,58 (stat.a).stadistinct AS n_distinct,59 (CASE60 WHEN (stat.a).stakind1 = 1 THEN (stat.a).stavalues161 WHEN (stat.a).stakind2 = 1 THEN (stat.a).stavalues262 WHEN (stat.a).stakind3 = 1 THEN (stat.a).stavalues363 WHEN (stat.a).stakind4 = 1 THEN (stat.a).stavalues464 WHEN (stat.a).stakind5 = 1 THEN (stat.a).stavalues565 END) AS most_common_vals,66 (CASE67 WHEN (stat.a).stakind1 = 1 THEN (stat.a).stanumbers168 WHEN (stat.a).stakind2 = 1 THEN (stat.a).stanumbers269 WHEN (stat.a).stakind3 = 1 THEN (stat.a).stanumbers370 WHEN (stat.a).stakind4 = 1 THEN (stat.a).stanumbers471 WHEN (stat.a).stakind5 = 1 THEN (stat.a).stanumbers572 END) AS most_common_freqs,73 (CASE74 WHEN (stat.a).stakind1 = 2 THEN (stat.a).stavalues175 WHEN (stat.a).stakind2 = 2 THEN (stat.a).stavalues276 WHEN (stat.a).stakind3 = 2 THEN (stat.a).stavalues377 WHEN (stat.a).stakind4 = 2 THEN (stat.a).stavalues478 WHEN (stat.a).stakind5 = 2 THEN (stat.a).stavalues579 END) AS histogram_bounds,80 (CASE81 WHEN (stat.a).stakind1 = 3 THEN (stat.a).stanumbers1[1]82 WHEN (stat.a).stakind2 = 3 THEN (stat.a).stanumbers2[1]83 WHEN (stat.a).stakind3 = 3 THEN (stat.a).stanumbers3[1]84 WHEN (stat.a).stakind4 = 3 THEN (stat.a).stanumbers4[1]85 WHEN (stat.a).stakind5 = 3 THEN (stat.a).stanumbers5[1]86 END) correlation,87 (CASE88 WHEN (stat.a).stakind1 = 4 THEN (stat.a).stavalues189 WHEN (stat.a).stakind2 = 4 THEN (stat.a).stavalues290 WHEN (stat.a).stakind3 = 4 THEN (stat.a).stavalues391 WHEN (stat.a).stakind4 = 4 THEN (stat.a).stavalues492 WHEN (stat.a).stakind5 = 4 THEN (stat.a).stavalues593 END) AS most_common_elems,94 (CASE95 WHEN (stat.a).stakind1 = 4 THEN (stat.a).stanumbers196 WHEN (stat.a).stakind2 = 4 THEN (stat.a).stanumbers297 WHEN (stat.a).stakind3 = 4 THEN (stat.a).stanumbers398 WHEN (stat.a).stakind4 = 4 THEN (stat.a).stanumbers499 WHEN (stat.a).stakind5 = 4 THEN (stat.a).stanumbers5100 END) AS most_common_elem_freqs,101 (CASE102 WHEN (stat.a).stakind1 = 5 THEN (stat.a).stanumbers1103 WHEN (stat.a).stakind2 = 5 THEN (stat.a).stanumbers2104 WHEN (stat.a).stakind3 = 5 THEN (stat.a).stanumbers3105 WHEN (stat.a).stakind4 = 5 THEN (stat.a).stanumbers4106 WHEN (stat.a).stakind5 = 5 THEN (stat.a).stanumbers5107 END) AS elem_count_histogram108 FROM pg_statistic_ext s JOIN pg_class c ON (c.oid = s.stxrelid)109 LEFT JOIN pg_statistic_ext_data sd ON (s.oid = sd.stxoid)110 LEFT JOIN pg_namespace cn ON (cn.oid = c.relnamespace)111 LEFT JOIN pg_namespace sn ON (sn.oid = s.stxnamespace)112 JOIN LATERAL (113 SELECT unnest(pg_get_statisticsobjdef_expressions(s.oid)) AS expr,114 unnest(sd.stxdexpr)::pg_statistic AS a115 ) stat ON (stat.expr IS NOT NULL)116 WHERE pg_has_role(c.relowner, 'USAGE')117 AND (c.relrowsecurity = false OR NOT row_security_active(c.oid));118 