* ALTER TABLE ALTER COLUMN SET/DROP DEFAULT * * Return the address of the affected column. */
| 9368 | * Return the address of the affected column. |
| 9369 | */ |
| 9370 | static ObjectAddress |
| 9371 | ATExecColumnDefault(Relation rel, const char *colName, |
| 9372 | Node *newDefault, LOCKMODE lockmode) |
| 9373 | { |
| 9374 | TupleDesc tupdesc = RelationGetDescr(rel); |
| 9375 | AttrNumber attnum; |
| 9376 | ObjectAddress address; |
| 9377 | |
| 9378 | /* |
| 9379 | * get the number of the attribute |
| 9380 | */ |
| 9381 | attnum = get_attnum(RelationGetRelid(rel), colName); |
| 9382 | if (attnum == InvalidAttrNumber) |
| 9383 | ereport(ERROR, |
| 9384 | (errcode(ERRCODE_UNDEFINED_COLUMN), |
| 9385 | errmsg("column \"%s\" of relation \"%s\" does not exist", |
| 9386 | colName, RelationGetRelationName(rel)))); |
| 9387 | |
| 9388 | /* Prevent them from altering a system attribute */ |
| 9389 | if (attnum <= 0) |
| 9390 | ereport(ERROR, |
| 9391 | (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 9392 | errmsg("cannot alter system column \"%s\"", |
| 9393 | colName))); |
| 9394 | |
| 9395 | if (TupleDescAttr(tupdesc, attnum - 1)->attidentity) |
| 9396 | ereport(ERROR, |
| 9397 | (errcode(ERRCODE_SYNTAX_ERROR), |
| 9398 | errmsg("column \"%s\" of relation \"%s\" is an identity column", |
| 9399 | colName, RelationGetRelationName(rel)), |
| 9400 | newDefault ? 0 : errhint("Use ALTER TABLE ... ALTER COLUMN ... DROP IDENTITY instead."))); |
| 9401 | |
| 9402 | if (TupleDescAttr(tupdesc, attnum - 1)->attgenerated) |
| 9403 | ereport(ERROR, |
| 9404 | (errcode(ERRCODE_SYNTAX_ERROR), |
| 9405 | errmsg("column \"%s\" of relation \"%s\" is a generated column", |
| 9406 | colName, RelationGetRelationName(rel)), |
| 9407 | newDefault || TupleDescAttr(tupdesc, attnum - 1)->attgenerated != ATTRIBUTE_GENERATED_STORED ? 0 : |
| 9408 | errhint("Use ALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION instead."))); |
| 9409 | |
| 9410 | /* |
| 9411 | * Remove any old default for the column. We use RESTRICT here for |
| 9412 | * safety, but at present we do not expect anything to depend on the |
| 9413 | * default. |
| 9414 | * |
| 9415 | * We treat removing the existing default as an internal operation when it |
| 9416 | * is preparatory to adding a new default, but as a user-initiated |
| 9417 | * operation when the user asked for a drop. |
| 9418 | */ |
| 9419 | RemoveAttrDefault(RelationGetRelid(rel), attnum, DROP_RESTRICT, false, |
| 9420 | newDefault == NULL ? false : true); |
| 9421 | |
| 9422 | if (newDefault) |
| 9423 | { |
| 9424 | /* SET DEFAULT */ |
| 9425 | RawColumnDefault *rawEnt; |
| 9426 | |
| 9427 | rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault)); |
no test coverage detected