PostgreSQL Source Code git master
Loading...
Searching...
No Matches
tablecmds.h File Reference
#include "access/htup.h"
#include "catalog/dependency.h"
#include "catalog/objectaddress.h"
#include "nodes/parsenodes.h"
#include "storage/lockdefs.h"
#include "utils/relcache.h"
Include dependency graph for tablecmds.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct AlterTableUtilityContext AlterTableUtilityContext
 

Functions

ObjectAddress DefineRelation (CreateStmt *stmt, char relkind, Oid ownerId, ObjectAddress *typaddress, const char *queryString)
 
TupleDesc BuildDescForRelation (const List *columns)
 
void RemoveRelations (DropStmt *drop)
 
Oid AlterTableLookupRelation (AlterTableStmt *stmt, LOCKMODE lockmode)
 
void AlterTable (AlterTableStmt *stmt, LOCKMODE lockmode, AlterTableUtilityContext *context)
 
LOCKMODE AlterTableGetLockLevel (List *cmds)
 
void ATExecChangeOwner (Oid relationOid, Oid newOwnerId, bool recursing, LOCKMODE lockmode)
 
void AlterTableInternal (Oid relid, List *cmds, bool recurse)
 
Oid AlterTableMoveAll (AlterTableMoveAllStmt *stmt)
 
ObjectAddress AlterTableNamespace (AlterObjectSchemaStmt *stmt, Oid *oldschema)
 
void AlterTableNamespaceInternal (Relation rel, Oid oldNspOid, Oid nspOid, ObjectAddresses *objsMoved)
 
void AlterRelationNamespaceInternal (Relation classRel, Oid relOid, Oid oldNspOid, Oid newNspOid, bool hasDependEntry, ObjectAddresses *objsMoved)
 
void CheckTableNotInUse (Relation rel, const char *stmt)
 
void ExecuteTruncate (TruncateStmt *stmt)
 
void ExecuteTruncateGuts (List *explicit_rels, List *relids, List *relids_logged, DropBehavior behavior, bool restart_seqs, bool run_as_table_owner)
 
void SetRelationHasSubclass (Oid relationId, bool relhassubclass)
 
bool CheckRelationTableSpaceMove (Relation rel, Oid newTableSpaceId)
 
void SetRelationTableSpace (Relation rel, Oid newTableSpaceId, RelFileNumber newRelFilenumber)
 
ObjectAddress renameatt (RenameStmt *stmt)
 
ObjectAddress RenameConstraint (RenameStmt *stmt)
 
ObjectAddress RenameRelation (RenameStmt *stmt)
 
void RenameRelationInternal (Oid myrelid, const char *newrelname, bool is_internal, bool is_index)
 
void ResetRelRewrite (Oid myrelid)
 
void find_composite_type_dependencies (Oid typeOid, Relation origRelation, const char *origTypeName)
 
void check_of_type (HeapTuple typetuple)
 
void register_on_commit_action (Oid relid, OnCommitAction action)
 
void remove_on_commit_action (Oid relid)
 
void PreCommit_on_commit_actions (void)
 
void AtEOXact_on_commit_actions (bool isCommit)
 
void AtEOSubXact_on_commit_actions (bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
 
void RangeVarCallbackMaintainsTable (const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
 
void RangeVarCallbackOwnsRelation (const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
 
bool PartConstraintImpliedByRelConstraint (Relation scanrel, List *partConstraint)
 

Typedef Documentation

◆ AlterTableUtilityContext

Function Documentation

◆ AlterRelationNamespaceInternal()

void AlterRelationNamespaceInternal ( Relation  classRel,
Oid  relOid,
Oid  oldNspOid,
Oid  newNspOid,
bool  hasDependEntry,
ObjectAddresses objsMoved 
)
extern

Definition at line 19520 of file tablecmds.c.

19524{
19528 bool already_done = false;
19529
19530 /* no rel lock for relkind=c so use LOCKTAG_TUPLE */
19533 elog(ERROR, "cache lookup failed for relation %u", relOid);
19535
19536 Assert(classForm->relnamespace == oldNspOid);
19537
19538 thisobj.classId = RelationRelationId;
19539 thisobj.objectId = relOid;
19540 thisobj.objectSubId = 0;
19541
19542 /*
19543 * If the object has already been moved, don't move it again. If it's
19544 * already in the right place, don't move it, but still fire the object
19545 * access hook.
19546 */
19548 if (!already_done && oldNspOid != newNspOid)
19549 {
19550 ItemPointerData otid = classTup->t_self;
19551
19552 /* check for duplicate name (more friendly than unique-index failure) */
19553 if (get_relname_relid(NameStr(classForm->relname),
19555 ereport(ERROR,
19557 errmsg("relation \"%s\" already exists in schema \"%s\"",
19558 NameStr(classForm->relname),
19560
19561 /* classTup is a copy, so OK to scribble on */
19562 classForm->relnamespace = newNspOid;
19563
19566
19567
19568 /* Update dependency on schema if caller said so */
19569 if (hasDependEntry &&
19571 relOid,
19573 oldNspOid,
19574 newNspOid) != 1)
19575 elog(ERROR, "could not change schema dependency for relation \"%s\"",
19576 NameStr(classForm->relname));
19577 }
19578 else
19580 if (!already_done)
19581 {
19583
19585 }
19586
19588}
#define NameStr(name)
Definition c.h:891
#define Assert(condition)
Definition c.h:999
bool object_address_present(const ObjectAddress *object, const ObjectAddresses *addrs)
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
void CatalogTupleUpdate(Relation heapRel, const ItemPointerData *otid, HeapTuple tup)
Definition indexing.c:313
void UnlockTuple(Relation relation, const ItemPointerData *tid, LOCKMODE lockmode)
Definition lmgr.c:601
#define InplaceUpdateTupleLock
Definition lockdefs.h:48
char * get_namespace_name(Oid nspid)
Definition lsyscache.c:3674
Oid get_relname_relid(const char *relname, Oid relnamespace)
Definition lsyscache.c:2191
static char * errmsg
#define InvokeObjectPostAlterHook(classId, objectId, subId)
FormData_pg_class * Form_pg_class
Definition pg_class.h:160
long changeDependencyFor(Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition pg_depend.c:470
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
#define InvalidOid
static int fb(int x)
HeapTuple SearchSysCacheLockedCopy1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:400

References add_exact_object_address(), Assert, CatalogTupleUpdate(), changeDependencyFor(), elog, ereport, errcode(), errmsg, ERROR, fb(), get_namespace_name(), get_relname_relid(), GETSTRUCT(), heap_freetuple(), HeapTupleIsValid, InplaceUpdateTupleLock, InvalidOid, InvokeObjectPostAlterHook, NameStr, object_address_present(), ObjectIdGetDatum(), SearchSysCacheLockedCopy1(), and UnlockTuple().

Referenced by AlterIndexNamespaces(), AlterSeqNamespaces(), AlterTableNamespaceInternal(), and AlterTypeNamespaceInternal().

◆ AlterTable()

void AlterTable ( AlterTableStmt stmt,
LOCKMODE  lockmode,
AlterTableUtilityContext context 
)
extern

Definition at line 4598 of file tablecmds.c.

4600{
4601 Relation rel;
4602
4603 /* Caller is required to provide an adequate lock. */
4604 rel = relation_open(context->relid, NoLock);
4605
4607
4608 ATController(stmt, rel, stmt->cmds, stmt->relation->inh, lockmode, context);
4609}
#define stmt
#define NoLock
Definition lockdefs.h:34
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition relation.c:48
static void CheckAlterTableIsSafe(Relation rel)
Definition tablecmds.c:4513
static void ATController(AlterTableStmt *parsetree, Relation rel, List *cmds, bool recurse, LOCKMODE lockmode, AlterTableUtilityContext *context)
Definition tablecmds.c:4939

References ATController(), CheckAlterTableIsSafe(), NoLock, relation_open(), AlterTableUtilityContext::relid, and stmt.

Referenced by ProcessUtilitySlow().

◆ AlterTableGetLockLevel()

LOCKMODE AlterTableGetLockLevel ( List cmds)
extern

Definition at line 4672 of file tablecmds.c.

4673{
4674 /*
4675 * This only works if we read catalog tables using MVCC snapshots.
4676 */
4677 ListCell *lcmd;
4679
4680 foreach(lcmd, cmds)
4681 {
4683 LOCKMODE cmd_lockmode = AccessExclusiveLock; /* default for compiler */
4684
4685 switch (cmd->subtype)
4686 {
4687 /*
4688 * These subcommands rewrite the heap, so require full locks.
4689 */
4690 case AT_AddColumn: /* may rewrite heap, in some cases and visible
4691 * to SELECT */
4692 case AT_SetAccessMethod: /* must rewrite heap */
4693 case AT_SetTableSpace: /* must rewrite heap */
4694 case AT_AlterColumnType: /* must rewrite heap */
4696 break;
4697
4698 /*
4699 * These subcommands may require addition of toast tables. If
4700 * we add a toast table to a table currently being scanned, we
4701 * might miss data added to the new toast table by concurrent
4702 * insert transactions.
4703 */
4704 case AT_SetStorage: /* may add toast tables, see
4705 * ATRewriteCatalogs() */
4707 break;
4708
4709 /*
4710 * Removing constraints can affect SELECTs that have been
4711 * optimized assuming the constraint holds true. See also
4712 * CloneFkReferenced.
4713 */
4714 case AT_DropConstraint: /* as DROP INDEX */
4715 case AT_DropNotNull: /* may change some SQL plans */
4717 break;
4718
4719 /*
4720 * Subcommands that may be visible to concurrent SELECTs
4721 */
4722 case AT_DropColumn: /* change visible to SELECT */
4723 case AT_AddColumnToView: /* CREATE VIEW */
4724 case AT_DropOids: /* used to equiv to DropColumn */
4725 case AT_EnableAlwaysRule: /* may change SELECT rules */
4726 case AT_EnableReplicaRule: /* may change SELECT rules */
4727 case AT_EnableRule: /* may change SELECT rules */
4728 case AT_DisableRule: /* may change SELECT rules */
4730 break;
4731
4732 /*
4733 * Changing owner may remove implicit SELECT privileges
4734 */
4735 case AT_ChangeOwner: /* change visible to SELECT */
4737 break;
4738
4739 /*
4740 * Changing foreign table options may affect optimization.
4741 */
4742 case AT_GenericOptions:
4745 break;
4746
4747 /*
4748 * These subcommands affect write operations only.
4749 */
4750 case AT_EnableTrig:
4753 case AT_EnableTrigAll:
4754 case AT_EnableTrigUser:
4755 case AT_DisableTrig:
4756 case AT_DisableTrigAll:
4757 case AT_DisableTrigUser:
4759 break;
4760
4761 /*
4762 * These subcommands affect write operations only. XXX
4763 * Theoretically, these could be ShareRowExclusiveLock.
4764 */
4765 case AT_ColumnDefault:
4767 case AT_AlterConstraint:
4768 case AT_AddIndex: /* from ADD CONSTRAINT */
4770 case AT_ReplicaIdentity:
4771 case AT_SetNotNull:
4776 case AT_AddIdentity:
4777 case AT_DropIdentity:
4778 case AT_SetIdentity:
4779 case AT_SetExpression:
4780 case AT_DropExpression:
4781 case AT_SetCompression:
4783 break;
4784
4785 case AT_AddConstraint:
4786 case AT_ReAddConstraint: /* becomes AT_AddConstraint */
4787 case AT_ReAddDomainConstraint: /* becomes AT_AddConstraint */
4788 if (IsA(cmd->def, Constraint))
4789 {
4790 Constraint *con = (Constraint *) cmd->def;
4791
4792 switch (con->contype)
4793 {
4794 case CONSTR_EXCLUSION:
4795 case CONSTR_PRIMARY:
4796 case CONSTR_UNIQUE:
4797
4798 /*
4799 * Cases essentially the same as CREATE INDEX. We
4800 * could reduce the lock strength to ShareLock if
4801 * we can work out how to allow concurrent catalog
4802 * updates. XXX Might be set down to
4803 * ShareRowExclusiveLock but requires further
4804 * analysis.
4805 */
4807 break;
4808 case CONSTR_FOREIGN:
4809
4810 /*
4811 * We add triggers to both tables when we add a
4812 * Foreign Key, so the lock level must be at least
4813 * as strong as CREATE TRIGGER.
4814 */
4816 break;
4817
4818 default:
4820 }
4821 }
4822 break;
4823
4824 /*
4825 * These subcommands affect inheritance behaviour. Queries
4826 * started before us will continue to see the old inheritance
4827 * behaviour, while queries started after we commit will see
4828 * new behaviour. No need to prevent reads or writes to the
4829 * subtable while we hook it up though. Changing the TupDesc
4830 * may be a problem, so keep highest lock.
4831 */
4832 case AT_AddInherit:
4833 case AT_DropInherit:
4835 break;
4836
4837 /*
4838 * These subcommands affect implicit row type conversion. They
4839 * have affects similar to CREATE/DROP CAST on queries. don't
4840 * provide for invalidating parse trees as a result of such
4841 * changes, so we keep these at AccessExclusiveLock.
4842 */
4843 case AT_AddOf:
4844 case AT_DropOf:
4846 break;
4847
4848 /*
4849 * Only used by CREATE OR REPLACE VIEW which must conflict
4850 * with an SELECTs currently using the view.
4851 */
4854 break;
4855
4856 /*
4857 * These subcommands affect general strategies for performance
4858 * and maintenance, though don't change the semantic results
4859 * from normal data reads and writes. Delaying an ALTER TABLE
4860 * behind currently active writes only delays the point where
4861 * the new strategy begins to take effect, so there is no
4862 * benefit in waiting. In this case the minimum restriction
4863 * applies: we don't currently allow concurrent catalog
4864 * updates.
4865 */
4866 case AT_SetStatistics: /* Uses MVCC in getTableAttrs() */
4867 case AT_ClusterOn: /* Uses MVCC in getIndexes() */
4868 case AT_DropCluster: /* Uses MVCC in getIndexes() */
4869 case AT_SetOptions: /* Uses MVCC in getTableAttrs() */
4870 case AT_ResetOptions: /* Uses MVCC in getTableAttrs() */
4872 break;
4873
4874 case AT_SetLogged:
4875 case AT_SetUnLogged:
4877 break;
4878
4879 case AT_ValidateConstraint: /* Uses MVCC in getConstraints() */
4881 break;
4882
4883 /*
4884 * Rel options are more complex than first appears. Options
4885 * are set here for tables, views and indexes; for historical
4886 * reasons these can all be used with ALTER TABLE, so we can't
4887 * decide between them using the basic grammar.
4888 */
4889 case AT_SetRelOptions: /* Uses MVCC in getIndexes() and
4890 * getTables() */
4891 case AT_ResetRelOptions: /* Uses MVCC in getIndexes() and
4892 * getTables() */
4894 break;
4895
4896 case AT_AttachPartition:
4898 break;
4899
4900 case AT_DetachPartition:
4901 if (((PartitionCmd *) cmd->def)->concurrent)
4903 else
4905 break;
4906
4909 break;
4910
4911 case AT_MergePartitions:
4912 case AT_SplitPartition:
4914 break;
4915
4916 default: /* oops */
4917 elog(ERROR, "unrecognized alter table type: %d",
4918 (int) cmd->subtype);
4919 break;
4920 }
4921
4922 /*
4923 * Take the greatest lockmode from any subcommand
4924 */
4925 if (cmd_lockmode > lockmode)
4926 lockmode = cmd_lockmode;
4927 }
4928
4929 return lockmode;
4930}
int LOCKMODE
Definition lockdefs.h:26
#define AccessExclusiveLock
Definition lockdefs.h:43
#define ShareRowExclusiveLock
Definition lockdefs.h:41
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
#define IsA(nodeptr, _type_)
Definition nodes.h:162
@ CONSTR_FOREIGN
@ CONSTR_UNIQUE
@ CONSTR_EXCLUSION
@ CONSTR_PRIMARY
@ AT_AddIndexConstraint
@ AT_MergePartitions
@ AT_DropOf
@ AT_SetOptions
@ AT_DropIdentity
@ AT_DisableTrigUser
@ AT_DropNotNull
@ AT_AddOf
@ AT_ResetOptions
@ AT_ReplicaIdentity
@ AT_ReplaceRelOptions
@ AT_EnableRowSecurity
@ AT_AddColumnToView
@ AT_ResetRelOptions
@ AT_EnableReplicaTrig
@ AT_DropOids
@ AT_SetIdentity
@ AT_SetUnLogged
@ AT_DisableTrig
@ AT_SetCompression
@ AT_DropExpression
@ AT_AddIndex
@ AT_EnableReplicaRule
@ AT_DropConstraint
@ AT_SetNotNull
@ AT_ClusterOn
@ AT_AddIdentity
@ AT_ForceRowSecurity
@ AT_EnableAlwaysRule
@ AT_SetAccessMethod
@ AT_AlterColumnType
@ AT_DetachPartitionFinalize
@ AT_AddInherit
@ AT_ReAddDomainConstraint
@ AT_EnableTrig
@ AT_DropColumn
@ AT_AlterColumnGenericOptions
@ AT_DisableTrigAll
@ AT_EnableRule
@ AT_NoForceRowSecurity
@ AT_DetachPartition
@ AT_SetStatistics
@ AT_AttachPartition
@ AT_AddConstraint
@ AT_DropInherit
@ AT_EnableAlwaysTrig
@ AT_SetLogged
@ AT_SetStorage
@ AT_DisableRule
@ AT_DisableRowSecurity
@ AT_SetRelOptions
@ AT_ChangeOwner
@ AT_EnableTrigUser
@ AT_SetExpression
@ AT_ReAddConstraint
@ AT_SetTableSpace
@ AT_GenericOptions
@ AT_ColumnDefault
@ AT_CookedColumnDefault
@ AT_AlterConstraint
@ AT_EnableTrigAll
@ AT_SplitPartition
@ AT_DropCluster
@ AT_ValidateConstraint
@ AT_AddColumn
#define lfirst(lc)
Definition pg_list.h:172
LOCKMODE AlterTableGetRelOptionsLockLevel(List *defList)
AlterTableType subtype
Definition pg_list.h:54

References AccessExclusiveLock, AlterTableGetRelOptionsLockLevel(), AT_AddColumn, AT_AddColumnToView, AT_AddConstraint, AT_AddIdentity, AT_AddIndex, AT_AddIndexConstraint, AT_AddInherit, AT_AddOf, AT_AlterColumnGenericOptions, AT_AlterColumnType, AT_AlterConstraint, AT_AttachPartition, AT_ChangeOwner, AT_ClusterOn, AT_ColumnDefault, AT_CookedColumnDefault, AT_DetachPartition, AT_DetachPartitionFinalize, AT_DisableRowSecurity, AT_DisableRule, AT_DisableTrig, AT_DisableTrigAll, AT_DisableTrigUser, AT_DropCluster, AT_DropColumn, AT_DropConstraint, AT_DropExpression, AT_DropIdentity, AT_DropInherit, AT_DropNotNull, AT_DropOf, AT_DropOids, AT_EnableAlwaysRule, AT_EnableAlwaysTrig, AT_EnableReplicaRule, AT_EnableReplicaTrig, AT_EnableRowSecurity, AT_EnableRule, AT_EnableTrig, AT_EnableTrigAll, AT_EnableTrigUser, AT_ForceRowSecurity, AT_GenericOptions, AT_MergePartitions, AT_NoForceRowSecurity, AT_ReAddConstraint, AT_ReAddDomainConstraint, AT_ReplaceRelOptions, AT_ReplicaIdentity, AT_ResetOptions, AT_ResetRelOptions, AT_SetAccessMethod, AT_SetCompression, AT_SetExpression, AT_SetIdentity, AT_SetLogged, AT_SetNotNull, AT_SetOptions, AT_SetRelOptions, AT_SetStatistics, AT_SetStorage, AT_SetTableSpace, AT_SetUnLogged, AT_SplitPartition, AT_ValidateConstraint, CONSTR_EXCLUSION, CONSTR_FOREIGN, CONSTR_PRIMARY, CONSTR_UNIQUE, Constraint::contype, AlterTableCmd::def, elog, ERROR, fb(), IsA, lfirst, ShareRowExclusiveLock, ShareUpdateExclusiveLock, and AlterTableCmd::subtype.

Referenced by AlterTableInternal(), and ProcessUtilitySlow().

◆ AlterTableInternal()

void AlterTableInternal ( Oid  relid,
List cmds,
bool  recurse 
)
extern

Definition at line 4627 of file tablecmds.c.

4628{
4629 Relation rel;
4630 LOCKMODE lockmode = AlterTableGetLockLevel(cmds);
4631
4632 rel = relation_open(relid, lockmode);
4633
4635
4636 ATController(NULL, rel, cmds, recurse, lockmode, NULL);
4637}
void EventTriggerAlterTableRelid(Oid objectId)
LOCKMODE AlterTableGetLockLevel(List *cmds)
Definition tablecmds.c:4672

References AlterTableGetLockLevel(), ATController(), EventTriggerAlterTableRelid(), fb(), and relation_open().

Referenced by AlterTableMoveAll(), and DefineVirtualRelation().

◆ AlterTableLookupRelation()

Oid AlterTableLookupRelation ( AlterTableStmt stmt,
LOCKMODE  lockmode 
)
extern

Definition at line 4539 of file tablecmds.c.

4540{
4541 return RangeVarGetRelidExtended(stmt->relation, lockmode,
4542 stmt->missing_ok ? RVR_MISSING_OK : 0,
4544 stmt);
4545}
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition namespace.c:442
@ RVR_MISSING_OK
Definition namespace.h:90
static void RangeVarCallbackForAlterRelation(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)

References RangeVarCallbackForAlterRelation(), RangeVarGetRelidExtended(), RVR_MISSING_OK, and stmt.

Referenced by ProcessUtilitySlow().

◆ AlterTableMoveAll()

Oid AlterTableMoveAll ( AlterTableMoveAllStmt stmt)
extern

Definition at line 17460 of file tablecmds.c.

17461{
17462 List *relations = NIL;
17463 ListCell *l;
17464 ScanKeyData key[1];
17465 Relation rel;
17466 TableScanDesc scan;
17467 HeapTuple tuple;
17470 List *role_oids = roleSpecsToIds(stmt->roles);
17471
17472 /* Ensure we were not asked to move something we can't */
17473 if (stmt->objtype != OBJECT_TABLE && stmt->objtype != OBJECT_INDEX &&
17474 stmt->objtype != OBJECT_MATVIEW)
17475 ereport(ERROR,
17477 errmsg("only tables, indexes, and materialized views exist in tablespaces")));
17478
17479 /* Get the orig and new tablespace OIDs */
17480 orig_tablespaceoid = get_tablespace_oid(stmt->orig_tablespacename, false);
17481 new_tablespaceoid = get_tablespace_oid(stmt->new_tablespacename, false);
17482
17483 /* Can't move shared relations in to or out of pg_global */
17484 /* This is also checked by ATExecSetTableSpace, but nice to stop earlier */
17487 ereport(ERROR,
17489 errmsg("cannot move relations in to or out of pg_global tablespace")));
17490
17491 /*
17492 * Must have CREATE rights on the new tablespace, unless it is the
17493 * database default tablespace (which all users implicitly have CREATE
17494 * rights on).
17495 */
17497 {
17499
17501 ACL_CREATE);
17502 if (aclresult != ACLCHECK_OK)
17505 }
17506
17507 /*
17508 * Now that the checks are done, check if we should set either to
17509 * InvalidOid because it is our database's default tablespace.
17510 */
17513
17516
17517 /* no-op */
17519 return new_tablespaceoid;
17520
17521 /*
17522 * Walk the list of objects in the tablespace and move them. This will
17523 * only find objects in our database, of course.
17524 */
17525 ScanKeyInit(&key[0],
17529
17531 scan = table_beginscan_catalog(rel, 1, key);
17532 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
17533 {
17535 Oid relOid = relForm->oid;
17536
17537 /*
17538 * Do not move objects in pg_catalog as part of this, if an admin
17539 * really wishes to do so, they can issue the individual ALTER
17540 * commands directly.
17541 *
17542 * Also, explicitly avoid any shared tables, temp tables, or TOAST
17543 * (TOAST will be moved with the main table).
17544 */
17545 if (IsCatalogNamespace(relForm->relnamespace) ||
17546 relForm->relisshared ||
17547 isAnyTempNamespace(relForm->relnamespace) ||
17548 IsToastNamespace(relForm->relnamespace))
17549 continue;
17550
17551 /* Only move the object type requested */
17552 if ((stmt->objtype == OBJECT_TABLE &&
17553 relForm->relkind != RELKIND_RELATION &&
17554 relForm->relkind != RELKIND_PARTITIONED_TABLE) ||
17555 (stmt->objtype == OBJECT_INDEX &&
17556 relForm->relkind != RELKIND_INDEX &&
17557 relForm->relkind != RELKIND_PARTITIONED_INDEX) ||
17558 (stmt->objtype == OBJECT_MATVIEW &&
17559 relForm->relkind != RELKIND_MATVIEW))
17560 continue;
17561
17562 /* Check if we are only moving objects owned by certain roles */
17563 if (role_oids != NIL && !list_member_oid(role_oids, relForm->relowner))
17564 continue;
17565
17566 /*
17567 * Handle permissions-checking here since we are locking the tables
17568 * and also to avoid doing a bunch of work only to fail part-way. Note
17569 * that permissions will also be checked by AlterTableInternal().
17570 *
17571 * Caller must be considered an owner on the table to move it.
17572 */
17575 NameStr(relForm->relname));
17576
17577 if (stmt->nowait &&
17579 ereport(ERROR,
17581 errmsg("aborting because lock on relation \"%s.%s\" is not available",
17582 get_namespace_name(relForm->relnamespace),
17583 NameStr(relForm->relname))));
17584 else
17586
17587 /* Add to our list of objects to move */
17588 relations = lappend_oid(relations, relOid);
17589 }
17590
17591 table_endscan(scan);
17593
17594 if (relations == NIL)
17597 errmsg("no matching relations in tablespace \"%s\" found",
17598 orig_tablespaceoid == InvalidOid ? "(database default)" :
17600
17601 /* Everything is locked, loop through and move all of the relations. */
17602 foreach(l, relations)
17603 {
17604 List *cmds = NIL;
17606
17608 cmd->name = stmt->new_tablespacename;
17609
17610 cmds = lappend(cmds, cmd);
17611
17613 /* OID is set by AlterTableInternal */
17614 AlterTableInternal(lfirst_oid(l), cmds, false);
17616 }
17617
17618 return new_tablespaceoid;
17619}
AclResult
Definition acl.h:183
@ ACLCHECK_OK
Definition acl.h:184
@ ACLCHECK_NOT_OWNER
Definition acl.h:186
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition aclchk.c:2672
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition aclchk.c:3880
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition aclchk.c:4134
char * get_tablespace_name(Oid spc_oid)
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
#define OidIsValid(objectId)
Definition c.h:914
bool IsToastNamespace(Oid namespaceId)
Definition catalog.c:261
bool IsCatalogNamespace(Oid namespaceId)
Definition catalog.c:243
#define NOTICE
Definition elog.h:36
void EventTriggerAlterTableStart(const Node *parsetree)
void EventTriggerAlterTableEnd(void)
Oid MyDatabaseTableSpace
Definition globals.c:98
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition heapam.c:1435
List * lappend(List *list, void *datum)
Definition list.c:339
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
bool list_member_oid(const List *list, Oid datum)
Definition list.c:722
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition lmgr.c:151
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition lmgr.c:107
#define AccessShareLock
Definition lockdefs.h:36
char get_rel_relkind(Oid relid)
Definition lsyscache.c:2309
Oid GetUserId(void)
Definition miscinit.c:470
bool isAnyTempNamespace(Oid namespaceId)
Definition namespace.c:3759
#define makeNode(_type_)
Definition nodes.h:159
ObjectType get_relkind_objtype(char relkind)
@ OBJECT_MATVIEW
@ OBJECT_TABLESPACE
@ OBJECT_INDEX
@ OBJECT_TABLE
#define ACL_CREATE
Definition parsenodes.h:85
#define NIL
Definition pg_list.h:68
#define lfirst_oid(lc)
Definition pg_list.h:174
unsigned int Oid
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition scankey.c:76
@ ForwardScanDirection
Definition sdir.h:28
#define BTEqualStrategyNumber
Definition stratnum.h:31
Definition nodes.h:133
void table_close(Relation relation, LOCKMODE lockmode)
Definition table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition table.c:40
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, ScanKeyData *key)
Definition tableam.c:113
static void table_endscan(TableScanDesc scan)
Definition tableam.h:1061
void AlterTableInternal(Oid relid, List *cmds, bool recurse)
Definition tablecmds.c:4627
List * roleSpecsToIds(List *memberNames)
Definition user.c:1665

References AccessExclusiveLock, AccessShareLock, ACL_CREATE, aclcheck_error(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, AlterTableInternal(), AT_SetTableSpace, BTEqualStrategyNumber, ConditionalLockRelationOid(), ereport, errcode(), errmsg, ERROR, EventTriggerAlterTableEnd(), EventTriggerAlterTableStart(), fb(), ForwardScanDirection, get_namespace_name(), get_rel_relkind(), get_relkind_objtype(), get_tablespace_name(), get_tablespace_oid(), GETSTRUCT(), GetUserId(), heap_getnext(), InvalidOid, isAnyTempNamespace(), IsCatalogNamespace(), IsToastNamespace(), lappend(), lappend_oid(), lfirst_oid, list_member_oid(), LockRelationOid(), makeNode, MyDatabaseTableSpace, AlterTableCmd::name, NameStr, NIL, NOTICE, object_aclcheck(), OBJECT_INDEX, OBJECT_MATVIEW, object_ownercheck(), OBJECT_TABLE, OBJECT_TABLESPACE, ObjectIdGetDatum(), OidIsValid, roleSpecsToIds(), ScanKeyInit(), stmt, AlterTableCmd::subtype, table_beginscan_catalog(), table_close(), table_endscan(), and table_open().

Referenced by ProcessUtilitySlow().

◆ AlterTableNamespace()

ObjectAddress AlterTableNamespace ( AlterObjectSchemaStmt stmt,
Oid oldschema 
)
extern

Definition at line 19412 of file tablecmds.c.

19413{
19414 Relation rel;
19415 Oid relid;
19416 Oid oldNspOid;
19417 Oid nspOid;
19418 RangeVar *newrv;
19421
19423 stmt->missing_ok ? RVR_MISSING_OK : 0,
19425 stmt);
19426
19427 if (!OidIsValid(relid))
19428 {
19430 (errmsg("relation \"%s\" does not exist, skipping",
19431 stmt->relation->relname)));
19432 return InvalidObjectAddress;
19433 }
19434
19435 rel = relation_open(relid, NoLock);
19436
19438
19439 /* If it's an owned sequence, disallow moving it by itself. */
19440 if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
19441 {
19442 Oid tableId;
19443 int32 colId;
19444
19445 if (sequenceIsOwned(relid, DEPENDENCY_AUTO, &tableId, &colId) ||
19447 ereport(ERROR,
19449 errmsg("cannot move an owned sequence into another schema"),
19450 errdetail("Sequence \"%s\" is linked to table \"%s\".",
19453 }
19454
19455 /* Get and lock schema OID and check its permissions. */
19456 newrv = makeRangeVar(stmt->newschema, RelationGetRelationName(rel), -1);
19458
19459 /* common checks on switching namespaces */
19461
19465
19467
19468 if (oldschema)
19470
19471 /* close rel, but keep lock until commit */
19472 relation_close(rel, NoLock);
19473
19474 return myself;
19475}
int32_t int32
Definition c.h:676
ObjectAddresses * new_object_addresses(void)
void free_object_addresses(ObjectAddresses *addrs)
@ DEPENDENCY_AUTO
Definition dependency.h:34
@ DEPENDENCY_INTERNAL
Definition dependency.h:35
int errdetail(const char *fmt,...) pg_attribute_printf(1
char * get_rel_name(Oid relid)
Definition lsyscache.c:2234
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition makefuncs.c:473
Oid RangeVarGetAndCheckCreationNamespace(RangeVar *relation, LOCKMODE lockmode, Oid *existing_relation_id)
Definition namespace.c:740
void CheckSetNamespace(Oid oldNspOid, Oid nspOid)
Definition namespace.c:3531
const ObjectAddress InvalidObjectAddress
#define ObjectAddressSet(addr, class_id, object_id)
bool sequenceIsOwned(Oid seqId, char deptype, Oid *tableId, int32 *colId)
Definition pg_depend.c:1032
#define RelationGetRelationName(relation)
Definition rel.h:550
#define RelationGetNamespace(relation)
Definition rel.h:557
void relation_close(Relation relation, LOCKMODE lockmode)
Definition relation.c:206
Form_pg_class rd_rel
Definition rel.h:111
void AlterTableNamespaceInternal(Relation rel, Oid oldNspOid, Oid nspOid, ObjectAddresses *objsMoved)

References AccessExclusiveLock, AlterTableNamespaceInternal(), CheckSetNamespace(), DEPENDENCY_AUTO, DEPENDENCY_INTERNAL, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), free_object_addresses(), get_rel_name(), InvalidObjectAddress, makeRangeVar(), new_object_addresses(), NoLock, NOTICE, ObjectAddressSet, OidIsValid, RangeVarCallbackForAlterRelation(), RangeVarGetAndCheckCreationNamespace(), RangeVarGetRelidExtended(), RelationData::rd_rel, relation_close(), relation_open(), RelationGetNamespace, RelationGetRelationName, RVR_MISSING_OK, sequenceIsOwned(), and stmt.

Referenced by ExecAlterObjectSchemaStmt().

◆ AlterTableNamespaceInternal()

void AlterTableNamespaceInternal ( Relation  rel,
Oid  oldNspOid,
Oid  nspOid,
ObjectAddresses objsMoved 
)
extern

Definition at line 19483 of file tablecmds.c.

19485{
19487
19488 Assert(objsMoved != NULL);
19489
19490 /* OK, modify the pg_class row and pg_depend entry */
19492
19494 nspOid, true, objsMoved);
19495
19496 /* Fix the table's row type too, if it has one */
19497 if (OidIsValid(rel->rd_rel->reltype))
19499 false, /* isImplicitArray */
19500 false, /* ignoreDependent */
19501 false, /* errorOnTableType */
19502 objsMoved);
19503
19504 /* Fix other dependent stuff */
19509 false, objsMoved);
19510
19512}
#define RowExclusiveLock
Definition lockdefs.h:38
void AlterConstraintNamespaces(Oid ownerId, Oid oldNspId, Oid newNspId, bool isType, ObjectAddresses *objsMoved)
#define RelationGetRelid(relation)
Definition rel.h:516
void AlterRelationNamespaceInternal(Relation classRel, Oid relOid, Oid oldNspOid, Oid newNspOid, bool hasDependEntry, ObjectAddresses *objsMoved)
static void AlterSeqNamespaces(Relation classRel, Relation rel, Oid oldNspOid, Oid newNspOid, ObjectAddresses *objsMoved, LOCKMODE lockmode)
static void AlterIndexNamespaces(Relation classRel, Relation rel, Oid oldNspOid, Oid newNspOid, ObjectAddresses *objsMoved)
Oid AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid, bool isImplicitArray, bool ignoreDependent, bool errorOnTableType, ObjectAddresses *objsMoved)
Definition typecmds.c:4196

References AccessExclusiveLock, AlterConstraintNamespaces(), AlterIndexNamespaces(), AlterRelationNamespaceInternal(), AlterSeqNamespaces(), AlterTypeNamespaceInternal(), Assert, fb(), OidIsValid, RelationData::rd_rel, RelationGetRelid, RowExclusiveLock, table_close(), and table_open().

Referenced by AlterObjectNamespace_oid(), and AlterTableNamespace().

◆ AtEOSubXact_on_commit_actions()

void AtEOSubXact_on_commit_actions ( bool  isCommit,
SubTransactionId  mySubid,
SubTransactionId  parentSubid 
)
extern

Definition at line 19925 of file tablecmds.c.

19927{
19929
19930 foreach(cur_item, on_commits)
19931 {
19933
19934 if (!isCommit && oc->creating_subid == mySubid)
19935 {
19936 /* cur_item must be removed */
19938 pfree(oc);
19939 }
19940 else
19941 {
19942 /* cur_item must be preserved */
19943 if (oc->creating_subid == mySubid)
19944 oc->creating_subid = parentSubid;
19945 if (oc->deleting_subid == mySubid)
19946 oc->deleting_subid = isCommit ? parentSubid : InvalidSubTransactionId;
19947 }
19948 }
19949}
#define InvalidSubTransactionId
Definition c.h:798
void pfree(void *pointer)
Definition mcxt.c:1619
#define foreach_delete_current(lst, var_or_cell)
Definition pg_list.h:423
static List * on_commits
Definition tablecmds.c:135

References fb(), foreach_delete_current, InvalidSubTransactionId, lfirst, on_commits, and pfree().

Referenced by AbortSubTransaction(), and CommitSubTransaction().

◆ AtEOXact_on_commit_actions()

void AtEOXact_on_commit_actions ( bool  isCommit)
extern

Definition at line 19893 of file tablecmds.c.

19894{
19896
19897 foreach(cur_item, on_commits)
19898 {
19900
19901 if (isCommit ? oc->deleting_subid != InvalidSubTransactionId :
19902 oc->creating_subid != InvalidSubTransactionId)
19903 {
19904 /* cur_item must be removed */
19906 pfree(oc);
19907 }
19908 else
19909 {
19910 /* cur_item must be preserved */
19911 oc->creating_subid = InvalidSubTransactionId;
19912 oc->deleting_subid = InvalidSubTransactionId;
19913 }
19914 }
19915}

References fb(), foreach_delete_current, InvalidSubTransactionId, lfirst, on_commits, and pfree().

Referenced by AbortTransaction(), CommitTransaction(), and PrepareTransaction().

◆ ATExecChangeOwner()

void ATExecChangeOwner ( Oid  relationOid,
Oid  newOwnerId,
bool  recursing,
LOCKMODE  lockmode 
)
extern

Definition at line 16546 of file tablecmds.c.

16547{
16550 HeapTuple tuple;
16552
16553 /*
16554 * Get exclusive lock till end of transaction on the target table. Use
16555 * relation_open so that we can work on indexes and sequences.
16556 */
16557 target_rel = relation_open(relationOid, lockmode);
16558
16559 /* Get its pg_class tuple, too */
16561
16562 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relationOid));
16563 if (!HeapTupleIsValid(tuple))
16564 elog(ERROR, "cache lookup failed for relation %u", relationOid);
16566
16567 /* Can we change the ownership of this tuple? */
16568 switch (tuple_class->relkind)
16569 {
16570 case RELKIND_RELATION:
16571 case RELKIND_VIEW:
16572 case RELKIND_MATVIEW:
16575 case RELKIND_PROPGRAPH:
16576 /* ok to change owner */
16577 break;
16578 case RELKIND_INDEX:
16579 if (!recursing)
16580 {
16581 /*
16582 * Because ALTER INDEX OWNER used to be allowed, and in fact
16583 * is generated by old versions of pg_dump, we give a warning
16584 * and do nothing rather than erroring out. Also, to avoid
16585 * unnecessary chatter while restoring those old dumps, say
16586 * nothing at all if the command would be a no-op anyway.
16587 */
16588 if (tuple_class->relowner != newOwnerId)
16591 errmsg("cannot change owner of index \"%s\"",
16592 NameStr(tuple_class->relname)),
16593 errhint("Change the ownership of the index's table instead.")));
16594 /* quick hack to exit via the no-op path */
16595 newOwnerId = tuple_class->relowner;
16596 }
16597 break;
16599 if (recursing)
16600 break;
16601 ereport(ERROR,
16603 errmsg("cannot change owner of index \"%s\"",
16604 NameStr(tuple_class->relname)),
16605 errhint("Change the ownership of the index's table instead.")));
16606 break;
16607 case RELKIND_SEQUENCE:
16608 if (!recursing &&
16609 tuple_class->relowner != newOwnerId)
16610 {
16611 /* if it's an owned sequence, disallow changing it by itself */
16612 Oid tableId;
16613 int32 colId;
16614
16615 if (sequenceIsOwned(relationOid, DEPENDENCY_AUTO, &tableId, &colId) ||
16617 ereport(ERROR,
16619 errmsg("cannot change owner of sequence \"%s\"",
16620 NameStr(tuple_class->relname)),
16621 errdetail("Sequence \"%s\" is linked to table \"%s\".",
16622 NameStr(tuple_class->relname),
16624 }
16625 break;
16627 if (recursing)
16628 break;
16629 ereport(ERROR,
16631 errmsg("\"%s\" is a composite type",
16632 NameStr(tuple_class->relname)),
16633 /* translator: %s is an SQL ALTER command */
16634 errhint("Use %s instead.",
16635 "ALTER TYPE")));
16636 break;
16637 case RELKIND_TOASTVALUE:
16638 if (recursing)
16639 break;
16641 default:
16642 ereport(ERROR,
16644 errmsg("cannot change owner of relation \"%s\"",
16645 NameStr(tuple_class->relname)),
16647 }
16648
16649 /*
16650 * If the new owner is the same as the existing owner, consider the
16651 * command to have succeeded. This is for dump restoration purposes.
16652 */
16653 if (tuple_class->relowner != newOwnerId)
16654 {
16658 Acl *newAcl;
16660 bool isNull;
16661 HeapTuple newtuple;
16662
16663 /* skip permission checks when recursing to index or toast table */
16664 if (!recursing)
16665 {
16666 /* Superusers can always do it */
16667 if (!superuser())
16668 {
16669 Oid namespaceOid = tuple_class->relnamespace;
16671
16672 /* Otherwise, must be owner of the existing object */
16673 if (!object_ownercheck(RelationRelationId, relationOid, GetUserId()))
16676
16677 /* Must be able to become new owner */
16679
16680 /* New owner must have CREATE privilege on namespace */
16682 ACL_CREATE);
16683 if (aclresult != ACLCHECK_OK)
16686 }
16687 }
16688
16689 memset(repl_null, false, sizeof(repl_null));
16690 memset(repl_repl, false, sizeof(repl_repl));
16691
16694
16695 /*
16696 * Determine the modified ACL for the new owner. This is only
16697 * necessary when the ACL is non-null.
16698 */
16701 &isNull);
16702 if (!isNull)
16703 {
16705 tuple_class->relowner, newOwnerId);
16706 repl_repl[Anum_pg_class_relacl - 1] = true;
16708 }
16709
16711
16712 CatalogTupleUpdate(class_rel, &newtuple->t_self, newtuple);
16713
16714 heap_freetuple(newtuple);
16715
16716 /*
16717 * We must similarly update any per-column ACLs to reflect the new
16718 * owner; for neatness reasons that's split out as a subroutine.
16719 */
16720 change_owner_fix_column_acls(relationOid,
16721 tuple_class->relowner,
16722 newOwnerId);
16723
16724 /*
16725 * Update owner dependency reference, if any. A composite type has
16726 * none, because it's tracked for the pg_type entry instead of here;
16727 * indexes and TOAST tables don't have their own entries either.
16728 */
16729 if (tuple_class->relkind != RELKIND_COMPOSITE_TYPE &&
16730 tuple_class->relkind != RELKIND_INDEX &&
16732 tuple_class->relkind != RELKIND_TOASTVALUE)
16734 newOwnerId);
16735
16736 /*
16737 * Also change the ownership of the table's row type, if it has one
16738 */
16739 if (OidIsValid(tuple_class->reltype))
16741
16742 /*
16743 * If we are operating on a table or materialized view, also change
16744 * the ownership of any indexes and sequences that belong to the
16745 * relation, as well as its toast table (if it has one).
16746 */
16747 if (tuple_class->relkind == RELKIND_RELATION ||
16749 tuple_class->relkind == RELKIND_MATVIEW ||
16750 tuple_class->relkind == RELKIND_TOASTVALUE)
16751 {
16753 ListCell *i;
16754
16755 /* Find all the indexes belonging to this relation */
16757
16758 /* For each index, recursively change its ownership */
16759 foreach(i, index_oid_list)
16760 ATExecChangeOwner(lfirst_oid(i), newOwnerId, true, lockmode);
16761
16763 }
16764
16765 /* If it has a toast table, recurse to change its ownership */
16766 if (tuple_class->reltoastrelid != InvalidOid)
16768 true, lockmode);
16769
16770 /* If it has dependent sequences, recurse to change them too */
16771 change_owner_recurse_to_sequences(relationOid, newOwnerId, lockmode);
16772 }
16773
16775
16776 ReleaseSysCache(tuple);
16779}
Acl * aclnewowner(const Acl *old_acl, Oid oldOwnerId, Oid newOwnerId)
Definition acl.c:1147
void check_can_set_role(Oid member, Oid role)
Definition acl.c:5371
#define DatumGetAclP(X)
Definition acl.h:120
#define pg_fallthrough
Definition c.h:217
int errhint(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:37
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition heaptuple.c:1118
int i
Definition isn.c:77
void list_free(List *list)
Definition list.c:1546
@ OBJECT_SCHEMA
int errdetail_relkind_not_supported(char relkind)
Definition pg_class.c:24
void changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
uint64_t Datum
Definition postgres.h:70
#define PointerGetDatum(X)
Definition postgres.h:354
#define RelationGetDescr(relation)
Definition rel.h:542
List * RelationGetIndexList(Relation relation)
Definition relcache.c:4846
ItemPointerData t_self
Definition htup.h:65
bool superuser(void)
Definition superuser.c:47
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
Datum SysCacheGetAttr(SysCacheIdentifier cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition syscache.c:596
void ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing, LOCKMODE lockmode)
static void change_owner_recurse_to_sequences(Oid relationOid, Oid newOwnerId, LOCKMODE lockmode)
static void change_owner_fix_column_acls(Oid relationOid, Oid oldOwnerId, Oid newOwnerId)
void AlterTypeOwnerInternal(Oid typeOid, Oid newOwnerId)
Definition typecmds.c:4027

References ACL_CREATE, aclcheck_error(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, aclnewowner(), AlterTypeOwnerInternal(), ATExecChangeOwner(), CatalogTupleUpdate(), change_owner_fix_column_acls(), change_owner_recurse_to_sequences(), changeDependencyOnOwner(), check_can_set_role(), DatumGetAclP, DEPENDENCY_AUTO, DEPENDENCY_INTERNAL, elog, ereport, errcode(), errdetail(), errdetail_relkind_not_supported(), errhint(), errmsg, ERROR, fb(), get_namespace_name(), get_rel_name(), get_rel_relkind(), get_relkind_objtype(), GETSTRUCT(), GetUserId(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, i, InvalidOid, InvokeObjectPostAlterHook, lfirst_oid, list_free(), NameStr, NoLock, object_aclcheck(), object_ownercheck(), OBJECT_SCHEMA, ObjectIdGetDatum(), OidIsValid, pg_fallthrough, PointerGetDatum, relation_close(), relation_open(), RelationGetDescr, RelationGetIndexList(), RelationGetRelationName, ReleaseSysCache(), RowExclusiveLock, SearchSysCache1(), sequenceIsOwned(), superuser(), SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), and WARNING.

Referenced by AlterTypeOwner_oid(), ATExecChangeOwner(), ATExecCmd(), change_owner_recurse_to_sequences(), and shdepReassignOwned_Owner().

◆ BuildDescForRelation()

TupleDesc BuildDescForRelation ( const List columns)
extern

Definition at line 1436 of file tablecmds.c.

1437{
1438 int natts;
1440 ListCell *l;
1441 TupleDesc desc;
1442 char *attname;
1443 Oid atttypid;
1444 int32 atttypmod;
1445 Oid attcollation;
1446 int attdim;
1447
1448 /*
1449 * allocate a new tuple descriptor
1450 */
1451 natts = list_length(columns);
1452 desc = CreateTemplateTupleDesc(natts);
1453
1454 attnum = 0;
1455
1456 foreach(l, columns)
1457 {
1458 ColumnDef *entry = lfirst(l);
1461
1462 /*
1463 * for each entry in the list, get the name and type information from
1464 * the list and have TupleDescInitEntry fill in the attribute
1465 * information we need.
1466 */
1467 attnum++;
1468
1469 attname = entry->colname;
1470 typenameTypeIdAndMod(NULL, entry->typeName, &atttypid, &atttypmod);
1471
1473 if (aclresult != ACLCHECK_OK)
1475
1476 attcollation = GetColumnDefCollation(NULL, entry, atttypid);
1478 if (attdim > PG_INT16_MAX)
1479 ereport(ERROR,
1481 errmsg("too many array dimensions"));
1482
1483 if (entry->typeName->setof)
1484 ereport(ERROR,
1486 errmsg("column \"%s\" cannot be declared SETOF",
1487 attname)));
1488
1490 atttypid, atttypmod, attdim);
1491 att = TupleDescAttr(desc, attnum - 1);
1492
1493 /* Override TupleDescInitEntry's settings as requested */
1494 TupleDescInitEntryCollation(desc, attnum, attcollation);
1495
1496 /* Fill in additional stuff not handled by TupleDescInitEntry */
1497 att->attnotnull = entry->is_not_null;
1498 att->attislocal = entry->is_local;
1499 att->attinhcount = entry->inhcount;
1500 att->attidentity = entry->identity;
1501 att->attgenerated = entry->generated;
1502 att->attcompression = GetAttributeCompression(att->atttypid, entry->compression);
1503 if (entry->storage)
1504 att->attstorage = entry->storage;
1505 else if (entry->storage_name)
1506 att->attstorage = GetAttributeStorage(att->atttypid, entry->storage_name);
1507
1509 }
1510
1511 TupleDescFinalize(desc);
1512
1513 return desc;
1514}
void aclcheck_error_type(AclResult aclerr, Oid typeOid)
Definition aclchk.c:2997
int16 AttrNumber
Definition attnum.h:21
#define PG_INT16_MAX
Definition c.h:726
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition parse_type.c:310
Oid GetColumnDefCollation(ParseState *pstate, const ColumnDef *coldef, Oid typeOid)
Definition parse_type.c:540
#define ACL_USAGE
Definition parsenodes.h:84
NameData attname
int16 attnum
FormData_pg_attribute * Form_pg_attribute
static int list_length(const List *l)
Definition pg_list.h:152
bool is_not_null
Definition parsenodes.h:777
char identity
Definition parsenodes.h:783
char * storage_name
Definition parsenodes.h:780
char * colname
Definition parsenodes.h:772
TypeName * typeName
Definition parsenodes.h:773
char generated
Definition parsenodes.h:786
char storage
Definition parsenodes.h:779
bool is_local
Definition parsenodes.h:776
int16 inhcount
Definition parsenodes.h:775
char * compression
Definition parsenodes.h:774
bool setof
Definition parsenodes.h:292
List * arrayBounds
Definition parsenodes.h:296
static char GetAttributeCompression(Oid atttypid, const char *compression)
static char GetAttributeStorage(Oid atttypid, const char *storagemode)
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:511
void populate_compact_attribute(TupleDesc tupdesc, int attnum)
Definition tupdesc.c:100
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition tupdesc.c:1093
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:909
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178

References ACL_USAGE, aclcheck_error_type(), ACLCHECK_OK, TypeName::arrayBounds, attname, attnum, ColumnDef::colname, ColumnDef::compression, CreateTemplateTupleDesc(), ereport, errcode(), errmsg, ERROR, fb(), ColumnDef::generated, GetAttributeCompression(), GetAttributeStorage(), GetColumnDefCollation(), GetUserId(), ColumnDef::identity, ColumnDef::inhcount, ColumnDef::is_local, ColumnDef::is_not_null, lfirst, list_length(), object_aclcheck(), PG_INT16_MAX, populate_compact_attribute(), TypeName::setof, ColumnDef::storage, ColumnDef::storage_name, TupleDescAttr(), TupleDescFinalize(), TupleDescInitEntry(), TupleDescInitEntryCollation(), ColumnDef::typeName, and typenameTypeIdAndMod().

Referenced by ATExecAddColumn(), createPartitionTable(), DefineRelation(), and DefineVirtualRelation().

◆ check_of_type()

void check_of_type ( HeapTuple  typetuple)
extern

Definition at line 7240 of file tablecmds.c.

7241{
7243 bool typeOk = false;
7244
7245 if (typ->typtype == TYPTYPE_COMPOSITE)
7246 {
7248
7249 Assert(OidIsValid(typ->typrelid));
7251 typeOk = (typeRelation->rd_rel->relkind == RELKIND_COMPOSITE_TYPE);
7252
7253 /*
7254 * Close the parent rel, but keep our AccessShareLock on it until xact
7255 * commit. That will prevent someone else from deleting or ALTERing
7256 * the type before the typed table creation/conversion commits.
7257 */
7259
7260 if (!typeOk)
7261 ereport(ERROR,
7263 errmsg("type %s is the row type of another table",
7264 format_type_be(typ->oid)),
7265 errdetail("A typed table must use a stand-alone composite type created with CREATE TYPE.")));
7266 }
7267 else
7268 ereport(ERROR,
7270 errmsg("type %s is not a composite type",
7271 format_type_be(typ->oid))));
7272}
char * format_type_be(Oid type_oid)
END_CATALOG_STRUCT typedef FormData_pg_type * Form_pg_type
Definition pg_type.h:265

References AccessShareLock, Assert, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), Form_pg_type, format_type_be(), GETSTRUCT(), NoLock, OidIsValid, relation_close(), and relation_open().

Referenced by ATExecAddOf(), and transformOfType().

◆ CheckRelationTableSpaceMove()

bool CheckRelationTableSpaceMove ( Relation  rel,
Oid  newTableSpaceId 
)
extern

Definition at line 3757 of file tablecmds.c.

3758{
3760
3761 /*
3762 * No work if no change in tablespace. Note that MyDatabaseTableSpace is
3763 * stored as 0.
3764 */
3765 oldTableSpaceId = rel->rd_rel->reltablespace;
3768 return false;
3769
3770 /*
3771 * We cannot support moving mapped relations into different tablespaces.
3772 * (In particular this eliminates all shared catalogs.)
3773 */
3774 if (RelationIsMapped(rel))
3775 ereport(ERROR,
3777 errmsg("cannot move system relation \"%s\"",
3779
3780 /* Cannot move a non-shared relation into pg_global */
3782 ereport(ERROR,
3784 errmsg("only shared relations can be placed in pg_global tablespace")));
3785
3786 /*
3787 * Do not allow moving temp tables of other backends ... their local
3788 * buffer manager is not going to cope.
3789 */
3790 if (RELATION_IS_OTHER_TEMP(rel))
3791 ereport(ERROR,
3793 errmsg("cannot move temporary tables of other sessions")));
3794
3795 return true;
3796}
#define RelationIsMapped(relation)
Definition rel.h:565
#define RELATION_IS_OTHER_TEMP(relation)
Definition rel.h:678

References ereport, errcode(), errmsg, ERROR, fb(), MyDatabaseTableSpace, RelationData::rd_rel, RELATION_IS_OTHER_TEMP, RelationGetRelationName, and RelationIsMapped.

Referenced by ATExecSetTableSpace(), ATExecSetTableSpaceNoStorage(), reindex_index(), and SetRelationTableSpace().

◆ CheckTableNotInUse()

void CheckTableNotInUse ( Relation  rel,
const char stmt 
)
extern

Definition at line 4480 of file tablecmds.c.

4481{
4482 int expected_refcnt;
4483
4484 expected_refcnt = rel->rd_isnailed ? 2 : 1;
4485 if (rel->rd_refcnt != expected_refcnt)
4486 ereport(ERROR,
4488 /* translator: first %s is a SQL command, eg ALTER TABLE */
4489 errmsg("cannot %s \"%s\" because it is being used by active queries in this session",
4491
4492 if (rel->rd_rel->relkind != RELKIND_INDEX &&
4493 rel->rd_rel->relkind != RELKIND_PARTITIONED_INDEX &&
4495 ereport(ERROR,
4497 /* translator: first %s is a SQL command, eg ALTER TABLE */
4498 errmsg("cannot %s \"%s\" because it has pending trigger events",
4500}
int rd_refcnt
Definition rel.h:59
bool rd_isnailed
Definition rel.h:62
bool AfterTriggerPendingOnRel(Oid relid)
Definition trigger.c:6150

References AfterTriggerPendingOnRel(), ereport, errcode(), errmsg, ERROR, fb(), RelationData::rd_isnailed, RelationData::rd_refcnt, RelationData::rd_rel, RelationGetRelationName, RelationGetRelid, and stmt.

Referenced by CheckAlterTableIsSafe(), cluster_rel(), DefineIndex(), DefineVirtualRelation(), heap_drop_with_catalog(), index_drop(), MergeAttributes(), RefreshMatViewByOid(), reindex_index(), and truncate_check_activity().

◆ DefineRelation()

ObjectAddress DefineRelation ( CreateStmt stmt,
char  relkind,
Oid  ownerId,
ObjectAddress typaddress,
const char queryString 
)
extern

Definition at line 817 of file tablecmds.c.

819{
820 char relname[NAMEDATALEN];
824 Relation rel;
831 List *nncols;
832 List *connames = NIL;
833 Datum reloptions;
836 bool partitioned;
837 const char *const validnsps[] = HEAP_RELOPT_NAMESPACES;
839 ObjectAddress address;
842
843 /*
844 * Truncate relname to appropriate length (probably a waste of time, as
845 * parser should have done this already).
846 */
847 strlcpy(relname, stmt->relation->relname, NAMEDATALEN);
848
849 /*
850 * Check consistency of arguments
851 */
852 if (stmt->oncommit != ONCOMMIT_NOOP
853 && stmt->relation->relpersistence != RELPERSISTENCE_TEMP)
856 errmsg("ON COMMIT can only be used on temporary tables")));
857
858 if (stmt->partspec != NULL)
859 {
860 if (relkind != RELKIND_RELATION)
861 elog(ERROR, "unexpected relkind: %d", (int) relkind);
862
864 partitioned = true;
865 }
866 else
867 partitioned = false;
868
869 if (relkind == RELKIND_PARTITIONED_TABLE &&
870 stmt->relation->relpersistence == RELPERSISTENCE_UNLOGGED)
873 errmsg("partitioned tables cannot be unlogged")));
874
875 /*
876 * Look up the namespace in which we are supposed to create the relation,
877 * check we have permission to create there, lock it against concurrent
878 * drop, and mark stmt->relation as RELPERSISTENCE_TEMP if a temporary
879 * namespace is selected.
880 */
883
884 /*
885 * Security check: disallow creating temp tables from security-restricted
886 * code. This is needed because calling code might not expect untrusted
887 * tables to appear in pg_temp at the front of its search path.
888 */
889 if (stmt->relation->relpersistence == RELPERSISTENCE_TEMP
893 errmsg("cannot create temporary table within security-restricted operation")));
894
895 /*
896 * Determine the lockmode to use when scanning parents. A self-exclusive
897 * lock is needed here.
898 *
899 * For regular inheritance, if two backends attempt to add children to the
900 * same parent simultaneously, and that parent has no pre-existing
901 * children, then both will attempt to update the parent's relhassubclass
902 * field, leading to a "tuple concurrently updated" error. Also, this
903 * interlocks against a concurrent ANALYZE on the parent table, which
904 * might otherwise be attempting to clear the parent's relhassubclass
905 * field, if its previous children were recently dropped.
906 *
907 * If the child table is a partition, then we instead grab an exclusive
908 * lock on the parent because its partition descriptor will be changed by
909 * addition of the new partition.
910 */
911 parentLockmode = (stmt->partbound != NULL ? AccessExclusiveLock :
913
914 /* Determine the list of OIDs of the parents. */
916 foreach(listptr, stmt->inhRelations)
917 {
918 RangeVar *rv = (RangeVar *) lfirst(listptr);
920
922
923 /*
924 * Reject duplications in the list of parents.
925 */
929 errmsg("relation \"%s\" would be inherited from more than once",
931
933 }
934
935 /*
936 * Select tablespace to use: an explicitly indicated one, or (in the case
937 * of a partitioned table) the parent's, if it has one.
938 */
939 if (stmt->tablespacename)
940 {
941 tablespaceId = get_tablespace_oid(stmt->tablespacename, false);
942
946 errmsg("cannot specify default tablespace for partitioned relations")));
947 }
948 else if (stmt->partbound)
949 {
952 }
953 else
955
956 /* still nothing? use the default */
958 tablespaceId = GetDefaultTablespace(stmt->relation->relpersistence,
960
961 /* Check permissions except when using database's default */
963 {
965
967 ACL_CREATE);
968 if (aclresult != ACLCHECK_OK)
971 }
972
973 /* In all cases disallow placing user relations in pg_global */
977 errmsg("only shared relations can be placed in pg_global tablespace")));
978
979 /* Identify user ID that will own the table */
980 if (!OidIsValid(ownerId))
981 ownerId = GetUserId();
982
983 /*
984 * Parse and validate reloptions, if any.
985 */
986 reloptions = transformRelOptions((Datum) 0, stmt->options, NULL, validnsps,
987 true, false);
988
989 switch (relkind)
990 {
991 case RELKIND_VIEW:
992 (void) view_reloptions(reloptions, true);
993 break;
995 (void) partitioned_table_reloptions(reloptions, true);
996 break;
997 default:
998 (void) heap_reloptions(relkind, reloptions, true);
999 }
1000
1001 if (stmt->ofTypename)
1002 {
1004
1005 ofTypeId = typenameTypeId(NULL, stmt->ofTypename);
1006
1008 if (aclresult != ACLCHECK_OK)
1010 }
1011 else
1013
1014 /*
1015 * Look up inheritance ancestors and generate relation schema, including
1016 * inherited attributes. (Note that stmt->tableElts is destructively
1017 * modified by MergeAttributes.)
1018 */
1019 stmt->tableElts =
1020 MergeAttributes(stmt->tableElts, inheritOids,
1021 stmt->relation->relpersistence,
1022 stmt->partbound != NULL,
1024
1025 /*
1026 * Create a tuple descriptor from the relation schema. Note that this
1027 * deals with column names, types, and in-descriptor NOT NULL flags, but
1028 * not default values, NOT NULL or CHECK constraints; we handle those
1029 * below.
1030 */
1031 descriptor = BuildDescForRelation(stmt->tableElts);
1032
1033 /*
1034 * Find columns with default values and prepare for insertion of the
1035 * defaults. Pre-cooked (that is, inherited) defaults go into a list of
1036 * CookedConstraint structs that we'll pass to heap_create_with_catalog,
1037 * while raw defaults go into a list of RawColumnDefault structs that will
1038 * be processed by AddRelationNewConstraints. (We can't deal with raw
1039 * expressions until we can do transformExpr.)
1040 */
1041 rawDefaults = NIL;
1043 attnum = 0;
1044
1045 foreach(listptr, stmt->tableElts)
1046 {
1048
1049 attnum++;
1050 if (colDef->raw_default != NULL)
1051 {
1053
1054 Assert(colDef->cooked_default == NULL);
1055
1057 rawEnt->attnum = attnum;
1058 rawEnt->raw_default = colDef->raw_default;
1059 rawEnt->generated = colDef->generated;
1061 }
1062 else if (colDef->cooked_default != NULL)
1063 {
1065
1067 cooked->contype = CONSTR_DEFAULT;
1068 cooked->conoid = InvalidOid; /* until created */
1069 cooked->name = NULL;
1070 cooked->attnum = attnum;
1071 cooked->expr = colDef->cooked_default;
1072 cooked->is_enforced = true;
1073 cooked->skip_validation = false;
1074 cooked->is_local = true; /* not used for defaults */
1075 cooked->inhcount = 0; /* ditto */
1076 cooked->is_no_inherit = false;
1078 }
1079 }
1080
1082
1083 /*
1084 * For relations with table AM and partitioned tables, select access
1085 * method to use: an explicitly indicated one, or (in the case of a
1086 * partitioned table) the parent's, if it has one.
1087 */
1088 if (stmt->accessMethod != NULL)
1089 {
1091 accessMethodId = get_table_am_oid(stmt->accessMethod, false);
1092 }
1093 else if (RELKIND_HAS_TABLE_AM(relkind) || relkind == RELKIND_PARTITIONED_TABLE)
1094 {
1095 if (stmt->partbound)
1096 {
1099 }
1100
1103 }
1104
1105 /*
1106 * Create the relation. Inherited defaults and CHECK constraints are
1107 * passed in for immediate handling --- since they don't need parsing,
1108 * they can be stored immediately.
1109 */
1113 InvalidOid,
1114 InvalidOid,
1115 ofTypeId,
1116 ownerId,
1118 descriptor,
1121 relkind,
1122 stmt->relation->relpersistence,
1123 false,
1124 false,
1125 stmt->oncommit,
1126 reloptions,
1127 true,
1129 false,
1130 InvalidOid,
1131 typaddress);
1132
1133 /*
1134 * We must bump the command counter to make the newly-created relation
1135 * tuple visible for opening.
1136 */
1138
1139 /*
1140 * Open the new relation and acquire exclusive lock on it. This isn't
1141 * really necessary for locking out other backends (since they can't see
1142 * the new rel anyway until we commit), but it keeps the lock manager from
1143 * complaining about deadlock risks.
1144 */
1146
1147 /*
1148 * Now add any newly specified column default and generation expressions
1149 * to the new relation. These are passed to us in the form of raw
1150 * parsetrees; we need to transform them to executable expression trees
1151 * before they can be added. The most convenient way to do that is to
1152 * apply the parser's transformExpr routine, but transformExpr doesn't
1153 * work unless we have a pre-existing relation. So, the transformation has
1154 * to be postponed to this final step of CREATE TABLE.
1155 *
1156 * This needs to be before processing the partitioning clauses because
1157 * those could refer to generated columns.
1158 */
1159 if (rawDefaults)
1161 true, true, false, queryString);
1162
1163 /*
1164 * Make column generation expressions visible for use by partitioning.
1165 */
1167
1168 /* Process and store partition bound, if any. */
1169 if (stmt->partbound)
1170 {
1171 PartitionBoundSpec *bound;
1172 ParseState *pstate;
1175 Relation parent,
1176 defaultRel = NULL;
1178
1179 /* Already have strong enough lock on the parent */
1180 parent = table_open(parentId, NoLock);
1181
1182 /*
1183 * We are going to try to validate the partition bound specification
1184 * against the partition key of parentRel, so it better have one.
1185 */
1186 if (parent->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1187 ereport(ERROR,
1189 errmsg("\"%s\" is not partitioned",
1190 RelationGetRelationName(parent))));
1191
1192 /*
1193 * The partition constraint of the default partition depends on the
1194 * partition bounds of every other partition. It is possible that
1195 * another backend might be about to execute a query on the default
1196 * partition table, and that the query relies on previously cached
1197 * default partition constraints. We must therefore take a table lock
1198 * strong enough to prevent all queries on the default partition from
1199 * proceeding until we commit and send out a shared-cache-inval notice
1200 * that will make them update their index lists.
1201 *
1202 * Order of locking: The relation being added won't be visible to
1203 * other backends until it is committed, hence here in
1204 * DefineRelation() the order of locking the default partition and the
1205 * relation being added does not matter. But at all other places we
1206 * need to lock the default relation before we lock the relation being
1207 * added or removed i.e. we should take the lock in same order at all
1208 * the places such that lock parent, lock default partition and then
1209 * lock the partition so as to avoid a deadlock.
1210 */
1213 true));
1216
1217 /* Transform the bound values */
1218 pstate = make_parsestate(NULL);
1219 pstate->p_sourcetext = queryString;
1220
1221 /*
1222 * Add an nsitem containing this relation, so that transformExpr
1223 * called on partition bound expressions is able to report errors
1224 * using a proper context.
1225 */
1227 NULL, false, false);
1228 addNSItemToQuery(pstate, nsitem, false, true, true);
1229
1230 bound = transformPartitionBound(pstate, parent, stmt->partbound);
1231
1232 /*
1233 * Check first that the new partition's bound is valid and does not
1234 * overlap with any of existing partitions of the parent.
1235 */
1236 check_new_partition_bound(relname, parent, bound, pstate);
1237
1238 /*
1239 * If the default partition exists, its partition constraints will
1240 * change after the addition of this new partition such that it won't
1241 * allow any row that qualifies for this new partition. So, check that
1242 * the existing data in the default partition satisfies the constraint
1243 * as it will exist after adding this partition.
1244 */
1246 {
1248 /* Keep the lock until commit. */
1250 }
1251
1252 /* Update the pg_class entry. */
1253 StorePartitionBound(rel, parent, bound);
1254
1255 table_close(parent, NoLock);
1256 }
1257
1258 /* Store inheritance information for new rel. */
1260
1261 /*
1262 * Process the partitioning specification (if any) and store the partition
1263 * key information into the catalog.
1264 */
1265 if (partitioned)
1266 {
1267 ParseState *pstate;
1268 int partnatts;
1269 AttrNumber partattrs[PARTITION_MAX_KEYS];
1271 Oid partcollation[PARTITION_MAX_KEYS];
1272 List *partexprs = NIL;
1273
1274 pstate = make_parsestate(NULL);
1275 pstate->p_sourcetext = queryString;
1276
1277 partnatts = list_length(stmt->partspec->partParams);
1278
1279 /* Protect fixed-size arrays here and in executor */
1280 if (partnatts > PARTITION_MAX_KEYS)
1281 ereport(ERROR,
1283 errmsg("cannot partition using more than %d columns",
1285
1286 /*
1287 * We need to transform the raw parsetrees corresponding to partition
1288 * expressions into executable expression trees. Like column defaults
1289 * and CHECK constraints, we could not have done the transformation
1290 * earlier.
1291 */
1292 stmt->partspec = transformPartitionSpec(rel, stmt->partspec);
1293
1294 ComputePartitionAttrs(pstate, rel, stmt->partspec->partParams,
1295 partattrs, &partexprs, partopclass,
1296 partcollation, stmt->partspec->strategy);
1297
1298 StorePartitionKey(rel, stmt->partspec->strategy, partnatts, partattrs,
1299 partexprs,
1300 partopclass, partcollation);
1301
1302 /* make it all visible */
1304 }
1305
1306 /*
1307 * If we're creating a partition, create now all the indexes, triggers,
1308 * FKs defined in the parent.
1309 *
1310 * We can't do it earlier, because DefineIndex wants to know the partition
1311 * key which we just stored.
1312 */
1313 if (stmt->partbound)
1314 {
1316 Relation parent;
1317 List *idxlist;
1318 ListCell *cell;
1319
1320 /* Already have strong enough lock on the parent */
1321 parent = table_open(parentId, NoLock);
1322 idxlist = RelationGetIndexList(parent);
1323
1324 /*
1325 * For each index in the parent table, create one in the partition
1326 */
1327 foreach(cell, idxlist)
1328 {
1330 AttrMap *attmap;
1333
1334 if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1335 {
1336 if (idxRel->rd_index->indisunique)
1337 ereport(ERROR,
1339 errmsg("cannot create foreign partition of partitioned table \"%s\"",
1340 RelationGetRelationName(parent)),
1341 errdetail("Table \"%s\" contains indexes that are unique.",
1342 RelationGetRelationName(parent))));
1343 else
1344 {
1346 continue;
1347 }
1348 }
1349
1351 RelationGetDescr(parent),
1352 false);
1353 idxstmt =
1357 RelationGetRelid(rel),
1358 idxstmt,
1359 InvalidOid,
1362 -1,
1363 false, false, false, false, false);
1364
1366 }
1367
1369
1370 /*
1371 * If there are any row-level triggers, clone them to the new
1372 * partition.
1373 */
1374 if (parent->trigdesc != NULL)
1375 CloneRowTriggersToPartition(parent, rel);
1376
1377 /*
1378 * And foreign keys too. Note that because we're freshly creating the
1379 * table, there is no need to verify these new constraints.
1380 */
1381 CloneForeignKeyConstraints(NULL, parent, rel);
1382
1383 table_close(parent, NoLock);
1384 }
1385
1386 /*
1387 * Now add any newly specified CHECK constraints to the new relation. Same
1388 * as for defaults above, but these need to come after partitioning is set
1389 * up. We save the constraint names that were used, to avoid dupes below.
1390 */
1391 if (stmt->constraints)
1392 {
1393 List *conlist;
1394
1395 conlist = AddRelationNewConstraints(rel, NIL, stmt->constraints,
1396 true, true, false, queryString);
1398 {
1399 if (cons->name != NULL)
1400 connames = lappend(connames, cons->name);
1401 }
1402 }
1403
1404 /*
1405 * Finally, merge the not-null constraints that are declared directly with
1406 * those that come from parent relations (making sure to count inheritance
1407 * appropriately for each), create them, and set the attnotnull flag on
1408 * columns that don't yet have it.
1409 */
1410 nncols = AddRelationNotNullConstraints(rel, stmt->nnconstraints,
1412 foreach_int(attrnum, nncols)
1413 set_attnotnull(NULL, rel, attrnum, true, false);
1414
1416
1417 /*
1418 * Clean up. We keep lock on new relation (although it shouldn't be
1419 * visible to anyone else anyway, until commit).
1420 */
1421 relation_close(rel, NoLock);
1422
1423 return address;
1424}
Oid get_table_am_oid(const char *amname, bool missing_ok)
Definition amcmds.c:173
AttrMap * build_attrmap_by_name(TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
Definition attmap.c:175
Oid GetDefaultTablespace(char relpersistence, bool partitioned)
#define palloc_object(type)
Definition fe_memutils.h:89
bool allowSystemTableMods
Definition globals.c:132
void StorePartitionKey(Relation rel, char strategy, int16 partnatts, AttrNumber *partattrs, List *partexprs, Oid *partopclass, Oid *partcollation)
Definition heap.c:3918
Oid heap_create_with_catalog(const char *relname, Oid relnamespace, Oid reltablespace, Oid relid, Oid reltypeid, Oid reloftypeid, Oid ownerid, Oid accessmtd, TupleDesc tupdesc, List *cooked_constraints, char relkind, char relpersistence, bool shared_relation, bool mapped_relation, OnCommitAction oncommit, Datum reloptions, bool use_user_acl, bool allow_system_table_mods, bool is_internal, Oid relrewrite, ObjectAddress *typaddress)
Definition heap.c:1140
List * AddRelationNotNullConstraints(Relation rel, List *constraints, List *old_notnulls, List *existing_constraints)
Definition heap.c:2917
void StorePartitionBound(Relation rel, Relation parent, PartitionBoundSpec *bound)
Definition heap.c:4074
List * AddRelationNewConstraints(Relation rel, List *newColDefaults, List *newConstraints, bool allow_merge, bool is_local, bool is_internal, const char *queryString)
Definition heap.c:2405
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:178
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:134
ObjectAddress DefineIndex(ParseState *pstate, Oid tableId, const IndexStmt *stmt, Oid indexRelationId, Oid parentIndexId, Oid parentConstraintId, int total_parts, bool is_alter_table, bool check_rights, bool check_not_in_use, bool skip_build, bool quiet)
Definition indexcmds.c:545
List * list_concat(List *list1, const List *list2)
Definition list.c:561
Oid get_rel_relam(Oid relid)
Definition lsyscache.c:2406
Oid get_rel_tablespace(Oid relid)
Definition lsyscache.c:2360
bool InSecurityRestrictedOperation(void)
Definition miscinit.c:640
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition namespace.h:98
ParseState * make_parsestate(ParseState *parentParseState)
Definition parse_node.c:39
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, LOCKMODE lockmode, Alias *alias, bool inh, bool inFromCl)
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition parse_type.c:291
PartitionBoundSpec * transformPartitionBound(ParseState *pstate, Relation parent, PartitionBoundSpec *spec)
IndexStmt * generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx, const AttrMap *attmap, Oid *constraintOid)
@ CONSTR_DEFAULT
void check_new_partition_bound(char *relname, Relation parent, PartitionBoundSpec *spec, ParseState *pstate)
void check_default_partition_contents(Relation parent, Relation default_rel, PartitionBoundSpec *new_spec)
PartitionDesc RelationGetPartitionDesc(Relation rel, bool omit_detached)
Definition partdesc.c:71
Oid get_default_oid_from_partdesc(PartitionDesc partdesc)
Definition partdesc.c:501
NameData relname
Definition pg_class.h:40
#define PARTITION_MAX_KEYS
#define NAMEDATALEN
#define foreach_ptr(type, var, lst)
Definition pg_list.h:501
#define linitial_oid(l)
Definition pg_list.h:180
#define foreach_int(var, lst)
Definition pg_list.h:502
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
@ ONCOMMIT_NOOP
Definition primnodes.h:59
bytea * view_reloptions(Datum reloptions, bool validate)
bytea * partitioned_table_reloptions(Datum reloptions, bool validate)
Datum transformRelOptions(Datum oldOptions, List *defList, const char *nameSpace, const char *const validnsps[], bool acceptOidsOff, bool isReset)
bytea * heap_reloptions(char relkind, Datum reloptions, bool validate)
#define HEAP_RELOPT_NAMESPACES
Definition reloptions.h:61
const char * p_sourcetext
Definition parse_node.h:214
TriggerDesc * trigdesc
Definition rel.h:117
char * default_table_access_method
Definition tableam.c:49
TupleDesc BuildDescForRelation(const List *columns)
Definition tablecmds.c:1436
static void ComputePartitionAttrs(ParseState *pstate, Relation rel, List *partParams, AttrNumber *partattrs, List **partexprs, Oid *partopclass, Oid *partcollation, PartitionStrategy strategy)
static void CloneRowTriggersToPartition(Relation parent, Relation partition)
static void StoreCatalogInheritance(Oid relationId, List *supers, bool child_is_partition)
Definition tablecmds.c:3585
static void CloneForeignKeyConstraints(List **wqueue, Relation parentRel, Relation partitionRel)
static PartitionSpec * transformPartitionSpec(Relation rel, PartitionSpec *partspec)
static void set_attnotnull(List **wqueue, Relation rel, AttrNumber attnum, bool is_valid, bool queue_validation)
Definition tablecmds.c:7937
static List * MergeAttributes(List *columns, const List *supers, char relpersistence, bool is_partition, List **supconstr, List **supnotnulls)
Definition tablecmds.c:2611
void CommandCounterIncrement(void)
Definition xact.c:1130

References AccessExclusiveLock, AccessShareLock, ACL_CREATE, ACL_USAGE, aclcheck_error(), aclcheck_error_type(), ACLCHECK_OK, addNSItemToQuery(), addRangeTableEntryForRelation(), AddRelationNewConstraints(), AddRelationNotNullConstraints(), allowSystemTableMods, Assert, attnum, build_attrmap_by_name(), BuildDescForRelation(), check_default_partition_contents(), check_new_partition_bound(), CloneForeignKeyConstraints(), CloneRowTriggersToPartition(), CommandCounterIncrement(), ComputePartitionAttrs(), CONSTR_DEFAULT, default_table_access_method, DefineIndex(), elog, ereport, errcode(), errdetail(), errmsg, ERROR, fb(), foreach_int, foreach_ptr, generateClonedIndexStmt(), get_default_oid_from_partdesc(), get_rel_name(), get_rel_relam(), get_rel_tablespace(), get_table_am_oid(), get_tablespace_name(), get_tablespace_oid(), GetDefaultTablespace(), GetUserId(), heap_create_with_catalog(), HEAP_RELOPT_NAMESPACES, heap_reloptions(), index_close(), index_open(), InSecurityRestrictedOperation(), InvalidOid, lappend(), lappend_oid(), lfirst, lfirst_oid, linitial_oid, list_concat(), list_free(), list_length(), list_member_oid(), make_parsestate(), MergeAttributes(), MyDatabaseTableSpace, NAMEDATALEN, NIL, NoLock, object_aclcheck(), OBJECT_TABLESPACE, ObjectAddressSet, OidIsValid, ONCOMMIT_NOOP, ParseState::p_sourcetext, palloc_object, PARTITION_MAX_KEYS, partitioned_table_reloptions(), RangeVarGetAndCheckCreationNamespace(), RangeVarGetRelid, RelationData::rd_rel, relation_close(), relation_open(), RelationGetDescr, RelationGetIndexList(), RelationGetPartitionDesc(), RelationGetRelationName, RelationGetRelid, relname, set_attnotnull(), ShareUpdateExclusiveLock, stmt, StoreCatalogInheritance(), StorePartitionBound(), StorePartitionKey(), strlcpy(), table_close(), table_open(), transformPartitionBound(), transformPartitionSpec(), transformRelOptions(), RelationData::trigdesc, TupleDescFinalize(), typenameTypeId(), and view_reloptions().

Referenced by create_ctas_internal(), CreatePropGraph(), DefineCompositeType(), DefineSequence(), DefineVirtualRelation(), and ProcessUtilitySlow().

◆ ExecuteTruncate()

void ExecuteTruncate ( TruncateStmt stmt)
extern

Definition at line 1923 of file tablecmds.c.

1924{
1925 List *rels = NIL;
1926 List *relids = NIL;
1928 ListCell *cell;
1929
1930 /*
1931 * Open, exclusive-lock, and check all the explicitly-specified relations
1932 */
1933 foreach(cell, stmt->relations)
1934 {
1935 RangeVar *rv = lfirst(cell);
1936 Relation rel;
1937 bool recurse = rv->inh;
1938 Oid myrelid;
1939 LOCKMODE lockmode = AccessExclusiveLock;
1940
1941 myrelid = RangeVarGetRelidExtended(rv, lockmode,
1943 NULL);
1944
1945 /* don't throw error for "TRUNCATE foo, foo" */
1946 if (list_member_oid(relids, myrelid))
1947 continue;
1948
1949 /* open the relation, we already hold a lock on it */
1950 rel = table_open(myrelid, NoLock);
1951
1952 /*
1953 * RangeVarGetRelidExtended() has done most checks with its callback,
1954 * but other checks with the now-opened Relation remain.
1955 */
1957
1958 rels = lappend(rels, rel);
1959 relids = lappend_oid(relids, myrelid);
1960
1961 /* Log this relation only if needed for logical decoding */
1964
1965 if (recurse)
1966 {
1967 ListCell *child;
1968 List *children;
1969
1970 children = find_all_inheritors(myrelid, lockmode, NULL);
1971
1972 foreach(child, children)
1973 {
1974 Oid childrelid = lfirst_oid(child);
1975
1976 if (list_member_oid(relids, childrelid))
1977 continue;
1978
1979 /* find_all_inheritors already got lock */
1981
1982 /*
1983 * It is possible that the parent table has children that are
1984 * temp tables of other backends. We cannot safely access
1985 * such tables (because of buffering issues), and the best
1986 * thing to do is to silently ignore them. Note that this
1987 * check is the same as one of the checks done in
1988 * truncate_check_activity() called below, still it is kept
1989 * here for simplicity.
1990 */
1991 if (RELATION_IS_OTHER_TEMP(rel))
1992 {
1993 table_close(rel, lockmode);
1994 continue;
1995 }
1996
1997 /*
1998 * Inherited TRUNCATE commands perform access permission
1999 * checks on the parent table only. So we skip checking the
2000 * children's permissions and don't call
2001 * truncate_check_perms() here.
2002 */
2005
2006 rels = lappend(rels, rel);
2007 relids = lappend_oid(relids, childrelid);
2008
2009 /* Log this relation only if needed for logical decoding */
2012 }
2013 }
2014 else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2015 ereport(ERROR,
2017 errmsg("cannot truncate only a partitioned table"),
2018 errhint("Do not specify the ONLY keyword, or use TRUNCATE ONLY on the partitions directly.")));
2019 }
2020
2021 ExecuteTruncateGuts(rels, relids, relids_logged,
2022 stmt->behavior, stmt->restart_seqs, false);
2023
2024 /* And close the rels */
2025 foreach(cell, rels)
2026 {
2027 Relation rel = (Relation) lfirst(cell);
2028
2029 table_close(rel, NoLock);
2030 }
2031}
struct RelationData * Relation
Definition genam.h:30
List * find_all_inheritors(Oid parentrelId, LOCKMODE lockmode, List **numparents)
#define RelationIsLogicallyLogged(relation)
Definition rel.h:721
bool inh
Definition primnodes.h:87
static void truncate_check_activity(Relation rel)
Definition tablecmds.c:2503
static void truncate_check_rel(Oid relid, Form_pg_class reltuple)
Definition tablecmds.c:2434
static void RangeVarCallbackForTruncate(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
void ExecuteTruncateGuts(List *explicit_rels, List *relids, List *relids_logged, DropBehavior behavior, bool restart_seqs, bool run_as_table_owner)
Definition tablecmds.c:2047

References AccessExclusiveLock, ereport, errcode(), errhint(), errmsg, ERROR, ExecuteTruncateGuts(), fb(), find_all_inheritors(), RangeVar::inh, lappend(), lappend_oid(), lfirst, lfirst_oid, list_member_oid(), NIL, NoLock, RangeVarCallbackForTruncate(), RangeVarGetRelidExtended(), RelationData::rd_rel, RELATION_IS_OTHER_TEMP, RelationGetRelid, RelationIsLogicallyLogged, stmt, table_close(), table_open(), truncate_check_activity(), and truncate_check_rel().

Referenced by standard_ProcessUtility().

◆ ExecuteTruncateGuts()

void ExecuteTruncateGuts ( List explicit_rels,
List relids,
List relids_logged,
DropBehavior  behavior,
bool  restart_seqs,
bool  run_as_table_owner 
)
extern

Definition at line 2047 of file tablecmds.c.

2052{
2053 List *rels;
2054 List *seq_relids = NIL;
2055 HTAB *ft_htab = NULL;
2056 EState *estate;
2058 ResultRelInfo *resultRelInfo;
2060 ListCell *cell;
2061 Oid *logrelids;
2062
2063 /*
2064 * Check the explicitly-specified relations.
2065 *
2066 * In CASCADE mode, suck in all referencing relations as well. This
2067 * requires multiple iterations to find indirectly-dependent relations. At
2068 * each phase, we need to exclusive-lock new rels before looking for their
2069 * dependencies, else we might miss something. Also, we check each rel as
2070 * soon as we open it, to avoid a faux pas such as holding lock for a long
2071 * time on a rel we have no permissions for.
2072 */
2073 rels = list_copy(explicit_rels);
2074 if (behavior == DROP_CASCADE)
2075 {
2076 for (;;)
2077 {
2078 List *newrelids;
2079
2081 if (newrelids == NIL)
2082 break; /* nothing else to add */
2083
2084 foreach(cell, newrelids)
2085 {
2086 Oid relid = lfirst_oid(cell);
2087 Relation rel;
2088
2089 rel = table_open(relid, AccessExclusiveLock);
2091 (errmsg("truncate cascades to table \"%s\"",
2093 truncate_check_rel(relid, rel->rd_rel);
2094 truncate_check_perms(relid, rel->rd_rel);
2096 rels = lappend(rels, rel);
2097 relids = lappend_oid(relids, relid);
2098
2099 /* Log this relation only if needed for logical decoding */
2102 }
2103 }
2104 }
2105
2106 /*
2107 * Check foreign key references. In CASCADE mode, this should be
2108 * unnecessary since we just pulled in all the references; but as a
2109 * cross-check, do it anyway if in an Assert-enabled build.
2110 */
2111#ifdef USE_ASSERT_CHECKING
2112 heap_truncate_check_FKs(rels, false);
2113#else
2114 if (behavior == DROP_RESTRICT)
2115 heap_truncate_check_FKs(rels, false);
2116#endif
2117
2118 /*
2119 * If we are asked to restart sequences, find all the sequences, lock them
2120 * (we need AccessExclusiveLock for ResetSequence), and check permissions.
2121 * We want to do this early since it's pointless to do all the truncation
2122 * work only to fail on sequence permissions.
2123 */
2124 if (restart_seqs)
2125 {
2126 foreach(cell, rels)
2127 {
2128 Relation rel = (Relation) lfirst(cell);
2131
2132 foreach(seqcell, seqlist)
2133 {
2136
2138
2139 /* This check must match AlterSequence! */
2143
2145
2147 }
2148 }
2149 }
2150
2151 /* Prepare to catch AFTER triggers. */
2153
2154 /*
2155 * To fire triggers, we'll need an EState as well as a ResultRelInfo for
2156 * each relation. We don't need to call ExecOpenIndices, though.
2157 *
2158 * We put the ResultRelInfos in the es_opened_result_relations list, even
2159 * though we don't have a range table and don't populate the
2160 * es_result_relations array. That's a bit bogus, but it's enough to make
2161 * ExecGetTriggerResultRel() find them.
2162 */
2163 estate = CreateExecutorState();
2165 palloc(list_length(rels) * sizeof(ResultRelInfo));
2166 resultRelInfo = resultRelInfos;
2167 foreach(cell, rels)
2168 {
2169 Relation rel = (Relation) lfirst(cell);
2170
2171 InitResultRelInfo(resultRelInfo,
2172 rel,
2173 0, /* dummy rangetable index */
2174 NULL,
2175 0);
2177 lappend(estate->es_opened_result_relations, resultRelInfo);
2178 resultRelInfo++;
2179 }
2180
2181 /*
2182 * Process all BEFORE STATEMENT TRUNCATE triggers before we begin
2183 * truncating (this is because one of them might throw an error). Also, if
2184 * we were to allow them to prevent statement execution, that would need
2185 * to be handled here.
2186 */
2187 resultRelInfo = resultRelInfos;
2188 foreach(cell, rels)
2189 {
2191
2193 SwitchToUntrustedUser(resultRelInfo->ri_RelationDesc->rd_rel->relowner,
2194 &ucxt);
2195 ExecBSTruncateTriggers(estate, resultRelInfo);
2198 resultRelInfo++;
2199 }
2200
2201 /*
2202 * OK, truncate each table.
2203 */
2205
2206 foreach(cell, rels)
2207 {
2208 Relation rel = (Relation) lfirst(cell);
2209
2210 /* Skip partitioned tables as there is nothing to do */
2211 if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2212 continue;
2213
2214 /*
2215 * Build the lists of foreign tables belonging to each foreign server
2216 * and pass each list to the foreign data wrapper's callback function,
2217 * so that each server can truncate its all foreign tables in bulk.
2218 * Each list is saved as a single entry in a hash table that uses the
2219 * server OID as lookup key.
2220 */
2221 if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2222 {
2224 bool found;
2226
2227 /* First time through, initialize hashtable for foreign tables */
2228 if (!ft_htab)
2229 {
2230 HASHCTL hctl;
2231
2232 memset(&hctl, 0, sizeof(HASHCTL));
2233 hctl.keysize = sizeof(Oid);
2234 hctl.entrysize = sizeof(ForeignTruncateInfo);
2236
2237 ft_htab = hash_create("TRUNCATE for Foreign Tables",
2238 32, /* start small and extend */
2239 &hctl,
2241 }
2242
2243 /* Find or create cached entry for the foreign table */
2244 ft_info = hash_search(ft_htab, &serverid, HASH_ENTER, &found);
2245 if (!found)
2246 ft_info->rels = NIL;
2247
2248 /*
2249 * Save the foreign table in the entry of the server that the
2250 * foreign table belongs to.
2251 */
2252 ft_info->rels = lappend(ft_info->rels, rel);
2253 continue;
2254 }
2255
2256 /*
2257 * Normally, we need a transaction-safe truncation here. However, if
2258 * the table was either created in the current (sub)transaction or has
2259 * a new relfilenumber in the current (sub)transaction, then we can
2260 * just truncate it in-place, because a rollback would cause the whole
2261 * table or the current physical file to be thrown away anyway.
2262 */
2263 if (rel->rd_createSubid == mySubid ||
2265 {
2266 /* Immediate, non-rollbackable truncation is OK */
2268 }
2269 else
2270 {
2274
2275 /*
2276 * This effectively deletes all rows in the table, and may be done
2277 * in a serializable transaction. In that case we must record a
2278 * rw-conflict in to this transaction from each transaction
2279 * holding a predicate lock on the table.
2280 */
2282
2283 /*
2284 * Need the full transaction-safe pushups.
2285 *
2286 * Create a new empty storage file for the relation, and assign it
2287 * as the relfilenumber value. The old storage file is scheduled
2288 * for deletion at commit.
2289 */
2290 RelationSetNewRelfilenumber(rel, rel->rd_rel->relpersistence);
2291
2293
2294 /*
2295 * The same for the toast table, if any.
2296 */
2297 toast_relid = rel->rd_rel->reltoastrelid;
2299 {
2302
2304 toastrel->rd_rel->relpersistence);
2306 }
2307
2308 /*
2309 * Reconstruct the indexes to match, and we're done.
2310 */
2313 }
2314
2316 }
2317
2318 /* Now go through the hash table, and truncate foreign tables */
2319 if (ft_htab)
2320 {
2323
2325
2326 PG_TRY();
2327 {
2328 while ((ft_info = hash_seq_search(&seq)) != NULL)
2329 {
2330 FdwRoutine *routine = GetFdwRoutineByServerId(ft_info->serverid);
2331
2332 /* truncate_check_rel() has checked that already */
2333 Assert(routine->ExecForeignTruncate != NULL);
2334
2335 routine->ExecForeignTruncate(ft_info->rels,
2336 behavior,
2337 restart_seqs);
2338 }
2339 }
2340 PG_FINALLY();
2341 {
2343 }
2344 PG_END_TRY();
2345 }
2346
2347 /*
2348 * Restart owned sequences if we were asked to.
2349 */
2350 foreach(cell, seq_relids)
2351 {
2352 Oid seq_relid = lfirst_oid(cell);
2353
2355 }
2356
2357 /*
2358 * Write a WAL record to allow this set of actions to be logically
2359 * decoded.
2360 *
2361 * Assemble an array of relids so we can write a single WAL record for the
2362 * whole action.
2363 */
2364 if (relids_logged != NIL)
2365 {
2367 int i = 0;
2368
2369 /* should only get here if effective_wal_level is 'logical' */
2371
2373 foreach(cell, relids_logged)
2374 logrelids[i++] = lfirst_oid(cell);
2375
2376 xlrec.dbId = MyDatabaseId;
2377 xlrec.nrelids = list_length(relids_logged);
2378 xlrec.flags = 0;
2379 if (behavior == DROP_CASCADE)
2380 xlrec.flags |= XLH_TRUNCATE_CASCADE;
2381 if (restart_seqs)
2383
2387
2389
2391 }
2392
2393 /*
2394 * Process all AFTER STATEMENT TRUNCATE triggers.
2395 */
2396 resultRelInfo = resultRelInfos;
2397 foreach(cell, rels)
2398 {
2400
2402 SwitchToUntrustedUser(resultRelInfo->ri_RelationDesc->rd_rel->relowner,
2403 &ucxt);
2404 ExecASTruncateTriggers(estate, resultRelInfo);
2407 resultRelInfo++;
2408 }
2409
2410 /* Handle queued AFTER triggers */
2411 AfterTriggerEndQuery(estate);
2412
2413 /* We can clean up the EState now */
2414 FreeExecutorState(estate);
2415
2416 /*
2417 * Close any rels opened by CASCADE (can't do this while EState still
2418 * holds refs)
2419 */
2420 rels = list_difference_ptr(rels, explicit_rels);
2421 foreach(cell, rels)
2422 {
2423 Relation rel = (Relation) lfirst(cell);
2424
2425 table_close(rel, NoLock);
2426 }
2427}
uint32 SubTransactionId
Definition c.h:796
void ResetSequence(Oid seq_relid)
Definition sequence.c:255
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition dynahash.c:889
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
Definition dynahash.c:360
void hash_destroy(HTAB *hashp)
Definition dynahash.c:802
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1352
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1317
#define PG_TRY(...)
Definition elog.h:374
#define PG_END_TRY(...)
Definition elog.h:399
#define PG_FINALLY(...)
Definition elog.h:391
void InitResultRelInfo(ResultRelInfo *resultRelInfo, Relation resultRelationDesc, Index resultRelationIndex, ResultRelInfo *partition_root_rri, int instrument_options)
Definition execMain.c:1280
void FreeExecutorState(EState *estate)
Definition execUtils.c:197
EState * CreateExecutorState(void)
Definition execUtils.c:90
FdwRoutine * GetFdwRoutineByServerId(Oid serverid)
Definition foreign.c:409
Oid GetForeignServerIdByRelId(Oid relid)
Definition foreign.c:387
Oid MyDatabaseId
Definition globals.c:96
List * heap_truncate_find_FKs(List *relationIds)
Definition heap.c:3791
void heap_truncate_check_FKs(List *relations, bool tempTables)
Definition heap.c:3696
void heap_truncate_one_rel(Relation rel)
Definition heap.c:3652
#define XLOG_HEAP_TRUNCATE
Definition heapam_xlog.h:36
#define XLH_TRUNCATE_RESTART_SEQS
#define SizeOfHeapTruncate
#define XLH_TRUNCATE_CASCADE
@ HASH_ENTER
Definition hsearch.h:109
#define HASH_CONTEXT
Definition hsearch.h:97
#define HASH_ELEM
Definition hsearch.h:90
#define HASH_BLOBS
Definition hsearch.h:92
bool reindex_relation(const ReindexStmt *stmt, Oid relid, int flags, const ReindexParams *params)
Definition index.c:3969
#define REINDEX_REL_PROCESS_TOAST
Definition index.h:166
List * list_difference_ptr(const List *list1, const List *list2)
Definition list.c:1263
List * list_copy(const List *oldlist)
Definition list.c:1573
void * palloc(Size size)
Definition mcxt.c:1390
MemoryContext CurrentMemoryContext
Definition mcxt.c:161
@ DROP_CASCADE
@ DROP_RESTRICT
@ OBJECT_SEQUENCE
List * getOwnedSequences(Oid relid)
Definition pg_depend.c:1140
void pgstat_count_truncate(Relation rel)
void CheckTableForSerializableConflictIn(Relation relation)
Definition predicate.c:4348
void RelationSetNewRelfilenumber(Relation relation, char persistence)
Definition relcache.c:3777
List * es_opened_result_relations
Definition execnodes.h:725
ExecForeignTruncate_function ExecForeignTruncate
Definition fdwapi.h:268
Size keysize
Definition hsearch.h:69
Size entrysize
Definition hsearch.h:70
MemoryContext hcxt
Definition hsearch.h:81
SubTransactionId rd_newRelfilelocatorSubid
Definition rel.h:104
SubTransactionId rd_createSubid
Definition rel.h:103
Relation ri_RelationDesc
Definition execnodes.h:514
static void truncate_check_perms(Oid relid, Form_pg_class reltuple)
Definition tablecmds.c:2485
void ExecBSTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition trigger.c:3282
void ExecASTruncateTriggers(EState *estate, ResultRelInfo *relinfo)
Definition trigger.c:3329
void AfterTriggerEndQuery(EState *estate)
Definition trigger.c:5161
void AfterTriggerBeginQuery(void)
Definition trigger.c:5141
void SwitchToUntrustedUser(Oid userid, UserContext *context)
Definition usercontext.c:33
void RestoreUserContext(UserContext *context)
Definition usercontext.c:87
SubTransactionId GetCurrentSubTransactionId(void)
Definition xact.c:793
#define XLogLogicalInfoActive()
Definition xlog.h:137
#define XLOG_INCLUDE_ORIGIN
Definition xlog.h:166
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition xloginsert.c:482
void XLogRegisterData(const void *data, uint32 len)
Definition xloginsert.c:372
void XLogSetRecordFlags(uint8 flags)
Definition xloginsert.c:464
void XLogBeginInsert(void)
Definition xloginsert.c:153

References AccessExclusiveLock, aclcheck_error(), ACLCHECK_NOT_OWNER, AfterTriggerBeginQuery(), AfterTriggerEndQuery(), Assert, CheckTableForSerializableConflictIn(), CreateExecutorState(), CurrentMemoryContext, DROP_CASCADE, DROP_RESTRICT, HASHCTL::entrysize, ereport, errmsg, EState::es_opened_result_relations, ExecASTruncateTriggers(), ExecBSTruncateTriggers(), FdwRoutine::ExecForeignTruncate, fb(), FreeExecutorState(), GetCurrentSubTransactionId(), GetFdwRoutineByServerId(), GetForeignServerIdByRelId(), getOwnedSequences(), GetUserId(), HASH_BLOBS, HASH_CONTEXT, hash_create(), hash_destroy(), HASH_ELEM, HASH_ENTER, hash_search(), hash_seq_init(), hash_seq_search(), HASHCTL::hcxt, heap_truncate_check_FKs(), heap_truncate_find_FKs(), heap_truncate_one_rel(), i, InitResultRelInfo(), HASHCTL::keysize, lappend(), lappend_oid(), lfirst, lfirst_oid, list_copy(), list_difference_ptr(), list_length(), MyDatabaseId, NIL, NoLock, NOTICE, object_ownercheck(), OBJECT_SEQUENCE, OidIsValid, palloc(), PG_END_TRY, PG_FINALLY, PG_TRY, pgstat_count_truncate(), RelationData::rd_createSubid, RelationData::rd_newRelfilelocatorSubid, RelationData::rd_rel, REINDEX_REL_PROCESS_TOAST, reindex_relation(), relation_close(), relation_open(), RelationGetRelationName, RelationGetRelid, RelationIsLogicallyLogged, RelationSetNewRelfilenumber(), ResetSequence(), RestoreUserContext(), ResultRelInfo::ri_RelationDesc, SizeOfHeapTruncate, SwitchToUntrustedUser(), table_close(), table_open(), truncate_check_activity(), truncate_check_perms(), truncate_check_rel(), XLH_TRUNCATE_CASCADE, XLH_TRUNCATE_RESTART_SEQS, XLOG_HEAP_TRUNCATE, XLOG_INCLUDE_ORIGIN, XLogBeginInsert(), XLogInsert(), XLogLogicalInfoActive, XLogRegisterData(), and XLogSetRecordFlags().

Referenced by apply_handle_truncate(), and ExecuteTruncate().

◆ find_composite_type_dependencies()

void find_composite_type_dependencies ( Oid  typeOid,
Relation  origRelation,
const char origTypeName 
)
extern

Definition at line 7033 of file tablecmds.c.

7035{
7037 ScanKeyData key[2];
7040
7041 /* since this function recurses, it could be driven to stack overflow */
7043
7044 /*
7045 * We scan pg_depend to find those things that depend on the given type.
7046 * (We assume we can ignore refobjsubid for a type.)
7047 */
7049
7050 ScanKeyInit(&key[0],
7054 ScanKeyInit(&key[1],
7057 ObjectIdGetDatum(typeOid));
7058
7060 NULL, 2, key);
7061
7063 {
7065 Relation rel;
7068
7069 /* Check for directly dependent types */
7070 if (pg_depend->classid == TypeRelationId)
7071 {
7072 /*
7073 * This must be an array, domain, or range containing the given
7074 * type, so recursively check for uses of this type. Note that
7075 * any error message will mention the original type not the
7076 * container; this is intentional.
7077 */
7080 continue;
7081 }
7082
7083 /* Else, ignore dependees that aren't relations */
7084 if (pg_depend->classid != RelationRelationId)
7085 continue;
7086
7089
7090 /*
7091 * If objsubid identifies a specific column, refer to that in error
7092 * messages. Otherwise, search to see if there's a user column of the
7093 * type. (We assume system columns are never of interesting types.)
7094 * The search is needed because an index containing an expression
7095 * column of the target type will just be recorded as a whole-relation
7096 * dependency. If we do not find a column of the type, the dependency
7097 * must indicate that the type is transiently referenced in an index
7098 * expression but not stored on disk, which we assume is OK, just as
7099 * we do for references in views. (It could also be that the target
7100 * type is embedded in some container type that is stored in an index
7101 * column, but the previous recursion should catch such cases.)
7102 */
7103 if (pg_depend->objsubid > 0 && pg_depend->objsubid <= tupleDesc->natts)
7104 att = TupleDescAttr(tupleDesc, pg_depend->objsubid - 1);
7105 else
7106 {
7107 att = NULL;
7108 for (int attno = 1; attno <= tupleDesc->natts; attno++)
7109 {
7110 att = TupleDescAttr(tupleDesc, attno - 1);
7111 if (att->atttypid == typeOid && !att->attisdropped)
7112 break;
7113 att = NULL;
7114 }
7115 if (att == NULL)
7116 {
7117 /* No such column, so assume OK */
7119 continue;
7120 }
7121 }
7122
7123 /*
7124 * We definitely should reject if the relation has storage. If it's
7125 * partitioned, then perhaps we don't have to reject: if there are
7126 * partitions then we'll fail when we find one, else there is no
7127 * stored data to worry about. However, it's possible that the type
7128 * change would affect conclusions about whether the type is sortable
7129 * or hashable and thus (if it's a partitioning column) break the
7130 * partitioning rule. For now, reject for partitioned rels too.
7131 */
7132 if (RELKIND_HAS_STORAGE(rel->rd_rel->relkind) ||
7133 RELKIND_HAS_PARTITIONS(rel->rd_rel->relkind))
7134 {
7135 if (origTypeName)
7136 ereport(ERROR,
7138 errmsg("cannot alter type \"%s\" because column \"%s.%s\" uses it",
7141 NameStr(att->attname))));
7142 else if (origRelation->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
7143 ereport(ERROR,
7145 errmsg("cannot alter type \"%s\" because column \"%s.%s\" uses it",
7148 NameStr(att->attname))));
7149 else if (origRelation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
7150 ereport(ERROR,
7152 errmsg("cannot alter foreign table \"%s\" because column \"%s.%s\" uses its row type",
7155 NameStr(att->attname))));
7156 else
7157 ereport(ERROR,
7159 errmsg("cannot alter table \"%s\" because column \"%s.%s\" uses its row type",
7162 NameStr(att->attname))));
7163 }
7164 else if (OidIsValid(rel->rd_rel->reltype))
7165 {
7166 /*
7167 * A view or composite type itself isn't a problem, but we must
7168 * recursively check for indirect dependencies via its rowtype.
7169 */
7172 }
7173
7175 }
7176
7178
7180}
void systable_endscan(SysScanDesc sysscan)
Definition genam.c:604
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition genam.c:515
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition genam.c:388
END_CATALOG_STRUCT typedef FormData_pg_depend * Form_pg_depend
Definition pg_depend.h:76
void check_stack_depth(void)
Definition stack_depth.c:96
void find_composite_type_dependencies(Oid typeOid, Relation origRelation, const char *origTypeName)
Definition tablecmds.c:7033

References AccessShareLock, BTEqualStrategyNumber, check_stack_depth(), ereport, errcode(), errmsg, ERROR, fb(), find_composite_type_dependencies(), Form_pg_depend, GETSTRUCT(), HeapTupleIsValid, NameStr, ObjectIdGetDatum(), OidIsValid, RelationData::rd_rel, relation_close(), relation_open(), RelationGetDescr, RelationGetRelationName, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_open(), and TupleDescAttr().

Referenced by ATPrepAlterColumnType(), ATRewriteTables(), find_composite_type_dependencies(), and get_rels_with_domain().

◆ PartConstraintImpliedByRelConstraint()

bool PartConstraintImpliedByRelConstraint ( Relation  scanrel,
List partConstraint 
)
extern

Definition at line 20530 of file tablecmds.c.

20532{
20534 TupleConstr *constr = RelationGetDescr(scanrel)->constr;
20535 int i;
20536
20537 if (constr && constr->has_not_null)
20538 {
20539 int natts = scanrel->rd_att->natts;
20540
20541 for (i = 1; i <= natts; i++)
20542 {
20543 CompactAttribute *att = TupleDescCompactAttr(scanrel->rd_att, i - 1);
20544
20545 /* invalid not-null constraint must be ignored here */
20546 if (att->attnullability == ATTNULLABLE_VALID && !att->attisdropped)
20547 {
20550
20551 ntest->arg = (Expr *) makeVar(1,
20552 i,
20553 wholeatt->atttypid,
20554 wholeatt->atttypmod,
20555 wholeatt->attcollation,
20556 0);
20557 ntest->nulltesttype = IS_NOT_NULL;
20558
20559 /*
20560 * argisrow=false is correct even for a composite column,
20561 * because attnotnull does not represent a SQL-spec IS NOT
20562 * NULL test in such a case, just IS DISTINCT FROM NULL.
20563 */
20564 ntest->argisrow = false;
20565 ntest->location = -1;
20567 }
20568 }
20569 }
20570
20572}
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition makefuncs.c:66
@ IS_NOT_NULL
Definition primnodes.h:1973
bool attisdropped
Definition tupdesc.h:78
char attnullability
Definition tupdesc.h:80
TupleDesc rd_att
Definition rel.h:112
bool has_not_null
Definition tupdesc.h:45
ParseLoc location
Definition primnodes.h:311
static bool ConstraintImpliedByRelConstraint(Relation scanrel, List *testConstraint, List *provenConstraint)
#define ATTNULLABLE_VALID
Definition tupdesc.h:86
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:195

References CompactAttribute::attisdropped, CompactAttribute::attnullability, ATTNULLABLE_VALID, ConstraintImpliedByRelConstraint(), fb(), TupleConstr::has_not_null, i, IS_NOT_NULL, lappend(), Var::location, makeNode, makeVar(), TupleDescData::natts, NIL, RelationData::rd_att, RelationGetDescr, TupleDescAttr(), and TupleDescCompactAttr().

Referenced by check_default_partition_contents(), and QueuePartitionConstraintValidation().

◆ PreCommit_on_commit_actions()

void PreCommit_on_commit_actions ( void  )
extern

Definition at line 19786 of file tablecmds.c.

19787{
19788 ListCell *l;
19791
19792 foreach(l, on_commits)
19793 {
19795
19796 /* Ignore entry if already dropped in this xact */
19797 if (oc->deleting_subid != InvalidSubTransactionId)
19798 continue;
19799
19800 switch (oc->oncommit)
19801 {
19802 case ONCOMMIT_NOOP:
19804 /* Do nothing (there shouldn't be such entries, actually) */
19805 break;
19807
19808 /*
19809 * If this transaction hasn't accessed any temporary
19810 * relations, we can skip truncating ON COMMIT DELETE ROWS
19811 * tables, as they must still be empty.
19812 */
19815 break;
19816 case ONCOMMIT_DROP:
19818 break;
19819 }
19820 }
19821
19822 /*
19823 * Truncate relations before dropping so that all dependencies between
19824 * relations are removed after they are worked on. Doing it like this
19825 * might be a waste as it is possible that a relation being truncated will
19826 * be dropped anyway due to its parent being dropped, but this makes the
19827 * code more robust because of not having to re-check that the relation
19828 * exists at truncation time.
19829 */
19830 if (oids_to_truncate != NIL)
19832
19833 if (oids_to_drop != NIL)
19834 {
19836
19837 foreach(l, oids_to_drop)
19838 {
19839 ObjectAddress object;
19840
19841 object.classId = RelationRelationId;
19842 object.objectId = lfirst_oid(l);
19843 object.objectSubId = 0;
19844
19846
19848 }
19849
19850 /*
19851 * Object deletion might involve toast table access (to clean up
19852 * toasted catalog entries), so ensure we have a valid snapshot.
19853 */
19855
19856 /*
19857 * Since this is an automatic drop, rather than one directly initiated
19858 * by the user, we pass the PERFORM_DELETION_INTERNAL flag.
19859 */
19862
19864
19865#ifdef USE_ASSERT_CHECKING
19866
19867 /*
19868 * Note that table deletion will call remove_on_commit_action, so the
19869 * entry should get marked as deleted.
19870 */
19871 foreach(l, on_commits)
19872 {
19874
19875 if (oc->oncommit != ONCOMMIT_DROP)
19876 continue;
19877
19878 Assert(oc->deleting_subid != InvalidSubTransactionId);
19879 }
19880#endif
19881 }
19882}
void performMultipleDeletions(const ObjectAddresses *objects, DropBehavior behavior, int flags)
Definition dependency.c:388
#define PERFORM_DELETION_QUIETLY
Definition dependency.h:94
#define PERFORM_DELETION_INTERNAL
Definition dependency.h:92
void heap_truncate(List *relids)
Definition heap.c:3611
@ ONCOMMIT_DELETE_ROWS
Definition primnodes.h:61
@ ONCOMMIT_PRESERVE_ROWS
Definition primnodes.h:60
@ ONCOMMIT_DROP
Definition primnodes.h:62
Snapshot GetTransactionSnapshot(void)
Definition snapmgr.c:272
void PushActiveSnapshot(Snapshot snapshot)
Definition snapmgr.c:682
void PopActiveSnapshot(void)
Definition snapmgr.c:775
int MyXactFlags
Definition xact.c:138
#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE
Definition xact.h:103

References add_exact_object_address(), Assert, ObjectAddress::classId, DROP_CASCADE, fb(), GetTransactionSnapshot(), heap_truncate(), InvalidSubTransactionId, lappend_oid(), lfirst, lfirst_oid, MyXactFlags, new_object_addresses(), NIL, object_address_present(), on_commits, ONCOMMIT_DELETE_ROWS, ONCOMMIT_DROP, ONCOMMIT_NOOP, ONCOMMIT_PRESERVE_ROWS, PERFORM_DELETION_INTERNAL, PERFORM_DELETION_QUIETLY, performMultipleDeletions(), PopActiveSnapshot(), PushActiveSnapshot(), and XACT_FLAGS_ACCESSEDTEMPNAMESPACE.

Referenced by CommitTransaction(), and PrepareTransaction().

◆ RangeVarCallbackMaintainsTable()

void RangeVarCallbackMaintainsTable ( const RangeVar relation,
Oid  relId,
Oid  oldRelId,
void arg 
)
extern

Definition at line 19960 of file tablecmds.c.

19962{
19963 char relkind;
19965
19966 /* Nothing to do if the relation was not found. */
19967 if (!OidIsValid(relId))
19968 return;
19969
19970 /*
19971 * If the relation does exist, check whether it's an index. But note that
19972 * the relation might have been dropped between the time we did the name
19973 * lookup and now. In that case, there's nothing to do.
19974 */
19975 relkind = get_rel_relkind(relId);
19976 if (!relkind)
19977 return;
19978 if (relkind != RELKIND_RELATION && relkind != RELKIND_TOASTVALUE &&
19979 relkind != RELKIND_MATVIEW && relkind != RELKIND_PARTITIONED_TABLE)
19980 ereport(ERROR,
19982 errmsg("\"%s\" is not a table or materialized view", relation->relname)));
19983
19984 /* Check permissions */
19986 if (aclresult != ACLCHECK_OK)
19989 relation->relname);
19990}
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
Definition aclchk.c:4083
#define ACL_MAINTAIN
Definition parsenodes.h:90
char * relname
Definition primnodes.h:84

References ACL_MAINTAIN, aclcheck_error(), ACLCHECK_OK, ereport, errcode(), errmsg, ERROR, fb(), get_rel_relkind(), get_relkind_objtype(), GetUserId(), OidIsValid, pg_class_aclcheck(), and RangeVar::relname.

Referenced by ExecRefreshMatView(), process_single_relation(), and ReindexTable().

◆ RangeVarCallbackOwnsRelation()

void RangeVarCallbackOwnsRelation ( const RangeVar relation,
Oid  relId,
Oid  oldRelId,
void arg 
)
extern

Definition at line 20020 of file tablecmds.c.

20022{
20023 HeapTuple tuple;
20024
20025 /* Nothing to do if the relation was not found. */
20026 if (!OidIsValid(relId))
20027 return;
20028
20029 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relId));
20030 if (!HeapTupleIsValid(tuple)) /* should not happen */
20031 elog(ERROR, "cache lookup failed for relation %u", relId);
20032
20035 relation->relname);
20036
20037 if (!allowSystemTableMods &&
20038 IsSystemClass(relId, (Form_pg_class) GETSTRUCT(tuple)))
20039 ereport(ERROR,
20041 errmsg("permission denied: \"%s\" is a system catalog",
20042 relation->relname)));
20043
20044 ReleaseSysCache(tuple);
20045}
bool IsSystemClass(Oid relid, Form_pg_class reltuple)
Definition catalog.c:86

References aclcheck_error(), ACLCHECK_NOT_OWNER, allowSystemTableMods, elog, ereport, errcode(), errmsg, ERROR, fb(), get_rel_relkind(), get_relkind_objtype(), GETSTRUCT(), GetUserId(), HeapTupleIsValid, IsSystemClass(), object_ownercheck(), ObjectIdGetDatum(), OidIsValid, ReleaseSysCache(), RangeVar::relname, and SearchSysCache1().

Referenced by AlterPropGraph(), AlterSequence(), CreatePropGraph(), ProcessUtilitySlow(), transformPartitionCmdForMerge(), and transformPartitionCmdForSplit().

◆ register_on_commit_action()

void register_on_commit_action ( Oid  relid,
OnCommitAction  action 
)
extern

Definition at line 19727 of file tablecmds.c.

19728{
19731
19732 /*
19733 * We needn't bother registering the relation unless there is an ON COMMIT
19734 * action we need to take.
19735 */
19736 if (action == ONCOMMIT_NOOP || action == ONCOMMIT_PRESERVE_ROWS)
19737 return;
19738
19740
19742 oc->relid = relid;
19743 oc->oncommit = action;
19744 oc->creating_subid = GetCurrentSubTransactionId();
19745 oc->deleting_subid = InvalidSubTransactionId;
19746
19747 /*
19748 * We use lcons() here so that ON COMMIT actions are processed in reverse
19749 * order of registration. That might not be essential but it seems
19750 * reasonable.
19751 */
19753
19755}
List * lcons(void *datum, List *list)
Definition list.c:495
MemoryContext CacheMemoryContext
Definition mcxt.c:170
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138

References CacheMemoryContext, fb(), GetCurrentSubTransactionId(), InvalidSubTransactionId, lcons(), MemoryContextSwitchTo(), on_commits, ONCOMMIT_NOOP, ONCOMMIT_PRESERVE_ROWS, and palloc_object.

Referenced by heap_create_with_catalog().

◆ remove_on_commit_action()

void remove_on_commit_action ( Oid  relid)
extern

Definition at line 19763 of file tablecmds.c.

19764{
19765 ListCell *l;
19766
19767 foreach(l, on_commits)
19768 {
19770
19771 if (oc->relid == relid)
19772 {
19774 break;
19775 }
19776 }
19777}
SubTransactionId deleting_subid
Definition tablecmds.c:132

References OnCommitItem::deleting_subid, fb(), GetCurrentSubTransactionId(), lfirst, and on_commits.

Referenced by heap_drop_with_catalog().

◆ RemoveRelations()

void RemoveRelations ( DropStmt drop)
extern

Definition at line 1596 of file tablecmds.c.

1597{
1598 ObjectAddresses *objects;
1599 char relkind;
1600 ListCell *cell;
1601 int flags = 0;
1602 LOCKMODE lockmode = AccessExclusiveLock;
1603
1604 /* DROP CONCURRENTLY uses a weaker lock, and has some restrictions */
1605 if (drop->concurrent)
1606 {
1607 /*
1608 * Note that for temporary relations this lock may get upgraded later
1609 * on, but as no other session can access a temporary relation, this
1610 * is actually fine.
1611 */
1612 lockmode = ShareUpdateExclusiveLock;
1613 Assert(drop->removeType == OBJECT_INDEX);
1614 if (list_length(drop->objects) != 1)
1615 ereport(ERROR,
1617 errmsg("DROP INDEX CONCURRENTLY does not support dropping multiple objects")));
1618 if (drop->behavior == DROP_CASCADE)
1619 ereport(ERROR,
1621 errmsg("DROP INDEX CONCURRENTLY does not support CASCADE")));
1622 }
1623
1624 /*
1625 * First we identify all the relations, then we delete them in a single
1626 * performMultipleDeletions() call. This is to avoid unwanted DROP
1627 * RESTRICT errors if one of the relations depends on another.
1628 */
1629
1630 /* Determine required relkind */
1631 switch (drop->removeType)
1632 {
1633 case OBJECT_TABLE:
1634 relkind = RELKIND_RELATION;
1635 break;
1636
1637 case OBJECT_INDEX:
1638 relkind = RELKIND_INDEX;
1639 break;
1640
1641 case OBJECT_SEQUENCE:
1642 relkind = RELKIND_SEQUENCE;
1643 break;
1644
1645 case OBJECT_VIEW:
1646 relkind = RELKIND_VIEW;
1647 break;
1648
1649 case OBJECT_MATVIEW:
1650 relkind = RELKIND_MATVIEW;
1651 break;
1652
1654 relkind = RELKIND_FOREIGN_TABLE;
1655 break;
1656
1657 case OBJECT_PROPGRAPH:
1658 relkind = RELKIND_PROPGRAPH;
1659 break;
1660
1661 default:
1662 elog(ERROR, "unrecognized drop object type: %d",
1663 (int) drop->removeType);
1664 relkind = 0; /* keep compiler quiet */
1665 break;
1666 }
1667
1668 /* Lock and validate each relation; build a list of object addresses */
1669 objects = new_object_addresses();
1670
1671 foreach(cell, drop->objects)
1672 {
1673 RangeVar *rel = makeRangeVarFromNameList((List *) lfirst(cell));
1674 Oid relOid;
1675 ObjectAddress obj;
1677
1678 /*
1679 * These next few steps are a great deal like relation_openrv, but we
1680 * don't bother building a relcache entry since we don't need it.
1681 *
1682 * Check for shared-cache-inval messages before trying to access the
1683 * relation. This is needed to cover the case where the name
1684 * identifies a rel that has been dropped and recreated since the
1685 * start of our transaction: if we don't flush the old syscache entry,
1686 * then we'll latch onto that entry and suffer an error later.
1687 */
1689
1690 /* Look up the appropriate relation using namespace search. */
1691 state.expected_relkind = relkind;
1692 state.heap_lockmode = drop->concurrent ?
1694 /* We must initialize these fields to show that no locks are held: */
1695 state.heapOid = InvalidOid;
1696 state.partParentOid = InvalidOid;
1697
1698 relOid = RangeVarGetRelidExtended(rel, lockmode, RVR_MISSING_OK,
1700 &state);
1701
1702 /* Not there? */
1703 if (!OidIsValid(relOid))
1704 {
1705 DropErrorMsgNonExistent(rel, relkind, drop->missing_ok);
1706 continue;
1707 }
1708
1709 /*
1710 * Decide if concurrent mode needs to be used here or not. The
1711 * callback retrieved the rel's persistence for us.
1712 */
1713 if (drop->concurrent &&
1714 state.actual_relpersistence != RELPERSISTENCE_TEMP)
1715 {
1716 Assert(list_length(drop->objects) == 1 &&
1717 drop->removeType == OBJECT_INDEX);
1719 }
1720
1721 /*
1722 * Concurrent index drop cannot be used with partitioned indexes,
1723 * either.
1724 */
1725 if ((flags & PERFORM_DELETION_CONCURRENTLY) != 0 &&
1726 state.actual_relkind == RELKIND_PARTITIONED_INDEX)
1727 ereport(ERROR,
1729 errmsg("cannot drop partitioned index \"%s\" concurrently",
1730 rel->relname)));
1731
1732 /*
1733 * If we're told to drop a partitioned index, we must acquire lock on
1734 * all the children of its parent partitioned table before proceeding.
1735 * Otherwise we'd try to lock the child index partitions before their
1736 * tables, leading to potential deadlock against other sessions that
1737 * will lock those objects in the other order.
1738 */
1739 if (state.actual_relkind == RELKIND_PARTITIONED_INDEX)
1740 (void) find_all_inheritors(state.heapOid,
1741 state.heap_lockmode,
1742 NULL);
1743
1744 /* OK, we're ready to delete this one */
1746 obj.objectId = relOid;
1747 obj.objectSubId = 0;
1748
1749 add_exact_object_address(&obj, objects);
1750 }
1751
1752 performMultipleDeletions(objects, drop->behavior, flags);
1753
1754 free_object_addresses(objects);
1755}
#define PERFORM_DELETION_CONCURRENTLY
Definition dependency.h:93
void AcceptInvalidationMessages(void)
Definition inval.c:930
RangeVar * makeRangeVarFromNameList(const List *names)
Definition namespace.c:3626
@ OBJECT_PROPGRAPH
@ OBJECT_FOREIGN_TABLE
@ OBJECT_VIEW
static void DropErrorMsgNonExistent(RangeVar *rel, char rightkind, bool missing_ok)
Definition tablecmds.c:1521
static void RangeVarCallbackForDropRelation(const RangeVar *rel, Oid relOid, Oid oldRelOid, void *arg)
Definition tablecmds.c:1764

References AcceptInvalidationMessages(), AccessExclusiveLock, add_exact_object_address(), Assert, ObjectAddress::classId, DROP_CASCADE, DropErrorMsgNonExistent(), elog, ereport, errcode(), errmsg, ERROR, fb(), find_all_inheritors(), free_object_addresses(), InvalidOid, lfirst, list_length(), makeRangeVarFromNameList(), new_object_addresses(), OBJECT_FOREIGN_TABLE, OBJECT_INDEX, OBJECT_MATVIEW, OBJECT_PROPGRAPH, OBJECT_SEQUENCE, OBJECT_TABLE, OBJECT_VIEW, ObjectAddress::objectId, ObjectAddress::objectSubId, OidIsValid, PERFORM_DELETION_CONCURRENTLY, performMultipleDeletions(), RangeVarCallbackForDropRelation(), RangeVarGetRelidExtended(), RangeVar::relname, RVR_MISSING_OK, and ShareUpdateExclusiveLock.

Referenced by ExecDropStmt().

◆ renameatt()

ObjectAddress renameatt ( RenameStmt stmt)
extern

Definition at line 4073 of file tablecmds.c.

4074{
4075 Oid relid;
4077 ObjectAddress address;
4078
4079 /* lock level taken here should match renameatt_internal */
4081 stmt->missing_ok ? RVR_MISSING_OK : 0,
4083 NULL);
4084
4085 if (!OidIsValid(relid))
4086 {
4088 (errmsg("relation \"%s\" does not exist, skipping",
4089 stmt->relation->relname)));
4090 return InvalidObjectAddress;
4091 }
4092
4093 attnum =
4094 renameatt_internal(relid,
4095 stmt->subname, /* old att name */
4096 stmt->newname, /* new att name */
4097 stmt->relation->inh, /* recursive? */
4098 false, /* recursing? */
4099 0, /* expected inhcount */
4100 stmt->behavior);
4101
4103
4104 return address;
4105}
#define ObjectAddressSubSet(addr, class_id, object_id, object_sub_id)
static AttrNumber renameatt_internal(Oid myrelid, const char *oldattname, const char *newattname, bool recurse, bool recursing, int expected_parents, DropBehavior behavior)
Definition tablecmds.c:3908
static void RangeVarCallbackForRenameAttribute(const RangeVar *rv, Oid relid, Oid oldrelid, void *arg)
Definition tablecmds.c:4053

References AccessExclusiveLock, attnum, ereport, errmsg, fb(), InvalidObjectAddress, NOTICE, ObjectAddressSubSet, OidIsValid, RangeVarCallbackForRenameAttribute(), RangeVarGetRelidExtended(), renameatt_internal(), RVR_MISSING_OK, and stmt.

Referenced by ExecRenameStmt().

◆ RenameConstraint()

ObjectAddress RenameConstraint ( RenameStmt stmt)
extern

Definition at line 4220 of file tablecmds.c.

4221{
4222 Oid relid = InvalidOid;
4223 Oid typid = InvalidOid;
4224
4225 if (stmt->renameType == OBJECT_DOMCONSTRAINT)
4226 {
4227 Relation rel;
4228 HeapTuple tup;
4229
4233 if (!HeapTupleIsValid(tup))
4234 elog(ERROR, "cache lookup failed for type %u", typid);
4237 table_close(rel, NoLock);
4238 }
4239 else
4240 {
4241 /* lock level taken here should match rename_constraint_internal */
4243 stmt->missing_ok ? RVR_MISSING_OK : 0,
4245 NULL);
4246 if (!OidIsValid(relid))
4247 {
4249 (errmsg("relation \"%s\" does not exist, skipping",
4250 stmt->relation->relname)));
4251 return InvalidObjectAddress;
4252 }
4253 }
4254
4255 return
4256 rename_constraint_internal(relid, typid,
4257 stmt->subname,
4258 stmt->newname,
4259 (stmt->relation &&
4260 stmt->relation->inh), /* recursive? */
4261 false, /* recursing? */
4262 0 /* expected inhcount */ );
4263}
TypeName * makeTypeNameFromNameList(List *names)
Definition makefuncs.c:531
#define castNode(_type_, nodeptr)
Definition nodes.h:180
@ OBJECT_DOMCONSTRAINT
static ObjectAddress rename_constraint_internal(Oid myrelid, Oid mytypid, const char *oldconname, const char *newconname, bool recurse, bool recursing, int expected_parents)
Definition tablecmds.c:4111
void checkDomainOwner(HeapTuple tup)
Definition typecmds.c:3526

References AccessExclusiveLock, castNode, checkDomainOwner(), elog, ereport, errmsg, ERROR, fb(), HeapTupleIsValid, InvalidObjectAddress, InvalidOid, makeTypeNameFromNameList(), NoLock, NOTICE, OBJECT_DOMCONSTRAINT, ObjectIdGetDatum(), OidIsValid, RangeVarCallbackForRenameAttribute(), RangeVarGetRelidExtended(), ReleaseSysCache(), rename_constraint_internal(), RowExclusiveLock, RVR_MISSING_OK, SearchSysCache1(), stmt, table_close(), table_open(), and typenameTypeId().

Referenced by ExecRenameStmt().

◆ RenameRelation()

ObjectAddress RenameRelation ( RenameStmt stmt)
extern

Definition at line 4270 of file tablecmds.c.

4271{
4272 bool is_index_stmt = stmt->renameType == OBJECT_INDEX;
4273 Oid relid;
4274 ObjectAddress address;
4275
4276 /*
4277 * Grab an exclusive lock on the target table, index, sequence, view,
4278 * materialized view, or foreign table, which we will NOT release until
4279 * end of transaction.
4280 *
4281 * Lock level used here should match RenameRelationInternal, to avoid lock
4282 * escalation. However, because ALTER INDEX can be used with any relation
4283 * type, we mustn't believe without verification.
4284 */
4285 for (;;)
4286 {
4287 LOCKMODE lockmode;
4288 char relkind;
4289 bool obj_is_index;
4290
4292
4293 relid = RangeVarGetRelidExtended(stmt->relation, lockmode,
4294 stmt->missing_ok ? RVR_MISSING_OK : 0,
4296 stmt);
4297
4298 if (!OidIsValid(relid))
4299 {
4301 (errmsg("relation \"%s\" does not exist, skipping",
4302 stmt->relation->relname)));
4303 return InvalidObjectAddress;
4304 }
4305
4306 /*
4307 * We allow mismatched statement and object types (e.g., ALTER INDEX
4308 * to rename a table), but we might've used the wrong lock level. If
4309 * that happens, retry with the correct lock level. We don't bother
4310 * if we already acquired AccessExclusiveLock with an index, however.
4311 */
4312 relkind = get_rel_relkind(relid);
4313 obj_is_index = (relkind == RELKIND_INDEX ||
4314 relkind == RELKIND_PARTITIONED_INDEX);
4316 break;
4317
4318 UnlockRelationOid(relid, lockmode);
4320 }
4321
4322 /* Do the work */
4323 RenameRelationInternal(relid, stmt->newname, false, is_index_stmt);
4324
4325 ObjectAddressSet(address, RelationRelationId, relid);
4326
4327 return address;
4328}
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition lmgr.c:229
void RenameRelationInternal(Oid myrelid, const char *newrelname, bool is_internal, bool is_index)
Definition tablecmds.c:4334

References AccessExclusiveLock, ereport, errmsg, fb(), get_rel_relkind(), InvalidObjectAddress, NOTICE, OBJECT_INDEX, ObjectAddressSet, OidIsValid, RangeVarCallbackForAlterRelation(), RangeVarGetRelidExtended(), RenameRelationInternal(), RVR_MISSING_OK, ShareUpdateExclusiveLock, stmt, and UnlockRelationOid().

Referenced by ExecRenameStmt().

◆ RenameRelationInternal()

void RenameRelationInternal ( Oid  myrelid,
const char newrelname,
bool  is_internal,
bool  is_index 
)
extern

Definition at line 4334 of file tablecmds.c.

4335{
4337 Relation relrelation; /* for RELATION relation */
4342
4343 /*
4344 * Grab a lock on the target relation, which we will NOT release until end
4345 * of transaction. We need at least a self-exclusive lock so that
4346 * concurrent DDL doesn't overwrite the rename if they start updating
4347 * while still seeing the old version. The lock also guards against
4348 * triggering relcache reloads in concurrent sessions, which might not
4349 * handle this information changing under them. For indexes, we can use a
4350 * reduced lock level because RelationReloadIndexInfo() handles indexes
4351 * specially.
4352 */
4355
4356 /*
4357 * Find relation's pg_class tuple, and make sure newrelname isn't in use.
4358 */
4360
4362 if (!HeapTupleIsValid(reltup)) /* shouldn't happen */
4363 elog(ERROR, "cache lookup failed for relation %u", myrelid);
4364 otid = reltup->t_self;
4366
4368 ereport(ERROR,
4370 errmsg("relation \"%s\" already exists",
4371 newrelname)));
4372
4373 /*
4374 * RenameRelation is careful not to believe the caller's idea of the
4375 * relation kind being handled. We don't have to worry about this, but
4376 * let's not be totally oblivious to it. We can process an index as
4377 * not-an-index, but not the other way around.
4378 */
4379 Assert(!is_index ||
4380 is_index == (targetrelation->rd_rel->relkind == RELKIND_INDEX ||
4381 targetrelation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX));
4382
4383 /*
4384 * Update pg_class tuple with new relname. (Scribbling on reltup is OK
4385 * because it's a copy...)
4386 */
4387 namestrcpy(&(relform->relname), newrelname);
4388
4391
4393 InvalidOid, is_internal);
4394
4397
4398 /*
4399 * Also rename the associated type, if any.
4400 */
4401 if (OidIsValid(targetrelation->rd_rel->reltype))
4402 RenameTypeInternal(targetrelation->rd_rel->reltype,
4404
4405 /*
4406 * Also rename the associated constraint, if any.
4407 */
4408 if (targetrelation->rd_rel->relkind == RELKIND_INDEX ||
4409 targetrelation->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
4410 {
4412
4415 }
4416
4417 /*
4418 * Close rel, but keep lock!
4419 */
4421}
void namestrcpy(Name name, const char *str)
Definition name.c:233
#define InvokeObjectPostAlterHookArg(classId, objectId, subId, auxiliaryId, is_internal)
void RenameConstraintById(Oid conId, const char *newname)
Oid get_index_constraint(Oid indexId)
Definition pg_depend.c:1192
void RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace)
Definition pg_type.c:763

References AccessExclusiveLock, Assert, CatalogTupleUpdate(), elog, ereport, errcode(), errmsg, ERROR, fb(), get_index_constraint(), get_relname_relid(), GETSTRUCT(), heap_freetuple(), HeapTupleIsValid, InplaceUpdateTupleLock, InvalidOid, InvokeObjectPostAlterHookArg, namestrcpy(), NoLock, ObjectIdGetDatum(), OidIsValid, relation_close(), relation_open(), RelationGetNamespace, RenameConstraintById(), RenameTypeInternal(), RowExclusiveLock, SearchSysCacheLockedCopy1(), ShareUpdateExclusiveLock, table_close(), table_open(), and UnlockTuple().

Referenced by ATExecAddIndexConstraint(), ATExecMergePartitions(), ATExecSplitPartition(), finish_heap_swap(), rename_constraint_internal(), RenameRelation(), and RenameType().

◆ ResetRelRewrite()

void ResetRelRewrite ( Oid  myrelid)
extern

Definition at line 4427 of file tablecmds.c.

4428{
4429 Relation relrelation; /* for RELATION relation */
4432
4433 /*
4434 * Find relation's pg_class tuple.
4435 */
4437
4439 if (!HeapTupleIsValid(reltup)) /* shouldn't happen */
4440 elog(ERROR, "cache lookup failed for relation %u", myrelid);
4442
4443 /*
4444 * Update pg_class tuple.
4445 */
4446 relform->relrewrite = InvalidOid;
4447
4449
4452}
#define SearchSysCacheCopy1(cacheId, key1)
Definition syscache.h:91

References CatalogTupleUpdate(), elog, ERROR, fb(), GETSTRUCT(), heap_freetuple(), HeapTupleIsValid, InvalidOid, ObjectIdGetDatum(), RowExclusiveLock, SearchSysCacheCopy1, table_close(), and table_open().

Referenced by finish_heap_swap().

◆ SetRelationHasSubclass()

void SetRelationHasSubclass ( Oid  relationId,
bool  relhassubclass 
)
extern

Definition at line 3711 of file tablecmds.c.

3712{
3714 HeapTuple tuple;
3716
3718 ShareUpdateExclusiveLock, false) ||
3720 ShareRowExclusiveLock, true));
3721
3722 /*
3723 * Fetch a modifiable copy of the tuple, modify it, update pg_class.
3724 */
3727 if (!HeapTupleIsValid(tuple))
3728 elog(ERROR, "cache lookup failed for relation %u", relationId);
3730
3731 if (classtuple->relhassubclass != relhassubclass)
3732 {
3733 classtuple->relhassubclass = relhassubclass;
3735 }
3736 else
3737 {
3738 /* no need to change tuple, but force relcache rebuild anyway */
3740 }
3741
3742 heap_freetuple(tuple);
3744}
void CacheInvalidateRelcacheByTuple(HeapTuple classTuple)
Definition inval.c:1666
bool CheckRelationOidLockedByMe(Oid relid, LOCKMODE lockmode, bool orstronger)
Definition lmgr.c:351

References Assert, CacheInvalidateRelcacheByTuple(), CatalogTupleUpdate(), CheckRelationOidLockedByMe(), elog, ERROR, fb(), GETSTRUCT(), heap_freetuple(), HeapTupleIsValid, ObjectIdGetDatum(), RowExclusiveLock, SearchSysCacheCopy1, ShareRowExclusiveLock, ShareUpdateExclusiveLock, HeapTupleData::t_self, table_close(), and table_open().

Referenced by acquire_inherited_sample_rows(), index_create(), IndexSetParentIndex(), and StoreCatalogInheritance1().

◆ SetRelationTableSpace()

void SetRelationTableSpace ( Relation  rel,
Oid  newTableSpaceId,
RelFileNumber  newRelFilenumber 
)
extern

Definition at line 3814 of file tablecmds.c.

3817{
3819 HeapTuple tuple;
3821 Form_pg_class rd_rel;
3822 Oid reloid = RelationGetRelid(rel);
3823
3825
3826 /* Get a modifiable copy of the relation's pg_class row. */
3828
3830 if (!HeapTupleIsValid(tuple))
3831 elog(ERROR, "cache lookup failed for relation %u", reloid);
3832 otid = tuple->t_self;
3833 rd_rel = (Form_pg_class) GETSTRUCT(tuple);
3834
3835 /* Update the pg_class row. */
3836 rd_rel->reltablespace = (newTableSpaceId == MyDatabaseTableSpace) ?
3839 rd_rel->relfilenode = newRelFilenumber;
3842
3843 /*
3844 * Record dependency on tablespace. This is only required for relations
3845 * that have no physical storage.
3846 */
3847 if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
3849 rd_rel->reltablespace);
3850
3851 heap_freetuple(tuple);
3853}
void changeDependencyOnTablespace(Oid classId, Oid objectId, Oid newTablespaceId)
#define RelFileNumberIsValid(relnumber)
Definition relpath.h:27
bool CheckRelationTableSpaceMove(Relation rel, Oid newTableSpaceId)
Definition tablecmds.c:3757

References Assert, CatalogTupleUpdate(), changeDependencyOnTablespace(), CheckRelationTableSpaceMove(), elog, ERROR, fb(), GETSTRUCT(), heap_freetuple(), HeapTupleIsValid, InplaceUpdateTupleLock, InvalidOid, MyDatabaseTableSpace, ObjectIdGetDatum(), RelationData::rd_rel, RelationGetRelid, RelFileNumberIsValid, RowExclusiveLock, SearchSysCacheLockedCopy1(), HeapTupleData::t_self, table_close(), table_open(), and UnlockTuple().

Referenced by ATExecSetTableSpace(), ATExecSetTableSpaceNoStorage(), and reindex_index().