Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ecB9v-0006Pu-Dq for pgsql-general@arkaria.postgresql.org; Thu, 18 Jan 2018 14:27:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ecB9t-0000gm-QT for pgsql-general@arkaria.postgresql.org; Thu, 18 Jan 2018 14:27:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ec9UA-0004i1-FL for pgsql-general@lists.postgresql.org; Thu, 18 Jan 2018 12:40:02 +0000 Received: from mail-wr0-x22a.google.com ([2a00:1450:400c:c0c::22a]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ec9U8-0003Vt-2I for pgsql-general@postgresql.org; Thu, 18 Jan 2018 12:40:01 +0000 Received: by mail-wr0-x22a.google.com with SMTP id 100so22371525wrb.7 for ; Thu, 18 Jan 2018 04:39:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=yhQpcBzTCREp96/9ooMZvSc1xTzMZtxsRugidwZanxE=; b=r/oWj1/WdK+9CyiKQ1kW641P8vsoAj8DBwJ4bBKKPJWuy8pDBCgLDlSLWa9fWvXQmK nF6FQSgirkHTValM/o3AjCLSZBVLEgbnzJCXAY4ch//ZG/WHiptUBS/TGFl7ivsPCe9h NtBLZsFw0jJLK1G3+sXAuTAjR3S6zqZwbjR04YMitb74m1NMXAo2iy0KU3ZV6Oo0eGV5 6Ym5V/JFwgUS59p8JcVv/45leDioaHXLtk/QkGHCYZ2UWD8eg5uvAr5702TA11xvhGnd XIGLxvYIF6/MNYe6Sqh3RXO++23FowdRWO8HzjbyrrUMvRKhKHOhvxIJuGzFN8Fonq1Q qVKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=yhQpcBzTCREp96/9ooMZvSc1xTzMZtxsRugidwZanxE=; b=Rw/WYhxQyx91lcbHdxMixXrSegd9DjUQpZg4zd8H05hyb4P6qVV7SbBV0Y6CL0a+XM 4nh3/l1v95LXyPaY02mkR10Fm1OyED9kqepEdTChhG4gBlTW4T7Yjoxna32V44pnAqyX O6kWC4wMkiev6C7o0ZCGFXq/Ll6D7X+G3zo494mfwSgXpfJJ/R+z8VRuJ86AEOlzyksk ZN5awZI7YXLJ39kuQEvbA18mr2fVyEuvrNVk+9ahdvPrvBCkco24tpawkP1ym14Jcc8G eGuDKS15XcF2CpklE0HDH92tWDcz+LynYy5FYGXRsnamVYfkDFywv1gWcScG6bs7+sWS OimA== X-Gm-Message-State: AKwxytcOkcQ7MMIIRtd+YwHE7T6PPS0/zRs3dXSQ+1Unnh32AwUGxmkS sJloRgrjNEFYXRjIDGsmIIl/AE2M X-Google-Smtp-Source: ACJfBosyxYTjs0jVDwk1SzBLk++vaM/q3S0wJPCfm2v07vkqcDgQtx8BLhY7pyOdw3TdLWo7kb4flg== X-Received: by 10.223.175.102 with SMTP id z93mr5666870wrc.48.1516279198328; Thu, 18 Jan 2018 04:39:58 -0800 (PST) Received: from [192.168.92.93] (ppp-static2-67.tis-dialog.ru. [83.219.159.67]) by smtp.gmail.com with ESMTPSA id 25sm4897037wrx.73.2018.01.18.04.39.57 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Jan 2018 04:39:57 -0800 (PST) To: pgsql-general@postgresql.org From: Nikolay Subject: Partitioning constraints vs before-trigger Message-ID: Date: Thu, 18 Jan 2018 14:39:55 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------DFFE3DE80F03D52B9E534678" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------DFFE3DE80F03D52B9E534678 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi all, Could anybody explain, what happens first: constraint check or before-trigger execution? I have a table, partitioned by date: CREATE TABLE foo (   unid text NOT NULL,   logtime timestamp with time zone NOT NULL,   size integer,   CONSTRAINT foo_pkey PRIMARY KEY (unid) ); -- There is an before-insert trigger which works perfectly, creates a new monthly partition if neccessary and inserts new record into the partition. -- Here is how partitions look like: CREATE TABLE foo_2018_01 (   CONSTRAINT foo_2018_01_pkey PRIMARY KEY (unid),   CONSTRAINT foo_2018_01_logtime_check CHECK (logtime >= '2018-01-01 00:00:00+00'::timestamp with time zone AND logtime < '2018-02-01 00:00:00+00'::timestamp with time zone) ) INHERITS (foo); I cannot change anything in the application, as it's proprietary. So I had to do partitioning myself with a trigger. Now there's a new problem. It looks like the application sometimes do UPDATEs to the "logtime" column, which I use for partitioning. So the application can do something like UPDATE foo SET logtime='2017-12-01 00:00:00+00', size=5 WHERE unid='blahblablah', althrough this record had logtime='2018-01-18 00:00:00+00' and was in different partition. In such case, I can see the error (and transaction aborts): ERROR:  new row for relation "foo_2018_01" violates check constraint "foo_2018_01_logtime_check" For business logic, it wouldn't be critical if I forbid/undo/replace modification of logtime column. But other columns must be updated by the application when neccessary. Now I need to ignore new value for "logtime" column for every UPDATE to table "foo". Here is my idea: CREATE OR REPLACE FUNCTION logtime_update_trigger() RETURNS trigger AS $BODY$ BEGIN   IF (NEW.logtime != OLD.logtime) THEN     NEW.logtime := OLD.logtime;   END IF;   RETURN NEW; END; $BODY$ LANGUAGE plpgsql VOLATILE; CREATE TRIGGER trg_foo_update BEFORE UPDATE ON foo   FOR EACH ROW EXECUTE PROCEDURE logtime_update_trigger(); Unfortunately, it seems like this trigger is not even being executed and I still get the same error: ERROR:  new row for relation "foo_2018_01" violates check constraint "foo_2018_01_logtime_check" I suppose that's because contraint check is performed before the trigger is fired? Is there any workarounds here? I also tried to create a rule: CREATE OR REPLACE RULE test_rule AS ON UPDATE TO foo    WHERE new.logtime <> old.logtime DO INSTEAD UPDATE foo SET size = new.size WHERE foo.unid = old.unid AND foo.logtime = old.logtime; But then I get recursion error: ERROR: infinite recursion detected in rules for relation "foo" Possibly because the recursion analysis doesn't take WHERE condition into account. Any help would be greatly appreciated. PostgreSQL version: 9.0.1 on CentOS 5 i686. Best regards, Nikolay Karikh. --------------DFFE3DE80F03D52B9E534678 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

Hi all,

Could anybody explain, what happens first: constraint check or before-trigger execution?

I have a table, partitioned by date:

CREATE TABLE foo
(
  unid text NOT NULL,
  logtime timestamp with time zone NOT NULL,
  size integer,
  CONSTRAINT foo_pkey PRIMARY KEY (unid)
);

-- There is an before-insert trigger which works perfectly, creates a new monthly partition if neccessary and inserts new record into the partition.
-- Here is how partitions look like:

CREATE TABLE foo_2018_01
(
  CONSTRAINT foo_2018_01_pkey PRIMARY KEY (unid),
  CONSTRAINT foo_2018_01_logtime_check CHECK (logtime >= '2018-01-01 00:00:00+00'::timestamp with time zone AND logtime < '2018-02-01 00:00:00+00'::timestamp with time zone)
)
INHERITS (foo);

I cannot change anything in the application, as it's proprietary. So I had to do partitioning myself with a trigger.

Now there's a new problem. It looks like the application sometimes do UPDATEs to the "logtime" column, which I use for partitioning.

So the application can do something like UPDATE foo SET logtime='2017-12-01 00:00:00+00', size=5 WHERE unid='blahblablah', althrough this record had logtime='2018-01-18 00:00:00+00' and was in different partition.

In such case, I can see the error (and transaction aborts):

ERROR:  new row for relation "foo_2018_01" violates check constraint "foo_2018_01_logtime_check"

For business logic, it wouldn't be critical if I forbid/undo/replace modification of logtime column. But other columns must be updated by the application when neccessary.

Now I need to ignore new value for "logtime" column for every UPDATE to table "foo".

Here is my idea:

CREATE OR REPLACE FUNCTION logtime_update_trigger() RETURNS trigger AS
$BODY$
BEGIN
  IF (NEW.logtime != OLD.logtime) THEN
    NEW.logtime := OLD.logtime;
  END IF;
  RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;

CREATE TRIGGER trg_foo_update BEFORE UPDATE ON foo
  FOR EACH ROW EXECUTE PROCEDURE logtime_update_trigger();


Unfortunately, it seems like this trigger is not even being executed and I still get the same error:

ERROR:  new row for relation "foo_2018_01" violates check constraint "foo_2018_01_logtime_check"

I suppose that's because contraint check is performed before the trigger is fired? Is there any workarounds here?

I also tried to create a rule:

CREATE OR REPLACE RULE test_rule AS ON UPDATE TO foo
   WHERE new.logtime <> old.logtime DO INSTEAD
UPDATE foo SET size = new.size WHERE foo.unid = old.unid AND foo.logtime = old.logtime;

But then I get recursion error:

ERROR: infinite recursion detected in rules for relation "foo"

Possibly because the recursion analysis doesn't take WHERE condition into account.

Any help would be greatly appreciated.

PostgreSQL version: 9.0.1 on CentOS 5 i686.

Best regards, Nikolay Karikh.


--------------DFFE3DE80F03D52B9E534678--