* expandTableLikeClause * * Process LIKE options that require knowing the final column numbers * assigned to the new table's columns. This executes after we have * run DefineRelation for the new table. It returns a list of utility * commands that should be run to generate indexes etc. */
| 1368 | * commands that should be run to generate indexes etc. |
| 1369 | */ |
| 1370 | List * |
| 1371 | expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause) |
| 1372 | { |
| 1373 | List *result = NIL; |
| 1374 | List *atsubcmds = NIL; |
| 1375 | AttrNumber parent_attno; |
| 1376 | Relation relation; |
| 1377 | Relation childrel; |
| 1378 | TupleDesc tupleDesc; |
| 1379 | TupleConstr *constr; |
| 1380 | AttrMap *attmap; |
| 1381 | char *comment; |
| 1382 | |
| 1383 | /* |
| 1384 | * Open the relation referenced by the LIKE clause. We should still have |
| 1385 | * the table lock obtained by transformTableLikeClause (and this'll throw |
| 1386 | * an assertion failure if not). Hence, no need to recheck privileges |
| 1387 | * etc. We must open the rel by OID not name, to be sure we get the same |
| 1388 | * table. |
| 1389 | */ |
| 1390 | if (!OidIsValid(table_like_clause->relationOid)) |
| 1391 | elog(ERROR, "expandTableLikeClause called on untransformed LIKE clause"); |
| 1392 | |
| 1393 | relation = relation_open(table_like_clause->relationOid, NoLock); |
| 1394 | |
| 1395 | tupleDesc = RelationGetDescr(relation); |
| 1396 | constr = tupleDesc->constr; |
| 1397 | |
| 1398 | /* |
| 1399 | * Open the newly-created child relation; we have lock on that too. |
| 1400 | */ |
| 1401 | childrel = relation_openrv(heapRel, NoLock); |
| 1402 | |
| 1403 | /* |
| 1404 | * Construct a map from the LIKE relation's attnos to the child rel's. |
| 1405 | * This re-checks type match etc, although it shouldn't be possible to |
| 1406 | * have a failure since both tables are locked. |
| 1407 | */ |
| 1408 | attmap = build_attrmap_by_name(RelationGetDescr(childrel), |
| 1409 | tupleDesc); |
| 1410 | |
| 1411 | /* |
| 1412 | * Process defaults, if required. |
| 1413 | */ |
| 1414 | if ((table_like_clause->options & |
| 1415 | (CREATE_TABLE_LIKE_DEFAULTS | CREATE_TABLE_LIKE_GENERATED)) && |
| 1416 | constr != NULL) |
| 1417 | { |
| 1418 | for (parent_attno = 1; parent_attno <= tupleDesc->natts; |
| 1419 | parent_attno++) |
| 1420 | { |
| 1421 | Form_pg_attribute attribute = TupleDescAttr(tupleDesc, |
| 1422 | parent_attno - 1); |
| 1423 | |
| 1424 | /* |
| 1425 | * Ignore dropped columns in the parent. |
| 1426 | */ |
| 1427 | if (attribute->attisdropped) |
no test coverage detected