Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b5Ehk-000879-9g for pgsql-admin@arkaria.postgresql.org; Tue, 24 May 2016 15:57:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b5Ehj-0008CS-Rn for pgsql-admin@arkaria.postgresql.org; Tue, 24 May 2016 15:57:11 +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 1b5Ehj-0008CD-AY for pgsql-admin@postgresql.org; Tue, 24 May 2016 15:57:11 +0000 Received: from mail-pf0-x22f.google.com ([2607:f8b0:400e:c00::22f]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1b5Ehf-0003wA-Fw for pgsql-admin@postgresql.org; Tue, 24 May 2016 15:57:10 +0000 Received: by mail-pf0-x22f.google.com with SMTP id b124so8290800pfb.0 for ; Tue, 24 May 2016 08:57:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:references:in-reply-to:subject:date:message-id :mime-version:content-transfer-encoding:thread-index :content-language; bh=Eb/S36ETFpg+Yx/8TlLKENUKlBLchJpimhpybnLwxB4=; b=iiPvfAebNQ9TFnjhcUvpvUh3Md0sHpyPQfmTQZv6ts1avtWUBKOFE5rO1W6Ejr/rNv cSycI3nxm/t22uqVHBPvpFvFLXUwCYLlq7OhK8sVGfBX0JqaELumEMTlfxLGIxkAZnMa vv78f52u1z8AovUq6xXPpradAlA6WKb70dXZJ4AgOwTxogUmrLJADNd/9yOqWCocok9k 4wqR2eHERfN1mS0g2UqkNu+UIlCFMy03cXKHkJiJ/H2Glcw6aZ9iYU+n3c7i73rNrkrR W0Xnt14yUaWYE4CDHjHqB0Zx1U5HjR8vnE7U3aMopFA69aahXpQMnzLPKXhfVA49m+2P w6Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:references:in-reply-to:subject:date :message-id:mime-version:content-transfer-encoding:thread-index :content-language; bh=Eb/S36ETFpg+Yx/8TlLKENUKlBLchJpimhpybnLwxB4=; b=NRSaYb483v/cYkUuuI/MOG2nGl0a87DPNk8cUd9rX6A0RtrNGqITXmkVyAbhyQ48T8 W8G8R6sgJlCZDxkuzC60sQ4nZ0RkNvdVvMTq8xr9SFIKid2gnvZx8Pb1JHzRFuR+ImP+ P1Fjb6mQQhYUkXUJqmVk2+PjTpTurhfHe+Q++lqeM0+vvDMfTlXJqMaSF/tTlqs7yPjp xrf47RzPhuLsYlWbvIr04G6SWCtUqc5wlemdEppsV5ncxSVhBlr3TosfbqClqJ6G7No4 ll12gATuBJ/yEuW7/IVOeMC0MUk13frK2XGg4oLZX669R6RPg4b6NBErpsj5sLV1Ir1b GS6g== X-Gm-Message-State: ALyK8tIU+5TuvSS+J3dDm2PWolrui6H5K3RjhJCMouXeZJAYCMsqUb1v5kyK9PXiPNgtew== X-Received: by 10.98.79.199 with SMTP id f68mr8109185pfj.44.1464105424915; Tue, 24 May 2016 08:57:04 -0700 (PDT) Received: from Samsung ([123.24.17.180]) by smtp.gmail.com with ESMTPSA id t8sm6281843paw.16.2016.05.24.08.57.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 24 May 2016 08:57:03 -0700 (PDT) From: "Hoai Nam" To: "'Albe Laurenz'" Cc: References: In-Reply-To: Subject: Re: The problem is related to concurrent resquests Date: Tue, 24 May 2016 22:56:47 +0700 Message-ID: <005301d1b5d4$e2434a50$a6c9def0$@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 15.0 Thread-Index: AQJAfIt5yz4TomieJ73NOhflbNRpaAH2q+/iAdApSisBiBhfIwGjEVBjAczB58kCr/oyAAKBuUm/Ai8PY8MBWvag6Z5e1kpg Content-Language: en-us X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org Many thank you, Mr.Laurenz This is useful for me. I will apply this to my DB. Best regards Nam -----Original Message----- From: Albe Laurenz [mailto:laurenz.albe@wien.gv.at]=20 Sent: Tuesday, May 24, 2016 5:32 PM To: 'Nguyen Hoai Nam *EXTERN*' Cc: pgsql-admin@postgresql.org Subject: RE: [ADMIN] The problem is related to concurrent resquests Please keep the list posted! Nguyen Hoai Nam wrote: > Step1: I would to create "network" table with three columns including=20 > id, network_id, subnet. It's like below: >=20 > +--------------------+---------------+ > | id |network_id | subnet | > +------------------------------------+ > | 1 | aa |192.168.1.0/24 | > | | | | > | | | | > +--------+-----------+---------------+ >=20 > This table have condition: If a new record with overlap subnet and=20 > same value's network_id then DB will not allow inster to DB >=20 > For example: >=20 > Request1: test=3D> INSERT INTO network VALUES (2, aa,'192.168.1.0/24');=20 > The result is that DB doesn't allow to insert to DB. Becase it violate=20 > overlap CIDR and same value's network_id >=20 > Request2: test=3D> INSERT INTO network VALUES (3, bb,'192.168.1.0/24');=20 > The result is that DB ALLOW to insert to DB. Because this reqest has=20 > network_id =3D bb, this value is different with existing value (aa) This is getting more difficult, but you can *still* do it with an exclusion= constraint in PostgreSQL. You need to install an extension with a GiST ope= rator class for varchar: test=3D# CREATE EXTENSION btree_gist; Then you can do the following: CREATE TABLE network ( id integer PRIMARY KEY, network_id varchar(20) NOT NULL, subnet cidr NOT NULL ); ALTER TABLE network ADD CONSTRAINT network_subnet_excl EXCLUDE USING gist ( network_id gist_text_ops WITH =3D, subnet inet_ops WITH && ); Then you get: test=3D> INSERT INTO network VALUES (1, 'aa','192.168.1.0/24'); INSERT 0 1 test=3D> INSERT INTO network VALUES (2, 'aa', '192.168.1.0/24'); ERROR: conflicting key value violates exclusion constraint "network_subnet= _excl" DETAIL: Key (network_id, subnet)=3D(aa, 192.168.1.0/24) conflicts with exi= sting key (network_id, subnet)=3D(aa, 192.168.1.0/24). test=3D> INSERT INTO network VALUES (3, 'bb', '192.168.1.0/24'); INSERT 0 1 As Kevin said, using SERIALIZABLE transactions is an alternative, but a con= straint is probably better. Yours, Laurenz Albe --=20 Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-admin