DROP MATERIALIZED VIEW IF EXISTS mv_category_locale_safexmlpath; CREATE MATERIALIZED VIEW mv_category_locale_safexmlpath AS SELECT c.id AS category_id, c.locale_id, getsafexmlpath( ( SELECT lower(array_to_string(array_agg(main_1.parent_name), '/'::text)) FROM ( SELECT node.id, ( SELECT category_texts.name FROM category_texts WHERE ((category_texts.locale_id = c.locale_id) AND (category_texts.category_id = parent.id))) AS parent_name FROM categories node, categories parent WHERE ((node.lft >= parent.lft) AND (node.lft <= parent.rgt) AND (node.id = c.id)) ORDER BY parent.lft) main_1 GROUP BY main_1.id), 2 ) AS safe_xml_path FROM ( SELECT c_1.id, s.id AS locale_id FROM locales s JOIN category_texts s_t ON s.id = s_t.locale_id JOIN categories c_1 ON s_t.category_id = c_1.id ) c; CREATE UNIQUE INDEX ON mv_category_locale_safexmlpath (category_id, locale_id); CREATE INDEX ON mv_category_locale_safexmlpath (locale_id, safe_xml_path); COMMENT ON MATERIALIZED VIEW IS 'Materialized view for fast lookup of category id from localized safe xml path and locale. Path construction logic mirrors v_esales_categorytree_i18n_unmaterialized but additionally applies the getsafexmlpath(..., 2) function.'; ALTER MATERIALIZED VIEW mv_category_locale_safexmlpath OWNER TO Photowall;