Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WLiXl-0005wq-Ie for pgsql-general@arkaria.postgresql.org; Fri, 07 Mar 2014 00:21:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WLiXk-0008Ay-UW for pgsql-general@arkaria.postgresql.org; Fri, 07 Mar 2014 00:21:40 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WLiXi-0008An-Ar for pgsql-general@postgresql.org; Fri, 07 Mar 2014 00:21:38 +0000 Received: from new1-smtp.messagingengine.com ([66.111.4.221]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WLiXd-0004Ud-W7 for pgsql-general@postgresql.org; Fri, 07 Mar 2014 00:21:37 +0000 Received: from compute5.internal (compute5.nyi.mail.srv.osa [10.202.2.45]) by gateway1.nyi.mail.srv.osa (Postfix) with ESMTP id 4817E59A for ; Thu, 6 Mar 2014 19:21:29 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute5.internal (MEProxy); Thu, 06 Mar 2014 19:21:30 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; s=mesmtp; bh=IU07hfTGsM1Jh//E0owdM5ea/Yw=; b=KjNzpFbPsCpjQbLcU08icTFIUban 3aiOWPHxHbKbSgP3qczvobrsyiotEVJvdHooh7znOav7b3+Cl/pKzMSp2gZM2sAu kvl3torKW9sEgu76Z8czHCTWJeYez7JT3qMGpFYF7TBans7paly+ZccIMFoVhAkA v/aFD3adNbW9lvM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=message-id:date:from:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=IU07hfTGsM1Jh//E0owdM5 ea/Yw=; b=HOmXYAzAbb3apyes9fnH5I54aaYqOUXDJTaB0iy1Zhcy+qFyBLLwtJ +BbHkj9TYDB4wmHrTc6KE2KEciuK76uZQmwUJHOTr2v0/PoNWromftkIGVrQnrNn xeWHXHKAQzfgRcJ9SpuUXlBkVc6T38VSdacz9taksECt7DPl/J/G0= X-Sasl-enc: jMkpi8+RRw+4jAsBs1zz/vmTTIvGGv4N5nHLr6Hq2H7l 1394151689 Received: from panda.site (unknown [97.113.11.16]) by mail.messagingengine.com (Postfix) with ESMTPA id DF522C007AF; Thu, 6 Mar 2014 19:21:28 -0500 (EST) Message-ID: <53191108.7020908@aklaver.com> Date: Thu, 06 Mar 2014 16:21:28 -0800 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 MIME-Version: 1.0 To: Susan Cassidy , "pgsql-general@postgresql.org" Subject: Re: problem with trigger function References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org 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-DYN 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