Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gFn2h-0003sT-Bi for pgsql-admin@arkaria.postgresql.org; Thu, 25 Oct 2018 21:19:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gFn2e-0003fz-Pk for pgsql-admin@arkaria.postgresql.org; Thu, 25 Oct 2018 21:19:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gFn25-0001X6-Fq for pgsql-admin@lists.postgresql.org; Thu, 25 Oct 2018 21:19:09 +0000 Received: from smtp5-g21.free.fr ([212.27.42.5]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gFn21-0006JU-UP for pgsql-admin@postgresql.org; Thu, 25 Oct 2018 21:19:07 +0000 Received: from firost (unknown [90.38.160.95]) (Authenticated sender: ioguix) by smtp5-g21.free.fr (Postfix) with ESMTPSA id D02D15FFB7; Thu, 25 Oct 2018 23:18:58 +0200 (CEST) Date: Thu, 25 Oct 2018 23:16:55 +0200 From: "Jehan-Guillaume (ioguix) de Rorthais" To: legrand legrand Cc: pgsql-admin@postgresql.org Subject: Re: Is there a work around for partition key needed for constraint Message-ID: <20181025231655.615d80c2@firost> In-Reply-To: <1540501366915-0.post@n3.nabble.com> References: <1540407880009-0.post@n3.nabble.com> <1540501366915-0.post@n3.nabble.com> X-Mailer: Claws Mail 3.17.1 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, 25 Oct 2018 14:02:46 -0700 (MST) legrand legrand wrote: > Maybe there is a solution equivalent to ON CONFLICT DO NOTHING, > with some BEFORE INSERT TRIGGERS returning NULL when row already exists. > > Something like: > > create table tabpart (account_id integer not null, customer_id integer not > null, date_added date ) partition by list (date_added); > create table tabpart1 partition of tabpart for values in ('2018-10-24'); > create table tabpart2 partition of tabpart for values in ('2018-10-25'); > ... > create index tabpart_index_id on tabpart(account_id,customer_id); > > CREATE OR REPLACE FUNCTION f_check_pkey() RETURNS TRIGGER AS $$ > BEGIN > IF count(1) >= 1 FROM tabpart WHERE account_id = NEW.account_id > AND customer_id = NEW.customer_id > THEN > RETURN NULL; > ELSE > RETURN NEW; > END IF; > END; > $$ LANGUAGE plpgsql ; It seems to me you lack some locking to avoid duplicates. cf. my blog post we discussed few weeks ago. (btw, sorry, I somehow missed your answers on previous thread b/c holidays).