pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
To: Michael Paquier <michael.paquier@gmail.com>
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Cc: gomer94@yandex.ru, PostgreSQL mailing lists <pgsql-bugs@postgresql.org>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Amit Langote <amitlangote09@gmail.com>
Subject: Re: BUG #14866: The generated constraint in the typed table causes the server to crash
Date: Thu, 9 Nov 2017 14:39:33 +0900
Message-ID: <c9be4efd-ea9e-16a7-8fd4-76ec8b04f66d@lab.ntt.co.jp> (raw)
In-Reply-To: <CAB7nPqSofu3i4kc1fEHpPTyDYKZ_DEEgE+n5uL_FKToMUYJ6DA@mail.gmail.com>
References: <20171023074458.1473.25799@wrigleys.postgresql.org>
	<CAB7nPqTtWi3S7ew6aCQqkHiUos5n=jigS=GMpV2vEbQCfU1qqA@mail.gmail.com>
	<CAB7nPqS+ofdMRy4+pNrLDyN6My6xDutX5SN_ObbJdKasYNH7wQ@mail.gmail.com>
	<8dd10932-16f0-7e07-6fd2-be33cbea962e@2ndquadrant.com>
	<CAB7nPqSofu3i4kc1fEHpPTyDYKZ_DEEgE+n5uL_FKToMUYJ6DA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Sorry Michael.  I didn't notice this in my gmail inbox until today.

On 2017/11/01 23:05, Michael Paquier wrote:
> On Wed, Nov 1, 2017 at 1:36 PM, Peter Eisentraut
> <peter.eisentraut@2ndquadrant.com> wrote:
>> On 10/23/17 18:21, Michael Paquier wrote:
>>> Attached is a patch to address the problem. There are a couple of
>>> things to consider:
>>> - transformColumnDefinition is missing the fact that a type may not be
>>> set for a column defined, and as far as I can see the type name is
>>> needed beforehand to allow the generation of all the serial commands.
>>> This can happen when using CREATE TABLE OF, as you reported, for which
>>> the data type can be found in the type defined. But this can happen as
>>> well when declaring a child partition.
>>
>> I wonder whether we should even allow this.  The SQL standard does not
>> allow identity columns in typed tables, so there is support for that.
> 
> OK. At the same time being able to support that is not complicated
> either, even if the patch I sent earlier is a bit grotty in the way it
> does handle it. I can see arguments in favor of either solution.

Agree that it's not that hard to support IDENTITY on typed table columns.

I rewrote your patch a bit so that it now makes transformColumnDefinition
use the ColumnDef initialized by transformOfType which contains enough
information to get the needed type OID.  I think you might find it a bit
simpler. :)

Also, I carved it out into the attached patch 0001.

>> I'm not sure whether it makes sense in partitions.  You are supposed to
>> insert through the partition root, so making identity columns in
>> partitions would just be confusing.
> 
> Robert, Amit, do you have opinions on the matter? It is possible to
> have multiple level of partitions as well.

IMHO, we should support IDENTITY on partitions, just like we support
specifying DEFAULT on partitions.  We support issuing direct INSERT on
partitions no matter where in the hierarchy it is, so having these
features work normally makes sense to me.

I looked at your patch and saw a minor problem with it.  With your patch,
there exists a lock-strength-upgrade deadlock hazard.  It is making an
earlier phase of partition creation (transformCreateStmt) take an
AccessShareLock on the parent table, whereas a later phase
(DefineRelation) will take an AccessExclusiveLock.  I modified your patch
to take the AccessExclusiveLock to begin with and added a comment nearby
clarifying the same.

You'll find the updated version in attached 0002.

Sorry again for the delay in replying.

Thanks,
Amit

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments: download all (2 files, .tar.gz)

0001-Make-IDENTITY-work-correctly-with-typed-tables.patch (text/plain, 6.1K) download From a6caf5eac2741759c29255d562571f60878515f5 Mon Sep 17 00:00:00 2001 From: amit <amitlangote09@gmail.com> Date: Thu, 9 Nov 2017 11:31:58 +0900 Subject: [PATCH 1/2] Make IDENTITY work correctly with typed tables In the case of CREATE TABLE OF, transformColumnDefinition should look at the ColumnDef added by transformOfType(), not the one passed down by gram.y which is mostly dummy. Authors: Michael Paquier, Amit Langote --- src/backend/commands/tablecmds.c | 1 + src/backend/parser/parse_utilcmd.c | 53 ++++++++++++++++++++++++++++++---- src/test/regress/expected/identity.out | 21 ++++++++++++++ src/test/regress/sql/identity.sql | 11 +++++++ 4 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index b7ddb335d2..192de790c4 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -1754,6 +1754,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence, coldef->raw_default = restdef->raw_default; coldef->cooked_default = restdef->cooked_default; coldef->constraints = restdef->constraints; + coldef->identity = restdef->identity; coldef->is_from_type = false; list_delete_cell(schema, rest, prev); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 30fc2d9ff8..1be36b846a 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -92,6 +92,7 @@ typedef struct IndexStmt *pkey; /* PRIMARY KEY index, if any */ bool ispartitioned; /* true if table is partitioned */ PartitionBoundSpec *partbound; /* transformed FOR VALUES */ + bool ofType; /* true if statement contains OF typeName */ } CreateStmtContext; /* State shared by transformCreateSchemaStmt and its subroutines */ @@ -240,6 +241,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString) cxt.alist = NIL; cxt.pkey = NULL; cxt.ispartitioned = stmt->partspec != NULL; + cxt.ofType = stmt->ofTypename != NULL; /* * Notice that we allow OIDs here only for plain tables, even though @@ -659,13 +661,8 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column) case CONSTR_IDENTITY: { - Type ctype; Oid typeOid; - ctype = typenameType(cxt->pstate, column->typeName, NULL); - typeOid = HeapTupleGetOid(ctype); - ReleaseSysCache(ctype); - if (saw_identity) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), @@ -674,6 +671,51 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column) parser_errposition(cxt->pstate, constraint->location))); + /* + * If this is CREATE TABLE OF, use the ColumnDef added by + * transformOfType(). + */ + if (cxt->ofType) + { + ListCell *lc; + ColumnDef *col; + + foreach (lc, cxt->columns) + { + col = lfirst_node(ColumnDef, lc); + + /* + * ColumnDef's coming from the type, which have + * a valid typeName set, are ahead in the list; + * only look at those. + */ + if (col->typeName && + strcmp(col->colname, column->colname) == 0) + break; + } + + /* + * None of the ColumnDef's coming from the type + * matched. + */ + if (col->typeName == NULL) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_COLUMN), + errmsg("column \"%s\" does not exist", + column->colname))); + + typeOid = col->typeName->typeOid; + } + else + { + Type ctype; + + ctype = typenameType(cxt->pstate, column->typeName, NULL); + typeOid = HeapTupleGetOid(ctype); + ReleaseSysCache(ctype); + } + + Assert(OidIsValid(typeOid)); generateSerialExtraStmts(cxt, column, typeOid, constraint->options, true, NULL, NULL); @@ -2697,6 +2739,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, cxt.pkey = NULL; cxt.ispartitioned = (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE); cxt.partbound = NULL; + cxt.ofType = false; /* * The only subtypes that currently require parse transformation handling diff --git a/src/test/regress/expected/identity.out b/src/test/regress/expected/identity.out index 5fa585d6cc..787edd4d06 100644 --- a/src/test/regress/expected/identity.out +++ b/src/test/regress/expected/identity.out @@ -333,3 +333,24 @@ SELECT * FROM itest8; RESET ROLE; DROP TABLE itest8; DROP USER regress_user1; +-- typed table +CREATE TYPE itest_type AS (f1 integer, f2 text, f3 bigint); +CREATE TABLE itest9 OF itest_type ( + f4 WITH OPTIONS GENERATED ALWAYS AS IDENTITY); -- error +ERROR: column "f4" does not exist +CREATE TABLE itest9 OF itest_type ( + f2 WITH OPTIONS GENERATED ALWAYS AS IDENTITY); -- error +ERROR: identity column type must be smallint, integer, or bigint +CREATE TABLE itest9 OF itest_type ( + f1 WITH OPTIONS GENERATED ALWAYS AS IDENTITY); -- ok +\d itest9 + Table "public.itest9" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+------------------------------ + f1 | integer | | not null | generated always as identity + f2 | text | | | + f3 | bigint | | | +Typed table of type: itest_type + +DROP TYPE itest_type CASCADE; +NOTICE: drop cascades to table itest9 diff --git a/src/test/regress/sql/identity.sql b/src/test/regress/sql/identity.sql index e1b5a074c9..b98180f644 100644 --- a/src/test/regress/sql/identity.sql +++ b/src/test/regress/sql/identity.sql @@ -194,3 +194,14 @@ SELECT * FROM itest8; RESET ROLE; DROP TABLE itest8; DROP USER regress_user1; + +-- typed table +CREATE TYPE itest_type AS (f1 integer, f2 text, f3 bigint); +CREATE TABLE itest9 OF itest_type ( + f4 WITH OPTIONS GENERATED ALWAYS AS IDENTITY); -- error +CREATE TABLE itest9 OF itest_type ( + f2 WITH OPTIONS GENERATED ALWAYS AS IDENTITY); -- error +CREATE TABLE itest9 OF itest_type ( + f1 WITH OPTIONS GENERATED ALWAYS AS IDENTITY); -- ok +\d itest9 +DROP TYPE itest_type CASCADE; -- 2.11.0
0002-Make-IDENTITY-work-correctly-with-partitions.patch (text/plain, 5.6K) download From f55ae5657c99ea8e01caf7454555352f8fa0c70e Mon Sep 17 00:00:00 2001 From: amit <amitlangote09@gmail.com> Date: Thu, 9 Nov 2017 14:19:31 +0900 Subject: [PATCH 2/2] Make IDENTITY work correctly with partitions In the case of CREATE TABLE PARTITION OF, transformColumnDefinition should look at the parent's attribute of the same name to get the type OID. ColumnDef passed down by gram.y is mostly dummy, so doesn't contain the needed information. Authors: Michael Paquier, Amit Langote --- src/backend/commands/tablecmds.c | 1 + src/backend/parser/parse_utilcmd.c | 40 ++++++++++++++++++++++++++++++++++ src/test/regress/expected/identity.out | 23 +++++++++++++++++++ src/test/regress/sql/identity.sql | 14 ++++++++++++ 4 files changed, 78 insertions(+) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 192de790c4..9597d594e3 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2258,6 +2258,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence, coldef->raw_default = restdef->raw_default; coldef->cooked_default = restdef->cooked_default; coldef->constraints = restdef->constraints; + coldef->identity = restdef->identity; coldef->is_from_parent = false; list_delete_cell(schema, rest, prev); } diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 1be36b846a..4dae4b4171 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -241,6 +241,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString) cxt.alist = NIL; cxt.pkey = NULL; cxt.ispartitioned = stmt->partspec != NULL; + cxt.partbound = stmt->partbound; cxt.ofType = stmt->ofTypename != NULL; /* @@ -706,6 +707,45 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column) typeOid = col->typeName->typeOid; } + else if (cxt->partbound) + { + Relation rel; + RangeVar *inh = linitial_node(RangeVar, + cxt->inhRelations); + int i; + + Assert(list_length(cxt->inhRelations) == 1); + + /* + * First time opening the parent table. We take an + * AccessExclusiveLock on it due to certain operations + * we do later in DefineRelation(). Don't want to + * risk a lock-upgrade deadlock hazard by taking a + * weaker lock now and stronger one later. + */ + rel = heap_openrv(inh, AccessExclusiveLock); + for (i = 0; i < rel->rd_att->natts; i++) + { + Form_pg_attribute attr = + TupleDescAttr(rel->rd_att, i); + + if (attr->attisdropped) + continue; + if (strcmp(NameStr(attr->attname), + column->colname) == 0) + { + typeOid = attr->atttypid; + break; + } + } + heap_close(rel, NoLock); + + if (i == rel->rd_att->natts) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_COLUMN), + errmsg("column \"%s\" does not exist", + column->colname))); + } else { Type ctype; diff --git a/src/test/regress/expected/identity.out b/src/test/regress/expected/identity.out index 787edd4d06..81ec422bbd 100644 --- a/src/test/regress/expected/identity.out +++ b/src/test/regress/expected/identity.out @@ -354,3 +354,26 @@ Typed table of type: itest_type DROP TYPE itest_type CASCADE; NOTICE: drop cascades to table itest9 +-- partitioned tables +CREATE TABLE itest_parent (f1 date NOT NULL, f2 text, f3 bigint) PARTITION BY RANGE (f1); +CREATE TABLE itest_child PARTITION OF itest_parent ( + f4 WITH OPTIONS GENERATED ALWAYS AS IDENTITY +) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- error +ERROR: column "f4" does not exist +CREATE TABLE itest_child PARTITION OF itest_parent ( + f2 WITH OPTIONS GENERATED ALWAYS AS IDENTITY +) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- error +ERROR: identity column type must be smallint, integer, or bigint +CREATE TABLE itest_child PARTITION OF itest_parent ( + f3 WITH OPTIONS GENERATED ALWAYS AS IDENTITY +) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- ok +\d itest_child + Table "public.itest_child" + Column | Type | Collation | Nullable | Default +--------+--------+-----------+----------+------------------------------ + f1 | date | | not null | + f2 | text | | | + f3 | bigint | | not null | generated always as identity +Partition of: itest_parent FOR VALUES FROM ('07-01-2016') TO ('08-01-2016') + +DROP TABLE itest_parent; diff --git a/src/test/regress/sql/identity.sql b/src/test/regress/sql/identity.sql index b98180f644..fe88abceb9 100644 --- a/src/test/regress/sql/identity.sql +++ b/src/test/regress/sql/identity.sql @@ -205,3 +205,17 @@ CREATE TABLE itest9 OF itest_type ( f1 WITH OPTIONS GENERATED ALWAYS AS IDENTITY); -- ok \d itest9 DROP TYPE itest_type CASCADE; + +-- partitioned tables +CREATE TABLE itest_parent (f1 date NOT NULL, f2 text, f3 bigint) PARTITION BY RANGE (f1); +CREATE TABLE itest_child PARTITION OF itest_parent ( + f4 WITH OPTIONS GENERATED ALWAYS AS IDENTITY +) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- error +CREATE TABLE itest_child PARTITION OF itest_parent ( + f2 WITH OPTIONS GENERATED ALWAYS AS IDENTITY +) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- error +CREATE TABLE itest_child PARTITION OF itest_parent ( + f3 WITH OPTIONS GENERATED ALWAYS AS IDENTITY +) FOR VALUES FROM ('2016-07-01') TO ('2016-08-01'); -- ok +\d itest_child +DROP TABLE itest_parent; -- 2.11.0

view thread (16+ messages)  latest in thread

Message-ID: <c9be4efd-ea9e-16a7-8fd4-76ec8b04f66d@lab.ntt.co.jp>
Permalink:  ../c9be4efd-ea9e-16a7-8fd4-76ec8b04f66d@lab.ntt.co.jp/
Also on:    postgresql.org/message-id/c9be4efd-ea9e-16a7-8fd4-76ec8b04f66d@lab.ntt.co.jp

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: Langote_Amit_f8@lab.ntt.co.jp, michael.paquier@gmail.com, peter.eisentraut@2ndquadrant.com, robertmhaas@gmail.com, amitlangote09@gmail.com
  Subject: Re: BUG #14866: The generated constraint in the typed table causes the server to crash
  In-Reply-To: <c9be4efd-ea9e-16a7-8fd4-76ec8b04f66d@lab.ntt.co.jp>
  References: <20171023074458.1473.25799@wrigleys.postgresql.org> <CAB7nPqTtWi3S7ew6aCQqkHiUos5n=jigS=GMpV2vEbQCfU1qqA@mail.gmail.com> <CAB7nPqS+ofdMRy4+pNrLDyN6My6xDutX5SN_ObbJdKasYNH7wQ@mail.gmail.com> <8dd10932-16f0-7e07-6fd2-be33cbea962e@2ndquadrant.com> <CAB7nPqSofu3i4kc1fEHpPTyDYKZ_DEEgE+n5uL_FKToMUYJ6DA@mail.gmail.com> <c9be4efd-ea9e-16a7-8fd4-76ec8b04f66d@lab.ntt.co.jp>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox