Path: redb/redb.Postgres/sql/v2-pvt/00_module_init.sql Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Postgres/sql/v2-pvt/00_module_init.sql ---- -- ===================================================================== -- v2-pvt module init -- ===================================================================== -- Purpose: PVT-based search engine for REDB free (PostgreSQL). -- Owner : redb core team. Forked helpers in 01..07 mirror legacy -- redb_facets_search.sql / redb_lazy_loading_search.sql. -- Version: see pvt_module_version() in 99_module_version.sql (applied LAST). -- -- This file must be applied FIRST. It performs two things: -- 1. Verifies that system infrastructure of REDB is in place -- (core tables and two system functions). -- 2. Drops every function this module owns (CASCADE) so the module -- can be redeployed cleanly — pvt_module_version() included, which is -- why the version is recreated LAST, in 99_module_version.sql: a -- failure anywhere in between leaves no version, and the next start -- applies the bundle again. -- ===================================================================== -- ---------- Guard: a newer module is never taken back ---------- -- An older package never puts its module over a newer one. Start-up does not apply this script then; -- the guard stops a hand run of an older script (GetUpgradeScript(), redb schema --upgrade) before it -- changes anything, with the sentence of RedbSchemaNewerException. A version that does not read as -- numbers is not newer. _own is the version 99_module_version.sql stamps (a test holds them equal). -- psql goes on after an error unless told to stop: run the script with -v ON_ERROR_STOP=1. DO $$ DECLARE _own constant text := '0.7.33'; _deployed text; _core text; _compat text; _package text; BEGIN IF to_regprocedure('pvt_module_version()') IS NULL THEN RETURN; END IF; EXECUTE 'SELECT pvt_module_version()' INTO _deployed; _core := split_part(coalesce(_deployed, ''), '-', 1); IF _core !~ '^[0-9]{1,9}(\.[0-9]{1,9}){1,3}$' OR string_to_array(_core, '.')::int[] <= string_to_array(_own, '.')::int[] THEN RETURN; END IF; IF to_regprocedure('pvt_module_compat()') IS NOT NULL THEN EXECUTE 'SELECT pvt_module_compat()' INTO _compat; END IF; _package := CASE WHEN position('|' IN coalesce(_compat, '')) > 0 THEN nullif(split_part(_compat, '|', 2), '') END; RAISE EXCEPTION 'The PostgreSQL database carries the redb module %, newer than % of this package, and %. Upgrade the redb packages of this application; the database was left unchanged.', _deployed, _own, CASE WHEN _package IS NULL THEN 'it does not state which packages it serves' ELSE 'it needs redb packages from ' || _package || ' on' END USING ERRCODE = 'object_not_in_prerequisite_state'; END $$; -- ---------- 0. Schema upgrades (idempotent, run on every redeploy) ---------- -- DDL for columns added after a database was created. The base DDL in -- redbPostgre.sql stays the source of truth for fresh databases; this block -- is the delivery to existing ones (SCHEMA_DELIVERY plan, V4). Every -- statement is a no-op once applied. -- 0.7.29: TimeOnly and TimeSpan are stored as Ticks in _Long (they were text, which compared wrong past a day and -- below zero). The type rows and their copy in the metadata cache say Long from now on; values already stored as -- text are not converted (UPGRADING.md gives the statement for a database that wants them). UPDATE _types SET _db_type = 'Long' WHERE _id IN (-9223372036854775685, -9223372036854775684) AND _db_type <> 'Long'; UPDATE _scheme_metadata_cache SET db_type = 'Long' WHERE _type_id IN (-9223372036854775685, -9223372036854775684) AND db_type <> 'Long'; -- 0.7.31: the field-name trigger compares C# keywords as written, not in lower case. C# keywords are case-sensitive, so -- Ref, Default or String are ordinary property names; they were refused at scheme sync ("is a C# reserved word"). -- The same body as in redbPostgre.sql, which a new database gets; this puts it into an existing one. CREATE OR REPLACE FUNCTION validate_structure_name() RETURNS TRIGGER AS $$ DECLARE reserved_fields text[] := ARRAY[ '_id', '_id_parent', '_id_scheme', '_id_owner', '_id_who_change', '_date_create', '_date_modify', '_date_begin', '_date_complete', '_key', '_name', '_note', '_hash', '_value_long', '_value_string', '_value_guid', '_value_bool', '_value_double', '_value_numeric', '_value_datetime', '_value_bytes' ]; field_name text; BEGIN -- Check only on INSERT and UPDATE of _name field IF TG_OP = 'INSERT' OR (TG_OP = 'UPDATE' AND OLD._name IS DISTINCT FROM NEW._name) THEN field_name := LOWER(NEW._name); -- Check 1: Name must not match reserved fields of _objects IF field_name = ANY(reserved_fields) THEN RAISE EXCEPTION 'Field name "_name" cannot match system object fields: %', NEW._name USING ERRCODE = '23514', HINT = 'Use a different name, avoid: ' || array_to_string(reserved_fields, ', '); END IF; -- Check 2: Name must not start with a digit IF NEW._name ~ '^[0-9]' THEN RAISE EXCEPTION 'Field name "_name" cannot start with a digit: %', NEW._name USING ERRCODE = '23514', HINT = 'Field name must start with a letter or underscore'; END IF; -- Check 3: Name must follow C# naming rules (letters, digits, underscores) IF NEW._name !~ '^[a-zA-Z_][a-zA-Z0-9_]*$' THEN RAISE EXCEPTION 'Field name "_name" contains invalid characters: %', NEW._name USING ERRCODE = '23514', HINT = 'Name can only contain Latin letters, digits, underscores. Must start with letter or underscore'; END IF; -- Check 4: Name must not be empty or contain only spaces IF LENGTH(TRIM(NEW._name)) = 0 THEN RAISE EXCEPTION 'Field name "_name" cannot be empty' USING ERRCODE = '23514'; END IF; -- Check 5: Maximum name length (reasonable limit) IF LENGTH(NEW._name) > 64 THEN RAISE EXCEPTION 'Field name "_name" is too long (max 64 characters): %', NEW._name USING ERRCODE = '23514'; END IF; -- Check 6: C# reserved words, compared as written: C# keywords are case-sensitive, so Ref, Default or -- String are ordinary property names and only ref, default, string are refused (0.7.31). IF NEW._name = ANY(ARRAY[ 'abstract', 'as', 'bool', 'break', 'byte', 'case', 'catch', 'char', 'checked', 'class', 'const', 'continue', 'decimal', 'default', 'delegate', 'do', 'double', 'else', 'enum', 'event', 'explicit', 'extern', 'false', 'finally', 'fixed', 'float', 'for', 'foreach', 'goto', 'if', 'implicit', 'in', 'int', 'interface', 'internal', 'is', 'lock', 'long', 'namespace', 'new', 'null', 'object', 'operator', 'out', 'override', 'params', 'private', 'protected', 'public', 'readonly', 'ref', 'return', 'sbyte', 'sealed', 'short', 'sizeof', 'stackalloc', 'static', 'string', 'struct', 'switch', 'this', 'throw', 'true', 'try', 'typeof', 'uint', 'ulong', 'unchecked', 'unsafe', 'ushort', 'using', 'virtual', 'void', 'volatile', 'while' ]) THEN RAISE EXCEPTION 'Field name "_name" is a C# reserved word: %', NEW._name USING ERRCODE = '23514', HINT = 'Use a different name or add prefix/suffix'; END IF; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; -- V4 (UNIQUE stage 2): [RedbUnique] key metadata and the key hash column. ALTER TABLE _structures ADD COLUMN IF NOT EXISTS _unique boolean NULL; ALTER TABLE _structures ADD COLUMN IF NOT EXISTS _unique_version bigint NULL; ALTER TABLE _values ADD COLUMN IF NOT EXISTS _unique uuid NULL; -- S2 (subtree-unique plan, 0.7.7): the key index covers keyed rows at ANY position now - -- nested scalar keys carry _array_parent_id, and the old positional filter kept them OUT of -- the index, so their "uniqueness" silently never fired. Recreate on the old predicate. DO $$ BEGIN IF EXISTS (SELECT 1 FROM pg_indexes WHERE schemaname = 'public' AND indexname = 'UIX__values__structure_unique' AND indexdef LIKE '%_array_parent_id%') THEN DROP INDEX "UIX__values__structure_unique"; END IF; END $$; CREATE UNIQUE INDEX IF NOT EXISTS "UIX__values__structure_unique" ON _values (_id_structure, _unique) INCLUDE (_id_object) WHERE _unique IS NOT NULL; ALTER TABLE _scheme_metadata_cache ADD COLUMN IF NOT EXISTS _unique boolean NULL; ALTER TABLE _scheme_metadata_cache ADD COLUMN IF NOT EXISTS _unique_version bigint NULL; -- V4 (LAZY Л2): the lazy-reference marker, read from `virtual` at synchronisation. ALTER TABLE _structures ADD COLUMN IF NOT EXISTS _lazy boolean NULL; ALTER TABLE _scheme_metadata_cache ADD COLUMN IF NOT EXISTS _lazy boolean NULL; UPDATE _scheme_metadata_cache c SET _lazy = s._lazy FROM _structures s WHERE s._id = c._structure_id AND c._lazy IS DISTINCT FROM s._lazy; -- V4 (UNIQUE stage 1): the object key column and its unique index. ALTER TABLE _objects ADD COLUMN IF NOT EXISTS _value_unique varchar(440) NULL; CREATE UNIQUE INDEX IF NOT EXISTS "UIX__objects__scheme_unique" ON _objects (_id_scheme, _value_unique) INCLUDE (_id) WHERE _value_unique IS NOT NULL; -- S3 (subtree-unique plan, 0.7.8): element-key scope of collection keys, and the free-form -- _tags marker on schemes and structures (future / custom extensions; sync never wipes it). ALTER TABLE _structures ADD COLUMN IF NOT EXISTS _unique_scope bigint NULL; ALTER TABLE _structures ADD COLUMN IF NOT EXISTS _tags varchar(450) NULL; ALTER TABLE _schemes ADD COLUMN IF NOT EXISTS _tags varchar(450) NULL; CREATE INDEX IF NOT EXISTS "IX__structures__tags" ON _structures (_tags) WHERE _tags IS NOT NULL; ALTER TABLE _scheme_metadata_cache ADD COLUMN IF NOT EXISTS _unique_scope bigint NULL; ALTER TABLE _scheme_metadata_cache ADD COLUMN IF NOT EXISTS _tags varchar(450) NULL; UPDATE _scheme_metadata_cache c SET _unique_scope = s._unique_scope, _tags = s._tags FROM _structures s WHERE s._id = c._structure_id AND (c._unique_scope IS DISTINCT FROM s._unique_scope OR c._tags IS DISTINCT FROM s._tags); -- 0.7.27: the previous names of renamed structures and schemes ([RedbRename], RedbScheme.RenamedFrom), oldest -- first, as a JSON array; read by the sync to stop a class older than the rename. ALTER TABLE _structures ADD COLUMN IF NOT EXISTS _renamed_from text NULL; ALTER TABLE _schemes ADD COLUMN IF NOT EXISTS _renamed_from text NULL; -- 0.7.32: the version of the Props class the scheme was last synchronised with (RedbScheme.Version); the sync deletes a -- structure or changes its type only for a class of a higher version. NULL counts as 0. ALTER TABLE _schemes ADD COLUMN IF NOT EXISTS _version integer NULL; -- FK-column indexes of _values (perf, 2026-09-10): _Object/_ListItem carry foreign keys, and -- without a leading index every DELETE of a referenced object or list item seq-scans the whole -- table for the FK check. Partial form keeps them nearly empty. CREATE INDEX IF NOT EXISTS "IX__values__ListItem_not_null" ON _values (_ListItem) WHERE _ListItem IS NOT NULL; CREATE INDEX IF NOT EXISTS "IX__values__Object_not_null" ON _values (_Object) WHERE _Object IS NOT NULL; -- Index cleanup (0.7.13, 2026-10-12): indexes another index of the same table fully replaces. They -- were removed from redbPostgre.sql, but that file only ever runs CREATE INDEX IF NOT EXISTS on a -- new database, so an existing one keeps them until they are dropped here. -- IX__values__object_structure_array_index - same key as IX__values__object_structure_lookup, -- which has no predicate and carries the values in INCLUDE. -- ix__objects_parent_scheme - commented out of the schema long ago; left behind on older -- databases. Same key and predicate as IX__objects__parent_id_descendant_lookup. -- the rest - a single column that is the leading column of a UNIQUE constraint or of a wider -- index with the same predicate. -- DROP INDEX takes a short exclusive lock on the table; none of these is read by any plan that -- the replacing index cannot serve. DROP INDEX IF EXISTS "IX__values__object_structure_array_index"; DROP INDEX IF EXISTS ix__objects_parent_scheme; DROP INDEX IF EXISTS "IX__functions__schemes"; DROP INDEX IF EXISTS "IX__permissions__roles"; DROP INDEX IF EXISTS "IX__dependencies__schemes_1"; DROP INDEX IF EXISTS "IX__users_roles__roles"; DROP INDEX IF EXISTS "IX__structures__not_collection"; -- 0.7.33: IX__values__array_parent_id (_array_parent_id) is the leading column of IX__values__array_parent_index -- (_array_parent_id, _array_index), which serves the same lookups and the ON DELETE CASCADE of FK__values__array_parent. -- It is dropped only where such a wider index exists: a database created by an old version may lack -- array_parent_index, and the cascade must keep an index. Drop by name only; the other index must be a valid, -- non-partial btree whose first column is _array_parent_id. DO $$ DECLARE _parent smallint; BEGIN IF to_regclass('"IX__values__array_parent_id"') IS NULL THEN RETURN; END IF; SELECT a.attnum INTO _parent FROM pg_attribute a WHERE a.attrelid = '_values'::regclass AND a.attname = '_array_parent_id'; IF EXISTS ( SELECT 1 FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid JOIN pg_am m ON m.oid = c.relam WHERE i.indrelid = '_values'::regclass AND c.relname <> 'IX__values__array_parent_id' AND m.amname = 'btree' AND i.indisvalid AND i.indpred IS NULL AND i.indkey[0] = _parent) THEN DROP INDEX "IX__values__array_parent_id"; END IF; END $$; -- Stock value index retired (0.7.14, 2026-10-12). It and the covering index of the streams form -- (IX__values__structure_object_scalar, redb.Postgres.Pro/Sql/pvt_streams_indexes.sql) were two -- indexes of the same size over the same rows; the queries read the covering one and the -- object-led ones. Measured before retiring it: only the row prefilter over a dictionary item or -- a number got slower, falling back to IX__values__structures (27 -> 52 ms, 3 -> 7 ms). -- IF QUERIES GOT SLOWER AFTER THIS UPGRADE, put it back, CONCURRENTLY so that writes go on: -- CREATE INDEX CONCURRENTLY IF NOT EXISTS "IX__values__structure_object_lookup" -- ON _values (_id_structure, _id_object, _Long, _DateTimeOffset, _Boolean, _Double, _Guid, _Numeric, _ListItem, _Object); -- This file drops it again whenever the module version changes, so report the case. DROP INDEX IF EXISTS "IX__values__structure_object_lookup"; -- Value-leading indexes (0.7.16, 2026-10-16): a filter on a field finds its objects by value. Measured on -- 100 000 employees: three matching rows 6 ms -> 0 ms, 5 % of the scheme 36 -> 10 ms, with the streams form -- that pushes the filter down. They replace the object-leading forms (_id_structure, _id_object, ), -- which only served reading the value of a known object; the covering index of the streams form does that. -- Built inside the module transaction, so not CONCURRENTLY: writes to _values wait while they are built, about -- a minute per index on 38 million rows. Created before the old ones are dropped, so no moment is left without. -- IF QUERIES GOT SLOWER AFTER THIS UPGRADE (the aggregate form, with EnablePvtPrefilter off, read the old ones), -- these bring the old ones back; the module drops them again on its next version change, so report the case: -- CREATE INDEX "IX__values__String_not_null" ON _values (_id_structure, _id_object, _String) WHERE _String IS NOT NULL AND length(_String) < 2000; -- CREATE INDEX "IX__values__Long_not_null" ON _values (_id_structure, _id_object, _Long) WHERE _Long IS NOT NULL; -- CREATE INDEX "IX__values__DateTimeOffset_not_null" ON _values (_id_structure, _id_object, _DateTimeOffset) WHERE _DateTimeOffset IS NOT NULL; -- CREATE INDEX "IX__values__Numeric_not_null" ON _values (_id_structure, _id_object, _Numeric) WHERE _Numeric IS NOT NULL; -- CREATE INDEX ix__values__array_null_structure ON _values (_id_structure, _id_object, _long) WHERE _array_index IS NULL; -- An index of one of these names whose definition differs in anything (key columns or their order, INCLUDE, the -- WHERE clause, uniqueness) or that is invalid (an interrupted CREATE INDEX CONCURRENTLY) is dropped and built -- again below: IF NOT EXISTS alone would keep it. A development build of 0.7.15 left them without INCLUDE. DO $$ DECLARE r record; BEGIN FOR r IN SELECT c.relname FROM (VALUES ('IX__values__structure_string_object', 'USING btree (_id_structure, _string, _id_object) INCLUDE (_array_index) WHERE ((_string IS NOT NULL) AND (octet_length(_string) < 2000))'), ('IX__values__structure_long_object', 'USING btree (_id_structure, _long, _id_object) INCLUDE (_array_index) WHERE (_long IS NOT NULL)'), ('IX__values__structure_numeric_object', 'USING btree (_id_structure, _numeric, _id_object) INCLUDE (_array_index) WHERE (_numeric IS NOT NULL)'), ('IX__values__structure_double_object', 'USING btree (_id_structure, _double, _id_object) INCLUDE (_array_index) WHERE (_double IS NOT NULL)'), ('IX__values__structure_datetimeoffset_object', 'USING btree (_id_structure, _datetimeoffset, _id_object) INCLUDE (_array_index) WHERE (_datetimeoffset IS NOT NULL)'), ('IX__values__structure_boolean_object', 'USING btree (_id_structure, _boolean, _id_object) INCLUDE (_array_index) WHERE (_boolean IS NOT NULL)'), ('IX__values__structure_guid_object', 'USING btree (_id_structure, _guid, _id_object) INCLUDE (_array_index) WHERE (_guid IS NOT NULL)'), ('IX__values__structure_listitem_object', 'USING btree (_id_structure, _listitem, _id_object) INCLUDE (_array_index) WHERE (_listitem IS NOT NULL)'), ('IX__values__structure_object_object', 'USING btree (_id_structure, _object, _id_object) INCLUDE (_array_index) WHERE (_object IS NOT NULL)') ) x(name, def) JOIN pg_class c ON c.relname = x.name JOIN pg_index i ON i.indexrelid = c.oid AND i.indrelid = '_values'::regclass WHERE NOT i.indisvalid OR i.indisunique OR substring(pg_get_indexdef(c.oid) from 'USING .*') IS DISTINCT FROM x.def LOOP EXECUTE format('DROP INDEX %I', r.relname); END LOOP; END $$; CREATE INDEX IF NOT EXISTS "IX__values__structure_string_object" ON _values (_id_structure, _String, _id_object) INCLUDE (_array_index) WHERE _String IS NOT NULL AND octet_length(_String) < 2000; CREATE INDEX IF NOT EXISTS "IX__values__structure_long_object" ON _values (_id_structure, _Long, _id_object) INCLUDE (_array_index) WHERE _Long IS NOT NULL; CREATE INDEX IF NOT EXISTS "IX__values__structure_numeric_object" ON _values (_id_structure, _Numeric, _id_object) INCLUDE (_array_index) WHERE _Numeric IS NOT NULL; CREATE INDEX IF NOT EXISTS "IX__values__structure_double_object" ON _values (_id_structure, _Double, _id_object) INCLUDE (_array_index) WHERE _Double IS NOT NULL; CREATE INDEX IF NOT EXISTS "IX__values__structure_datetimeoffset_object" ON _values (_id_structure, _DateTimeOffset, _id_object) INCLUDE (_array_index) WHERE _DateTimeOffset IS NOT NULL; CREATE INDEX IF NOT EXISTS "IX__values__structure_boolean_object" ON _values (_id_structure, _Boolean, _id_object) INCLUDE (_array_index) WHERE _Boolean IS NOT NULL; CREATE INDEX IF NOT EXISTS "IX__values__structure_guid_object" ON _values (_id_structure, _Guid, _id_object) INCLUDE (_array_index) WHERE _Guid IS NOT NULL; CREATE INDEX IF NOT EXISTS "IX__values__structure_listitem_object" ON _values (_id_structure, _ListItem, _id_object) INCLUDE (_array_index) WHERE _ListItem IS NOT NULL; CREATE INDEX IF NOT EXISTS "IX__values__structure_object_object" ON _values (_id_structure, _Object, _id_object) INCLUDE (_array_index) WHERE _Object IS NOT NULL; DROP INDEX IF EXISTS "IX__values__String_not_null"; DROP INDEX IF EXISTS "IX__values__Long_not_null"; DROP INDEX IF EXISTS "IX__values__DateTimeOffset_not_null"; DROP INDEX IF EXISTS "IX__values__Numeric_not_null"; DROP INDEX IF EXISTS ix__values__array_null_structure; -- ---------- 1. System infrastructure check ------------------------------ DO $$ BEGIN -- Required system function: scheme metadata reader. Source lives in -- redbPostgre.sql; ships in the generated bundle redb_init.sql. IF NOT EXISTS ( SELECT 1 FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace WHERE p.proname = 'get_scheme_definition' AND n.nspname = 'public' ) THEN RAISE EXCEPTION 'v2-pvt: required system function public.get_scheme_definition(bigint) is missing. Deploy the REDB core schema first (redbPostgre.sql / generated redb_init.sql).'; END IF; -- NOTE: get_object_json() is now OWNED by this module (defined in -- 08_core_object_json.sql), so it is no longer guarded as an external -- prerequisite — it is (re)created later in the same bundle. This lets -- its bug fixes ride the versioned auto-redeploy. -- Required core tables. IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '_objects') THEN RAISE EXCEPTION 'v2-pvt: required table public._objects is missing.'; END IF; IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '_values') THEN RAISE EXCEPTION 'v2-pvt: required table public._values is missing.'; END IF; IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '_structures') THEN RAISE EXCEPTION 'v2-pvt: required table public._structures is missing.'; END IF; IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '_list_items') THEN RAISE EXCEPTION 'v2-pvt: required table public._list_items is missing.'; END IF; IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = '_scheme_metadata_cache') THEN RAISE EXCEPTION 'v2-pvt: required cache table public._scheme_metadata_cache is missing. Deploy redb_metadata_cache.sql first.'; END IF; END $$; -- ---------- 2. DROP every pvt_* function this module owns --------------- -- Universal drop: enumerate all functions in the public schema whose name -- starts with `pvt_` and drop them with their actual signatures. This -- protects the module against signature drift between releases. DO $$ DECLARE r record; BEGIN FOR r IN SELECT p.oid::regprocedure::text AS sig FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace WHERE n.nspname = 'public' AND p.proname LIKE 'pvt\_%' ESCAPE '\' LOOP EXECUTE 'DROP FUNCTION IF EXISTS ' || r.sig || ' CASCADE'; END LOOP; END $$; -- ---------- 3a. Unicode-aware case folding ----------------------------- -- Case folding in PostgreSQL is driven by the collation's ctype. A database -- created with LC_CTYPE=C folds ASCII and nothing else, so on such a database -- 'Привет' ILIKE '%привет%' -> false -- lower('Привет') -> 'Привет' -- while the same statements are correct on a database created with a real -- locale. The fix is to attach an explicit collation to the operand. -- -- The collation name arrives through a GUC rather than a function parameter. -- That is deliberate: every entry point of this module (pvt_build_query_sql and -- friends) would otherwise need an extra argument threaded through five layers, -- which is a signature change on each. The GUC costs nothing at the call sites -- and the C# provider sets it once per connection. -- -- Unset GUC means unchanged behaviour, byte for byte, which is what makes this -- safe to deploy to a database whose owner never asked for it. -- -- STABLE, not IMMUTABLE: the result depends on a run-time setting. CREATE OR REPLACE FUNCTION pvt_fold_case(p_expr text) RETURNS text LANGUAGE plpgsql STABLE AS $BODY$ DECLARE v_collation text; BEGIN -- The second argument makes a missing setting return NULL instead of raising. v_collation := btrim(coalesce(current_setting('redb.string_collation', true), '')); IF v_collation = '' THEN RETURN p_expr; END IF; -- quote_ident is the escaping, not decoration: the name is an identifier and -- therefore cannot be a bound parameter, so it is concatenated into SQL text. -- It doubles embedded quotes, which neutralises an injected name. RETURN '(' || p_expr || ' COLLATE ' || quote_ident(v_collation) || ')'; END; $BODY$; COMMENT ON FUNCTION pvt_fold_case(text) IS 'Wraps a text expression in COLLATE when redb.string_collation is set, so ILIKE/LOWER/UPPER fold every script and not just ASCII. Returns the expression untouched when the GUC is unset.'; -- ---------- pvt_like_escape: make a sugar operand LITERAL under LIKE/ILIKE ---------- -- BR-7 (2026-09-02): $startsWith/$endsWith/$contains (+IgnoreCase; field, class-field, dict, -- array and expression sugar) splice their operand into a LIKE pattern. The operand is a -- LITERAL by contract: '%'/'_' must not act as wildcards and a backslash must not act as -- LIKE's own escape character (PostgreSQL's default escape). The raw $like/$ilike/$matches -- operators do NOT pass through here - their pattern belongs to the caller. CREATE OR REPLACE FUNCTION pvt_like_escape(p_v text) RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE RETURNS NULL ON NULL INPUT AS $$ SELECT replace(replace(replace(p_v, '\', '\\'), '%', '\%'), '_', '\_') $$; COMMENT ON FUNCTION pvt_like_escape(text) IS 'Escapes LIKE metacharacters (backslash, %, _) so a sugar operand matches literally. BR-7 2026-09-02.'; -- ---------- 4. Shared legacy helpers used by pvt_* code ---------------- -- Forked verbatim from sql/deprecated/redb_facets_search.sql. They are -- referenced by pvt_build_inner_condition / pvt_build_single_facet_condition -- and were left in the legacy file before the PG free path was rewritten on -- top of v2-pvt. Kept here (not under deprecated/) so the module is fully -- self-contained — the bundled redb_init.sql no longer ships the legacy -- facets_search file. Names keep the underscore prefix to avoid touching -- every call site inside the pvt_* functions. DROP TYPE IF EXISTS structure_info_type CASCADE; CREATE TYPE structure_info_type AS ( root_structure_id bigint, nested_structure_id bigint, root_type_info jsonb, nested_type_info jsonb ); CREATE OR REPLACE FUNCTION _format_json_array_for_in( array_data jsonb ) RETURNS text LANGUAGE 'plpgsql' IMMUTABLE AS $BODY$ DECLARE in_values text := ''; json_element jsonb; first_item boolean := true; element_text text; BEGIN IF jsonb_typeof(array_data) != 'array' THEN RAISE EXCEPTION 'JSON array expected, got: %', jsonb_typeof(array_data); END IF; FOR json_element IN SELECT value FROM jsonb_array_elements(array_data) LOOP IF NOT first_item THEN in_values := in_values || ', '; END IF; first_item := false; CASE jsonb_typeof(json_element) WHEN 'string' THEN element_text := quote_literal(json_element #>> '{}'); WHEN 'number' THEN element_text := json_element::text; WHEN 'boolean' THEN element_text := CASE WHEN (json_element)::boolean THEN 'true' ELSE 'false' END; ELSE element_text := quote_literal(json_element #>> '{}'); END CASE; in_values := in_values || element_text; END LOOP; RETURN in_values; END; $BODY$; COMMENT ON FUNCTION _format_json_array_for_in(jsonb) IS 'Converts JSONB array to string of values for SQL IN clause. Forked from redb_facets_search.sql into the v2-pvt module bundle (00_module_init.sql).'; -- pvt_resolve_field_path_table: TABLE-returning resolver used by -- 26_pvt_array_groupby.sql. Forked verbatim from -- sql/deprecated/redb_aggregation.sql (resolve_field_path). The PVT module -- also ships pvt_resolve_field_path(bigint, text) RETURNS jsonb (see -- 01_pvt_field_path.sql) — that one mirrors C# SchemeFieldResolver and is -- used by the rest of pvt_*. Keep both: the table form is what the -- array_groupby builder consumes (structure_id / db_type / is_array / -- array_index / dict_key / is_dictionary). CREATE OR REPLACE FUNCTION pvt_resolve_field_path_table( p_scheme_id bigint, p_field_path text ) RETURNS TABLE(structure_id bigint, db_type text, is_array boolean, array_index int, dict_key text, is_dictionary boolean) LANGUAGE plpgsql AS $BODY$ DECLARE v_segments text[]; v_segment text; v_clean_segment text; v_current_parent_id bigint := NULL; v_structure_id bigint; v_db_type text; v_is_collection boolean := false; v_is_dictionary boolean := false; v_found_collection_type bigint; v_array_index int := NULL; v_dict_key text := NULL; v_index_match text[]; v_key_match text[]; v_collection_type_name text; BEGIN v_index_match := regexp_match(p_field_path, '\[(\d+)\]'); IF v_index_match IS NOT NULL THEN v_array_index := v_index_match[1]::int; END IF; v_key_match := regexp_match(p_field_path, '\[([A-Za-z_][A-Za-z0-9_-]*)\]'); IF v_key_match IS NOT NULL THEN v_dict_key := v_key_match[1]; END IF; v_segments := string_to_array(regexp_replace(p_field_path, '\[[^\]]*\]', '', 'g'), '.'); FOREACH v_segment IN ARRAY v_segments LOOP v_clean_segment := trim(v_segment); IF v_clean_segment = '' THEN CONTINUE; END IF; SELECT c._structure_id, c.db_type, c._collection_type INTO v_structure_id, v_db_type, v_found_collection_type FROM _scheme_metadata_cache c WHERE c._scheme_id = p_scheme_id AND c._name = v_clean_segment AND ( (v_current_parent_id IS NULL AND c._parent_structure_id IS NULL) OR c._parent_structure_id = v_current_parent_id ) LIMIT 1; IF v_structure_id IS NULL THEN RAISE EXCEPTION 'Field segment "%" not found in path "%" (scheme=%). Check cache: SELECT * FROM warmup_all_metadata_caches();', v_clean_segment, p_field_path, p_scheme_id; END IF; IF v_found_collection_type IS NOT NULL THEN v_is_collection := true; SELECT t._name INTO v_collection_type_name FROM _types t WHERE t._id = v_found_collection_type; IF v_collection_type_name = 'Dictionary' THEN v_is_dictionary := true; END IF; END IF; v_current_parent_id := v_structure_id; END LOOP; structure_id := v_structure_id; db_type := v_db_type; is_array := v_is_collection OR (p_field_path ~ '\[[^\]]*\]'); array_index := v_array_index; dict_key := v_dict_key; is_dictionary := v_is_dictionary; RETURN NEXT; END; $BODY$; COMMENT ON FUNCTION pvt_resolve_field_path_table(bigint, text) IS 'TABLE-returning field-path resolver forked from redb_aggregation.sql. Consumed by pvt_build_array_groupby_sql (26_pvt_array_groupby.sql).';