Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VN3oJ-0003vN-NL for pgsql-sql@arkaria.postgresql.org; Fri, 20 Sep 2013 16:44:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VN3oJ-0003d5-7c for pgsql-sql@arkaria.postgresql.org; Fri, 20 Sep 2013 16:44:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VN3oI-0003cz-Io for pgsql-sql@postgresql.org; Fri, 20 Sep 2013 16:44:02 +0000 Received: from mail-pa0-f52.google.com ([209.85.220.52]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VN3oE-0007Ob-Mb for pgsql-sql@postgresql.org; Fri, 20 Sep 2013 16:44:01 +0000 Received: by mail-pa0-f52.google.com with SMTP id kq13so922567pab.11 for ; Fri, 20 Sep 2013 09:43:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:user-agent:date:subject:from:to:message-id :thread-topic:in-reply-to:mime-version:content-type :content-transfer-encoding; bh=2FRJdJHRwajmMpg/txYVq466VNT1ak81cW1NhwIwJnE=; b=WnLhyFXhrFTAW0dNViY+5C1MgTHsNqEGokHs5YlSpx+Inghk9Z/7FhulB5GoRq5HV4 Qknz6LKT3RV6QV3fQHx9RPfcB0I6I0PnAK4XD/T0n4tKUJDY8k7JNDt2RlPiQNZGLVKe KlJ/w9ZpOX1djK7QKVYfcVVt7+tPzYLNoXFKzSYTPobWLhC/7Il9beZmJ7cqoDpKTZ4L cN/ImzDLnsXrpsHgPyVtldc5N9/imjQfOq2Jv+HFGq9+hYrj21Vefq/EbW6bUXa9so8H NL1EZUEHlBTo8NV24mzPuXww360RQ/HVqWSCmBWRWI1R3EI4BC6BaD255676xCSkvVOY tZtQ== X-Gm-Message-State: ALoCoQm8rWAV02tkTWUV+vQrXLqtq/2aqbbvPXyw4psSv11LzLdWrF5t3xlLDy1O7gPVL431Tk6t X-Received: by 10.68.200.34 with SMTP id jp2mr8967438pbc.53.1379695437016; Fri, 20 Sep 2013 09:43:57 -0700 (PDT) Received: from [10.0.1.13] (adsl-074-245-040-156.sip.clt.bellsouth.net. [74.245.40.156]) by mx.google.com with ESMTPSA id xe9sm20181048pab.0.1969.12.31.16.00.00 (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 20 Sep 2013 09:43:56 -0700 (PDT) User-Agent: Microsoft-MacOutlook/14.3.7.130812 Date: Fri, 20 Sep 2013 12:43:47 -0400 Subject: the value of OLD on an initial row insert From: James Sharrett To: Message-ID: Thread-Topic: the value of OLD on an initial row insert In-Reply-To: Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org I have a number of trigger functions on a table that are performing various calculations. The table is a column-wise orientation with multiple columns that could be updated on a single row. In one of the triggers, I'm performing a calculation but don't want the code to run if the OLD and NEW values are the same value. This can be resulting from other triggers that are running on the table. If there is a truly NEW (non-NULL) value, I want to run the code. To deal with this, I'm using the following test in my code where I loop through the columns that could be updated and test to determine which column on the row is getting a value assigned. EXECUTE 'SELECT (' ||quote_literal(NEW) || '::' || TG_RELID::regclass ||').' || quote_ident(metric_record.column_name) INTO changed_metric; if not changed_metric is null then EXECUTE 'SELECT (' ||quote_literal(OLD) || '::' || TG_RELID::regclass ||').' || quote_ident(metric_record.column_name) INTO old_value; if changed_metric <> old_value then {calculation code} This is all doing exactly what I want when the row exists. However, I think I'm getting an error if there is a new row getting generated. I'm getting the following error when the code runs sometimes: ERROR: record "old" is not assigned yet SQL state: 55000 Detail: The tuple structure of a not-yet-assigned record is indeterminate. Is this what's happening? If so, how can I avoid the issue. Thanks, James -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql