0

I am facing a problem I have a function which used to create schema and tables inside that schema after table creation I am calling a function which supposed to populate this schema however feels like the second function doesn't set the working schema and throws the error that object doesn't exits (

ERROR:  relation "table" does not exist
LINE 1: INSERT INTO table

here is what the function looks like.

    CREATE OR REPLACE FUNCTION create_schema(
        t_shema character varying,
        t_country TEXT
        )
        RETURNS character varying
        LANGUAGE 'plpgsql'
        COST 100
        VOLATILE PARALLEL UNSAFE
    AS $BODY$
    DECLARE
    
        tname text := t_shema;
        tschem_name text := tname||'_work';
        tsql_dyn text ;
        tschema_check numeric := 0 ;
    
     BEGIN
    
        SELECT 1 
          INTO TSCHEMA_CHECK
          FROM PG_NAMESPACE
         WHERE NSPNAME = TSCHEM_NAME;
    
     IF TSCHEMA_CHECK  = 1 THEN 
     
        RETURN 'Schema '||tschem_name ||' Already exists';
        
     ELSE 
        tsql_dyn := 'CREATE SCHEMA '||tschem_name||';';
        raise notice 'EXECUTE %', tsql_dyn;
            EXECUTE tsql_dyn;
    
        tsql_dyn := 'SET search_path TO  '||tschem_name ||';';
        raise notice 'EXECUTE %', tsql_dyn;
            EXECUTE tsql_dyn;
    
    
    --other DDLs
---execute of function which populates freshly created schema

    SELECT public.populate_empty_schema(tname, t_country);
    
    RETURN tname ||' created';
    
    END IF;
    
     END;
    $BODY$;

The second function also has a statement as first which sets the working schema. Both functions work fine if get called separately, trows error only if second get called from the first

2 Answers 2

2

Your function is vulnerable to SQL injection.

Instead of

tsql_dyn := 'CREATE SCHEMA '||tschem_name||';';

write

tsql_dyn := format('CREATE SCHEMA %I', tschem_name);

To set the search_path in populate_empty_schema, pass the schema name to the function and have it execute

PERFORM set_config('search_path', v_schema, TRUE);
Sign up to request clarification or add additional context in comments.

2 Comments

PERFORM set_config('search_path', v_schema, TRUE); in second function still doesn't work, still same error as it doesn't see working schema
Well, you never showed the second function. It should work.
0

feels like Postgres cannot set working schema from the second function, my solution. was just deleting the set search path from the second function and it is working. feels like it couldn't set it as shcema is not committed at the point when the 2-nd function gets called.

2 Comments

There must be a different reason, but I can't tell what's going on.
@LaurenzAlbe probably but I couldn't find it, so at least it is working :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.