pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Susan Cassidy <susan.cassidy@decisionsciencescorp.com>
To: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: problem with trigger function
Date: Thu, 06 Mar 2014 16:21:28 -0800
Message-ID: <53191108.7020908@aklaver.com> (raw)
In-Reply-To: <CAE3Q8on=r2hc+6pJ34C5mR_=JVgiiV5Z0_K1B23rLM7JnUdnFw@mail.gmail.com>
References: <CAE3Q8on=r2hc+6pJ34C5mR_=JVgiiV5Z0_K1B23rLM7JnUdnFw@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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



view thread (10+ messages)  latest in thread

Message-ID: <53191108.7020908@aklaver.com>
Permalink:  ../53191108.7020908@aklaver.com/
Also on:    postgresql.org/message-id/53191108.7020908@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, susan.cassidy@decisionsciencescorp.com
  Subject: Re: problem with trigger function
  In-Reply-To: <53191108.7020908@aklaver.com>
  References: <CAE3Q8on=r2hc+6pJ34C5mR_=JVgiiV5Z0_K1B23rLM7JnUdnFw@mail.gmail.com> <53191108.7020908@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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