Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Disable triggers

    Code Block
    languagesql
    collapsetrue
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE TRIGGER audit_item;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE  TRIGGER check_statistical_code_references_on_insert;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE  TRIGGER check_statistical_code_references_on_update;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE  TRIGGER set_id_in_jsonb;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE  TRIGGER set_item_md_json_trigger;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE  TRIGGER set_item_md_trigger;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE  TRIGGER set_item_ol_version_trigger;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE  TRIGGER update_item_references;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE  TRIGGER update_item_status_date;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE  TRIGGER updatecompleteupdateddate_item_delete;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item DISABLE  TRIGGER updatecompleteupdateddate_item_insert_update;


  2. Create uuid_range table

    Code Block
    languagesql
    collapsetrue
    create table public.uuid_range
    (
        id             serial primary key,
        partition      uuid,
        subrange_start uuid,
        subrange_end   uuid,
        completed      boolean default false
    );-- If table exists and filled then do:
    UPDATE public.uuid_range SET completed=false;
    -- If table was just created then fill it:
    
    DO
    $$
        DECLARE
            partition_start UUID;
            subrange_start  UUID;
            subrange_end    UUID;
        BEGIN
            FOR i IN 0..15
                LOOP
                    partition_start := (rpad(to_hex(i), 8, '0') || '-0000-0000-0000-000000000000')::UUID;
                    subrange_start := partition_start;
    
                    FOR j IN 1..4096
                        LOOP
                            IF i < 15 OR (i = 15 AND j < 4096) THEN
                                if (j < 4096) then
                                    subrange_end := (to_hex(i)::text || rpad(lpad(to_hex(j), 3, '0'), 7, '0') ||
                                                     '-0000-0000-0000-000000000000')::UUID;
                                else
                                    subrange_end := (rpad(to_hex(i + 1), 8, '0') || '-0000-0000-0000-000000000000')::UUID;
                                end if;
                            ELSE
                                subrange_end := 'ffffffff-ffff-ffff-ffff-ffffffffffff'::UUID; -- upper bound for last subrange in last partition
                            END IF;
    
                            INSERT INTO public.uuid_range (partition, subrange_start, subrange_end)
                            VALUES (partition_start, subrange_start, subrange_end);
    
                            subrange_start := subrange_end;
                        END LOOP;
                END LOOP;
        END;
    $$
    LANGUAGE plpgsql; 


  3. Create/replace function to recalculate effective values and shelving order in items

    Code Block
    languagesql
    SET search_path = <tenant>_mod_inventory_storage;
    
    CREATE OR REPLACE FUNCTION set_item_effective_values(holding jsonb, item jsonb)
      RETURNS jsonb AS $$
      BEGIN
        RETURN set_effective_shelving_order(
              item
              || jsonb_build_object('effectiveLocationId',
                   COALESCE(item->>'temporaryLocationId', item->>'permanentLocationId', holding->>'effectiveLocationId'))
              || effective_call_number_components(holding, item));
      END;
    $$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE STRICT;


  4. Update step should be executed for each partition created from step #2. Update script can be executed concurrently.

    Code Block
    languagesql
    collapsetrue
    set search_path = "{tenant}_mod_inventory_storage", "public";
    
    do
    $$
        declare
            partition_to_process uuid := '{partition}'::uuid;
            range_rec           public.uuid_range%rowtype;
        begin
            for range_rec in select ur.*
                             from public.uuid_range ur
                             where ur.partition = partition_to_process
                               and ur.completed = false
                loop
                    --             start transaction;
                    UPDATE item i SET jsonb = set_item_effective_values(item_data.holdings, item_data.item)
    				FROM (
    					select item.id, hr.jsonb as holdings, item.jsonb as item from holdings_record hr
    					join item on item.holdingsRecordId = hr.id
                    	where item.id between range_rec.subrange_start and range_rec.subrange_end
                    	and item.jsonb->'effectiveCallNumberComponents'->'callNumber' IS NOT NULL
    				) AS item_data
    				WHERE i.id = item_data.id;
     
                    update uuid_range
                    set completed = true
                    where id = range_rec.id;
                    --
                    raise notice 'subrange id: % completed', range_rec.id;
                    commit;
                end loop;
        end
    $$;


  5. Monitor how many sub-partitions in partitions are left to be processed

    Code Block
    languagesql
    collapsetrue
    select partition, count(partition)
    from public.uuid_range
    where completed = false
    group by partition
    order by partition;  


  6. Enable triggers

    Code Block
    languagesql
    collapsetrue
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER audit_item;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER check_statistical_code_references_on_insert;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER check_statistical_code_references_on_update;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER set_id_in_jsonb;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER set_item_md_json_trigger;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER set_item_md_trigger;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER set_item_ol_version_trigger;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER update_item_references;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER update_item_status_date;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER updatecompleteupdateddate_item_delete;
    
    ALTER TABLE {tenant}_mod_inventory_storage.item ENABLE TRIGGER updatecompleteupdateddate_item_insert_update;


...