* Alter Table ALL ... SET TABLESPACE * * Allows a user to move all objects of some type in a given tablespace in the * current database to another tablespace. Objects can be chosen based on the * owner of the object also, to allow users to move only their objects. * The user must have CREATE rights on the new tablespace, as usual. The main * permissions handling is done by the lower-level
| 16575 | * lock can't be acquired then we ereport(ERROR). |
| 16576 | */ |
| 16577 | Oid |
| 16578 | AlterTableMoveAll(AlterTableMoveAllStmt *stmt) |
| 16579 | { |
| 16580 | List *relations = NIL; |
| 16581 | ListCell *l; |
| 16582 | ScanKeyData key[1]; |
| 16583 | Relation rel; |
| 16584 | TableScanDesc scan; |
| 16585 | HeapTuple tuple; |
| 16586 | Oid orig_tablespaceoid; |
| 16587 | Oid new_tablespaceoid; |
| 16588 | List *role_oids = roleSpecsToIds(stmt->roles); |
| 16589 | |
| 16590 | /* Ensure we were not asked to move something we can't */ |
| 16591 | if (stmt->objtype != OBJECT_TABLE && stmt->objtype != OBJECT_INDEX && |
| 16592 | stmt->objtype != OBJECT_MATVIEW) |
| 16593 | ereport(ERROR, |
| 16594 | (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 16595 | errmsg("only tables, indexes, and materialized views exist in tablespaces"))); |
| 16596 | |
| 16597 | /* Get the orig and new tablespace OIDs */ |
| 16598 | orig_tablespaceoid = get_tablespace_oid(stmt->orig_tablespacename, false); |
| 16599 | new_tablespaceoid = get_tablespace_oid(stmt->new_tablespacename, false); |
| 16600 | |
| 16601 | /* Can't move shared relations in to or out of pg_global */ |
| 16602 | /* This is also checked by ATExecSetTableSpace, but nice to stop earlier */ |
| 16603 | if (orig_tablespaceoid == GLOBALTABLESPACE_OID || |
| 16604 | new_tablespaceoid == GLOBALTABLESPACE_OID) |
| 16605 | ereport(ERROR, |
| 16606 | (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 16607 | errmsg("cannot move relations in to or out of pg_global tablespace"))); |
| 16608 | |
| 16609 | /* |
| 16610 | * Must have CREATE rights on the new tablespace, unless it is the |
| 16611 | * database default tablespace (which all users implicitly have CREATE |
| 16612 | * rights on). |
| 16613 | */ |
| 16614 | if (OidIsValid(new_tablespaceoid) && new_tablespaceoid != MyDatabaseTableSpace) |
| 16615 | { |
| 16616 | AclResult aclresult; |
| 16617 | |
| 16618 | aclresult = pg_tablespace_aclcheck(new_tablespaceoid, GetUserId(), |
| 16619 | ACL_CREATE); |
| 16620 | if (aclresult != ACLCHECK_OK) |
| 16621 | aclcheck_error(aclresult, OBJECT_TABLESPACE, |
| 16622 | get_tablespace_name(new_tablespaceoid)); |
| 16623 | } |
| 16624 | |
| 16625 | /* |
| 16626 | * Now that the checks are done, check if we should set either to |
| 16627 | * InvalidOid because it is our database's default tablespace. |
| 16628 | */ |
| 16629 | if (orig_tablespaceoid == MyDatabaseTableSpace) |
| 16630 | orig_tablespaceoid = InvalidOid; |
| 16631 | |
| 16632 | if (new_tablespaceoid == MyDatabaseTableSpace) |
| 16633 | new_tablespaceoid = InvalidOid; |
| 16634 |
no test coverage detected