Notiffy problem

Started by adasko98almost 14 years ago9 messagesgeneral
Jump to latest
#1adasko98
adasko.86@gmail.com

Hi
In first sorry for my english :) I have got a problem with notify/listener.
I do a function which returns a trigger. Everything is ok but when i want
send in a second parameter a variable NOTIFY say: "syntax error"

This is works example with no variable:
CREATE OR REPLACE FUNCTION notify_demo()
RETURNS trigger AS
$BODY$
DECLARE
BEGIN
Notify demoApp, 'some text';
RETURN null;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

This is example with variable (not work):
CREATE OR REPLACE FUNCTION notify_demo()
RETURNS trigger AS
$BODY$
DECLARE
n_user text;
BEGIN
n_user :='sda';
Notify demoApp, n_user ; <----here is a problem
RETURN null;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Notiffy-problem-tp5714744.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

#2Richard Huxton
dev@archonet.com
In reply to: adasko98 (#1)
Re: Notiffy problem

On 29/06/12 09:01, adasko98 wrote:

Hi
In first sorry for my english :) I have got a problem with notify/listener.
I do a function which returns a trigger. Everything is ok but when i want
send in a second parameter a variable NOTIFY say: "syntax error"

Notify demoApp, 'some text';

n_user :='sda';
Notify demoApp, n_user ;<----here is a problem

Looks like a limitation of the plpgsql parser, perhaps even counts as a
bug. You can work around it with EXECUTE though, something like:
cmd := 'NOTIFY demoApp, ' || quote_literal(n_user);
EXECUTE cmd;
or just
EXECUTE 'NOTIFY demoApp, ' || quote_literal(n_user);

--
Richard Huxton
Archonet Ltd

#3adasko98
adasko.86@gmail.com
In reply to: Richard Huxton (#2)
Re: Notiffy problem

Thanks for your answer. Now it works.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Notiffy-problem-tp5714744p5714750.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

#4Merlin Moncure
mmoncure@gmail.com
In reply to: Richard Huxton (#2)
Re: Notiffy problem

On Fri, Jun 29, 2012 at 3:29 AM, Richard Huxton <dev@archonet.com> wrote:

On 29/06/12 09:01, adasko98 wrote:

Hi
In first sorry for my english :) I have got a problem with
notify/listener.
I do a function which returns a trigger. Everything is ok but when i want
send in a second parameter a variable NOTIFY say: "syntax error"

    Notify demoApp, 'some text';

       n_user :='sda';
    Notify demoApp, n_user ;<----here is a problem

Looks like a limitation of the plpgsql parser, perhaps even counts as a bug.
You can work around it with EXECUTE though, something like:
 cmd := 'NOTIFY demoApp, ' || quote_literal(n_user);
 EXECUTE cmd;
or just
 EXECUTE 'NOTIFY demoApp, ' || quote_literal(n_user);

also see pg_notify() function.

merlin

#5adasko98
adasko.86@gmail.com
In reply to: Merlin Moncure (#4)
Re: Notiffy problem

On Fri, Jun 29, 2012 at 3:29 AM, Richard Huxton <dev@archonet.com> wrote:

On 29/06/12 09:01, adasko98 wrote:

Hi
In first sorry for my english :) I have got a problem with
notify/listener.
I do a function which returns a trigger. Everything is ok but when i want
send in a second parameter a variable NOTIFY say: "syntax error"

    Notify demoApp, 'some text';

       n_user :='sda';
    Notify demoApp, n_user ;<----here is a problem

Looks like a limitation of the plpgsql parser, perhaps even counts as a
bug.
You can work around it with EXECUTE though, something like:
 cmd := 'NOTIFY demoApp, ' || quote_literal(n_user);
 EXECUTE cmd;
or just
 EXECUTE 'NOTIFY demoApp, ' || quote_literal(n_user);

also see pg_notify() function.

Yes i'm looking for that. But i connect c# application with postgres and
pg_notify() don't work with my notify event. Anyway thanks for help

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Notiffy-problem-tp5714744p5714782.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

#6Merlin Moncure
mmoncure@gmail.com
In reply to: adasko98 (#5)
Re: Notiffy problem

On Fri, Jun 29, 2012 at 8:58 AM, adasko98 <adasko.86@gmail.com> wrote:

On Fri, Jun 29, 2012 at 3:29 AM, Richard Huxton <dev@archonet.com> wrote:

On 29/06/12 09:01, adasko98 wrote:

Hi
In first sorry for my english :) I have got a problem with
notify/listener.
I do a function which returns a trigger. Everything is ok but when i want
send in a second parameter a variable NOTIFY say: "syntax error"

    Notify demoApp, 'some text';

       n_user :='sda';
    Notify demoApp, n_user ;<----here is a problem

Looks like a limitation of the plpgsql parser, perhaps even counts as a
bug.
You can work around it with EXECUTE though, something like:
 cmd := 'NOTIFY demoApp, ' || quote_literal(n_user);
 EXECUTE cmd;
or just
 EXECUTE 'NOTIFY demoApp, ' || quote_literal(n_user);

also see pg_notify() function.

Yes i'm looking for that. But i connect c# application with postgres and
pg_notify() don't work with my notify event. Anyway thanks for help

huh? pg_notify() is just an alternate way of sending notifications.
it should be available from any client stack that allows calling
custom backend functions(including C#).

postgres=# listen test;
LISTEN
postgres=# select pg_notify('test', 'hello!');
pg_notify
-----------

(1 row)

Asynchronous notification "test" with payload "hello!" received from
server process with PID 31740.

merlin

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Merlin Moncure (#6)
Re: Notiffy problem

Merlin Moncure <mmoncure@gmail.com> writes:

On Fri, Jun 29, 2012 at 8:58 AM, adasko98 <adasko.86@gmail.com> wrote:

Notify demoApp, n_user ;<----here is a problem

Looks like a limitation of the plpgsql parser, perhaps even counts as a
bug.

It is not a bug, but a documented limitation of the NOTIFY command: the
payload has to be a simple string literal.

(The technical reason for that is that NOTIFY isn't a plannable
statement, but a utility command, and utility commands generally don't
evaluate expressions. In principle we could fix that, but in practice
it's not going to change, because the pg_notify() function serves just
fine for every case where you want a non-constant payload.)

regards, tom lane

#8adasko98
adasko.86@gmail.com
In reply to: Tom Lane (#7)
Re: Notiffy problem

Hi thanks for help. Now i know why pg_notify() does not works for me. I'm
named listener in c# code 'Demo' and this is a problem. In name can't be a
capital letter because postges change this name to small letter i think. So
if someone want use pg_notify use only small letter like this
"pg_notify('demo', variable);" and it will be works

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Notiffy-problem-tp5714744p5715157.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

#9Kevin Grittner
Kevin.Grittner@wicourts.gov
In reply to: adasko98 (#8)
Re: Notiffy problem

adasko98 wrote:

Hi thanks for help. Now i know why pg_notify() does not works for
me. I'm named listener in c# code 'Demo' and this is a problem. In
name can't be a capital letter because postges change this name to
small letter i think. So if someone want use pg_notify use only
small letter like this "pg_notify('demo', variable);" and it will
be works

The channel for a LISTEN or NOTIFY command is an identifier, so it
follows the normal rules for identifiers, including folding to lower
case if not enclosed in quotes. The pg_notify() function wraps the
given channel name in quotes if needed. Most people find it easiest
to keep all identifiers lower case to avoid such issues.

-Kevin