Skip to content

Commit b992e20

Browse files
committed
NOT NULL implementation (submitted by Robson Paniago de Miranda).
1 parent b99c63c commit b992e20

File tree

16 files changed

+453
-337
lines changed

16 files changed

+453
-337
lines changed

src/backend/access/common/tupdesc.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/common/tupdesc.c,v 1.13 1997/08/18 20:51:31 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/common/tupdesc.c,v 1.14 1997/08/19 04:42:31 vadim Exp $
1111
*
1212
* NOTES
1313
* some of the executor utility code such as "ExecTypeFromTL" should be
@@ -60,6 +60,7 @@ CreateTemplateTupleDesc(int natts)
6060
size = natts * sizeof (AttributeTupleForm);
6161
desc = (TupleDesc) palloc(sizeof(struct tupleDesc));
6262
desc->attrs = (AttributeTupleForm*) palloc(size);
63+
desc->constr = NULL;
6364
memset(desc->attrs, 0, size);
6465

6566
desc->natts = natts;
@@ -87,7 +88,7 @@ CreateTupleDesc(int natts, AttributeTupleForm* attrs)
8788
desc = (TupleDesc) palloc(sizeof(struct tupleDesc));
8889
desc->attrs = attrs;
8990
desc->natts = natts;
90-
91+
desc->constr = NULL;
9192

9293
return (desc);
9394
}
@@ -117,6 +118,11 @@ CreateTupleDescCopy(TupleDesc tupdesc)
117118
tupdesc->attrs[i],
118119
ATTRIBUTE_TUPLE_SIZE);
119120
}
121+
if (tupdesc->constr) {
122+
desc->constr = (AttrConstr *) palloc(sizeof(struct attrConstr));
123+
memmove(desc->constr, tupdesc->constr, sizeof(struct attrConstr));
124+
} else
125+
desc->constr = NULL;
120126
return desc;
121127
}
122128

@@ -379,6 +385,15 @@ BuildDescForRelation(List *schema, char *relname)
379385
if (entry->typename->typlen > 0) {
380386
desc->attrs[attnum - 1]->attlen = entry->typename->typlen;
381387
}
388+
389+
/* This is for constraints */
390+
if (entry->is_not_null) {
391+
if (!desc->constr)
392+
desc->constr = (AttrConstr *) palloc(sizeof(struct attrConstr));
393+
desc->constr->has_not_null = true;
394+
}
395+
desc->attrs[attnum-1]->attnotnull = entry->is_not_null;
396+
382397
}
383398
return desc;
384399
}

src/backend/catalog/heap.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.15 1997/08/12 22:52:07 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.16 1997/08/19 04:42:54 vadim Exp $
1111
*
1212
* INTERFACE ROUTINES
1313
* heap_creatr() - Create an uncataloged heap relation
@@ -72,57 +72,57 @@
7272

7373
static FormData_pg_attribute a1 = {
7474
0xffffffff, {"ctid"}, 27l, 0l, 0l, 0l, sizeof (ItemPointerData),
75-
SelfItemPointerAttributeNumber, 0, '\0', '\001', 0l, 'i'
75+
SelfItemPointerAttributeNumber, 0, '\0', '\001', 0l, 'i', '\0'
7676
};
7777

7878
static FormData_pg_attribute a2 = {
7979
0xffffffff, {"oid"}, 26l, 0l, 0l, 0l, sizeof(Oid),
80-
ObjectIdAttributeNumber, 0, '\001', '\001', 0l, 'i'
80+
ObjectIdAttributeNumber, 0, '\001', '\001', 0l, 'i','\0'
8181
};
8282

8383
static FormData_pg_attribute a3 = {
8484
0xffffffff, {"xmin"}, 28l, 0l, 0l, 0l, sizeof (TransactionId),
85-
MinTransactionIdAttributeNumber, 0, '\0', '\001', 0l, 'i',
85+
MinTransactionIdAttributeNumber, 0, '\0', '\001', 0l, 'i', '\0'
8686
};
8787

8888
static FormData_pg_attribute a4 = {
8989
0xffffffff, {"cmin"}, 29l, 0l, 0l, 0l, sizeof (CommandId),
90-
MinCommandIdAttributeNumber, 0, '\001', '\001', 0l, 's'
90+
MinCommandIdAttributeNumber, 0, '\001', '\001', 0l, 's', '\0'
9191
};
9292

9393
static FormData_pg_attribute a5 = {
9494
0xffffffff, {"xmax"}, 28l, 0l, 0l, 0l, sizeof (TransactionId),
95-
MaxTransactionIdAttributeNumber, 0, '\0', '\001', 0l, 'i'
95+
MaxTransactionIdAttributeNumber, 0, '\0', '\001', 0l, 'i', '\0'
9696
};
9797

9898
static FormData_pg_attribute a6 = {
9999
0xffffffff, {"cmax"}, 29l, 0l, 0l, 0l, sizeof (CommandId),
100-
MaxCommandIdAttributeNumber, 0, '\001', '\001', 0l, 's'
100+
MaxCommandIdAttributeNumber, 0, '\001', '\001', 0l, 's', '\0'
101101
};
102102

103103
static FormData_pg_attribute a7 = {
104104
0xffffffff, {"chain"}, 27l, 0l, 0l, 0l, sizeof (ItemPointerData),
105-
ChainItemPointerAttributeNumber, 0, '\0', '\001', 0l, 'i',
105+
ChainItemPointerAttributeNumber, 0, '\0', '\001', 0l, 'i', '\0'
106106
};
107107

108108
static FormData_pg_attribute a8 = {
109109
0xffffffff, {"anchor"}, 27l, 0l, 0l, 0l, sizeof (ItemPointerData),
110-
AnchorItemPointerAttributeNumber, 0, '\0', '\001', 0l, 'i'
110+
AnchorItemPointerAttributeNumber, 0, '\0', '\001', 0l, 'i', '\0'
111111
};
112112

113113
static FormData_pg_attribute a9 = {
114114
0xffffffff, {"tmin"}, 20l, 0l, 0l, 0l, sizeof (AbsoluteTime),
115-
MinAbsoluteTimeAttributeNumber, 0, '\001', '\001', 0l, 'i'
115+
MinAbsoluteTimeAttributeNumber, 0, '\001', '\001', 0l, 'i', '\0'
116116
};
117117

118118
static FormData_pg_attribute a10 = {
119119
0xffffffff, {"tmax"}, 20l, 0l, 0l, 0l, sizeof (AbsoluteTime),
120-
MaxAbsoluteTimeAttributeNumber, 0, '\001', '\001', 0l, 'i'
120+
MaxAbsoluteTimeAttributeNumber, 0, '\001', '\001', 0l, 'i', '\0'
121121
};
122122

123123
static FormData_pg_attribute a11 = {
124124
0xffffffff, {"vtype"}, 18l, 0l, 0l, 0l, sizeof (char),
125-
VersionTypeAttributeNumber, 0, '\001', '\001', 0l, 'c'
125+
VersionTypeAttributeNumber, 0, '\001', '\001', 0l, 'c', '\0'
126126
};
127127

128128
static AttributeTupleForm HeapAtt[] =
@@ -565,7 +565,6 @@ AddNewAttributeTuples(Oid new_rel_oid,
565565
(char *) *dpp);
566566

567567
heap_insert(rdesc, tup);
568-
569568
if (hasindex)
570569
CatalogIndexInsert(idescs, Num_pg_attr_indices, rdesc, tup);
571570

src/backend/catalog/index.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.16 1997/08/12 22:52:09 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.17 1997/08/19 04:42:55 vadim Exp $
1111
*
1212
*
1313
* INTERFACE ROUTINES
@@ -112,17 +112,17 @@ static void DefaultBuild(Relation heapRelation, Relation indexRelation,
112112
* ----------------------------------------------------------------
113113
*/
114114
static FormData_pg_attribute sysatts[] = {
115-
{ 0l, {"ctid"}, 27l, 0l, 0l, 0l, 6, -1, 0, '\0', '\001', 0l, 'i' },
116-
{ 0l, {"oid"}, 26l, 0l, 0l, 0l, 4, -2, 0, '\001', '\001', 0l, 'i' },
117-
{ 0l, {"xmin"}, 28l, 0l, 0l, 0l, 5, -3, 0, '\0', '\001', 0l, 'i' },
118-
{ 0l, {"cmin"}, 29l, 0l, 0l, 0l, 1, -4, 0, '\001', '\001', 0l, 's' },
119-
{ 0l, {"xmax"}, 28l, 0l, 0l, 0l, 5, -5, 0, '\0', '\001', 0l, 'i' },
120-
{ 0l, {"cmax"}, 29l, 0l, 0l, 0l, 1, -6, 0, '\001', '\001', 0l, 's' },
121-
{ 0l, {"chain"}, 27l, 0l, 0l, 0l, 6, -7, 0, '\0', '\001', 0l, 'i' },
122-
{ 0l, {"anchor"}, 27l, 0l, 0l, 0l, 6, -8, 0, '\0', '\001', 0l, 'i' },
123-
{ 0l, {"tmin"}, 20l, 0l, 0l, 0l, 4, -9, 0, '\001', '\001', 0l, 'i' },
124-
{ 0l, {"tmax"}, 20l, 0l, 0l, 0l, 4, -10, 0, '\001', '\001', 0l, 'i' },
125-
{ 0l, {"vtype"}, 18l, 0l, 0l, 0l, 1, -11, 0, '\001', '\001', 0l, 'c' },
115+
{ 0l, {"ctid"}, 27l, 0l, 0l, 0l, 6, -1, 0, '\0', '\001', 0l, 'i', '\0' },
116+
{ 0l, {"oid"}, 26l, 0l, 0l, 0l, 4, -2, 0, '\001', '\001', 0l, 'i', '\0' },
117+
{ 0l, {"xmin"}, 28l, 0l, 0l, 0l, 5, -3, 0, '\0', '\001', 0l, 'i', '\0' },
118+
{ 0l, {"cmin"}, 29l, 0l, 0l, 0l, 1, -4, 0, '\001', '\001', 0l, 's', '\0' },
119+
{ 0l, {"xmax"}, 28l, 0l, 0l, 0l, 5, -5, 0, '\0', '\001', 0l, 'i', '\0' },
120+
{ 0l, {"cmax"}, 29l, 0l, 0l, 0l, 1, -6, 0, '\001', '\001', 0l, 's', '\0' },
121+
{ 0l, {"chain"}, 27l, 0l, 0l, 0l, 6, -7, 0, '\0', '\001', 0l, 'i', '\0' },
122+
{ 0l, {"anchor"}, 27l, 0l, 0l, 0l, 6, -8, 0, '\0', '\001', 0l, 'i', '\0' },
123+
{ 0l, {"tmin"}, 20l, 0l, 0l, 0l, 4, -9, 0, '\001', '\001', 0l, 'i', '\0' },
124+
{ 0l, {"tmax"}, 20l, 0l, 0l, 0l, 4, -10, 0, '\001', '\001', 0l, 'i', '\0' },
125+
{ 0l, {"vtype"}, 18l, 0l, 0l, 0l, 1, -11, 0, '\001', '\001', 0l, 'c', '\0' },
126126
};
127127

128128
/* ----------------------------------------------------------------

src/backend/commands/command.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.9 1997/08/18 20:52:11 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.10 1997/08/19 04:43:27 vadim Exp $
1111
*
1212
* NOTES
1313
* The PortalExecutorHeapMemory crap needs to be eliminated
@@ -279,7 +279,11 @@ PerformAddAttribute(char *relationName,
279279
elog(WARN, "PerformAddAttribute: you do not own class \"%s\"",
280280
relationName);
281281
#endif
282-
282+
/*
283+
* we can't add a not null attribute
284+
*/
285+
if (colDef->is_not_null)
286+
elog(WARN,"Can't add a not null attribute to a existent relation");
283287
/*
284288
* if the first element in the 'schema' list is a "*" then we are
285289
* supposed to add this attribute to all classes that inherit from
@@ -454,6 +458,7 @@ PerformAddAttribute(char *relationName,
454458
attribute->attcacheoff = -1;
455459
attribute->attisset = (bool) (form->typtype == 'c');
456460
attribute->attalign = form->typalign;
461+
attribute->attnotnull = false;
457462

458463
heap_insert(attrdesc, attributeTuple);
459464
if (hasindex)

src/backend/commands/copy.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.25 1997/08/18 02:14:34 momjian Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.26 1997/08/19 04:43:28 vadim Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -602,6 +602,22 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
602602
tuple = heap_formtuple(tupDesc, values, nulls);
603603
if (oids)
604604
tuple->t_oid = loaded_oid;
605+
606+
/* ----------------
607+
* Check the constraints of a tuple
608+
* ----------------
609+
*/
610+
611+
if (rel->rd_att->constr && rel->rd_att->constr->has_not_null)
612+
{
613+
int attrChk;
614+
for (attrChk = 1; attrChk <= rel->rd_att->natts; attrChk++) {
615+
if (rel->rd_att->attrs[attrChk-1]->attnotnull && heap_attisnull(tuple,attrChk))
616+
elog(WARN,"CopyFrom: Fail to add null value in not null attribute %s",
617+
rel->rd_att->attrs[attrChk-1]->attname.data);
618+
}
619+
}
620+
605621
heap_insert(rel, tuple);
606622

607623
if (has_index) {

src/backend/commands/creatinh.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.11 1997/08/18 20:52:16 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.12 1997/08/19 04:43:30 vadim Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -276,14 +276,16 @@ MergeAttributes(List *schema, List *supers)
276276
AttributeTupleForm attribute = tupleDesc->attrs[attrno];
277277
char *attributeName;
278278
char *attributeType;
279+
AttrConstr constraints;
279280
HeapTuple tuple;
280281
ColumnDef *def;
281282
TypeName *typename;
282283

283284
/*
284-
* form name and type
285+
* form name, type and constraints
285286
*/
286287
attributeName = (attribute->attname).data;
288+
constraints.has_not_null = attribute->attnotnull;
287289
tuple =
288290
SearchSysCacheTuple(TYPOID,
289291
ObjectIdGetDatum(attribute->atttypid),
@@ -311,7 +313,8 @@ MergeAttributes(List *schema, List *supers)
311313
def->colname = pstrdup(attributeName);
312314
typename->name = pstrdup(attributeType);
313315
def->typename = typename;
314-
partialResult = lcons(def, partialResult);
316+
def->is_not_null = constraints.has_not_null;
317+
partialResult = lcons(def, partialResult);
315318
}
316319

317320
/*

src/backend/executor/execMain.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
*
2828
* IDENTIFICATION
29-
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.15 1997/08/18 20:52:25 momjian Exp $
29+
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.16 1997/08/19 04:43:45 vadim Exp $
3030
*
3131
*-------------------------------------------------------------------------
3232
*/
@@ -401,6 +401,7 @@ InitPlan(CmdType operation, Query *parseTree, Plan *plan, EState *estate)
401401
if (resultRelation != 0 && operation != CMD_SELECT) {
402402
/* ----------------
403403
* if we have a result relation, open it and
404+
404405
* initialize the result relation info stuff.
405406
* ----------------
406407
*/
@@ -910,6 +911,21 @@ ExecAppend(TupleTableSlot *slot,
910911
* ----------------
911912
*/
912913

914+
/* ----------------
915+
* Check the constraints of a tuple
916+
* ----------------
917+
*/
918+
919+
if (resultRelationDesc->rd_att->constr && resultRelationDesc->rd_att->constr->has_not_null)
920+
{
921+
int attrChk;
922+
for (attrChk = 1; attrChk <= resultRelationDesc->rd_att->natts; attrChk++) {
923+
if (resultRelationDesc->rd_att->attrs[attrChk-1]->attnotnull && heap_attisnull(tuple,attrChk))
924+
elog(WARN,"ExecAppend: Fail to add null value in not null attribute %s",
925+
resultRelationDesc->rd_att->attrs[attrChk-1]->attname.data);
926+
}
927+
}
928+
913929
/* ----------------
914930
* insert the tuple
915931
* ----------------
@@ -1030,6 +1046,21 @@ ExecReplace(TupleTableSlot *slot,
10301046
* ----------------
10311047
*/
10321048

1049+
/* ----------------
1050+
* Check the constraints of a tuple
1051+
* ----------------
1052+
*/
1053+
1054+
if (resultRelationDesc->rd_att->constr && resultRelationDesc->rd_att->constr->has_not_null)
1055+
{
1056+
int attrChk;
1057+
for (attrChk = 1; attrChk <= resultRelationDesc->rd_att->natts; attrChk++) {
1058+
if (resultRelationDesc->rd_att->attrs[attrChk-1]->attnotnull && heap_attisnull(tuple,attrChk))
1059+
elog(WARN,"ExecReplace: Fail to update null value in not null attribute %s",
1060+
resultRelationDesc->rd_att->attrs[attrChk-1]->attname.data);
1061+
}
1062+
}
1063+
10331064
/* ----------------
10341065
* replace the heap tuple
10351066
*

src/backend/parser/gram.y

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 1.35 1997/08/12 20:15:33 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 1.36 1997/08/19 04:44:01 vadim Exp $
1414
*
1515
* HISTORY
1616
* AUTHOR DATE MAJOR EVENT
@@ -135,7 +135,7 @@ static Node *makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr);
135135
def_list, opt_indirection, group_clause, groupby_list
136136

137137
%type <boolean> opt_inh_star, opt_binary, opt_instead, opt_with_copy,
138-
index_opt_unique, opt_verbose, opt_analyze
138+
index_opt_unique, opt_verbose, opt_analyze, opt_null
139139

140140
%type <ival> copy_dirn, archive_type, OptArchiveType, OptArchiveLocation,
141141
def_type, opt_direction, remove_type, opt_column, event
@@ -333,14 +333,20 @@ AddAttrStmt: ALTER TABLE relation_name opt_inh_star ADD COLUMN columnDef
333333
}
334334
;
335335

336-
columnDef: Id Typename
336+
columnDef: Id Typename opt_null
337337
{
338338
$$ = makeNode(ColumnDef);
339339
$$->colname = $1;
340340
$$->typename = $2;
341+
$$->is_not_null = $3;
341342
}
342343
;
343344

345+
opt_null: PNULL { $$ = false; }
346+
| NOT PNULL { $$ = true; }
347+
| NOTNULL { $$ = true; }
348+
| /* EMPTY */ { $$ = false; }
349+
;
344350

345351
/*****************************************************************************
346352
*

0 commit comments

Comments
 (0)