Skip to content

Commit

Permalink
Merge pull request citusdata#2469 from citusdata/refactor/commands-ex…
Browse files Browse the repository at this point in the history
…tract

Refactor the UtilityProcess function into its own module
  • Loading branch information
thanodnl authored Nov 14, 2018
2 parents ce463e9 + f383e4f commit bafec7e
Show file tree
Hide file tree
Showing 47 changed files with 4,945 additions and 4,779 deletions.
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@ EXTENSION = citus
MODULE_big = citus

OBJS = src/backend/distributed/shared_library_init.o \
src/backend/distributed/commands/cluster.o \
src/backend/distributed/commands/create_distributed_table.o \
src/backend/distributed/commands/drop_distributed_table.o \
src/backend/distributed/commands/extension.o \
src/backend/distributed/commands/foreign_constraint.o \
src/backend/distributed/commands/grant.o \
src/backend/distributed/commands/index.o \
src/backend/distributed/commands/multi_copy.o \
src/backend/distributed/commands/policy.o \
src/backend/distributed/commands/rename.o \
src/backend/distributed/commands/schema.o \
src/backend/distributed/commands/sequence.o \
src/backend/distributed/commands/subscription.o \
src/backend/distributed/commands/table.o \
src/backend/distributed/commands/transmit.o \
src/backend/distributed/commands/truncate.o \
src/backend/distributed/commands/utility_hook.o \
src/backend/distributed/commands/vacuum.o \
src/backend/distributed/connection/connection_configuration.o \
src/backend/distributed/connection/connection_management.o \
src/backend/distributed/connection/placement_connection.o \
src/backend/distributed/connection/remote_commands.o \
src/backend/distributed/ddl/foreign_constraint.o \
src/backend/distributed/ddl/policy.o \
src/backend/distributed/executor/citus_custom_scan.o \
src/backend/distributed/executor/insert_select_executor.o \
src/backend/distributed/executor/intermediate_results.o \
Expand All @@ -24,7 +36,6 @@ OBJS = src/backend/distributed/shared_library_init.o \
src/backend/distributed/executor/multi_router_executor.o \
src/backend/distributed/executor/multi_server_executor.o \
src/backend/distributed/executor/multi_task_tracker_executor.o \
src/backend/distributed/executor/multi_utility.o \
src/backend/distributed/executor/query_stats.o \
src/backend/distributed/executor/subplan_execution.o \
src/backend/distributed/master/citus_create_restore_point.o \
Expand Down
54 changes: 54 additions & 0 deletions src/backend/distributed/commands/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Commands

The commands module is modeled after `backend/commands` from the postgres repository and
contains the logic for Citus on how to run these commands on distributed objects. Even
though the structure of the directory has some resemblence to its postgres relative files
here are somewhat more finegrained. This is due to the nature of citus commands that are
heavily focused on distributed tables. Instead of having all commands in `tablecmds.c`
they are often moved to files that are named after the command.

| File | Description |
|------------------------------|-------------|
| `create_distributed_table.c` | Implementation of UDF's for creating distributed tables |
| `drop_distributed_table.c` | Implementation for dropping metadata for partitions of distributed tables |
| `extension.c` | Implementation of `CREATE EXTENSION` commands for citus specific checks |
| `foreign_constraint.c` | Implementation of and helper functions for foreign key constraints |
| `grant.c` | Placeholder for code granting users access to relations, implemented as enterprise feature |
| `index.c` | Implementation of commands specific to indices on distributed tables |
| `multi_copy.c` | Implementation of `COPY` command. There are multiple different copy modes which are described in detail below |
| `policy.c` | Implementation of `CREATE\ALTER POLICY` commands. |
| `rename.c` | Implementation of `ALTER ... RENAME ...` commands. It implements the renaming of applicable objects, otherwise provides the user with a warning |
| `schema.c` | |
| `sequence.c` | Implementation of `CREATE/ALTER SEQUENCE` commands. Primarily checks correctness of sequence statements as they are not propagated to the worker nodes |
| `table.c` | |
| `transmit.c` | Implementation of `COPY` commands with `format transmit` set in the options. This format is used to transfer files from one node to another node |
| `truncate.c` | Implementation of `TRUNCATE` commands on distributed tables |
| `utility_hook.c` | This is the entry point from postgres into the commands module of citus. It contains the implementation that gets registered in postgres' `ProcessUtility_hook` callback to extends the functionality of the original ProcessUtility. This code is used to route the incomming commands to their respective implementation in Citus |
| `vacuum.c` | Implementation of `VACUUM` commands on distributed tables |

# COPY

The copy command is overloaded for a couple of use-cases specific to citus. The syntax of
the command stays the same, however the implementation might slightly differ from the
stock implementation. The overloading is mostly done via extra options that Citus uses to
indicate how to operate the copy. The options used are described below.

## FORMAT transmit

Implemented in `transmit.c`

TODO: to be written by someone with enough knowledge to write how, when and why it is used.

## FORMAT result

Implemented in `multi_copy.c`

TODO: to be written by someone with enough knowledge to write how, when and why it is used.

## MASTER_HOST host

Implemented in `multi_copy.c`

Triggered by the `MASTER_HOST` option being set on the copy command. Also accepts `MASTER_PORT`

TODO: to be written by someone with enough knowledge to write how, when and why it is used.
49 changes: 49 additions & 0 deletions src/backend/distributed/commands/cluster.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*-------------------------------------------------------------------------
*
* cluster.c
* Commands for CLUSTER statement
*
* Copyright (c) 2018, Citus Data, Inc.
*
*-------------------------------------------------------------------------
*/

#include "postgres.h"

#include "catalog/namespace.h"
#include "distributed/commands.h"
#include "distributed/metadata_cache.h"


/* placeholder for PlanClusterStmt */
List *
PlanClusterStmt(ClusterStmt *clusterStmt, const char *clusterCommand)
{
bool showPropagationWarning = false;

/* CLUSTER all */
if (clusterStmt->relation == NULL)
{
showPropagationWarning = true;
}
else
{
Oid relationId = InvalidOid;
bool missingOK = false;

relationId = RangeVarGetRelid(clusterStmt->relation, AccessShareLock,
missingOK);

if (OidIsValid(relationId))
{
showPropagationWarning = IsDistributedTable(relationId);
}
}

if (showPropagationWarning)
{
ereport(WARNING, (errmsg("not propagating CLUSTER command to worker nodes")));
}

return NIL;
}
6 changes: 2 additions & 4 deletions src/backend/distributed/commands/create_distributed_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,20 @@
#include "commands/defrem.h"
#include "commands/extension.h"
#include "commands/trigger.h"
#include "distributed/commands/multi_copy.h"
#include "distributed/citus_ruleutils.h"
#include "distributed/colocation_utils.h"
#include "distributed/commands.h"
#include "distributed/distribution_column.h"
#include "distributed/foreign_constraint.h"
#include "distributed/master_metadata_utility.h"
#include "distributed/master_protocol.h"
#include "distributed/metadata_cache.h"
#include "distributed/metadata_sync.h"
#include "distributed/multi_copy.h"
#include "distributed/multi_executor.h"
#include "distributed/multi_logical_planner.h"
#include "distributed/multi_partitioning_utils.h"
#include "distributed/multi_utility.h"
#include "distributed/pg_dist_colocation.h"
#include "distributed/pg_dist_partition.h"
#include "distributed/policy.h"
#include "distributed/reference_table_utils.h"
#include "distributed/relation_access_tracking.h"
#include "distributed/remote_commands.h"
Expand Down
4 changes: 2 additions & 2 deletions src/backend/distributed/commands/drop_distributed_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#include "postgres.h"
#include "miscadmin.h"

#include "distributed/commands/utility_hook.h"
#include "distributed/master_metadata_utility.h"
#include "distributed/master_protocol.h"
#include "distributed/metadata_sync.h"
#include "distributed/multi_utility.h"
#include "distributed/worker_transaction.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
Expand Down Expand Up @@ -112,7 +112,7 @@ master_remove_distributed_table_metadata_from_workers(PG_FUNCTION_ARGS)

/*
* MasterRemoveDistributedTableMetadataFromWorkers drops the table and removes
* all the metadata beloning the distributed table in the worker nodes
* all the metadata belonging the distributed table in the worker nodes
* with metadata. The function doesn't drop the tables that are
* the shards on the workers.
*
Expand Down
117 changes: 117 additions & 0 deletions src/backend/distributed/commands/extension.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*-------------------------------------------------------------------------
*
* extension.c
* Commands for creating and altering extensions.
*
* Copyright (c) 2018, Citus Data, Inc.
*
*-------------------------------------------------------------------------
*/

#include "postgres.h"

#include "citus_version.h"
#include "distributed/commands.h"
#include "distributed/metadata_cache.h"
#include "nodes/parsenodes.h"

/* Local functions forward declarations for helper functions */
static char * ExtractNewExtensionVersion(Node *parsetree);


/*
* IsCitusExtensionStmt returns whether a given utility is a CREATE or ALTER
* EXTENSION statement which references the citus extension. This function
* returns false for all other inputs.
*/
bool
IsCitusExtensionStmt(Node *parsetree)
{
char *extensionName = "";

if (IsA(parsetree, CreateExtensionStmt))
{
extensionName = ((CreateExtensionStmt *) parsetree)->extname;
}
else if (IsA(parsetree, AlterExtensionStmt))
{
extensionName = ((AlterExtensionStmt *) parsetree)->extname;
}

return (strcmp(extensionName, "citus") == 0);
}


/*
* ErrorIfUnstableCreateOrAlterExtensionStmt compares CITUS_EXTENSIONVERSION
* and version given CREATE/ALTER EXTENSION statement will create/update to. If
* they are not same in major or minor version numbers, this function errors
* out. It ignores the schema version.
*/
void
ErrorIfUnstableCreateOrAlterExtensionStmt(Node *parsetree)
{
char *newExtensionVersion = ExtractNewExtensionVersion(parsetree);

if (newExtensionVersion != NULL)
{
/* explicit version provided in CREATE or ALTER EXTENSION UPDATE; verify */
if (!MajorVersionsCompatible(newExtensionVersion, CITUS_EXTENSIONVERSION))
{
ereport(ERROR, (errmsg("specified version incompatible with loaded "
"Citus library"),
errdetail("Loaded library requires %s, but %s was specified.",
CITUS_MAJORVERSION, newExtensionVersion),
errhint("If a newer library is present, restart the database "
"and try the command again.")));
}
}
else
{
/*
* No version was specified, so PostgreSQL will use the default_version
* from the citus.control file.
*/
CheckAvailableVersion(ERROR);
}
}


/*
* ExtractNewExtensionVersion returns the new extension version specified by
* a CREATE or ALTER EXTENSION statement. Other inputs are not permitted. This
* function returns NULL for statements with no explicit version specified.
*/
static char *
ExtractNewExtensionVersion(Node *parsetree)
{
char *newVersion = NULL;
List *optionsList = NIL;
ListCell *optionsCell = NULL;

if (IsA(parsetree, CreateExtensionStmt))
{
optionsList = ((CreateExtensionStmt *) parsetree)->options;
}
else if (IsA(parsetree, AlterExtensionStmt))
{
optionsList = ((AlterExtensionStmt *) parsetree)->options;
}
else
{
/* input must be one of the two above types */
Assert(false);
}

foreach(optionsCell, optionsList)
{
DefElem *defElement = (DefElem *) lfirst(optionsCell);
if (strncmp(defElement->defname, "new_version", NAMEDATALEN) == 0)
{
newVersion = strVal(defElement->arg);
break;
}
}

return newVersion;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#endif
#include "catalog/pg_type.h"
#include "distributed/colocation_utils.h"
#include "distributed/foreign_constraint.h"
#include "distributed/commands.h"
#include "distributed/master_protocol.h"
#include "distributed/multi_join_order.h"
#include "distributed/version_compat.h"
Expand Down
19 changes: 19 additions & 0 deletions src/backend/distributed/commands/grant.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*-------------------------------------------------------------------------
*
* grant.c
* Commands for granting access to distributed tables.
*
* Copyright (c) 2018, Citus Data, Inc.
*
*-------------------------------------------------------------------------
*/

#include "distributed/commands.h"


/* placeholder for PlanGrantStmt */
List *
PlanGrantStmt(GrantStmt *grantStmt)
{
return NIL;
}
Loading

0 comments on commit bafec7e

Please sign in to comment.