PostgreSQL
 SQL >> Database >  >> RDS >> PostgreSQL

Ottenere il nome della funzione al suo interno con PL/pgSQL

A partire da Postgres 9.4, questa funzione restituisce il proprio nome:

CREATE OR REPLACE FUNCTION your_schema.get_curr_fx_name()
RETURNS text AS $$
DECLARE
    stack text;
    fcesig text;
BEGIN
    GET DIAGNOSTICS stack = PG_CONTEXT;
    fcesig := substring(stack from 'function (.*?) line');
    RETURN fcesig::regprocedure::text;
END;
$$ LANGUAGE plpgsql;

Il trucco sta in GET DIAGNOSTICS ... = PG_CONTEXT, che espone lo stack di chiamate corrente: da lì si estrae la firma della funzione e la si converte in testo tramite regprocedure, ottenendo anche il nome dello schema.