pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Problem with trigger function
10+ messages / 8 participants
[nested] [flat]

* Problem with trigger function
@ 2008-09-03 21:12 Mira Dimitrijevic <mira@lanl.gov>
  2008-09-03 21:59 ` Re: Problem with trigger function Stephan Szabo <sszabo@megazone.bigpanda.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Mira Dimitrijevic @ 2008-09-03 21:12 UTC (permalink / raw)
  To: pgsql-general

Hi,
I wrote the trigger function below and when trying to execute it, I  
get the following error:

  15:00:42  [CREATE - 0 row(s), 0.000 secs]  [Error Code: 0, SQL  
State: 42601]  ERROR: syntax error at or near "INSERT"

I am using DBVisualizer's "SQL Commander" window, not the "create  
funciton" option. I know the SQL is correct - I can execute it just  
by itself. The problem is somewhere in the
syntax for a postgres trigger function. Any input on what the problem  
might be would be just fabulous!

Many thanks,
Mira




CREATE OR REPLACE FUNCTION audit_sequence_update() RETURNS trigger AS '
BEGIN
     IF tg_op = ''INSERT'' THEN
         INSERT INTO audit_sequence_update(operation, "day",  
owner_uid, "sequence")
         VALUES
         ('INSERT', NEW."day", NEW.owner_uid, NEW."sequence");
     ELSE
         INSERT INTO audit_sequence_update(operation, "day",  
owner_uid, "sequence")
         SELECT
         'UPDATE', "day", owner_uid, "sequence"
         FROM sequence where isdid=NEW.isdid;
     END IF;
     RETURN NEW;
END
' LANGUAGE plpgsql;



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Problem with trigger function
  2008-09-03 21:12 Problem with trigger function Mira Dimitrijevic <mira@lanl.gov>
@ 2008-09-03 21:59 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Stephan Szabo @ 2008-09-03 21:59 UTC (permalink / raw)
  To: Mira Dimitrijevic <mira@lanl.gov>; +Cc: pgsql-general

On Wed, 3 Sep 2008, Mira Dimitrijevic wrote:

> Hi,
> I wrote the trigger function below and when trying to execute it, I
> get the following error:
>
>   15:00:42  [CREATE - 0 row(s), 0.000 secs]  [Error Code: 0, SQL
> State: 42601]  ERROR: syntax error at or near "INSERT"
>
> I am using DBVisualizer's "SQL Commander" window, not the "create
> funciton" option. I know the SQL is correct - I can execute it just
> by itself. The problem is somewhere in the
> syntax for a postgres trigger function. Any input on what the problem
> might be would be just fabulous!

It looks like you're not always quoting properly if the below is an exact
quote. For example you double the single quotes in the IF tg_op =
''INSERT'' but don't double them in the VALUES ('INSERT').

You might find it easier to use dollar quoting for the function body
(using $$ instead of ' to wrap the body) if you're using a version
that supports it which will mean you shouldn't need to double any of the
quotes inside the function.

> CREATE OR REPLACE FUNCTION audit_sequence_update() RETURNS trigger AS '
> BEGIN
>      IF tg_op = ''INSERT'' THEN
>          INSERT INTO audit_sequence_update(operation, "day",
> owner_uid, "sequence")
>          VALUES
>          ('INSERT', NEW."day", NEW.owner_uid, NEW."sequence");
>      ELSE
>          INSERT INTO audit_sequence_update(operation, "day",
> owner_uid, "sequence")
>          SELECT
>          'UPDATE', "day", owner_uid, "sequence"
>          FROM sequence where isdid=NEW.isdid;
>      END IF;
>      RETURN NEW;
> END
> ' LANGUAGE plpgsql;



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* problem with trigger function
@ 2014-03-07 00:08 Susan Cassidy <susan.cassidy@decisionsciencescorp.com>
  2014-03-07 00:21 ` Re: problem with trigger function Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Susan Cassidy @ 2014-03-07 00:08 UTC (permalink / raw)
  To: pgsql-general

I'm having a problem with a trigger function.  I've been googling for over
an hour, with no luck with my specific problem.

I get this error:
ERROR:  missing FROM-clause entry for table "new"
LINE 1: insert into metric_double_values_201203 values (NEW.metricID...
                                                        ^
QUERY:  insert into metric_double_values_201203 values (NEW.metricID,
NEW.sourceID, NEW.timestamp, NEW.value, NEW.datetimeval)
CONTEXT:  PL/pgSQL function metric_double_insert_func() line 8 at EXECUTE
statement

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: problem with trigger function
  2014-03-07 00:08 problem with trigger function Susan Cassidy <susan.cassidy@decisionsciencescorp.com>
@ 2014-03-07 00:21 ` Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Adrian Klaver @ 2014-03-07 00:21 UTC (permalink / raw)
  To: Susan Cassidy <susan.cassidy@decisionsciencescorp.com>; pgsql-general

On 03/06/2014 04:08 PM, Susan Cassidy wrote:
> I'm having a problem with a trigger function.  I've been googling for
> over an hour, with no luck with my specific problem.
>
> I get this error:
> ERROR:  missing FROM-clause entry for table "new"
> LINE 1: insert into metric_double_values_201203 values (NEW.metricID...
>                                                          ^
> QUERY:  insert into metric_double_values_201203 values (NEW.metricID,
> NEW.sourceID, NEW.timestamp, NEW.value, NEW.datetimeval)
> CONTEXT:  PL/pgSQL function metric_double_insert_func() line 8 at
> EXECUTE statement
>
>
>  From this trigger function:
>
> CREATE OR REPLACE FUNCTION metric_double_insert_func()
> RETURNS TRIGGER AS $$
>    DECLARE insert_sql text;
> BEGIN
>       insert_sql:='insert into metric_double_values_' ||
> to_char(NEW.datetimeval,'YYYYMM') || ' values (NEW.metricID,
> NEW.sourceID, NEW.timestamp, NEW.value, NEW.datetimeval)';
>      EXECUTE insert_sql using NEW;
>      RETURN NULL;
> END;
> $$
> LANGUAGE plpgsql;

The basic problem is here:

insert_sql:='insert into metric_double_values_' || 
to_char(NEW.datetimeval,'YYYYMM') || ' values (NEW.metricID, 
NEW.sourceID, NEW.timestamp, NEW.value, NEW.datetimeval)';

in particular:

  ' values (NEW.metricID,...'

You are quoting the NEW values which Postgres then interprets as values 
coming from the table new as new.metric_id, etc.

You need to use the parameter placeholders,$1, $2, etc. See here for 
some examples:

http://www.postgresql.org/docs/9.3/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-...

40.5.4. Executing Dynamic Commands

>
> DROP TRIGGER insert_metric_double_insert_trigger on metric_double_values;
> CREATE TRIGGER insert_metric_double_insert_trigger
>          BEFORE INSERT ON metric_double_values
>          FOR EACH ROW EXECUTE PROCEDURE metric_double_insert_func();
>
>
> This was an attempt at eliminating the error I got when trying to insert
> with values (NEW.*) using NEW:
> ERROR:  missing FROM-clause entry for table "new"
> LINE 1: insert into metric_double_values_201203 values (NEW.*)
>                                                          ^
> QUERY:  insert into metric_double_values_201203 values (NEW.*)
> CONTEXT:  PL/pgSQL function metric_double_insert_func() line 7 at
> EXECUTE statement
>
> I don't know what from clause it is talking about
>
> This is a trigger for inserting rows into the proper partition table
> based on date.
>
> Any help appreciated.
>
> Thanks,
> Susan


-- 
Adrian Klaver
adrian.klaver@aklaver.com


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Problem with trigger function
@ 2021-02-11 19:47 James B. Byrne <byrnejb@harte-lyne.ca>
  2021-02-11 19:56 ` Re: Problem with trigger function Steve Baldwin <steve.baldwin@gmail.com>
  0 siblings, 1 reply; 10+ messages in thread

From: James B. Byrne @ 2021-02-11 19:47 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

I am trying to implement a trigger in a PostgreSQL-9.6.17 database:

CREATE OR REPLACE FUNCTION customer_num_informix()
  RETURNS trigger AS $$
BEGIN
  -- An Aubit/Informix 4GL INSERT statement passes the value 0 to Informix
  -- DBMS for columns that have the SERIAL data type.  Informix will then
  -- use the incremented serial number in place of 0. PostgreSQL instead
  -- will simply take the value 0 and replace the incremented serial number.
  -- This trigger function emulates the Informix DBMS behaviour.
  --
  -- The NEW variable contains the data for the row to be INSERTed or
  -- UPDATEd. It, and its complement OLD used for UPDATE and DELETE,
  -- are automatically created and populated by PostgreSQL whenever
  -- a data-change function is called.
  --
  IF NEW.customer_num = 0 THEN
    SELECT nextval('customer_customer_num_seq') INTO NEW.customer_customer_num;
  ELSE
    IF NEW.customer_customer_num > 0 THEN
	  PERFORM setval('customer_customer_num_seq', NEW.customer_customer_num);
	END IF;
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

ALTER FUNCTION public.customer_num_informix() OWNER TO hll_4gl_testing;

CREATE TRIGGER customer_num_serial
  BEFORE INSERT ON customer
    FOR EACH ROW EXECUTE customer_num_informix();

The problem is that I am getting a syntax error on the CREATE TRIGGER statement:

ERROR:  syntax error at or near "customer_num_informix"
LINE 3:     FOR EACH ROW EXECUTE customer_num_informix();

I do not see what the error is. What is wrong with the syntax I used?

-- 
***          e-Mail is NOT a SECURE channel          ***
        Do NOT transmit sensitive data via e-Mail
   Unencrypted messages have no legal claim to privacy
 Do NOT open attachments nor follow links sent by e-Mail

James B. Byrne                mailto:ByrneJB@Harte-Lyne.ca
Harte & Lyne Limited          http://www.harte-lyne.ca
9 Brockley Drive              vox: +1 905 561 1241
Hamilton, Ontario             fax: +1 905 561 0757
Canada  L8E 3C3






^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Problem with trigger function
  2021-02-11 19:47 Problem with trigger function James B. Byrne <byrnejb@harte-lyne.ca>
@ 2021-02-11 19:56 ` Steve Baldwin <steve.baldwin@gmail.com>
  2021-02-11 20:04   ` Re: Problem with trigger function Dave Cramer <davecramer@postgres.rocks>
  2021-02-11 20:05   ` Re: Problem with trigger function David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 2 replies; 10+ messages in thread

From: Steve Baldwin @ 2021-02-11 19:56 UTC (permalink / raw)
  To: byrnejb@harte-lyne.ca; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Try ... EXECUTE PROCEDURE customer_num_informix()

Steve

On Fri, Feb 12, 2021 at 6:47 AM James B. Byrne <byrnejb@harte-lyne.ca>
wrote:

> I am trying to implement a trigger in a PostgreSQL-9.6.17 database:
>
> CREATE OR REPLACE FUNCTION customer_num_informix()
>   RETURNS trigger AS $$
> BEGIN
>   -- An Aubit/Informix 4GL INSERT statement passes the value 0 to Informix
>   -- DBMS for columns that have the SERIAL data type.  Informix will then
>   -- use the incremented serial number in place of 0. PostgreSQL instead
>   -- will simply take the value 0 and replace the incremented serial
> number.
>   -- This trigger function emulates the Informix DBMS behaviour.
>   --
>   -- The NEW variable contains the data for the row to be INSERTed or
>   -- UPDATEd. It, and its complement OLD used for UPDATE and DELETE,
>   -- are automatically created and populated by PostgreSQL whenever
>   -- a data-change function is called.
>   --
>   IF NEW.customer_num = 0 THEN
>     SELECT nextval('customer_customer_num_seq') INTO
> NEW.customer_customer_num;
>   ELSE
>     IF NEW.customer_customer_num > 0 THEN
>           PERFORM setval('customer_customer_num_seq',
> NEW.customer_customer_num);
>         END IF;
>   END IF;
>   RETURN NEW;
> END;
> $$ LANGUAGE plpgsql;
>
> ALTER FUNCTION public.customer_num_informix() OWNER TO hll_4gl_testing;
>
> CREATE TRIGGER customer_num_serial
>   BEFORE INSERT ON customer
>     FOR EACH ROW EXECUTE customer_num_informix();
>
> The problem is that I am getting a syntax error on the CREATE TRIGGER
> statement:
>
> ERROR:  syntax error at or near "customer_num_informix"
> LINE 3:     FOR EACH ROW EXECUTE customer_num_informix();
>
> I do not see what the error is. What is wrong with the syntax I used?
>
> --
> ***          e-Mail is NOT a SECURE channel          ***
>         Do NOT transmit sensitive data via e-Mail
>    Unencrypted messages have no legal claim to privacy
>  Do NOT open attachments nor follow links sent by e-Mail
>
> James B. Byrne                mailto:ByrneJB@Harte-Lyne.ca
> Harte & Lyne Limited          http://www.harte-lyne.ca
> 9 Brockley Drive              vox: +1 905 561 1241
> Hamilton, Ontario             fax: +1 905 561 0757
> Canada  L8E 3C3
>
>
>
>

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Problem with trigger function
  2021-02-11 19:47 Problem with trigger function James B. Byrne <byrnejb@harte-lyne.ca>
  2021-02-11 19:56 ` Re: Problem with trigger function Steve Baldwin <steve.baldwin@gmail.com>
@ 2021-02-11 20:04   ` Dave Cramer <davecramer@postgres.rocks>
  1 sibling, 0 replies; 10+ messages in thread

From: Dave Cramer @ 2021-02-11 20:04 UTC (permalink / raw)
  To: Steve Baldwin <steve.baldwin@gmail.com>; +Cc: byrnejb@harte-lyne.ca, "pgsql-generallists.postgresql.org" <pgsql-general@lists.postgresql.org>

FWIW, messing with serial numbers like this is pretty risky.Sequences have
transactional semantics for a reason.

Dave Cramer
www.postgres.rocks


On Thu, 11 Feb 2021 at 14:57, Steve Baldwin <steve.baldwin@gmail.com> wrote:

> Try ... EXECUTE PROCEDURE customer_num_informix()
>
> Steve
>
> On Fri, Feb 12, 2021 at 6:47 AM James B. Byrne <byrnejb@harte-lyne.ca>
> wrote:
>
>> I am trying to implement a trigger in a PostgreSQL-9.6.17 database:
>>
>> CREATE OR REPLACE FUNCTION customer_num_informix()
>>   RETURNS trigger AS $$
>> BEGIN
>>   -- An Aubit/Informix 4GL INSERT statement passes the value 0 to Informix
>>   -- DBMS for columns that have the SERIAL data type.  Informix will then
>>   -- use the incremented serial number in place of 0. PostgreSQL instead
>>   -- will simply take the value 0 and replace the incremented serial
>> number.
>>   -- This trigger function emulates the Informix DBMS behaviour.
>>   --
>>   -- The NEW variable contains the data for the row to be INSERTed or
>>   -- UPDATEd. It, and its complement OLD used for UPDATE and DELETE,
>>   -- are automatically created and populated by PostgreSQL whenever
>>   -- a data-change function is called.
>>   --
>>   IF NEW.customer_num = 0 THEN
>>     SELECT nextval('customer_customer_num_seq') INTO
>> NEW.customer_customer_num;
>>   ELSE
>>     IF NEW.customer_customer_num > 0 THEN
>>           PERFORM setval('customer_customer_num_seq',
>> NEW.customer_customer_num);
>>         END IF;
>>   END IF;
>>   RETURN NEW;
>> END;
>> $$ LANGUAGE plpgsql;
>>
>> ALTER FUNCTION public.customer_num_informix() OWNER TO hll_4gl_testing;
>>
>> CREATE TRIGGER customer_num_serial
>>   BEFORE INSERT ON customer
>>     FOR EACH ROW EXECUTE customer_num_informix();
>>
>> The problem is that I am getting a syntax error on the CREATE TRIGGER
>> statement:
>>
>> ERROR:  syntax error at or near "customer_num_informix"
>> LINE 3:     FOR EACH ROW EXECUTE customer_num_informix();
>>
>> I do not see what the error is. What is wrong with the syntax I used?
>>
>> --
>> ***          e-Mail is NOT a SECURE channel          ***
>>         Do NOT transmit sensitive data via e-Mail
>>    Unencrypted messages have no legal claim to privacy
>>  Do NOT open attachments nor follow links sent by e-Mail
>>
>> James B. Byrne                mailto:ByrneJB@Harte-Lyne.ca
>> Harte & Lyne Limited          http://www.harte-lyne.ca
>> 9 Brockley Drive              vox: +1 905 561 1241
>> Hamilton, Ontario             fax: +1 905 561 0757
>> Canada  L8E 3C3
>>
>>
>>
>>

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Problem with trigger function
  2021-02-11 19:47 Problem with trigger function James B. Byrne <byrnejb@harte-lyne.ca>
  2021-02-11 19:56 ` Re: Problem with trigger function Steve Baldwin <steve.baldwin@gmail.com>
@ 2021-02-11 20:05   ` David G. Johnston <david.g.johnston@gmail.com>
  2021-02-11 20:14     ` Re: Problem with trigger function Steve Baldwin <steve.baldwin@gmail.com>
  1 sibling, 1 reply; 10+ messages in thread

From: David G. Johnston @ 2021-02-11 20:05 UTC (permalink / raw)
  To: Steve Baldwin <steve.baldwin@gmail.com>; +Cc: byrnejb@harte-lyne.ca <byrnejb@harte-lyne.ca>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Thursday, February 11, 2021, Steve Baldwin <steve.baldwin@gmail.com>
wrote:

> Try ... EXECUTE PROCEDURE customer_num_informix()
>


FUNCTION, not PROCEDURE

David J.

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Problem with trigger function
  2021-02-11 19:47 Problem with trigger function James B. Byrne <byrnejb@harte-lyne.ca>
  2021-02-11 19:56 ` Re: Problem with trigger function Steve Baldwin <steve.baldwin@gmail.com>
  2021-02-11 20:05   ` Re: Problem with trigger function David G. Johnston <david.g.johnston@gmail.com>
@ 2021-02-11 20:14     ` Steve Baldwin <steve.baldwin@gmail.com>
  2021-02-11 20:18       ` Re: Problem with trigger function David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Steve Baldwin @ 2021-02-11 20:14 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: byrnejb@harte-lyne.ca <byrnejb@harte-lyne.ca>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

David, from what I can see of the docs, for 9.6 it is PROCEDURE.  It seems
FUNCTION didn't appear until 11.

Steve

On Fri, Feb 12, 2021 at 7:05 AM David G. Johnston <
david.g.johnston@gmail.com> wrote:

>
> On Thursday, February 11, 2021, Steve Baldwin <steve.baldwin@gmail.com>
> wrote:
>
>> Try ... EXECUTE PROCEDURE customer_num_informix()
>>
>
>
> FUNCTION, not PROCEDURE
>
> David J.
>

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Problem with trigger function
  2021-02-11 19:47 Problem with trigger function James B. Byrne <byrnejb@harte-lyne.ca>
  2021-02-11 19:56 ` Re: Problem with trigger function Steve Baldwin <steve.baldwin@gmail.com>
  2021-02-11 20:05   ` Re: Problem with trigger function David G. Johnston <david.g.johnston@gmail.com>
  2021-02-11 20:14     ` Re: Problem with trigger function Steve Baldwin <steve.baldwin@gmail.com>
@ 2021-02-11 20:18       ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 10+ messages in thread

From: David G. Johnston @ 2021-02-11 20:18 UTC (permalink / raw)
  To: Steve Baldwin <steve.baldwin@gmail.com>; +Cc: byrnejb@harte-lyne.ca <byrnejb@harte-lyne.ca>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Thursday, February 11, 2021, Steve Baldwin <steve.baldwin@gmail.com>
wrote:

> David, from what I can see of the docs, for 9.6 it is PROCEDURE.  It seems
> FUNCTION didn't appear until 11.
>

Indeed.  I didn’t pay attention to the version.

David J.

^ permalink  raw  reply  [nested|flat] 10+ messages in thread


end of thread, other threads:[~2021-02-11 20:18 UTC | newest]

Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2008-09-03 21:12 Problem with trigger function Mira Dimitrijevic <mira@lanl.gov>
2008-09-03 21:59 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
2014-03-07 00:08 problem with trigger function Susan Cassidy <susan.cassidy@decisionsciencescorp.com>
2014-03-07 00:21 ` Re: problem with trigger function Adrian Klaver <adrian.klaver@aklaver.com>
2021-02-11 19:47 Problem with trigger function James B. Byrne <byrnejb@harte-lyne.ca>
2021-02-11 19:56 ` Steve Baldwin <steve.baldwin@gmail.com>
2021-02-11 20:04   ` Dave Cramer <davecramer@postgres.rocks>
2021-02-11 20:05   ` David G. Johnston <david.g.johnston@gmail.com>
2021-02-11 20:14     ` Steve Baldwin <steve.baldwin@gmail.com>
2021-02-11 20:18       ` David G. Johnston <david.g.johnston@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox