Skip to content

Commit 8a93c15

Browse files
committed
notes on pg set/drop non null
1 parent 5583424 commit 8a93c15

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

_posts/2024-09-15-postgres-online-ddl.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,34 @@ whether a rewrite will be triggered.
279279
PS. Remember to drop the test event trigger
280280
`drop event trigger log_rewrite_table`.
281281

282+
### Alter table "table_name" alter column "column_name" set/drop non null
283+
284+
Making a column nullable or non nullable needs `AccessExclusiveLock`. Changing
285+
a column to nullable is simple. But changing a column to non nullable needs a
286+
column scan. If one tuple is null at this column, then the change fails with a
287+
message
288+
[column xxx of relation xxx contains null values](https://github.com/postgres/postgres/blob/3f9b9621766796983c37e192f73c5f8751872c18/src/backend/commands/tablecmds.c#L6326).
289+
The call stack is
290+
291+
```
292+
ATController
293+
-> ATRewriteTables
294+
-> ATRewriteTable
295+
296+
while (table_scan_getnextslot(scan, ForwardScanDirection, oldslot)) {
297+
...
298+
if (slot_attisnull(insertslot, attn + 1))
299+
}
300+
301+
302+
```
303+
304+
Note, the function is named `ATRewriteTable`, but it actually can either be
305+
rewriting the table or scanning the table. Also, the code path does not trigger
306+
`table_rewrite` event as the code path is
307+
[here](https://github.com/postgres/postgres/blob/3f9b9621766796983c37e192f73c5f8751872c18/src/backend/commands/tablecmds.c#L5931).
308+
It is not the branch and triggers the `table_rewrite` event.
309+
282310
## Index
283311

284312
### Processes and Memory

0 commit comments

Comments
 (0)