Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfc032be

Browse files
committed
Be more careful about marking catalog columns NOT NULL by default.
The bug fixed in commit72eab84 would not have occurred if initdbhad a less surprising rule about which columns should be markedNOT NULL by default. Let's make that rule be strictly that thecolumn must be fixed-width and its predecessors must be fixed-widthand NOT NULL, removing the hacky and unsafe exceptions for oidvectorand int2vector.Since we do still want all existing oidvector and int2vector columnsto be marked NOT NULL, we have to put BKI_FORCE_NOT_NULL labels onthem. But making this less magic and more documented seems like agood idea, even if it's a shade more verbose.I didn't bump catversion since the initial catalog contents arenot actually changed by this patch. Note however that thecontents of postgres.bki do change, and feeding an old copy ofthat to a new backend will produce wrong results.Discussion:https://postgr.es/m/204760.1595181800@sss.pgh.pa.us
1 parent3e66019 commitfc032be

File tree

9 files changed

+46
-42
lines changed

9 files changed

+46
-42
lines changed

‎doc/src/sgml/bki.sgml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@
119119
require all columns that should be non-nullable to be marked so
120120
in <structname>pg_attribute</structname>. The bootstrap code will
121121
automatically mark catalog columns as <literal>NOT NULL</literal>
122-
if they are fixed-width and are not preceded by any nullable column.
122+
if they are fixed-width and are not preceded by any nullable or
123+
variable-width column.
123124
Where this rule is inadequate, you can force correct marking by using
124125
<literal>BKI_FORCE_NOT_NULL</literal>
125126
and <literal>BKI_FORCE_NULL</literal> annotations as needed.

‎src/backend/bootstrap/bootstrap.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -770,25 +770,18 @@ DefineAttr(char *name, char *type, int attnum, int nullness)
770770

771771
/*
772772
* Mark as "not null" if type is fixed-width and prior columns are
773-
* too. This corresponds to case where column can be accessed
774-
* directly via C struct declaration.
775-
*
776-
* oidvector and int2vector are also treated as not-nullable, even
777-
* though they are no longer fixed-width.
773+
* likewise fixed-width and not-null. This corresponds to case where
774+
* column can be accessed directly via C struct declaration.
778775
*/
779-
#defineMARKNOTNULL(att) \
780-
((att)->attlen > 0 || \
781-
(att)->atttypid == OIDVECTOROID || \
782-
(att)->atttypid == INT2VECTOROID)
783-
784-
if (MARKNOTNULL(attrtypes[attnum]))
776+
if (attrtypes[attnum]->attlen>0)
785777
{
786778
inti;
787779

788780
/* check earlier attributes */
789781
for (i=0;i<attnum;i++)
790782
{
791-
if (!attrtypes[i]->attnotnull)
783+
if (attrtypes[i]->attlen <=0||
784+
!attrtypes[i]->attnotnull)
792785
break;
793786
}
794787
if (i==attnum)

‎src/backend/catalog/genbki.pl

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -713,17 +713,21 @@ sub gen_pg_attribute
713713
push@tables_needing_macros,$table_name;
714714

715715
# Generate entries for user attributes.
716-
my$attnum = 0;
717-
my$priornotnull = 1;
716+
my$attnum= 0;
717+
my$priorfixedwidth = 1;
718718
foreachmy$attr (@{$table->{columns} })
719719
{
720720
$attnum++;
721721
my%row;
722722
$row{attnum} =$attnum;
723723
$row{attrelid} =$table->{relation_oid};
724724

725-
morph_row_for_pgattr(\%row,$schema,$attr,$priornotnull);
726-
$priornotnull &= ($row{attnotnull}eq't');
725+
morph_row_for_pgattr(\%row,$schema,$attr,$priorfixedwidth);
726+
727+
# Update $priorfixedwidth --- must match morph_row_for_pgattr
728+
$priorfixedwidth &=
729+
($row{attnotnull}eq't'
730+
&& ($row{attlen}eq'NAMEDATALEN' ||$row{attlen} > 0));
727731

728732
# If it's bootstrapped, put an entry in postgres.bki.
729733
print_bki_insert(\%row,$schema)if$table->{bootstrap};
@@ -765,13 +769,13 @@ sub gen_pg_attribute
765769

766770
# Given $pgattr_schema (the pg_attribute schema for a catalog sufficient for
767771
# AddDefaultValues), $attr (the description of a catalog row), and
768-
# $priornotnull (whetherall priorattributes in this catalog are not null),
772+
# $priorfixedwidth (all priorcolumns are fixed-width and not null),
769773
# modify the $row hashref for print_bki_insert. This includes setting data
770774
# from the corresponding pg_type element and filling in any default values.
771775
# Any value not handled here must be supplied by caller.
772776
submorph_row_for_pgattr
773777
{
774-
my ($row,$pgattr_schema,$attr,$priornotnull) =@_;
778+
my ($row,$pgattr_schema,$attr,$priorfixedwidth) =@_;
775779
my$attname =$attr->{name};
776780
my$atttype =$attr->{type};
777781

@@ -801,19 +805,18 @@ sub morph_row_for_pgattr
801805
{
802806
$row->{attnotnull} ='f';
803807
}
804-
elsif ($priornotnull)
808+
elsif ($priorfixedwidth)
805809
{
806810

807811
# attnotnull will automatically be set if the type is
808-
# fixed-width and prior columns are all NOT NULL ---
809-
# compare DefineAttr in bootstrap.c. oidvector and
810-
# int2vector are also treated as not-nullable.
812+
# fixed-width and prior columns are likewise fixed-width
813+
# and NOT NULL --- compare DefineAttr in bootstrap.c.
814+
# At this point the width of type name is still symbolic,
815+
# so we need a special test.
811816
$row->{attnotnull} =
812-
$type->{typname}eq'oidvector' ?'t'
813-
:$type->{typname}eq'int2vector' ?'t'
814-
:$type->{typlen}eq'NAMEDATALEN' ?'t'
815-
:$type->{typlen} > 0 ?'t'
816-
:'f';
817+
$row->{attlen}eq'NAMEDATALEN' ?'t'
818+
:$row->{attlen} > 0 ?'t'
819+
:'f';
817820
}
818821
else
819822
{

‎src/include/catalog/genbki.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
/*
4747
* Variable-length catalog fields (except possibly the first not nullable one)
4848
* should not be visible in C structures, so they are made invisible by #ifdefs
49-
* of an undefined symbol. See alsoMARKNOTNULLin bootstrap.c for how this is
50-
* handled.
49+
* of an undefined symbol. See alsothe BOOTCOL_NULL_AUTO codein bootstrap.c
50+
*for how this ishandled.
5151
*/
5252
#undef CATALOG_VARLEN
5353

‎src/include/catalog/pg_index.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ CATALOG(pg_index,2610,IndexRelationId) BKI_SCHEMA_MACRO
4444
boolindisreplident;/* is this index the identity for replication? */
4545

4646
/* variable-length fields start here, but we allow direct access to indkey */
47-
int2vectorindkey;/* column numbers of indexed cols, or 0 */
47+
int2vectorindkeyBKI_FORCE_NOT_NULL;/* column numbers of indexed cols,
48+
* or 0 */
4849

4950
#ifdefCATALOG_VARLEN
50-
oidvectorindcollation;/* collation identifiers */
51-
oidvectorindclass;/* opclass identifiers */
52-
int2vectorindoption;/* per-column flags (AM-specific meanings) */
51+
oidvectorindcollationBKI_FORCE_NOT_NULL;/* collation identifiers */
52+
oidvectorindclassBKI_FORCE_NOT_NULL;/* opclass identifiers */
53+
int2vectorindoptionBKI_FORCE_NOT_NULL;/* per-column flags
54+
* (AM-specific meanings) */
5355
pg_node_treeindexprs;/* expression trees for index attributes that
5456
* are not simple column references; one for
5557
* each zero entry in indkey[] */

‎src/include/catalog/pg_partitioned_table.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ CATALOG(pg_partitioned_table,3350,PartitionedRelationId)
4141
* field of a heap tuple can be reliably accessed using its C struct
4242
* offset, as previous fields are all non-nullable fixed-length fields.
4343
*/
44-
int2vectorpartattrs;/* each member of the array is the attribute
45-
* number of a partition key column, or 0 if
46-
* the column is actually an expression */
44+
int2vectorpartattrsBKI_FORCE_NOT_NULL;/* each member of the array is
45+
* the attribute number of a
46+
* partition key column, or 0
47+
* if the column is actually
48+
* an expression */
4749

4850
#ifdefCATALOG_VARLEN
49-
oidvectorpartclass;/* operator class to compare keys */
50-
oidvectorpartcollation;/* user-specified collation for keys */
51+
oidvectorpartclassBKI_FORCE_NOT_NULL;/* operator class to compare
52+
* keys */
53+
oidvectorpartcollationBKI_FORCE_NOT_NULL;/* user-specified
54+
* collation for keys */
5155
pg_node_treepartexprs;/* list of expressions in the partition key;
5256
* one item for each zero entry in partattrs[] */
5357
#endif

‎src/include/catalog/pg_proc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ CATALOG(pg_proc,1255,ProcedureRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(81,Proce
9292
*/
9393

9494
/* parameter types (excludes OUT params) */
95-
oidvectorproargtypesBKI_LOOKUP(pg_type);
95+
oidvectorproargtypesBKI_LOOKUP(pg_type)BKI_FORCE_NOT_NULL;
9696

9797
#ifdefCATALOG_VARLEN
9898

‎src/include/catalog/pg_statistic_ext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ CATALOG(pg_statistic_ext,3381,StatisticExtRelationId)
4747
* variable-length fields start here, but we allow direct access to
4848
* stxkeys
4949
*/
50-
int2vectorstxkeys;/* array of column keys */
50+
int2vectorstxkeysBKI_FORCE_NOT_NULL;/* array of column keys */
5151

5252
#ifdefCATALOG_VARLEN
5353
charstxkind[1]BKI_FORCE_NOT_NULL;/* statistics kinds requested

‎src/include/catalog/pg_trigger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ CATALOG(pg_trigger,2620,TriggerRelationId)
5454
* Variable-length fields start here, but we allow direct access to
5555
* tgattr. Note: tgattr and tgargs must not be null.
5656
*/
57-
int2vectortgattr;/* column numbers, if trigger is on columns */
57+
int2vectortgattrBKI_FORCE_NOT_NULL;/* column numbers, if trigger is
58+
* on columns */
5859

5960
#ifdefCATALOG_VARLEN
6061
byteatgargsBKI_FORCE_NOT_NULL;/* first\000second\000tgnargs\000 */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp