Many apologies! I thought that this definition worked, but on trying it again later, it didn't - I must have had a different definition (probably using a constant instead of the var in the copy statement) lying around in PSQL. However, as penance for this flagrant disinformation, I have found a solution. It's not pretty tho!
CREATE OR REPLACE FUNCTION test__upload(varchar) RETURNS integer
AS '
DECLARE
p_filename ALIAS FOR $1;
BEGIN
EXECUTE ''COPY mydb FROM ''''''
|| p_filename
|| '''''' DELIMITERS ''''|'''' '';
RETURN 0;
END;' LANGUAGE 'plpgsql';
Nice... Anyway, I think the root of your problem was that COPY
will only accept a literal string for the filename and not a variable. The above nastiness creates the appropriate COPY
command by interpolating the variable into the right position and executes it uses PL/PGSQLs dynamic query interpretation. Be extra careful with the quotes - PL/PGSQL gets a bit hairy when you want to quote quoted quotes...
I hope this works for you, I tested it on PostgreSQL 7.2.4. Apologies again for the earlier mistake!
PS - the above definition works for text
as well as varchar
, I think.