Обсуждение: How to use slash commands in a function

Поиск
Список
Период
Сортировка

How to use slash commands in a function

От
vanessa
Дата:
Hi guys,

I was wondering how could i (if at all possible) to use say a command like:
\! touch fred.txt     in a function?
i.e.

CREATE FUNCTION myfunc() RETURNS TRIGGER AS
'BEGIN
        \! touch fred.txt
         RETURN NEW;
END;'
LANGUAGE 'plpgsql';

At the moment i get:
ERROR: syntax error at or near "\"
Does this mean i should encapsulate the line in quotation marks or something
like that?

Cheers.
Vanessa


--
View this message in context: http://www.nabble.com/How-to-use-slash-commands-in-a-function-tf3237240.html#a8997475
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: How to use slash commands in a function

От
Alvaro Herrera
Дата:
vanessa escribió:
>
> Hi guys,
>
> I was wondering how could i (if at all possible) to use say a command like:
> \! touch fred.txt     in a function?
> i.e.
>
> CREATE FUNCTION myfunc() RETURNS TRIGGER AS
> 'BEGIN
>         \! touch fred.txt
>          RETURN NEW;
> END;'
> LANGUAGE 'plpgsql';
>
> At the moment i get:
> ERROR: syntax error at or near "\"
> Does this mean i should encapsulate the line in quotation marks or something
> like that?

No, it means you can't do it at all, because backslash commands are
psql-only, thus you cannot put them in functions (which are
server-executed).  PL/pgSQL functions are trusted, meaning you can't
access the outside world (disk, network, etc) with them.  If you really
need to do that, consider using an untrusted language (C, plperlu, etc).
I think there's even a PL/sh (shell) but I don't think you can use it
for trigger functions.

--
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support