Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
0fd0b22
Add initial docs for placement rules
morgo Oct 14, 2021
d762a54
Update with suggestions by Mattias
morgo Oct 14, 2021
18cbbee
Add infoschema
morgo Oct 16, 2021
bf55f27
Merge remote-tracking branch 'upstream/master' into placement-rules
morgo Oct 27, 2021
5926cdf
Address review feedback
morgo Oct 27, 2021
2738dd1
Merge remote-tracking branch 'upstream/master' into placement-rules
morgo Nov 2, 2021
1a20a6a
Incorporate feedback, add placement-checks sysvar
morgo Nov 2, 2021
08e4b5c
add scheduling State
morgo Nov 2, 2021
e4b8655
Add limitations section
morgo Nov 3, 2021
2db5cda
fix linter issues
morgo Nov 3, 2021
925ac6f
Update configure-placement-rules.md
morgo Nov 4, 2021
6843f0b
Update placement-rules.md
morgo Nov 4, 2021
5dc6675
Update schedule-replicas-by-topology-labels.md
morgo Nov 4, 2021
0e7c8bd
Update sql-statements/sql-statement-alter-placement-policy.md
morgo Nov 4, 2021
ef953ae
Update sql-statements/sql-statement-create-placement-policy.md
morgo Nov 4, 2021
df9fea4
Update sql-statements/sql-statement-create-placement-policy.md
morgo Nov 4, 2021
9929b5f
Update sql-statements/sql-statement-show-placement-for.md
morgo Nov 4, 2021
ac3d6d9
Update information-schema/information-schema-placement-rules.md
morgo Nov 4, 2021
312a363
Update placement-rules.md
morgo Nov 4, 2021
9659ada
Update information-schema/information-schema-placement-rules.md
morgo Nov 4, 2021
66a8451
Rename placement-rules.md to placement-rules-in-sql.md
morgo Nov 4, 2021
27cafc6
Update sql-statements/sql-statement-create-placement-policy.md
morgo Nov 5, 2021
91b50d5
Update sql-statements/sql-statement-alter-placement-policy.md
morgo Nov 5, 2021
83a799a
Update placement-rules-in-sql.md
morgo Nov 5, 2021
bdf1323
Update placement-rules-in-sql.md
morgo Nov 5, 2021
42749fd
Update placement-rules-in-sql.md
morgo Nov 5, 2021
6b8d359
Update placement-rules-in-sql.md
morgo Nov 5, 2021
f43fdf0
Update sql-statements/sql-statement-drop-placement-policy.md
morgo Nov 5, 2021
3b8bb24
Update sql-statements/sql-statement-alter-placement-policy.md
morgo Nov 5, 2021
0dcc1bb
Update placement-rules-in-sql.md
morgo Nov 5, 2021
d5e3686
Update placement-rules-in-sql.md
morgo Nov 5, 2021
2a2a033
Update placement-rules-in-sql.md
morgo Nov 5, 2021
769c8a8
Update placement-rules-in-sql.md
morgo Nov 5, 2021
2f38813
Update placement-rules-in-sql.md
morgo Nov 5, 2021
bba9404
Update placement-rules-in-sql.md
morgo Nov 5, 2021
3c5087a
Update sql-statements/sql-statement-drop-placement-policy.md
morgo Nov 5, 2021
53f6d9f
Update placement-rules-in-sql.md
morgo Nov 5, 2021
bbc9312
Update placement-rules-in-sql.md
morgo Nov 5, 2021
63bd263
Update placement-rules-in-sql.md
morgo Nov 5, 2021
7e8764d
Update placement-rules-in-sql.md
morgo Nov 5, 2021
0232ea6
Update placement-rules-in-sql.md
morgo Nov 5, 2021
973ff23
Update placement-rules-in-sql.md
morgo Nov 5, 2021
1311b97
Update placement-rules-in-sql.md
morgo Nov 5, 2021
761bc80
Update placement-rules-in-sql.md
morgo Nov 5, 2021
77be485
Update placement-rules-in-sql.md
morgo Nov 5, 2021
d7dcda0
Update placement-rules-in-sql.md
morgo Nov 5, 2021
86f837d
Update placement-rules-in-sql.md
morgo Nov 5, 2021
66f3593
Update placement-rules-in-sql.md
morgo Nov 5, 2021
13e76a5
Update placement-rules-in-sql.md
morgo Nov 5, 2021
3e9ea39
Update placement-rules-in-sql.md
morgo Nov 5, 2021
05ff647
Update placement-rules-in-sql.md
morgo Nov 5, 2021
3a6a707
Adjust warning
morgo Nov 5, 2021
ba7a5c0
Merge branch 'placement-rules' of github.com:morgo/docs into placemen…
morgo Nov 5, 2021
d11405b
Update system-variables.md
morgo Nov 8, 2021
3769ea7
Update sql-statements/sql-statement-show-placement.md
morgo Nov 8, 2021
d673d8d
Update sql-statements/sql-statement-show-placement-labels.md
morgo Nov 8, 2021
bbf4d00
Update sql-statements/sql-statement-show-placement-for.md
morgo Nov 8, 2021
429d193
Update sql-statements/sql-statement-show-create-placement-policy.md
morgo Nov 8, 2021
d6421be
Update placement-rules-in-sql.md
morgo Nov 8, 2021
051b372
Update sql-statements/sql-statement-drop-placement-policy.md
morgo Nov 8, 2021
5422310
Update sql-statements/sql-statement-create-placement-policy.md
morgo Nov 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
+ [`ALTER DATABASE`](/sql-statements/sql-statement-alter-database.md)
+ [`ALTER INDEX`](/sql-statements/sql-statement-alter-index.md)
+ [`ALTER INSTANCE`](/sql-statements/sql-statement-alter-instance.md)
+ [`ALTER PLACEMENT POLICY`](/sql-statements/sql-statement-alter-placement-policy.md)
+ [`ALTER TABLE`](/sql-statements/sql-statement-alter-table.md)
+ [`ALTER USER`](/sql-statements/sql-statement-alter-user.md)
+ [`ANALYZE TABLE`](/sql-statements/sql-statement-analyze-table.md)
Expand All @@ -287,6 +288,7 @@
+ [`CREATE [GLOBAL|SESSION] BINDING`](/sql-statements/sql-statement-create-binding.md)
+ [`CREATE DATABASE`](/sql-statements/sql-statement-create-database.md)
+ [`CREATE INDEX`](/sql-statements/sql-statement-create-index.md)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some newly added documents are missing in the TOC. They are added in #6766.

+ [`CREATE PLACEMENT POLICY`](/sql-statements/sql-statement-create-placement-policy.md)
+ [`CREATE ROLE`](/sql-statements/sql-statement-create-role.md)
+ [`CREATE SEQUENCE`](/sql-statements/sql-statement-create-sequence.md)
+ [`CREATE TABLE LIKE`](/sql-statements/sql-statement-create-table-like.md)
Expand Down Expand Up @@ -346,6 +348,7 @@
+ [`SHOW COLLATION`](/sql-statements/sql-statement-show-collation.md)
+ [`SHOW [FULL] COLUMNS FROM`](/sql-statements/sql-statement-show-columns-from.md)
+ [`SHOW CONFIG`](/sql-statements/sql-statement-show-config.md)
+ [`SHOW CREATE PLACEMENT POLICY`](/sql-statements/sql-statement-show-create-placement-policy.md)
+ [`SHOW CREATE SEQUENCE`](/sql-statements/sql-statement-show-create-sequence.md)
+ [`SHOW CREATE TABLE`](/sql-statements/sql-statement-show-create-table.md)
+ [`SHOW CREATE USER`](/sql-statements/sql-statement-show-create-user.md)
Expand All @@ -359,6 +362,8 @@
+ [`SHOW INDEXES [FROM|IN]`](/sql-statements/sql-statement-show-indexes.md)
+ [`SHOW KEYS [FROM|IN]`](/sql-statements/sql-statement-show-keys.md)
+ [`SHOW MASTER STATUS`](/sql-statements/sql-statement-show-master-status.md)
+ [`SHOW PLACEMENT`](/sql-statements/sql-statement-show-placement.md)
+ [`SHOW PLACEMENT FOR`](/sql-statements/sql-statement-show-placement-for.md)
+ [`SHOW PLUGINS`](/sql-statements/sql-statement-show-plugins.md)
+ [`SHOW PRIVILEGES`](/sql-statements/sql-statement-show-privileges.md)
+ [`SHOW [FULL] PROCESSSLIST`](/sql-statements/sql-statement-show-processlist.md)
Expand Down Expand Up @@ -426,6 +431,7 @@
+ [Views](/views.md)
+ [Partitioning](/partitioned-table.md)
+ [Character Set and Collation](/character-set-and-collation.md)
+ [Placement Rules in SQL](/placement-rules-in-sql.md)
+ System Tables
+ [`mysql`](/mysql-schema.md)
+ INFORMATION_SCHEMA
Expand Down
4 changes: 4 additions & 0 deletions configure-placement-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ aliases: ['/docs/dev/configure-placement-rules/','/docs/dev/how-to/configure/pla
>
> In the scenario of using TiFlash, the Placement Rules feature has been extensively tested and can be used in the production environment. Except for the scenario where TiFlash is used, using Placement Rules alone has not been extensively tested, so it is not recommended to enable this feature separately in the production environment.

> **Note:**
>
> TiDB v5.3.0 introduces an experimental support for [Placement Rules in SQL](/placement-rules-in-sql.md). This offers a more convenient way to configure the placement of tables and partitions. Placement Rules in SQL might replace placement configuration with PD in future releases.

Placement Rules is an experimental feature of the Placement Driver (PD) introduced in v4.0. It is a replica rule system that guides PD to generate corresponding schedules for different types of data. By combining different scheduling rules, you can finely control the attributes of any continuous data range, such as the number of replicas, the storage location, the host type, whether to participate in Raft election, and whether to act as the Raft leader.

## Rule system
Expand Down
80 changes: 80 additions & 0 deletions information-schema/information-schema-placement-rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
title: PLACEMENT_RULES
summary: Learn the `PLACEMENT_RULES` information_schema table.
---

# PLACEMENT_RULES

The `PLACEMENT_RULES` table provides information on all explicitly configured [Placement Rules in SQL](/placement-rules-in-sql.md). The information includes both placement policies and directly attached rules.

{{< copyable "sql" >}}

```sql
USE information_schema;
DESC placement_rules;
```

```sql
+----------------------+--------------+------+------+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+------+---------+-------+
| POLICY_ID | bigint(64) | NO | | NULL | |
| CATALOG_NAME | varchar(512) | NO | | NULL | |
| POLICY_NAME | varchar(5) | YES | | NULL | |
| SCHEMA_NAME | varchar(5) | YES | | NULL | |
| TABLE_NAME | varchar(5) | YES | | NULL | |
Comment thread
morgo marked this conversation as resolved.
| PARTITION_NAME | varchar(5) | YES | | NULL | |
| PRIMARY_REGION | varchar(5) | NO | | NULL | |
| REGIONS | varchar(5) | NO | | NULL | |
| CONSTRAINTS | varchar(5) | NO | | NULL | |
| LEADER_CONSTRAINTS | varchar(5) | NO | | NULL | |
| FOLLOWER_CONSTRAINTS | varchar(5) | NO | | NULL | |
| LEARNER_CONSTRAINTS | varchar(5) | NO | | NULL | |
| SCHEDULE | varchar(20) | NO | | NULL | |
| FOLLOWERS | bigint(64) | NO | | NULL | |
| LEARNERS | bigint(64) | NO | | NULL | |
+----------------------+--------------+------+------+---------+-------+
15 rows in set (0.00 sec)
```

## Examples

The `PLACEMENT_RULES` table only shows explicitly configured rules. To see the canonical version of placement rules (including placement policies attached to tables), use the statement `SHOW PLACEMENT` instead:

{{< copyable "sql" >}}

```sql
CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT) primary_region="us-east-1" regions="us-east-1";
CREATE PLACEMENT POLICY p1 primary_region="us-east-1" regions="us-east-1";
CREATE TABLE t3 (a INT) PLACEMENT POLICY=p1;
SHOW PLACEMENT; -- Includes t3.
SELECT * FROM information_schema.placement_rules; -- Does not include t3.
```

```sql
Query OK, 0 rows affected (0.09 sec)

Query OK, 0 rows affected (0.11 sec)

Query OK, 0 rows affected (0.08 sec)

Query OK, 0 rows affected (0.11 sec)

+---------------+------------------------------------------------+
| Target | Placement |
+---------------+------------------------------------------------+
| POLICY p1 | PRIMARY_REGION="us-east-1" REGIONS="us-east-1" |
| TABLE test.t2 | PRIMARY_REGION="us-east-1" REGIONS="us-east-1" |
| TABLE test.t3 | PRIMARY_REGION="us-east-1" REGIONS="us-east-1" |
+---------------+------------------------------------------------+
3 rows in set (0.00 sec)

+-----------+--------------+-------------+-------------+------------+----------------+----------------+-----------+-------------+--------------------+----------------------+---------------------+----------+-----------+----------+
| POLICY_ID | CATALOG_NAME | POLICY_NAME | SCHEMA_NAME | TABLE_NAME | PARTITION_NAME | PRIMARY_REGION | REGIONS | CONSTRAINTS | LEADER_CONSTRAINTS | FOLLOWER_CONSTRAINTS | LEARNER_CONSTRAINTS | SCHEDULE | FOLLOWERS | LEARNERS |
+-----------+--------------+-------------+-------------+------------+----------------+----------------+-----------+-------------+--------------------+----------------------+---------------------+----------+-----------+----------+
| 3 | def | p1 | NULL | NULL | NULL | us-east-1 | us-east-1 | | | | | | 0 | 0 |
| NULL | def | NULL | test | t2 | NULL | us-east-1 | us-east-1 | | | | | | 0 | 0 |
+-----------+--------------+-------------+-------------+------------+----------------+----------------+-----------+-------------+--------------------+----------------------+---------------------+----------+-----------+----------+
2 rows in set (0.00 sec)
```
1 change: 1 addition & 0 deletions information-schema/information-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Many `INFORMATION_SCHEMA` tables have a corresponding `SHOW` command. The benefi
| [`METRICS_SUMMARY`](/information-schema/information-schema-metrics-summary.md) | A summary of metrics extracted from Prometheus. |
| `METRICS_SUMMARY_BY_LABEL` | See `METRICS_SUMMARY` table. |
| [`METRICS_TABLES`](/information-schema/information-schema-metrics-tables.md) | Provides the PromQL definitions for tables in `METRICS_SCHEMA`. |
| [`PLACEMENT_RULES`](/information-schema/information-schema-placement-rules.md) | Provides information on all objects that have explicit placement rules assigned. |
| [`SEQUENCES`](/information-schema/information-schema-sequences.md) | The TiDB implementation of sequences is based on MariaDB. |
| [`SLOW_QUERY`](/information-schema/information-schema-slow-query.md) | Provides information on slow queries on the current TiDB server. |
| [`STATEMENTS_SUMMARY`](/statement-summary-tables.md) | Similar to performance_schema statement summary in MySQL. |
Expand Down
169 changes: 169 additions & 0 deletions placement-rules-in-sql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
---
title: Placement Rules in SQL
summary: Learn how to schedule placement of tables and partitions.
---

# Placement Rules in SQL

> **Warning:**
>
> Placement Rules in SQL is an experimental feature. The syntax might change before its GA, and there might also be bugs.
>
> If you understand the risks, you can enable this experiment feature by executing `SET GLOBAL tidb_enable_alter_placement = 1;`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a note like:

We recommend using this feature with the help of the official tidb team to help solve specific scenarios. Over-configuration of the placement plan will increase the difficulty of maintenance and troubleshooting, so reasonable planning is very necessary. Usually, the points that need to be considered, including disaster recovery issues, high availability issues, performance issues and so on.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @nolouch. Some comments in line:

We recommend using this feature with the help of the official tidb team to help solve specific scenarios. Over-configuration of the placement plan will increase the difficulty of maintenance and troubleshooting, so reasonable planning is very necessary.

To a large degree we removed over configuration as a possibility between the original proposal and the newer proposal. You can no longer specify multiple constraints; and syntactic sugar constraints are mutually exclusive with regular constraints. The only complex scenarios that remain are both FOLLOWER_CONSTRAINTS and CONSTRAINTS can apply, and a user could set a complex constraint in dictionary format.

Usually, the points that need to be considered, including disaster recovery issues, high availability issues, performance issues and so on.

We don't document this well in the current experimental version. Because it is experimental, users are not encouraged to use it in production, so high availability/DR did not seem relevant (yet).

I have an alternative suggestion:

We value your feedback when evaluating experimental features. Please feel free to create a GitHub issue or reach out to us on TiDB Slack. We are interested to both the problems you encounter and your intended use-cases.


Placement Rules allow you to configure where data will be stored in a TiKV cluster. This is useful for scenarios including optimizing a high availability strategy, ensuring that local copies of data will be available for local stale reads, and adhering to compliance requirements.
Comment thread
morgo marked this conversation as resolved.

## Specify placement options

To use Placement Rules in SQL, you need to specify one or more placement options in a SQL statement. To specify the Placement options, you can either use _direct placement_ or use a _placement policy_.

In the following example, both tables `t1` and `t2` have the same rules. `t1` is specified rules using a direct placement while `t2` is specified rules using a placement policy.

```sql
CREATE TABLE t1 (a INT) PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1";
CREATE PLACEMENT POLICY eastandwest PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-west-1";
CREATE TABLE t2 (a INT) PLACEMENT POLICY=eastandwest;
```

It is recommended to use placement policies for simpler rule management. When you change a placement policy (via [`ALTER PLACEMENT POLICY`](/sql-statements/sql-statement-alter-placement-policy.md)), the change automatically propagates to all database objects.

If you use direct placement options, you have to alter rules for each object (for example, tables and partitions).

`PLACEMENT POLICY` is not associated with any database schema and has the global scope. Therefore, assigning a placement policy does not require any additional privileges over the `CREATE TABLE` privilege.

## Option reference

> **Note:**
>
> Placement options depend on labels correctly specified in the configuration of each TiKV node. For example, the `PRIMARY_REGION` option depends on the `region` label in TiKV. To see a summary of all labels available in your TiKV cluster, use the statement [`SHOW PLACEMENT LABELS`](/sql-statements/sql-statement-show-placement-labels.md):
>
> ```sql
> mysql> show placement labels;
> +--------+----------------+
> | Key | Values |
> +--------+----------------+
> | disk | ["ssd"] |
> | region | ["us-east-1"] |
> | zone | ["us-east-1a"] |
> +--------+----------------+
> 3 rows in set (0.00 sec)
> ```

| Option Name | Description |
|----------------------------|------------------------------------------------------------------------------------------------|
| `PRIMARY_REGION` | Raft leaders are placed in stores that have the `region` label that matches the value of this option. |
| `REGIONS` | Raft followers are placed in stores that have the `region` label that matches the value of this option. |
| `SCHEDULE` | The strategy used to schedule the placement of followers. The value options are `EVEN` (default) or `MAJORITY_IN_PRIMARY`. |
| `FOLLOWERS` | The number of followers. For example, `FOLLOWERS=2` means that there will be 3 replicas of the data (2 followers and 1 leader). |

In addition to the placement options above, you can also use the advance configurations. For details, see [Advance placement](#advanced-placement).

| Option Name | Description |
|----------------------------|------------------------------------------------------------------------------------------------|
| `CONSTRAINTS` | A list of constraints that apply to all roles. For example, `CONSTRAINTS="[+disk=ssd]`. |
| `FOLLOWER_CONSTRAINTS` | A list of constraints that only apply to followers. |

## Examples

### Increase the number of replicas

The default configuration of [`max-replicas`](/pd-configuration-file.md#max-replicas) is `3`. To increase this for a specific set of tables, you can use a placement policy as follows:

```sql
CREATE PLACEMENT POLICY fivereplicas FOLLOWERS=4;
CREATE TABLE t1 (a INT) PLACEMENT POLICY=fivereplicas;
```

Note that the PD configuration includes the leader and follower count, thus 4 followers + 1 leader equals 5 replicas in total.

To expand on this example, you can also use `PRIMARY_REGION` and `REGIONS` placement options to describe the placement for the followers:

```sql
CREATE PLACEMENT POLICY eastandwest PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-east-2,us-west-1" SCHEDULE="MAJORITY_IN_PRIMARY" FOLLOWERS=4;
CREATE TABLE t1 (a INT) PLACEMENT POLICY=eastandwest;
```

The `SCHEDULE` option instructs TiDB on how to balance the followers.

The default schedule of `EVEN` ensures a balance of followers in all regions. You can use the `MAJORITY_IN_PRIMARY` schedule to ensure that enough followers are placed in the primary region (`us-east-1`) so that quorum can be achieved. If the primary region completely fails, you can the `MAJORITY_IN_PRIMARY` schedule for lower latency transactions at the expense of availability.

### Assign placement to a partitioned table

> **Note:**
>
> The following example uses list partitioning, which is currently an experimental feature of TiDB. Partitioned tables also require the `PRIMARY KEY` to be included in all columns in the table's partitioning function.

In addition to assigning placement options to tables, you can also assign the options to table partitions. For example:

```sql
CREATE PLACEMENT POLICY europe PRIMARY_REGION="eu-central-1" REGIONS="eu-central-1,eu-west-1";
CREATE PLACEMENT POLICY northamerica PRIMARY_REGION="us-east-1" REGIONS="us-east-1";

SET tidb_enable_list_partition = 1;
CREATE TABLE t1 (
country VARCHAR(10) NOT NULL,
userdata VARCHAR(100) NOT NULL
) PARTITION BY LIST COLUMNS (country) (
PARTITION pEurope VALUES IN ('DE', 'FR', 'GB') PLACEMENT POLICY=europe,
PARTITION pNorthAmerica VALUES IN ('US', 'CA', 'MX') PLACEMENT POLICY=northamerica
);
```

### Set the default placement for a schema

You can directly attach the default placement options to a database schema. This works similar to setting the default character set or collation for a schema. Your specified placement options apply when no other options are specified. For example:

```sql
CREATE TABLE t1 (a INT); -- Creates a table t1 with no placement options.

ALTER DATABASE test FOLLOWERS=4; -- Changes the default placement option, and does not apply to the existing table t1.

CREATE TABLE t2 (a INT); -- Creates a table t2 with the default placement of FOLLOWERS=4.

CREATE TABLE t3 (a INT) PRIMARY_REGION="us-east-1" REGIONS="us-east-1,us-east-2"; -- Creates a table t3 without the default FOLLOWERS=4 placement, because this statement has specified another placement.

ALTER DATABASE test FOLLOWERS=2; -- Changes the default placement, and does not apply to existing tables.

CREATE TABLE t4 (a INT); -- Creates a table t4 with the default FOLLOWERS=2 option.
```

Because placement options are only inherited from the database schema when a table is created, it is recommended to set the default placement option using a `PLACEMENT POLICY`. This ensures that future changes to the policy propagate to existing tables.

### Advanced placement

The placement options `PRIMARY_REGION`, `REGIONS`, and `SCHEDULE` meet the basic needs of data placement at the loss of some flexibility. For more complex scenarios with the need for higher flexibility, you can also use the advanced placement options of `CONSTRAINTS` and `FOLLOWER_CONSTRAINTS`. These two options are mutually exclusive. If you specify both at the same time, an error will be returned.

For example, to set constraints that data must reside on a TiKV store where the label `disk` must match a value:

```sql
CREATE PLACEMENT POLICY storeonfastssd CONSTRAINTS="[+disk=ssd]";
CREATE PLACEMENT POLICY storeonhdd CONSTRAINTS="[+disk=hdd]";
CREATE PLACEMENT POLICY companystandardpolicy CONSTRAINTS="";

CREATE TABLE t1 (id INT, name VARCHAR(50), purchased DATE)
PLACEMENT POLICY=companystandardpolicy
PARTITION BY RANGE( YEAR(purchased) ) (
PARTITION p0 VALUES LESS THAN (2000) PLACEMENT POLICY=storeonhdd,
PARTITION p1 VALUES LESS THAN (2005),
PARTITION p2 VALUES LESS THAN (2010),
PARTITION p3 VALUES LESS THAN (2015),
PARTITION p4 VALUES LESS THAN MAXVALUE PLACEMENT POLICY=storeonfastssd
);
```

You can either specify constraints in list format (`[+disk=ssd]`) or in dictionary format (`{+disk=ssd:1,+disk=hdd:2}`).

In list format, constraints are specified as a list of key-value pairs. The key starts with either a `+` or a `-` with `+disk=ssd` indicating that the label `disk` must be set to `ssd`, and `-disk=hdd`, indicating that the label `disk` must not be `hdd`.

In dictionary format, constraints also indicate a number of instances that apply to that rule. For example `FOLLOWER_CONSTRAINTS="{+region=us-east-1:1,+region=us-east-2:1,+region=us-west-1:1,+any:1}";` indicates that 1 follower is in us-east-1, 1 follower is in us-east-2, 1 follower is in us-west-1, and 1 follower can be in any region.

## Known limitations

The following known limitations exist in the experimental release of Placement Rules in SQL:

* Dumpling does not support dumping placement policies. See [issue #29371](https://github.com/pingcap/tidb/issues/29371).
* TiDB tools, including Backup & Restore (BR), TiCDC, TiDB Lightning, and TiDB Data Migration (DM), do not yet support placement rules.
* Temporary tables do not support placement options (either via direct placement or placement policies).
* Syntactic sugar rules exist for setting `PRIMARY_REGION` and `REGIONS`, but in future we plan to add varieties for `PRIMARY_RACK`, `PRIMARY_ZONE` and `PRIMARY_HOST`. See [issue #18030](https://github.com/pingcap/tidb/issues/18030)
Comment thread
TomShawn marked this conversation as resolved.
* TiFlash learners are not configurable through Placement Rules syntax.
* Placement rules only ensure that data at rest resides on the correct TiKV store. The rules do not guarantee that data in transit (via either user-queries or internal operations) only occurs in a specific region.
Loading