forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commitf4a3fdf
committed
Avoid order-of-execution problems with ALTER TABLE ADD PRIMARY KEY.
Up to now, DefineIndex() was responsible for adding attnotnull constraintsto the columns of a primary key, in any case where it hadn't beenconvenient for transformIndexConstraint() to mark those columns asis_not_null. It (or rather its minion index_check_primary_key) did thisby executing an ALTER TABLE SET NOT NULL command for the target table.The trouble with this solution is that if we're creating the index dueto ALTER TABLE ADD PRIMARY KEY, and the outer ALTER TABLE has additionalsub-commands, the inner ALTER TABLE's operations executed at the wrongtime with respect to the outer ALTER TABLE's operations. In particular,the inner ALTER would perform a validation scan at a point where thetable's storage might be inconsistent with its catalog entries. (This ison the hairy edge of being a security problem, but AFAICS it isn't onebecause the inner scan would only be interested in the tuples' nullbitmaps.) This can result in unexpected failures, such as the one seenin bug #15580 from Allison Kaptur.To fix, let's remove the attempt to do SET NOT NULL from DefineIndex(),reducing index_check_primary_key's role to verifying that the columns arealready not null. (It shouldn't ever see such a case, but it seems wiseto keep the check for safety.) Instead, make transformIndexConstraint()generate ALTER TABLE SET NOT NULL subcommands to be executed ahead ofthe ADD PRIMARY KEY operation in every case where it can't force thecolumn to be created already-not-null. This requires only minor surgeryin parse_utilcmd.c, and it makes for a much more satisfying spec fortransformIndexConstraint(): it's no longer having to take it on faiththat someone else will handle addition of NOT NULL constraints.To make that work, we have to move the execution of AT_SetNotNull intoan ALTER pass that executes ahead of AT_PASS_ADD_INDEX. I moved it toAT_PASS_COL_ATTRS, and put that after AT_PASS_ADD_COL to avoid failurewhen the column is being added in the same command. This incidentallyfixes a bug in the only previous usage of AT_PASS_COL_ATTRS, forAT_SetIdentity: it didn't work either for a newly-added column.Playing around with this exposed a separate bug in ALTER TABLE ONLY ...ADD PRIMARY KEY for partitioned tables. The intent of the ONLY modifierin that context is to prevent doing anything that would require holdinglock for a long time --- but the implied SET NOT NULL would recurse tothe child partitions, and do an expensive validation scan for any childwhere the column(s) were not already NOT NULL. To fix that, invent anew ALTER subcommand AT_CheckNotNull that just insists that a childcolumn be already NOT NULL, and apply that, not AT_SetNotNull, whenrecursing to children in this scenario. This results in a slightly laxerdefinition of ALTER TABLE ONLY ... SET NOT NULL for partitioned tables,too: that command will now work as long as all children are already NOTNULL, whereas before it just threw up its hands if there were anypartitions.In passing, clean up the API of generateClonedIndexStmt(): remove auseless argument, ensure that the output argument is not left undefined,update the header comment.A small side effect of this change is that no-such-column errors in ALTERTABLE ADD PRIMARY KEY now produce a different message that includes thetable name, because they are now detected by the SET NOT NULL step whichhas historically worded its error that way. That seems fine to me, soI didn't make any effort to avoid the wording change.The basic bug #15580 is of very long standing, and these other bugsaren't new in v12 either. However, this is a pretty significant changein the way ALTER TABLE ADD PRIMARY KEY works. On balance it seems bestnot to back-patch, at least not till we get some more confidence thatthis patch has no new bugs.Patch by me, but thanks to Jie Zhang for a preliminary version.Discussion:https://postgr.es/m/15580-d1a6de5a3d65da51@postgresql.orgDiscussion:https://postgr.es/m/1396E95157071C4EBBA51892C5368521017F2E6E63@G08CNEXMBPEKD02.g08.fujitsu.local1 parentc06e355 commitf4a3fdf
File tree
14 files changed
+320
-109
lines changed- src
- backend
- catalog
- commands
- parser
- include
- nodes
- parser
- test
- modules/test_ddl_deparse
- expected
- regress
- expected
- sql
14 files changed
+320
-109
lines changedLines changed: 15 additions & 32 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
185 | 185 |
| |
186 | 186 |
| |
187 | 187 |
| |
188 |
| - | |
189 |
| - | |
190 |
| - | |
191 |
| - | |
| 188 | + | |
192 | 189 |
| |
193 |
| - | |
194 |
| - | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
195 | 196 |
| |
196 | 197 |
| |
197 | 198 |
| |
| |||
202 | 203 |
| |
203 | 204 |
| |
204 | 205 |
| |
205 |
| - | |
206 | 206 |
| |
207 | 207 |
| |
208 | 208 |
| |
209 |
| - | |
210 |
| - | |
| 209 | + | |
| 210 | + | |
211 | 211 |
| |
212 | 212 |
| |
213 | 213 |
| |
| |||
222 | 222 |
| |
223 | 223 |
| |
224 | 224 |
| |
225 |
| - | |
| 225 | + | |
| 226 | + | |
226 | 227 |
| |
227 |
| - | |
228 | 228 |
| |
229 | 229 |
| |
230 | 230 |
| |
| |||
249 | 249 |
| |
250 | 250 |
| |
251 | 251 |
| |
252 |
| - | |
253 |
| - | |
254 |
| - | |
255 |
| - | |
256 |
| - | |
257 |
| - | |
258 |
| - | |
259 |
| - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
260 | 256 |
| |
261 | 257 |
| |
262 | 258 |
| |
263 |
| - | |
264 |
| - | |
265 |
| - | |
266 |
| - | |
267 |
| - | |
268 |
| - | |
269 |
| - | |
270 |
| - | |
271 |
| - | |
272 |
| - | |
273 |
| - | |
274 |
| - | |
275 |
| - | |
276 | 259 |
| |
277 | 260 |
| |
278 | 261 |
| |
|
Lines changed: 104 additions & 22 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
142 | 142 |
| |
143 | 143 |
| |
144 | 144 |
| |
145 |
| - | |
146 | 145 |
| |
147 |
| - | |
| 146 | + | |
| 147 | + | |
148 | 148 |
| |
149 | 149 |
| |
150 | 150 |
| |
| |||
370 | 370 |
| |
371 | 371 |
| |
372 | 372 |
| |
373 |
| - | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
374 | 376 |
| |
375 | 377 |
| |
| 378 | + | |
| 379 | + | |
376 | 380 |
| |
377 | 381 |
| |
378 | 382 |
| |
| |||
1068 | 1072 |
| |
1069 | 1073 |
| |
1070 | 1074 |
| |
1071 |
| - | |
| 1075 | + | |
1072 | 1076 |
| |
1073 | 1077 |
| |
1074 | 1078 |
| |
| |||
3765 | 3769 |
| |
3766 | 3770 |
| |
3767 | 3771 |
| |
| 3772 | + | |
| 3773 | + | |
| 3774 | + | |
| 3775 | + | |
| 3776 | + | |
| 3777 | + | |
| 3778 | + | |
| 3779 | + | |
| 3780 | + | |
3768 | 3781 |
| |
3769 | 3782 |
| |
3770 | 3783 |
| |
| |||
3889 | 3902 |
| |
3890 | 3903 |
| |
3891 | 3904 |
| |
3892 |
| - | |
3893 | 3905 |
| |
3894 | 3906 |
| |
3895 | 3907 |
| |
3896 | 3908 |
| |
3897 |
| - | |
| 3909 | + | |
| 3910 | + | |
| 3911 | + | |
| 3912 | + | |
| 3913 | + | |
| 3914 | + | |
3898 | 3915 |
| |
3899 | 3916 |
| |
3900 |
| - | |
| 3917 | + | |
3901 | 3918 |
| |
3902 | 3919 |
| |
3903 | 3920 |
| |
| |||
4214 | 4231 |
| |
4215 | 4232 |
| |
4216 | 4233 |
| |
| 4234 | + | |
| 4235 | + | |
| 4236 | + | |
4217 | 4237 |
| |
4218 | 4238 |
| |
4219 | 4239 |
| |
| |||
5966 | 5986 |
| |
5967 | 5987 |
| |
5968 | 5988 |
| |
5969 |
| - | |
5970 |
| - | |
5971 |
| - | |
5972 | 5989 |
| |
5973 | 5990 |
| |
5974 | 5991 |
| |
| |||
5990 | 6007 |
| |
5991 | 6008 |
| |
5992 | 6009 |
| |
| 6010 | + | |
| 6011 | + | |
| 6012 | + | |
| 6013 | + | |
| 6014 | + | |
5993 | 6015 |
| |
5994 | 6016 |
| |
5995 | 6017 |
| |
| |||
6116 | 6138 |
| |
6117 | 6139 |
| |
6118 | 6140 |
| |
6119 |
| - | |
| 6141 | + | |
| 6142 | + | |
| 6143 | + | |
6120 | 6144 |
| |
6121 | 6145 |
| |
6122 |
| - | |
6123 |
| - | |
6124 |
| - | |
| 6146 | + | |
| 6147 | + | |
6125 | 6148 |
| |
6126 |
| - | |
| 6149 | + | |
| 6150 | + | |
| 6151 | + | |
| 6152 | + | |
| 6153 | + | |
| 6154 | + | |
| 6155 | + | |
| 6156 | + | |
| 6157 | + | |
| 6158 | + | |
6127 | 6159 |
| |
6128 |
| - | |
| 6160 | + | |
6129 | 6161 |
| |
6130 |
| - | |
6131 |
| - | |
6132 |
| - | |
6133 |
| - | |
6134 |
| - | |
| 6162 | + | |
| 6163 | + | |
| 6164 | + | |
6135 | 6165 |
| |
| 6166 | + | |
| 6167 | + | |
6136 | 6168 |
| |
6137 | 6169 |
| |
6138 | 6170 |
| |
| |||
6207 | 6239 |
| |
6208 | 6240 |
| |
6209 | 6241 |
| |
| 6242 | + | |
| 6243 | + | |
| 6244 | + | |
| 6245 | + | |
| 6246 | + | |
| 6247 | + | |
| 6248 | + | |
| 6249 | + | |
| 6250 | + | |
| 6251 | + | |
| 6252 | + | |
| 6253 | + | |
| 6254 | + | |
| 6255 | + | |
| 6256 | + | |
| 6257 | + | |
| 6258 | + | |
| 6259 | + | |
| 6260 | + | |
| 6261 | + | |
| 6262 | + | |
| 6263 | + | |
| 6264 | + | |
| 6265 | + | |
| 6266 | + | |
| 6267 | + | |
| 6268 | + | |
| 6269 | + | |
| 6270 | + | |
| 6271 | + | |
| 6272 | + | |
| 6273 | + | |
| 6274 | + | |
| 6275 | + | |
| 6276 | + | |
| 6277 | + | |
| 6278 | + | |
| 6279 | + | |
| 6280 | + | |
| 6281 | + | |
6210 | 6282 |
| |
6211 | 6283 |
| |
6212 | 6284 |
| |
| |||
11269 | 11341 |
| |
11270 | 11342 |
| |
11271 | 11343 |
| |
| 11344 | + | |
| 11345 | + | |
| 11346 | + | |
| 11347 | + | |
| 11348 | + | |
| 11349 | + | |
| 11350 | + | |
| 11351 | + | |
| 11352 | + | |
| 11353 | + | |
11272 | 11354 |
| |
11273 | 11355 |
| |
11274 | 11356 |
| |
| |||
15649 | 15731 |
| |
15650 | 15732 |
| |
15651 | 15733 |
| |
15652 |
| - | |
| 15734 | + | |
15653 | 15735 |
| |
15654 | 15736 |
| |
15655 | 15737 |
| |
|
0 commit comments
Comments
(0)