CREATE TABLE — define a new table
CREATE [ UNLOGGED ] TABLE [ IF NOT EXISTS ]table_name( [ {column_namedata_type[ COLLATEcollation] [column_constraint[ ... ] ] |table_constraint} [, ... ] ] ) [ USINGmethod] [ WITH (storage_parameter[=value] [, ... ] ) ] [ TABLESPACEtablespace_name] CREATE TABLEtable_name( [ {column_namedata_type} [, ... ] ] ) [ WITH ( [[distributed_by= 'column_name' ], [num_parts=number_of_partitions], [colocate_with= 'colocation_table_name' ], [partition_by= 'column_name' ], [partition_bounds= 'array_of_partition_bound_exprs' ]] | [global] ) ] wherecolumn_constraintis: [ CONSTRAINTconstraint_name] { NOT NULL | NULL | CHECK (expression) [ NO INHERIT ] | DEFAULTdefault_expr| UNIQUEindex_parameters| PRIMARY KEYindex_parameters} [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] andtable_constraintis: [ CONSTRAINTconstraint_name] { CHECK (expression) [ NO INHERIT ] | UNIQUE (column_name[, ... ] )index_parameters| PRIMARY KEY (column_name[, ... ] )index_parameters| EXCLUDE [ USINGindex_method] (exclude_elementWITHoperator[, ... ] )index_parameters[ WHERE (predicate) ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
Shardman extension of the CREATE TABLE
syntax enables creation of sharded tables distributed across
all replication groups with a single DDL statement.
The extended CREATE TABLE syntax imposes
limitations on the general syntax of the command.
For example, there is currently no support for:
All serial types.
Generated columns.
REFERENCES and FOREIGN KEY constraints.
PARTITION BY and PARTITION OF clauses.
Shardman extends storage parameters of tables with its own storage metaparameters. They are not stored in the corresponding catalog entry, but are used to tell the Shardman extension to perform some additional actions. Regular storage parameters are transparently passed to table partitions.
distributed_by (text)
This specifies the name of the column to use for the table partitioning.
Only hash partitioning is currently supported, so this is effectively
an equivalent of PARTITION BY HASH, but all the partition
leaves will be created immediately on all replication groups and the table
will be registered in the Shardman metadata.
num_parts (integer)
This sets the number of partitions that will be created for this table. This parameter is optional. If it is not specified, the value of the global setting shardman.num_parts will be used by default.
colocate_with (text)
This specifies the name of the table to colocate with. If set,
Shardman will try to place partitions
of the created table with the same partition key on the same nodes
as colocation_table_name.
This parameter is optional.
partition_by (text)
This specifies the name of the column to use for the second level table
partitioning. Only range partitioning is currently supported.
When this option is used, each table partition is created as
partitioned table. Subpartitions can be created immediately if
partition_bounds
parameter is set.
This parameter is optional.
partition_bounds (text)
This parameter sets bounds of second level table partitions. Bounds should be a string representation of two-dimensional array. Each array member is a pair of lower and higher bound for partitions. If lower and upper bounds are both NULL, the default partition is created. Number of partitions is determined by the first array dimension. This parameter is optional.
global (boolean)
This defines that the table is global. If set, the table will be distributed on all replication groups and will be synchronized by triggers. This parameter is optional.
Create table pgbench_branches, colocated table
pgbench_accounts and pgbench_history.
Each partiion of the pgbench_history table is additionally
subpartitioned by range.
CREATE TABLE pgbench_branches (
bid integer NOT NULL PRIMARY KEY,
bbalance integer,
filler character(88)
)
WITH (distributed_by = 'bid',
num_parts = 8);
CREATE TABLE pgbench_accounts (
aid integer NOT NULL,
bid integer,
abalance integer,
filler character(84),
PRIMARY KEY (bid, aid)
)
WITH (distributed_by = 'bid',
num_parts = 8,
colocate_with = 'pgbench_branches');
CREATE TABLE public.pgbench_history (
tid integer,
bid integer,
aid integer,
delta integer,
mtime timestamp without time zone,
filler character(22)
)
WITH (distributed_by = 'bid',
num_parts = 8,
colocate_with = 'pgbench_branches',
partition_by = 'mtime',
partition_bounds =
$${{minvalue, '2021-01-01 00:00'},{'2021-01-01 00:00', '2022-01-01 00:00'},{'2022-01-01 00:00', maxvalue}}$$
);