|
| 1 | +-- Fix: Change language column from regconfig to TEXT to allow pg_upgrade |
| 2 | +-- The regconfig type stores OIDs that change between PostgreSQL versions, |
| 3 | +-- breaking pg_upgrade. Store as TEXT and convert at query time instead. |
| 4 | + |
| 5 | +-- 1. Drop the generated column that depends on language |
| 6 | +ALTER TABLE posts DROP COLUMN search; |
| 7 | + |
| 8 | +-- 2. Change language column from regconfig to TEXT |
| 9 | +-- The regconfig values like 'english'::regconfig convert to their name when cast to TEXT |
| 10 | +ALTER TABLE posts ALTER COLUMN language TYPE TEXT USING language::TEXT; |
| 11 | + |
| 12 | +-- 3. Drop and recreate the mapping function to accept config names directly |
| 13 | +-- Must be IMMUTABLE for use in generated columns |
| 14 | +DROP FUNCTION IF EXISTS map_language_to_tsvector(TEXT); |
| 15 | +CREATE FUNCTION map_language_to_tsvector(config_name TEXT) |
| 16 | +RETURNS regconfig AS $$ |
| 17 | +BEGIN |
| 18 | + RETURN CASE config_name |
| 19 | + WHEN 'arabic' THEN 'arabic'::regconfig |
| 20 | + WHEN 'dutch' THEN 'dutch'::regconfig |
| 21 | + WHEN 'english' THEN 'english'::regconfig |
| 22 | + WHEN 'french' THEN 'french'::regconfig |
| 23 | + WHEN 'german' THEN 'german'::regconfig |
| 24 | + WHEN 'italian' THEN 'italian'::regconfig |
| 25 | + WHEN 'portuguese' THEN 'portuguese'::regconfig |
| 26 | + WHEN 'russian' THEN 'russian'::regconfig |
| 27 | + WHEN 'spanish' THEN 'spanish'::regconfig |
| 28 | + WHEN 'swedish' THEN 'swedish'::regconfig |
| 29 | + WHEN 'turkish' THEN 'turkish'::regconfig |
| 30 | + -- Unsupported languages fall back to 'simple': |
| 31 | + -- Chinese, Czech, Greek, Japanese, Korean, Persian, Polish, Sinhala, Slovak |
| 32 | + ELSE 'simple'::regconfig |
| 33 | + END; |
| 34 | +END; |
| 35 | +$$ LANGUAGE plpgsql IMMUTABLE; |
| 36 | + |
| 37 | +-- 4. Recreate the search column using the IMMUTABLE function |
| 38 | +ALTER TABLE posts ADD search tsvector GENERATED ALWAYS AS ( |
| 39 | + CASE WHEN language <> 'simple' THEN |
| 40 | + setweight(to_tsvector(map_language_to_tsvector(language), title::TEXT), 'A') || |
| 41 | + setweight(to_tsvector(map_language_to_tsvector(language), description), 'B') || |
| 42 | + setweight(to_tsvector('simple'::regconfig, title::TEXT), 'C') || |
| 43 | + setweight(to_tsvector('simple'::regconfig, description), 'D')::tsvector |
| 44 | + ELSE |
| 45 | + setweight(to_tsvector('simple'::regconfig, title::TEXT), 'A') || |
| 46 | + setweight(to_tsvector('simple'::regconfig, description), 'B')::tsvector |
| 47 | + END |
| 48 | +) STORED; |
| 49 | + |
| 50 | +-- 5. Recreate the GIN index |
| 51 | +CREATE INDEX idx_posts_search_gin ON posts USING GIN (search); |
0 commit comments