PL / PGSQL - How to make the SP "comite" the data

1

Good afternoon, I have an SP (SP1) that invokes two other SP (SP2 and SP3). SP2 is invoked first from SP1, and updates a record in a table. SP3 is then invoked from SP1, and queries that same record, but returns the previous state as a result, that is, it does not take the change (update) made by SP2. Supposedly, when executing an SP, within the SP itself the changes are not comitted, but when leaving, but that is not happening. Yes it is changed when the main SP (SP1) ends. Do you have any idea how to make changes to SP2 comiteed so that they "see" in the execution of SP3 ?. Thank you,     Diego

SP code

CREATE OR REPLACE FUNCTION admin_siias.f_fin_ok_dw (p_instancia_ejecucion_proceso bigint, p_job character varying, p_transformacion character varying, p_fecha_ejecucione date, p_basee character varying, p_moduloe character varying, p_procesoe character varying)
 RETURNS numeric
 LANGUAGE plpgsql
 IMMUTABLE STRICT
AS $function$

DECLARE

    v_Descripcion varchar(1000);
    v_fecha_ini timestamp;
    v_cant_registros bigint;
    v_Estado varchar(100); 
    v_Procedimiento varchar(100);
    v_Nivel varchar(100);   

    v_dw_org numeric;
    v_termina_carga_dw numeric;
    v_fecha_actual date;
    p_resultado numeric;

BEGIN


    v_Procedimiento = 'FIN DE CARGA DW';  
    v_Nivel = 'DWH';    

    v_fecha_ini := (SELECT clock_timestamp());  
    v_termina_carga_dw := 0;
    v_fecha_actual := coalesce (nullif (p_fecha_ejecucione::varchar, '')::date, now()::date);

    v_termina_carga_dw := (SELECT admin_siias.f_termina_carga_dw (v_fecha_actual, p_basee, p_moduloe, p_procesoe));

    if v_termina_carga_dw <> -1 then

        v_dw_org := (SELECT admin_siias.f_verifico_fin_carga (v_fecha_actual, p_basee, p_moduloe, p_procesoe));

        -- -------------------------------------------------------------
        -- Inicio Auditoría 
        -- -------------------------------------------------------------        
            v_Descripcion := 'Fin ' || p_procesoe || ' : '|| v_dw_org::varchar;
            v_cant_registros := 0;          
            v_Estado := 'FINALIZADO';

            PERFORM  etl_siias.sp_etl_registroauditorialog (p_instancia_ejecucion_proceso, v_Descripcion, v_fecha_ini, v_cant_registros, v_Estado, p_job, p_transformacion, v_Procedimiento, v_Nivel);                                                                              
            v_fecha_ini := (SELECT clock_timestamp());  
        -- -------------------------------------------------------------
        -- Fin Auditoría 
        -- -------------------------------------------------------------

    else

        -- -------------------------------------------------------------
        -- Inicio Auditoría 
        -- -------------------------------------------------------------        
            v_Descripcion :='Error en ADMIN_SIIAS.F_TERMINA_CARGA_DW()';
            v_cant_registros := 0;          
            v_Estado := 'ERROR';

            PERFORM etl_siias.sp_etl_registroauditorialog (p_instancia_ejecucion_proceso, v_Descripcion, v_fecha_ini, v_cant_registros, v_Estado, p_job, p_transformacion, v_Procedimiento, v_Nivel);                                                                               
            v_fecha_ini := (SELECT clock_timestamp());  
        -- -------------------------------------------------------------
        -- Fin Auditoría 
        -- -------------------------------------------------------------

    end if;

    p_resultado := v_termina_carga_dw;      

    RETURN p_resultado;


EXCEPTION 

    -- Si el proceso Falla
    WHEN others THEN

    -- -------------------------------------------------------------
        -- Inicio Auditoría 
        -- -------------------------------------------------------------        
            v_Descripcion := SQLERRM;
            v_cant_registros := 0;          
            v_Estado := 'ERROR';

            PERFORM etl_siias.sp_etl_registroauditorialog (p_instancia_ejecucion_proceso, v_Descripcion, v_fecha_ini, v_cant_registros, v_Estado, p_job, p_transformacion, v_Procedimiento, v_Nivel);
        -- -------------------------------------------------------------
        -- Fin Auditoría 
        -- -------------------------------------------------------------

    p_resultado := -1;      -- Error Proceso

    RETURN p_resultado;

END;
$function$
CREATE OR REPLACE FUNCTION admin_siias.f_termina_carga_dw(fecha_ejecucione date, basee character varying, moduloe character varying, procesoe character varying)
 RETURNS numeric
 LANGUAGE plpgsql
AS $function$

declare

    hay_tupla numeric;
    hay_tupla_sig numeric;
    v_mes_siguiente numeric;
    v_fecha_desde date;
    v_fecha_hasta date;

begin

    hay_tupla := 0;
    hay_tupla_sig := 0;
    v_mes_siguiente := 0;
    v_fecha_desde := null;
    v_fecha_hasta := null;

    -- Verifica si hay proceso pendiente
    select
        count(*)
    into
        hay_tupla 
    from
        admin_siias.control_procesos
    where 
        estado = 7 
    and proceso = procesoe 
    and modulo  = moduloe 
    and base_datos = basee
    and fecha_ejecucion::date <= now()::date;    

    if (hay_tupla >= 1) then

        -- Obtiene la información del proceso pendiente
        select
            mes_siguiente, fecha_desde, fecha_hasta
        into
            v_mes_siguiente, v_fecha_desde, v_fecha_hasta
        from
            admin_siias.control_procesos
        where 
            estado = 7 
        and proceso = procesoe 
        and modulo = moduloe 
        and base_datos = basee
        and fecha_ejecucion::date <= now()::date
        and fecha_desde = (
                select
                    max (b.fecha_desde) 
                from
                    admin_siias.control_procesos b
                where 
                    b.estado = 7
                and b.proceso = procesoe 
                and b.modulo = moduloe 
                and b.base_datos = basee 
                and fecha_ejecucion::date <= now()::date    
            );    

        -- Verifica si hay tupla para el siguiente mes
        select
            count (1)
        into
            hay_tupla_sig
        from
            admin_siias.control_procesos
        where
            proceso = procesoe 
        and fecha_desde = (v_fecha_hasta + (v_mes_siguiente * '1 month'::interval))::date;--add_months (v_fecha_hasta, v_mes_siguiente);

        if ( v_mes_siguiente > 0 and hay_tupla_sig = 0 ) -- no existe ya una tupla para ese proceso para el mes siguiente
        then

            -- Inserta la tupla para el siguiente mes
            INSERT INTO admin_siias.control_procesos
            SELECT 
                modulo, 
                proceso, 
                '9999-12-31'::date as fecha_ejecucion, 
                null::date as fecha_hora_inicio, 
                null::date as fecha_hora_fin, 
                7 as estado, 
                (fecha_hasta + (mes_siguiente * '1 month'::interval))::date as fecha_desde,--add_months (fecha_hasta, mes_siguiente) as fecha_desde, 
                (fecha_hasta + ((mes_siguiente + frecuencia - 1) * '1 month'::interval))::date as fecha_hasta,--add_months (fecha_hasta, mes_siguiente) as fecha_hasta, 
                descripcion,
                now() as fecha_alta, 
                base_datos,
                proceso_stg, 
                fecha_hora_fin_stg, 
                tipo_tabla, 
                fase, 
                padre, 
                frecuencia, 
                mes_siguiente
            FROM
                admin_siias.control_procesos
            WHERE
                estado = 7
            AND proceso = procesoe 
            AND modulo = moduloe 
            AND base_datos = basee
            AND fecha_ejecucion::date <= now()::date
            AND fecha_desde = v_fecha_desde;

        end if;

        -- Termina la tupla pendiente
        update 
            admin_siias.control_procesos
        set 
            fecha_hora_fin = now(), 
            estado = 8
        where 
            estado = 7 
        and proceso = procesoe 
        and modulo = moduloe 
        and base_datos = basee
        and fecha_ejecucion::date <= now()::date
        and fecha_desde = v_fecha_desde;   

        return 1;

    else

        return 2;
        -- no hay tupla programada   

    end if;

end;

$function$
CREATE OR REPLACE FUNCTION admin_siias.f_verifico_fin_carga(fecha_ejecucione date, basee character varying, moduloe character varying, procesoe character varying)
 RETURNS numeric
 LANGUAGE plpgsql
 IMMUTABLE STRICT
AS $function$

DECLARE

    hay_tupla numeric;
    resultado numeric;
    v_fecha_carga_desde date;

BEGIN

    -- Asignación de valores a variables locales

    hay_tupla := 0;
    resultado := 0;

    SELECT  fecha_desde::date
    INTO    v_fecha_carga_desde
    FROM    admin_siias.fecha_carga;

    SELECT  count (*)
    INTO    hay_tupla 
    FROM    admin_siias.control_procesos    
    WHERE 
            estado = 8 
    AND     proceso = procesoE 
    AND     base_datos = baseE 
    AND     modulo = moduloE
--  AND     fecha_ejecucion::date <= fecha_ejecucionE::date
    AND     fecha_desde::date = v_fecha_carga_desde::date;  -- Se sustituye la función F_FECHA_CARGA_DESDE() por la consulta directa en la variable v_fecha_carga_desde.

    IF (hay_tupla >= 1) THEN
        resultado := 1;
    END IF;

    RETURN resultado;

EXCEPTION 

    WHEN others then 

    RETURN -1; -- Error Proceso

END;

$function$
    
asked by Diego 17.05.2017 в 18:33
source

1 answer

2

I already saw what it was: IMMUTABLE STRICT. It does not take the changes made in the function itself.

    
answered by 18.05.2017 в 17:46