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

Commitc30f177

Browse files
committed
Apply ALTER ... SET NOT NULL recursively in ALTER ... ADD PRIMARY KEY.
If you do ALTER COLUMN SET NOT NULL against an inheritance parent table,it will recurse to mark all the child columns as NOT NULL as well. Thisis necessary for consistency: if the column is labeled NOT NULL thenreading it should never produce nulls.However, that didn't happen in the case where ALTER ... ADD PRIMARY KEYmarks a target column NOT NULL that wasn't before. That was questionablefrom the beginning, and now Tushar Ahuja points out that it can lead todump/restore failures in some cases. So let's make that case recurse too.Although this is meant to fix a bug, it's enough of a behavioral changethat I'm pretty hesitant to back-patch, especially in view of the lackof similar field complaints. It doesn't seem to be too late to put itinto v10 though.Michael Paquier, editorialized on slightly by meDiscussion:https://postgr.es/m/b8794d6a-38f0-9d7c-ad4b-e85adf860fc9@enterprisedb.com
1 parent97d3a0b commitc30f177

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

‎src/backend/catalog/index.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ relationHasPrimaryKey(Relation rel)
185185
* created NOT NULL during CREATE TABLE), do an ALTER SET NOT NULL to mark
186186
* them so --- or fail if they are not in fact nonnull.
187187
*
188+
* As of PG v10, the SET NOT NULL is applied to child tables as well, so
189+
* that the behavior is like a manual SET NOT NULL.
190+
*
188191
* Caller had better have at least ShareLock on the table, else the not-null
189192
* checking isn't trustworthy.
190193
*/
@@ -253,17 +256,13 @@ index_check_primary_key(Relation heapRel,
253256
}
254257

255258
/*
256-
* XXX: Shouldn't the ALTER TABLE .. SET NOT NULL cascade to child tables?
257-
* Currently, since the PRIMARY KEY itself doesn't cascade, we don't
258-
* cascade the notnull constraint(s) either; but this is pretty debatable.
259-
*
260259
* XXX: possible future improvement: when being called from ALTER TABLE,
261260
* it would be more efficient to merge this with the outer ALTER TABLE, so
262261
* as to avoid two scans. But that seems to complicate DefineIndex's API
263262
* unduly.
264263
*/
265264
if (cmds)
266-
AlterTableInternal(RelationGetRelid(heapRel),cmds,false);
265+
AlterTableInternal(RelationGetRelid(heapRel),cmds,true);
267266
}
268267

269268
/*

‎src/test/regress/expected/alter_table.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ Number of child tables: 1 (Use \d+ to list them.)
328328
Table "public.constraint_rename_test2"
329329
Column | Type | Collation | Nullable | Default
330330
--------+---------+-----------+----------+---------
331-
a | integer | | |
331+
a | integer | |not null |
332332
b | integer | | |
333333
c | integer | | |
334334
d | integer | | |

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp