Re: not null constraints, again

2024-11-09 Thread Alvaro Herrera
On 2024-Nov-08, Tom Lane wrote: > Alvaro Herrera writes: > > But we'll see what else the buildfarm has to say now that I pushed it ... > > A lot of the buildfarm is saying > > adder | 2024-11-08 13:04:39 | > ../pgsql/src/backend/catalog/pg_constraint.c:708:37: warning: comparison is

Re: not null constraints, again

2024-11-08 Thread Tom Lane
Alvaro Herrera writes: > But we'll see what else the buildfarm has to say now that I pushed it ... A lot of the buildfarm is saying adder | 2024-11-08 13:04:39 | ../pgsql/src/backend/catalog/pg_constraint.c:708:37: warning: comparison is always true due to limited range of data type [

Re: not null constraints, again

2024-11-08 Thread Alvaro Herrera
On 2024-Nov-08, jian he wrote: > > Here's v11, which I intended to commit today, but didn't get around to. > > CI is happy with it, so I'll probably do it tomorrow first thing. > > > v11 still has column_constraint versus table_constraint inconsistency. > > create table t7 (a int generated by def

Re: not null constraints, again

2024-11-08 Thread jian he
> Here's v11, which I intended to commit today, but didn't get around to. > CI is happy with it, so I'll probably do it tomorrow first thing. > v11 still has column_constraint versus table_constraint inconsistency. create table t7 (a int generated by default as identity, constraint foo not null a

Re: not null constraints, again

2024-11-08 Thread Alvaro Herrera
On 2024-Nov-08, jian he wrote: > > Here's v11, which I intended to commit today, but didn't get around to. > > CI is happy with it, so I'll probably do it tomorrow first thing. > > > > CREATE TABLE notnull_tbl2 (a INTEGER CONSTRAINT blah NOT NULL, b > INTEGER CONSTRAINT blah NOT NULL); > > Relat

Re: not null constraints, again

2024-11-07 Thread jian he
> > Here's v11, which I intended to commit today, but didn't get around to. > CI is happy with it, so I'll probably do it tomorrow first thing. > CREATE TABLE notnull_tbl2 (a INTEGER CONSTRAINT blah NOT NULL, b INTEGER CONSTRAINT blah NOT NULL); RelationGetNotNullConstraints, StoreRelNotNull will

Re: not null constraints, again

2024-11-07 Thread Alvaro Herrera
On 2024-Nov-07, jian he wrote: > RemoveInheritance > if (copy_con->coninhcount <= 0) /* shouldn't happen */ > elog(ERROR, "relation %u has non-inherited constraint \"%s\"", > RelationGetRelid(child_rel), NameStr(copy_con->conname)); > dropconstraint

Re: not null constraints, again

2024-11-06 Thread jian he
RemoveInheritance if (copy_con->coninhcount <= 0) /* shouldn't happen */ elog(ERROR, "relation %u has non-inherited constraint \"%s\"", RelationGetRelid(child_rel), NameStr(copy_con->conname)); dropconstraint_internal if (childcon->coninhcoun

Re: not null constraints, again

2024-11-06 Thread jian he
sql-altertable.html SET/DROP NOT NULL These forms change whether a column is marked to allow null values or to reject null values. If this table is a partition, one cannot perform DROP NOT NULL on a column if it is marked NOT NULL in the paren

Re: not null constraints, again

2024-10-09 Thread jian he
tricky case: drop table if exists part, part0 cascade; create table part (a int not null) partition by range (a); create table part0 (a int primary key); alter table part attach partition part0 for values from (0) to (1000); alter table ONLY part add primary key(a); alter table ONLY part drop const

Re: not null constraints, again

2024-10-09 Thread jian he
I did some refactoring on transformColumnDefinition since transformColumnDefinition only deals with a single ColumnDef. and serial/primary/identity cannot allow not-null no inherit. We can preliminary iterate through ColumnDef->constraints to check that ColumnDef can allow not-null no inherit or no

Re: not null constraints, again

2024-10-08 Thread jian he
On Fri, Oct 4, 2024 at 9:11 PM Alvaro Herrera wrote: > > Here's v8 of this patch. in AdjustNotNullInheritance if (count > 0) { conform->coninhcount += count; changed = true; } if (is_local) { conform->conislocal = true;

Re: not null constraints, again

2024-10-04 Thread Alvaro Herrera
On 2024-Oct-03, jian he wrote: > I thought SearchSysCacheCopyAttNum is expensive. > Relation->rd_att is enough for checking attnotnull. > > What do you think of the following refactoring of set_attnotnull? Eh, sure, why not. I mean, I expect that this is going to be barely noticeable performanc

Re: not null constraints, again

2024-10-03 Thread jian he
I thought SearchSysCacheCopyAttNum is expensive. Relation->rd_att is enough for checking attnotnull. What do you think of the following refactoring of set_attnotnull? static void set_attnotnull(List **wqueue, Relation rel, AttrNumber attnum, LOCKMODE lockmode) { Oid

Re: not null constraints, again

2024-10-02 Thread Alvaro Herrera
On 2024-Oct-02, Alvaro Herrera wrote: > On 2024-Oct-02, jian he wrote: > > > On Tue, Oct 1, 2024 at 11:20 PM Alvaro Herrera > > wrote: > > after v7, still not bullet-proof. as before, pg_dump/restore will fail > > for the following: > > > > drop table if exists t2, t2_0 > > create table t2 (a

Re: not null constraints, again

2024-10-02 Thread Alvaro Herrera
On 2024-Oct-02, jian he wrote: > On Tue, Oct 1, 2024 at 11:20 PM Alvaro Herrera > wrote: > > > > On 2024-Oct-01, jian he wrote: > > > > > create table t2 (a int primary key constraint foo not null no inherit); > > > primary key cannot coexist with not-null no inherit? > > > here t2, pg_dump/res

Re: not null constraints, again

2024-10-02 Thread jian he
On Tue, Oct 1, 2024 at 11:20 PM Alvaro Herrera wrote: > > On 2024-Oct-01, jian he wrote: > > > create table t2 (a int primary key constraint foo not null no inherit); > > primary key cannot coexist with not-null no inherit? > > here t2, pg_dump/restore will fail. > > Yeah, this needs to throw an

Re: not null constraints, again

2024-10-01 Thread jian he
CREATE TABLE a (aa TEXT); CREATE TEMP TABLE z (b TEXT, UNIQUE(aa, b)) inherits (a); \d+ z Table "pg_temp_0.z" Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description +--+---+--+--

Re: not null constraints, again

2024-10-01 Thread jian he
create table t2 (a int primary key constraint foo not null no inherit); primary key cannot coexist with not-null no inherit? here t2, pg_dump/restore will fail. create table t7 (a int generated by default as identity, constraint foo not null a no inherit, b int); create table t7 (a int generated

Re: not null constraints, again

2024-10-01 Thread Alvaro Herrera
On 2024-Oct-01, jian he wrote: > create table t2 (a int primary key constraint foo not null no inherit); > primary key cannot coexist with not-null no inherit? > here t2, pg_dump/restore will fail. Yeah, this needs to throw an error. If you use a table constraint, it does fail as expected: cre

Re: not null constraints, again

2024-10-01 Thread jian he
ATExecDropInherit /* * If the parent has a primary key, then we decrement counts for all NOT * NULL constraints */ ObjectAddressSet(address, RelationRelationId, RelationGetRelid(parent_rel)); only not-null constraint, with ALTER TABLE NO INHERIT we still

Re: not null constraints, again

2024-09-26 Thread Alvaro Herrera
On 2024-Sep-26, jian he wrote: > +-- a PK in parent must have a not-null in child that it can mark inherited > +create table inh_parent (a int primary key); > +create table inh_child (a int primary key); > +alter table inh_child inherit inh_parent; -- nope > +alter table inh_child alter a set not

Re: not null constraints, again

2024-09-26 Thread jian he
+-- a PK in parent must have a not-null in child that it can mark inherited +create table inh_parent (a int primary key); +create table inh_child (a int primary key); +alter table inh_child inherit inh_parent; -- nope +alter table inh_child alter a set not null; +alter table inh_child inherit inh_p

Re: not null constraints, again

2024-09-26 Thread jian he
Please check the attached minor doc changes. make the create_foreign_table.sgml, alter_foreign_table.sgml not-null description consistent with normal tables. change doc/src/sgml/ref/create_table.sgml Parameters section from NOT NULL to NOT NULL [ NO INHERIT ] . in doc/src/sgml/ref/alter_table.

Re: not null constraints, again

2024-09-25 Thread Alvaro Herrera
On 2024-Sep-25, jian he wrote: > copy from src/test/regress/sql/index_including.sql > -- Unique index and unique constraint > CREATE TABLE tbl_include_unique1 (c1 int, c2 int, c3 int, c4 box); > INSERT INTO tbl_include_unique1 SELECT x, 2*x, 3*x, box('4,4,4,4') > FROM generate_series(1,10) AS x; >

Re: not null constraints, again

2024-09-25 Thread jian he
in ATExecSetNotNull /* * If we find an appropriate constraint, we're almost done, but just * need to change some properties on it: if we're recursing, increment * coninhcount; if not, set conislocal if not already set. */ if (recursing) {

Re: not null constraints, again

2024-09-25 Thread jian he
copy from src/test/regress/sql/index_including.sql -- Unique index and unique constraint CREATE TABLE tbl_include_unique1 (c1 int, c2 int, c3 int, c4 box); INSERT INTO tbl_include_unique1 SELECT x, 2*x, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; CREATE UNIQUE INDEX tbl_include_unique1_idx

Re: not null constraints, again

2024-09-24 Thread Alvaro Herrera
On 2024-Sep-24, jian he wrote: > sql-createtable.html > SECTION: LIKE source_table [ like_option ... ] > INCLUDING CONSTRAINTS > CHECK constraints will be copied. No distinction is made between > column constraints and table constraints. Not-null constraints are > always copied to the new table. >

Re: not null constraints, again

2024-09-24 Thread Alvaro Herrera
On 2024-Sep-24, jian he wrote: > static void > set_attnotnull(List **wqueue, Relation rel, AttrNumber attnum, bool recurse, >LOCKMODE lockmode) > { > What do you think of the above refactor? > (I intentionally deleted empty new line) Looks nicer ... but you know what? After spen

Re: not null constraints, again

2024-09-24 Thread Alvaro Herrera
On 2024-Sep-24, jian he wrote: > static Oid > StoreRelNotNull(Relation rel, const char *nnname, AttrNumber attnum, > bool is_validated, bool is_local, int inhcount, > bool is_no_inherit) > { > OidconstrOid; > Assert(attnum > InvalidAttrNumber); >

Re: not null constraints, again

2024-09-24 Thread jian he
static Oid StoreRelNotNull(Relation rel, const char *nnname, AttrNumber attnum, bool is_validated, bool is_local, int inhcount, bool is_no_inherit) { OidconstrOid; Assert(attnum > InvalidAttrNumber); constrOid = CreateConstraintEntry(n

Re: not null constraints, again

2024-09-24 Thread jian he
On Fri, Sep 20, 2024 at 8:08 PM Alvaro Herrera wrote: > > On 2024-Sep-20, jian he wrote: > > > about set_attnotnull. > > > > we can make set_attnotnull look less recursive. > > instead of calling find_inheritance_children, > > let's just one pass, directly call find_all_inheritors > > overall, I

Re: not null constraints, again

2024-09-23 Thread jian he
On Sat, Sep 21, 2024 at 5:15 AM Alvaro Herrera wrote: > > Okay, so here is v4 with these problems fixed, including correct > propagation of constraint names to children tables, which I had > inadvertently broken earlier. This one does pass the pg_upgrade tests > and as far as I can see pg_dump do

Re: not null constraints, again

2024-09-22 Thread Tender Wang
Alvaro Herrera 于2024年9月21日周六 05:15写道: > On 2024-Sep-20, Alvaro Herrera wrote: > > > Yeah, there's a bunch of conflicts in current master. I rebased > > yesterday but I'm still composing the email for v4. Coming soon. > > Okay, so here is v4 with these problems fixed, including correct > propaga

Re: not null constraints, again

2024-09-20 Thread Alvaro Herrera
On 2024-Sep-20, jian he wrote: > about set_attnotnull. > > we can make set_attnotnull look less recursive. > instead of calling find_inheritance_children, > let's just one pass, directly call find_all_inheritors > overall, I think it would be more intuitive. > > please check the attached refac

Re: not null constraints, again

2024-09-20 Thread jian he
> > We only have this Synopsis > > ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL > > Yeah, this syntax is intended to add a "normal" not-null constraint, > i.e. one that inherits. > > > --tests from src/test/regress/sql/inherit.sql > > CREATE TABLE inh_nn_parent (a int, NOT NULL a NO INHERIT

Re: not null constraints, again

2024-09-20 Thread jian he
about set_attnotnull. we can make set_attnotnull look less recursive. instead of calling find_inheritance_children, let's just one pass, directly call find_all_inheritors overall, I think it would be more intuitive. please check the attached refactored set_attnotnull. regress test passed, i onl

Re: not null constraints, again

2024-09-20 Thread Alvaro Herrera
On 2024-Sep-20, Tender Wang wrote: > By the way, the v3 failed applying on Head(d35e293878) > git am v3-0001-Catalog-not-null-constraints.patch > Applying: Catalog not-null constraints > error: patch failed: doc/src/sgml/ref/create_table.sgml:77 Yeah, there's a bunch of conflicts in current mast

Re: not null constraints, again

2024-09-20 Thread Alvaro Herrera
On 2024-Sep-20, Tender Wang wrote: > jian he 于2024年9月20日周五 11:34写道: > > > another bug. > > I will dig later, just want to share it first. > > > > minimum producer: > > drop table if exists pp1,cc1, cc2,cc3; > > create table pp1 (f1 int ); > > create table cc1 () inherits (pp1); > > create table

Re: not null constraints, again

2024-09-19 Thread Tender Wang
By the way, the v3 failed applying on Head(d35e293878) git am v3-0001-Catalog-not-null-constraints.patch Applying: Catalog not-null constraints error: patch failed: doc/src/sgml/ref/create_table.sgml:77 error: doc/src/sgml/ref/create_table.sgml: patch does not apply error: patch failed: src/backen

Re: not null constraints, again

2024-09-19 Thread Tender Wang
jian he 于2024年9月20日周五 11:34写道: > another bug. > I will dig later, just want to share it first. > > minimum producer: > drop table if exists pp1,cc1, cc2,cc3; > create table pp1 (f1 int ); > create table cc1 () inherits (pp1); > create table cc2() inherits(pp1,cc1); > create table cc3() inherits(p

Re: not null constraints, again

2024-09-19 Thread jian he
another bug. I will dig later, just want to share it first. minimum producer: drop table if exists pp1,cc1, cc2,cc3; create table pp1 (f1 int ); create table cc1 () inherits (pp1); create table cc2() inherits(pp1,cc1); create table cc3() inherits(pp1,cc1,cc2); alter table pp1 alter f1 set not nul

Re: not null constraints, again

2024-09-19 Thread jian he
On Thu, Sep 19, 2024 at 4:26 PM Alvaro Herrera wrote: > > > > drop table if exists idxpart, idxpart0, idxpart1 cascade; > > create table idxpart (a int) partition by range (a); > > create table idxpart0 (a int primary key); > > alter table idxpart attach partition idxpart0 for values from (0) to (

Re: not null constraints, again

2024-09-19 Thread Alvaro Herrera
On 2024-Sep-19, jian he wrote: > still based on v3. > in src/sgml/html/ddl-partitioning.html > << Both CHECK and NOT NULL constraints of a partitioned table are always > inherited by all its partitions. > CHECK constraints that are marked NO INHERIT are not allowed to be > created on partitioned t

Re: not null constraints, again

2024-09-18 Thread jian he
still based on v3. in src/sgml/html/ddl-partitioning.html <<

Re: not null constraints, again

2024-09-18 Thread jian he
On Tue, Sep 17, 2024 at 1:47 AM Alvaro Herrera wrote: > still digging inheritance related issues. drop table if exists pp1,cc1, cc2; create table pp1 (f1 int, constraint nn check (f1 > 1)); create table cc1 (f2 text, f3 int ) inherits (pp1); create table cc2(f4 float, constraint nn check (f1 >

Re: not null constraints, again

2024-09-12 Thread Alvaro Herrera
On 2024-Sep-12, jian he wrote: > ---exampleA > drop table pp1,cc1, cc2; > create table pp1 (f1 int); > create table cc1 (f2 text, f3 int) inherits (pp1); > create table cc2(f4 float) inherits(pp1,cc1); > alter table pp1 alter column f1 set not null; > execute constr_meta('{pp1,cc1, cc2}'); > > --

Re: not null constraints, again

2024-09-12 Thread jian he
> > On Wed, Sep 11, 2024 at 2:18 AM Alvaro Herrera > > wrote: > > > > > > Hello, here's a v2 of this patch. I have fixed --I think-- all the > > > issues you and Tender Wang reported (unless I declined a fix in some > > > previous email). > > > PREPARE constr_meta (name[]) AS with cte as ( sele

Re: not null constraints, again

2024-09-11 Thread Alvaro Herrera
On 2024-Sep-11, jian he wrote: > On Wed, Sep 11, 2024 at 2:18 AM Alvaro Herrera > wrote: > > > > Hello, here's a v2 of this patch. I have fixed --I think-- all the > > issues you and Tender Wang reported (unless I declined a fix in some > > previous email). > > > > + /* > + * The constraint mu

Re: not null constraints, again

2024-09-11 Thread jian he
On Wed, Sep 11, 2024 at 9:11 AM jian he wrote: > > On Wed, Sep 11, 2024 at 2:18 AM Alvaro Herrera > wrote: > > > > Hello, here's a v2 of this patch. I have fixed --I think-- all the > > issues you and Tender Wang reported (unless I declined a fix in some > > previous email). > > after applying

Re: not null constraints, again

2024-09-10 Thread jian he
On Wed, Sep 11, 2024 at 2:18 AM Alvaro Herrera wrote: > > Hello, here's a v2 of this patch. I have fixed --I think-- all the > issues you and Tender Wang reported (unless I declined a fix in some > previous email). > + /* + * The constraint must appear as inherited in children, so create a + * m

Re: not null constraints, again

2024-09-10 Thread Alvaro Herrera
On 2024-Sep-02, Tender Wang wrote: > The attached patch adds List *nnconstraints, which store the not-null > definition, in struct CreateStmt. This makes me a little confused > about List *constraints in struct CreateStmt. Actually, the List > constraints store ckeck constraint, and it will be b

Re: not null constraints, again

2024-09-10 Thread Alvaro Herrera
On 2024-Sep-09, jian he wrote: > bold idea. print out the constraint name: violates not-null constraint \"%s\" > for the following code: > ereport(ERROR, > (errcode(ERRCODE_NOT_NULL_VIOLATION), > errmsg("null value in column \"%s\" o

Re: not null constraints, again

2024-09-09 Thread Tender Wang
jian he 于2024年9月9日周一 16:31写道: > On Mon, Sep 2, 2024 at 6:33 PM Tender Wang wrote: > > > > > > > > The attached patch adds List *nnconstraints, which store the not-null > definition, in struct CreateStmt. > > This makes me a little confused about List *constraints in struct > CreateStmt. Actuall

Re: not null constraints, again

2024-09-09 Thread jian he
On Mon, Sep 2, 2024 at 6:33 PM Tender Wang wrote: > > > > The attached patch adds List *nnconstraints, which store the not-null > definition, in struct CreateStmt. > This makes me a little confused about List *constraints in struct CreateStmt. > Actually, the List constraints > store ckeck cons

Re: not null constraints, again

2024-09-08 Thread jian he
On Sat, Aug 31, 2024 at 11:59 AM Alvaro Herrera wrote: > > Hello > > Here I present another attempt at making not-null constraints be > catalogued. This is largely based on the code reverted at 9ce04b50e120, > except that we now have a not-null constraint automatically created for > every column

Re: not null constraints, again

2024-09-03 Thread Tender Wang
Alvaro Herrera 于2024年8月31日周六 11:59写道: > Hello > > Here I present another attempt at making not-null constraints be > catalogued. This is largely based on the code reverted at 9ce04b50e120, > except that we now have a not-null constraint automatically created for > every column of a primary key,

Re: not null constraints, again

2024-09-02 Thread Tender Wang
Alvaro Herrera 于2024年8月31日周六 11:59写道: > Hello > > Here I present another attempt at making not-null constraints be > catalogued. This is largely based on the code reverted at 9ce04b50e120, > except that we now have a not-null constraint automatically created for > every column of a primary key,

Re: not null constraints, again

2024-08-31 Thread Tender Wang
Alvaro Herrera 于2024年8月31日周六 11:59写道: > Hello > > Here I present another attempt at making not-null constraints be > catalogued. This is largely based on the code reverted at 9ce04b50e120, > except that we now have a not-null constraint automatically created for > every column of a primary key,