C.3. Tables

C.3.1. What kind of tables are there in Shardman?
C.3.2. What are global tables?
C.3.3. What are global tables suitable for?
C.3.4. What are sharded (distributed) tables?
C.3.5. Which partitioning parameters are optimal when creating a sharded table?
C.3.6. What are colocated tables?
C.3.7. How to create a colocated table?
C.3.8. What are local tables?
C.3.9. Are foreign keys supported in Shardman?

C.3.1. What kind of tables are there in Shardman?

Tables in Shardman can be of types: global, distributed, colocated, local, and temporary.

C.3.2. What are global tables?

A global table in Shardman is a table that has the same schema and contents on all shards in the cluster. Global tables are created as follows:

CREATE TABLE g(id bigint PRIMARY KEY, t text) WITH(global);
    

A copy of such a table is created on each shard. Data replication of global tables is based on triggers. When data is inserted into such a table on any node of the cluster, data replication to other nodes occurs. When creating a global table, it is necessary to specify the primary key. Supported primary key types are bigint, bigserial, uuid.

C.3.3. What are global tables suitable for?

Global tables are suitable for directories and other relatively small and infrequently modified tables. Global tables are NOT suitable for storing large amounts of data and for intensive INSERT/UPDATE/DELETE workload, especially with highly competitive access (storefronts, queues, etc.)

C.3.4. What are sharded (distributed) tables?

Sharded tables are tables whose parts are hosted on different shards. Each shard stores its own piece of data from such a table. A sharded table can be created as follows:

CREATE TABLE ... WITH(distributed_by = 'column_name', num_parts = number_of_partitions);
    

Here:

distributed_by — table field being the sharding key,

num_parts> — (default = 20) number of parts into which the table is initially divided.

These parts are then distributed to shards.

C.3.5.  Which partitioning parameters are optimal when creating a sharded table?

num_parts = number of shards if known and fixed, or a number with quite a few divisors like 12 or 24, so you can evenly divide the table into 2, 3, 4 or 6 shards. However, if you make it too large, for example 1024, it will take too much time to search each of the pieces and then merge the results.

C.3.6.  What are colocated tables?

Colocated tables are used when a table is often joined with another sharded table (usually by foreign keys) and therefore it is better to physically place their parts on the same shards.

C.3.7.  How to create a colocated table?

CREATE TABLE ... WITH(distributed_by = 'column_name', num_parts = number_of_partitions, colocate_with = 'distributed_table');
    

Here:

distributed_by = 'column_name' — the name of the sharding key as it is called in the colocated table (not the colocating table) being created,

colocate_with = 'distributed_table' — the name of the table with which you want to colocate parts of the colocated table.

C.3.8.  What are local tables?

A local table is a table only hosted on the shard where it was created.

C.3.9.  Are foreign keys supported in Shardman?

Foreign keys are allowed in Shardman but with some limitations:

  • On global tables, both from sharded tables and from other global tables

  • Between sharded colocated tables.

Foreign keys are NOT allowed:

  • From global to sharded tables

  • Between sharded tables if they are not colocated.