Merge pull request #4215 from citusdata/fix/rls-moves

pull/4227/head
Marco Slot 2020-10-06 14:16:40 +02:00 committed by GitHub
commit f904ce1726
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 70 deletions

View File

@ -287,8 +287,8 @@ GetShellTableDDLEventsForCitusLocalTable(Oid relationId)
*/ */
bool includeSequenceDefaults = true; bool includeSequenceDefaults = true;
List *shellTableDDLEvents = GetTableDDLEvents(relationId, List *shellTableDDLEvents = GetFullTableCreationCommands(relationId,
includeSequenceDefaults); includeSequenceDefaults);
shellTableDDLEvents = list_concat(shellTableDDLEvents, foreignConstraintCommands); shellTableDDLEvents = list_concat(shellTableDDLEvents, foreignConstraintCommands);
return shellTableDDLEvents; return shellTableDDLEvents;

View File

@ -1576,7 +1576,7 @@ RelationUsesHeapAccessMethodOrNone(Relation relation)
* *
* The undistributed table will have the same name, columns and rows. It will also have * The undistributed table will have the same name, columns and rows. It will also have
* partitions, views, sequences of the old table. Finally it will have everything created * partitions, views, sequences of the old table. Finally it will have everything created
* by GetTableConstructionCommands function, which include indexes. These will be * by GetPostLoadTableCreationCommands function, which include indexes. These will be
* re-created during undistribution, so their oids are not preserved either (except for * re-created during undistribution, so their oids are not preserved either (except for
* sequences). However, their names are preserved. * sequences). However, their names are preserved.
* *
@ -1618,11 +1618,11 @@ UndistributeTable(Oid relationId)
} }
List *tableBuildingCommands = GetTableBuildingCommands(relationId, true); List *preLoadCommands = GetPreLoadTableCreationCommands(relationId, true);
List *tableConstructionCommands = GetTableConstructionCommands(relationId); List *postLoadCommands = GetPostLoadTableCreationCommands(relationId);
tableConstructionCommands = list_concat(tableConstructionCommands, postLoadCommands = list_concat(postLoadCommands,
GetViewCreationCommandsOfTable(relationId)); GetViewCreationCommandsOfTable(relationId));
int spiResult = SPI_connect(); int spiResult = SPI_connect();
if (spiResult != SPI_OK_CONNECT) if (spiResult != SPI_OK_CONNECT)
@ -1655,8 +1655,8 @@ UndistributeTable(Oid relationId)
{ {
ereport(ERROR, (errmsg("could not run SPI query"))); ereport(ERROR, (errmsg("could not run SPI query")));
} }
tableBuildingCommands = lappend(tableBuildingCommands, preLoadCommands = lappend(preLoadCommands,
attachPartitionCommand); attachPartitionCommand);
UndistributeTable(partitionRelationId); UndistributeTable(partitionRelationId);
} }
} }
@ -1670,7 +1670,7 @@ UndistributeTable(Oid relationId)
ereport(NOTICE, (errmsg("Creating a new local table for %s", ereport(NOTICE, (errmsg("Creating a new local table for %s",
quote_qualified_identifier(schemaName, relationName)))); quote_qualified_identifier(schemaName, relationName))));
foreach_ptr(tableCreationCommand, tableBuildingCommands) foreach_ptr(tableCreationCommand, preLoadCommands)
{ {
Node *parseTree = ParseTreeNode(tableCreationCommand); Node *parseTree = ParseTreeNode(tableCreationCommand);
@ -1682,7 +1682,7 @@ UndistributeTable(Oid relationId)
ReplaceTable(relationId, get_relname_relid(tempName, schemaId)); ReplaceTable(relationId, get_relname_relid(tempName, schemaId));
char *tableConstructionCommand = NULL; char *tableConstructionCommand = NULL;
foreach_ptr(tableConstructionCommand, tableConstructionCommands) foreach_ptr(tableConstructionCommand, postLoadCommands)
{ {
spiResult = SPI_execute(tableConstructionCommand, false, 0); spiResult = SPI_execute(tableConstructionCommand, false, 0);
if (spiResult != SPI_OK_UTILITY) if (spiResult != SPI_OK_UTILITY)

View File

@ -398,7 +398,8 @@ MetadataCreateCommands(void)
} }
List *workerSequenceDDLCommands = SequenceDDLCommandsForTable(relationId); List *workerSequenceDDLCommands = SequenceDDLCommandsForTable(relationId);
List *ddlCommandList = GetTableDDLEvents(relationId, includeSequenceDefaults); List *ddlCommandList = GetFullTableCreationCommands(relationId,
includeSequenceDefaults);
char *tableOwnerResetCommand = TableOwnerResetCommand(relationId); char *tableOwnerResetCommand = TableOwnerResetCommand(relationId);
List *sequenceDependencyCommandList = SequenceDependencyCommandList(relationId); List *sequenceDependencyCommandList = SequenceDependencyCommandList(relationId);
@ -510,13 +511,10 @@ GetDistributedTableDDLEvents(Oid relationId)
commandList = list_concat(commandList, sequenceDDLCommands); commandList = list_concat(commandList, sequenceDDLCommands);
/* commands to create the table */ /* commands to create the table */
List *tableDDLCommands = GetTableDDLEvents(relationId, includeSequenceDefaults); List *tableDDLCommands = GetFullTableCreationCommands(relationId,
includeSequenceDefaults);
commandList = list_concat(commandList, tableDDLCommands); commandList = list_concat(commandList, tableDDLCommands);
/* command to reset the table owner */
char *tableOwnerResetCommand = TableOwnerResetCommand(relationId);
commandList = lappend(commandList, tableOwnerResetCommand);
/* command to associate sequences with table */ /* command to associate sequences with table */
List *sequenceDependencyCommandList = SequenceDependencyCommandList(relationId); List *sequenceDependencyCommandList = SequenceDependencyCommandList(relationId);
commandList = list_concat(commandList, sequenceDependencyCommandList); commandList = list_concat(commandList, sequenceDependencyCommandList);

View File

@ -221,7 +221,8 @@ master_get_table_ddl_events(PG_FUNCTION_ARGS)
functionContext->multi_call_memory_ctx); functionContext->multi_call_memory_ctx);
/* allocate DDL statements, and then save position in DDL statements */ /* allocate DDL statements, and then save position in DDL statements */
List *tableDDLEventList = GetTableDDLEvents(relationId, includeSequenceDefaults); List *tableDDLEventList = GetFullTableCreationCommands(relationId,
includeSequenceDefaults);
tableDDLEventCell = list_head(tableDDLEventList); tableDDLEventCell = list_head(tableDDLEventList);
ListCellAndListWrapper *wrapper = palloc0(sizeof(ListCellAndListWrapper)); ListCellAndListWrapper *wrapper = palloc0(sizeof(ListCellAndListWrapper));
wrapper->list = tableDDLEventList; wrapper->list = tableDDLEventList;
@ -522,7 +523,7 @@ ResolveRelationId(text *relationName, bool missingOk)
/* /*
* GetTableDDLEvents takes in a relationId, includeSequenceDefaults flag, * GetFullTableCreationCommands takes in a relationId, includeSequenceDefaults flag,
* and returns the list of DDL commands needed to reconstruct the relation. * and returns the list of DDL commands needed to reconstruct the relation.
* When the flag includeSequenceDefaults is set, the function also creates * When the flag includeSequenceDefaults is set, the function also creates
* DEFAULT clauses for columns getting their default values from a sequence. * DEFAULT clauses for columns getting their default values from a sequence.
@ -531,28 +532,30 @@ ResolveRelationId(text *relationName, bool missingOk)
* constraint and trigger definitions. * constraint and trigger definitions.
*/ */
List * List *
GetTableDDLEvents(Oid relationId, bool includeSequenceDefaults) GetFullTableCreationCommands(Oid relationId, bool includeSequenceDefaults)
{ {
List *tableDDLEventList = NIL; List *tableDDLEventList = NIL;
List *tableCreationCommandList = GetTableCreationCommands(relationId, List *preLoadCreationCommandList =
includeSequenceDefaults); GetPreLoadTableCreationCommands(relationId, includeSequenceDefaults);
tableDDLEventList = list_concat(tableDDLEventList, tableCreationCommandList);
List *otherCommands = GetTableConstructionCommands(relationId); tableDDLEventList = list_concat(tableDDLEventList, preLoadCreationCommandList);
tableDDLEventList = list_concat(tableDDLEventList, otherCommands);
List *postLoadCreationCommandList =
GetPostLoadTableCreationCommands(relationId);
tableDDLEventList = list_concat(tableDDLEventList, postLoadCreationCommandList);
return tableDDLEventList; return tableDDLEventList;
} }
/* /*
* GetTableConstructionCommands takes in a relationId and returns the list * GetPostLoadTableCreationCommands takes in a relationId and returns the list
* of DDL commands needed to reconstruct the relation except the ones that actually * of DDL commands that should be applied after loading the data.
* create the table.
*/ */
List * List *
GetTableConstructionCommands(Oid relationId) GetPostLoadTableCreationCommands(Oid relationId)
{ {
List *tableDDLEventList = NIL; List *tableDDLEventList = NIL;
@ -562,9 +565,6 @@ GetTableConstructionCommands(Oid relationId)
List *replicaIdentityEvents = GetTableReplicaIdentityCommand(relationId); List *replicaIdentityEvents = GetTableReplicaIdentityCommand(relationId);
tableDDLEventList = list_concat(tableDDLEventList, replicaIdentityEvents); tableDDLEventList = list_concat(tableDDLEventList, replicaIdentityEvents);
List *policyCommands = CreatePolicyCommands(relationId);
tableDDLEventList = list_concat(tableDDLEventList, policyCommands);
List *triggerCommands = GetExplicitTriggerCommandList(relationId); List *triggerCommands = GetExplicitTriggerCommandList(relationId);
tableDDLEventList = list_concat(tableDDLEventList, triggerCommands); tableDDLEventList = list_concat(tableDDLEventList, triggerCommands);
@ -604,12 +604,12 @@ GetTableReplicaIdentityCommand(Oid relationId)
/* /*
* GetTableCreationCommands takes in a relationId, and returns the list of DDL * GetPreLoadTableCreationCommands takes in a relationId, and returns the list of DDL
* commands needed to reconstruct the relation, excluding indexes and * commands needed to reconstruct the relation, excluding indexes and constraints,
* constraints. * to facilitate faster data load.
*/ */
List * List *
GetTableCreationCommands(Oid relationId, bool includeSequenceDefaults) GetPreLoadTableCreationCommands(Oid relationId, bool includeSequenceDefaults)
{ {
List *tableDDLEventList = NIL; List *tableDDLEventList = NIL;
@ -629,30 +629,6 @@ GetTableCreationCommands(Oid relationId, bool includeSequenceDefaults)
tableDDLEventList = lappend(tableDDLEventList, serverDef); tableDDLEventList = lappend(tableDDLEventList, serverDef);
} }
List *tableBuildingCommands = GetTableBuildingCommands(relationId,
includeSequenceDefaults);
tableDDLEventList = list_concat(tableDDLEventList,
tableBuildingCommands);
/* revert back to original search_path */
PopOverrideSearchPath();
return tableDDLEventList;
}
/*
* GetTableBuildingCommands takes in a relationId, and returns the list of DDL
* commands needed to rebuild the relation. This does not include the schema
* and the server commands.
*/
List *
GetTableBuildingCommands(Oid relationId, bool includeSequenceDefaults)
{
List *tableDDLEventList = NIL;
PushOverrideEmptySearchPath(CurrentMemoryContext);
/* fetch table schema and column option definitions */ /* fetch table schema and column option definitions */
char *tableSchemaDef = pg_get_tableschemadef_string(relationId, char *tableSchemaDef = pg_get_tableschemadef_string(relationId,
includeSequenceDefaults); includeSequenceDefaults);
@ -670,6 +646,9 @@ GetTableBuildingCommands(Oid relationId, bool includeSequenceDefaults)
tableDDLEventList = lappend(tableDDLEventList, tableOwnerDef); tableDDLEventList = lappend(tableDDLEventList, tableOwnerDef);
} }
List *policyCommands = CreatePolicyCommands(relationId);
tableDDLEventList = list_concat(tableDDLEventList, policyCommands);
/* revert back to original search_path */ /* revert back to original search_path */
PopOverrideSearchPath(); PopOverrideSearchPath();

View File

@ -775,7 +775,7 @@ CopyShardCommandList(ShardInterval *shardInterval, const char *sourceNodeName,
copyShardDataCommand->data); copyShardDataCommand->data);
} }
List *indexCommandList = GetTableIndexAndConstraintCommands(relationId); List *indexCommandList = GetPostLoadTableCreationCommands(relationId);
indexCommandList = WorkerApplyShardDDLCommandList(indexCommandList, shardId); indexCommandList = WorkerApplyShardDDLCommandList(indexCommandList, shardId);
copyShardToNodeCommandsList = list_concat(copyShardToNodeCommandsList, copyShardToNodeCommandsList = list_concat(copyShardToNodeCommandsList,
@ -980,8 +980,8 @@ RecreateTableDDLCommandList(Oid relationId)
} }
List *dropCommandList = list_make1(dropCommand->data); List *dropCommandList = list_make1(dropCommand->data);
List *createCommandList = GetTableCreationCommands(relationId, List *createCommandList = GetPreLoadTableCreationCommands(relationId,
includeSequenceDefaults); includeSequenceDefaults);
List *recreateCommandList = list_concat(dropCommandList, createCommandList); List *recreateCommandList = list_concat(dropCommandList, createCommandList);
return recreateCommandList; return recreateCommandList;

View File

@ -396,7 +396,8 @@ CreateAppendDistributedShardPlacements(Oid relationId, int64 shardId,
List *foreignConstraintCommandList = List *foreignConstraintCommandList =
GetReferencingForeignConstaintCommands(relationId); GetReferencingForeignConstaintCommands(relationId);
bool includeSequenceDefaults = false; bool includeSequenceDefaults = false;
List *ddlCommandList = GetTableDDLEvents(relationId, includeSequenceDefaults); List *ddlCommandList = GetFullTableCreationCommands(relationId,
includeSequenceDefaults);
uint32 connectionFlag = FOR_DDL; uint32 connectionFlag = FOR_DDL;
char *relationOwner = TableOwner(relationId); char *relationOwner = TableOwner(relationId);
@ -500,8 +501,8 @@ CreateShardsOnWorkers(Oid distributedRelationId, List *shardPlacements,
bool useExclusiveConnection, bool colocatedShard) bool useExclusiveConnection, bool colocatedShard)
{ {
bool includeSequenceDefaults = false; bool includeSequenceDefaults = false;
List *ddlCommandList = GetTableDDLEvents(distributedRelationId, List *ddlCommandList = GetFullTableCreationCommands(distributedRelationId,
includeSequenceDefaults); includeSequenceDefaults);
List *foreignConstraintCommandList = List *foreignConstraintCommandList =
GetReferencingForeignConstaintCommands(distributedRelationId); GetReferencingForeignConstaintCommands(distributedRelationId);

View File

@ -100,10 +100,10 @@ extern bool CStoreTable(Oid relationId);
extern uint64 GetNextShardId(void); extern uint64 GetNextShardId(void);
extern uint64 GetNextPlacementId(void); extern uint64 GetNextPlacementId(void);
extern Oid ResolveRelationId(text *relationName, bool missingOk); extern Oid ResolveRelationId(text *relationName, bool missingOk);
extern List * GetTableDDLEvents(Oid relationId, bool forShardCreation); extern List * GetFullTableCreationCommands(Oid relationId, bool includeSequenceDefaults);
extern List * GetTableConstructionCommands(Oid relationId); extern List * GetPostLoadTableCreationCommands(Oid relationId);
extern List * GetTableCreationCommands(Oid relationId, bool forShardCreation); extern List * GetPreLoadTableCreationCommands(Oid relationId,
extern List * GetTableBuildingCommands(Oid relationId, bool includeSequenceDefaults); bool includeSequenceDefaults);
extern List * GetTableIndexAndConstraintCommands(Oid relationId); extern List * GetTableIndexAndConstraintCommands(Oid relationId);
extern bool IndexImpliedByAConstraint(Form_pg_index indexForm); extern bool IndexImpliedByAConstraint(Form_pg_index indexForm);
extern char ShardStorageType(Oid relationId); extern char ShardStorageType(Oid relationId);