From e6a2811ab73b8548bee383fa3e5b6fa61f3fae54 Mon Sep 17 00:00:00 2001 From: jian he Date: Wed, 9 Oct 2024 22:17:19 +0800 Subject: [PATCH v8 1/1] transformColumnDefinition minor refactor --- src/backend/parser/parse_utilcmd.c | 50 +++++++++++++++++------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index c3758dd92f..037188e9d1 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -596,7 +596,7 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column) bool saw_identity; bool saw_generated; bool need_notnull = false; - bool need_pk_notnull = false; + bool not_allow_notnull_noinherit = false; Constraint *notnull_constraint = NULL; cxt->columns = lappend(cxt->columns, column); @@ -696,11 +696,30 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column) /* have a not-null constraint added later */ need_notnull = true; + not_allow_notnull_noinherit = true; } /* Process column constraints, if any... */ transformConstraintAttrs(cxt, column->constraints); + /* + * We can't use a NO INHERIT not-null constraint with a PK/identity/serial + */ + if (!not_allow_notnull_noinherit) + { + foreach_node(Constraint, constraint, column->constraints) + { + switch (constraint->contype) + { + case CONSTR_IDENTITY: + case CONSTR_PRIMARY: + not_allow_notnull_noinherit = true; + break; + default: + break; + } + } + } saw_nullable = false; saw_default = false; saw_identity = false; @@ -737,6 +756,11 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column) parser_errposition(cxt->pstate, constraint->location))); + if (not_allow_notnull_noinherit && constraint->is_no_inherit) + ereport(ERROR, + errcode(ERRCODE_SYNTAX_ERROR), + errmsg("conflicting NO INHERIT declarations for not-null constraints on column \"%s\"", + column->colname)); /* * If this is the first time we see this column being marked * not-null, add the constraint entry and keep track of it. @@ -750,13 +774,6 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column) */ if (!column->is_not_null) { - /* We can't use a NO INHERIT constraint with a PK. */ - if (need_pk_notnull && constraint->is_no_inherit) - ereport(ERROR, - errcode(ERRCODE_SYNTAX_ERROR), - errmsg("conflicting NO INHERIT declarations for not-null constraints on column \"%s\"", - column->colname)); - column->is_not_null = true; saw_nullable = true; need_notnull = false; @@ -871,25 +888,14 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column) break; case CONSTR_PRIMARY: - /* primary key columns need a NOT NULL constraint */ - if (notnull_constraint) - { - /* we have one -- but check it's not NO INHERIT */ - if (notnull_constraint->is_no_inherit) - ereport(ERROR, - errcode(ERRCODE_SYNTAX_ERROR), - errmsg("conflicting NO INHERIT declarations for not-null constraints on column \"%s\"", - column->colname)); - } - else if (saw_nullable && !column->is_not_null) + if (saw_nullable && !column->is_not_null) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("conflicting NULL/NOT NULL declarations for column \"%s\" of table \"%s\"", column->colname, cxt->relation->relname), parser_errposition(cxt->pstate, - constraint->location))); - else - need_notnull = need_pk_notnull = true; + constraint->location))); + need_notnull = true; if (cxt->isforeign) ereport(ERROR, -- 2.34.1