Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gXZD6-0006O5-9X for pgsql-bugs@arkaria.postgresql.org; Thu, 13 Dec 2018 22:12:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gXZD3-000213-Jk for pgsql-bugs@arkaria.postgresql.org; Thu, 13 Dec 2018 22:11:57 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gXZD3-00020w-Bg for pgsql-bugs@lists.postgresql.org; Thu, 13 Dec 2018 22:11:57 +0000 Received: from mail-ed1-x531.google.com ([2a00:1450:4864:20::531]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gXZCw-0003GQ-Jz for pgsql-bugs@postgresql.org; Thu, 13 Dec 2018 22:11:56 +0000 Received: by mail-ed1-x531.google.com with SMTP id y56so3333313edd.11 for ; Thu, 13 Dec 2018 14:11:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WZzLApZt/QiI55KsmnyzymUzkkVGApVoV0vpNMH49T4=; b=NohJ8c/HWEsfYbw7zX1d8z/bfwnHWptb+EuVLGnkBWztqzH2VmFKdE8/LpbF7TTcqF 7wQZwH267ysMLboXcN79+Ho/USfR7rc8IUxj/m2boEr58LOr8ECZGLGCoUJRu7kR/Tg7 eRUiT6uwYhL5IWN9/3SzrK1/Y4sBVYxKcFXWvhfSKSLzre93XEq7nHhS609d795O37PU zhHCgEToBOtOrUsRysr+aLwbUYliFqx21KmPt+puPwgVOSpK/lhbgzgSEM28Ju59Vb9D 1raqtNe/OiIrEEJ4XxV4/e03oAPPQc6SG2/l201U6j+qZehDWEVKUTYgyG92wta/WTwH q9bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WZzLApZt/QiI55KsmnyzymUzkkVGApVoV0vpNMH49T4=; b=oHs9QE6ck9r6Euf4r65Wty45opzpQF/W1WOvxRfp36WJNlXcZ62UtRZBZ0a/4r6ZNW xzln5Q8SvPGqhPwvB77T5N1k2U3eU7y5peFlal7jDoF8LIjqmU92jEJ9XcYm7fG9+T7o WHFPB6RtbkfM/picv6ySI/aKy8/NEtu8A/lH1XSsRuGaT533if5zcJexPxELekw+zRLZ nbTn1DgD3ggPibqMrwtshvHMoGHPbqZliRxXpLpYArKwHfmRT4Zf+wtEk62Rv4udaQoY LrjRm6eayxMMAxliEfARAzveOgMkJVxZKWYSESWcN4cwoJJVkQgvUrSKWeJe3fTQk1Rm fwNA== X-Gm-Message-State: AA+aEWaLjZqoYHX0FXrbKqOaPR1HtidU7V3z4upq6de/sfTAFEvfeoLD N2whpHsTTZTp9hZ6i/c0A1iv0XZFtvc= X-Google-Smtp-Source: AFSGD/UAnhcalSkV6xXthgU9AfcM6IOiwlWttm6g5iZsmf8VhC1g2dZc0DwHPPy5Nz326zMcfBs+GQ== X-Received: by 2002:a17:906:71cd:: with SMTP id i13-v6mr660526ejk.37.1544739108289; Thu, 13 Dec 2018 14:11:48 -0800 (PST) Received: from station53.ousa.org ([2.89.154.1]) by smtp.gmail.com with ESMTPSA id x30sm1033221edd.30.2018.12.13.14.11.46 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 13 Dec 2018 14:11:47 -0800 (PST) From: Stuart To: Amit Langote Cc: pgsql-bugs@postgresql.org Subject: Re: Errors creating partitioned tables from existing using (LIKE Date: Fri, 14 Dec 2018 01:11:43 +0300 Message-ID: <1894770.yaXzFAPJxP@station53.ousa.org> In-Reply-To: <977c72a1-8d56-b77c-37de-808627033654@lab.ntt.co.jp> References: <2047094.V130LYfLq4@station53.ousa.org> <977c72a1-8d56-b77c-37de-808627033654@lab.ntt.co.jp> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2018 Zvita 13, China 12:03:35 +03 Amit Langote wrote: > Hi, >=20 > On 2018/12/13 5:00, Stuart wrote: > > ousa_new=3D# \d+ knowledge_vectors_old > >=20 > > Table > > "public.knowledge_v= ec > > tors_old" > > =20 > > Column | Type | Collation | Nullable | = =20 > > Default | Storage | Stats target | Description>= =20 > > ---------------+-----------------------------+-----------+----------+--= =2D-- > > ---------------------------+----------+--------------+------------->=20 > > entry_date | timestamp(3) with time zone | | not null | > > now() | plain | | revision_da= te > > | timestamp(3) with time zone | | | NULL::timestamp > > with time zone | plain | | entered_by | text = =20 > > | | not null | "current_user"() | > > extended | | revised_by | text = |=20 > > | | ''::text | extended | = =20 > > | source_id | bigint | | = =20 > > | | plain | | object_= id > > | bigint | | not null | = =20 > > | plain | | description | text = =20 > > | | | ''::text = =20 > > | extended | | vectors | tsvector = =20 > > | | not null | | extended | = =20 > > |>=20 > > Indexes: > > "knowledgevectorsold_pk" PRIMARY KEY, btree (object_id), tablespace > > "pgindex" "knowledgevectorsold_vector_idx" gin (vectors), tablespace > > "pgindex">=20 > > Check constraints: > > "knowledgevectors_vectors_ck" CHECK (vectors <> ''::tsvector) > >=20 > > Foreign-key constraints: > > "knowledgevectorsold_objectid_fk" FOREIGN KEY (object_id) REFERENCES > > knowledge(object_id)>=20 > > Triggers: > > knowledgevectors_revision_tr BEFORE UPDATE ON knowledge_vectors_old > > FOR EACH ROW EXECUTE PROCEDURE revised()>=20 > > Inherits: ousa_objects > >=20 > > ousa_new=3D# alter table knowledge_vectors_old rename constraint > > knowledgevectors_vectors_ck to knowledgevectorsold_vectors_ck ; ALTER > > TABLE > >=20 > > ousa_new=3D# create table knowledge_vectors (like knowledge_vectors_old > > INCLUDING ALL ) PARTITION BY RANGE ( object_id ) ; ERROR: constraint > > "knowledgevectors_vectors_ck" for table "knowledge_vectors_old" does not > > exist > >=20 > > Only after I dropped the new constraint and recreated it, did the create > > table (like ) work. > Thanks for the report. >=20 > There is a bug here, but it's not of CREATE TABLE. It is rather of ALTER > TABLE RENAME CONSTRAINT, which fails to reflect the changed constraint > name in the target table's relation info cache. Here is another > reproducer of this behavior: >=20 > create table foo (a int, constraint check_a check (a > 0)); > alter table foo rename CONSTRAINT check_a to check_a_gt_zero; > -- in the same session > create table bar (like foo including all); > ERROR: constraint "check_a" for table "foo" does not exist >=20 > What might be worse is that if you specify INCLUDING CONSTRAINTS (not > ALL), it proceeds with creating the constraint with the outdated name: >=20 > create table bar (like foo including constraints); > \d bar > Table "public.bar" > Column =E2=94=82 Type =E2=94=82 Collation =E2=94=82 Nullable =E2=94= =82 Default > =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80 > a =E2=94=82 integer =E2=94=82 =E2=94=82 =E2=94=82 > Check constraints: > "check_a" CHECK (a > 0) >=20 > What's happening here is that when the ALTER TABLE RENAME CONSTRAINT is > followed by CREATE TABLE (LIKE .. INCLUDING ALL) in the same session, the > latter is referring to *stale* information about constraints of the source > table. You said it works correctly after you drop and re-create the > constraint, but that's only because ALTER TABLE DROP/ADD CONSTRAINT will > correctly invalidate the cached information, so that subsequent CREATE > TABLE sees the correct information from the updated cache. The way to fix > it is to teach ALTER TABLE RENAME CONSTRAINT to reset the cached > information. >=20 > Attached a patch which does that. With the patch: >=20 > create table foo (a int, constraint check_a check (a > 0)); > alter table foo rename CONSTRAINT check_a to check_a_gt_zero; > create table bar (like foo including all); > \d bar > Table "public.bar" > Column =E2=94=82 Type =E2=94=82 Collation =E2=94=82 Nullable =E2=94= =82 Default > =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80 > a =E2=94=82 integer =E2=94=82 =E2=94=82 =E2=94=82 > Check constraints: > "check_a_gt_zero" CHECK (a > 0) >=20 > -- INCLUDING CONSTRAINTS works correctly too > drop table bar; > create table bar (like foo including constraints); > \d bar > Table "public.bar" > Column =E2=94=82 Type =E2=94=82 Collation =E2=94=82 Nullable =E2=94= =82 Default > =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=BC=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=BC=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80 > a =E2=94=82 integer =E2=94=82 =E2=94=82 =E2=94=82 > Check constraints: > "check_a_gt_zero" CHECK (a > 0) >=20 > Thanks, > Amit Thanks Amit.