Search code examples
pythonpostgresqlpython-2.7multicorn

Pass variable to multicorn foreign data wrapper


I am trying to pass my fdw an option who's value is a variable. My instinct is to do it like this, but it says that there is a syntax error at x abc. What do I need to do to get this to work?

DO $$
DECLARE abc varchar :='hi';
Begin
    drop server partner cascade;
    create server partner foreign data wrapper multicorn options(
    wrapper 'multicorn.fdw.Fdw'
);
create foreign table partner_accounts(
    name text
) server partner options(
    x abc
);
END $$

Solution

  • Tried getting something similar to work once and finally went with executing a formatted query:

    DO $$
    DECLARE abc varchar :='hi';
    BEGIN
        drop server partner cascade;
        create server partner foreign data wrapper multicorn options(
        wrapper 'multicorn.fdw.Fdw'
    );
    EXECUTE format('create foreign table partner_accounts(
        name text
    ) server partner options(
        x %L
    )', abc);
    END $$
    

    %L formats a value as an SQL literal, so 'hi' in this case. You can't use USING and parameter symbols with utility statements, so you have to insert the value textually.