|
| 1 | +--- |
| 2 | +sidebar_label: Query-level Settings |
| 3 | +title: Query-level Settings |
| 4 | +slug: /en/operations/settings/query-level |
| 5 | +--- |
| 6 | + |
| 7 | +There are multiple ways to set ClickHouse query-level settings. Settings are configured in layers, and each subsequent layer redefines the previous values of a setting. |
| 8 | + |
| 9 | +The order of priority for defining a setting is: |
| 10 | + |
| 11 | +1. Applying a setting to a user directly, or within a settings profile |
| 12 | + |
| 13 | + - SQL (recommended) |
| 14 | + - adding one or more XML or YAML files to `/etc/clickhouse-server/users.d` |
| 15 | + |
| 16 | +2. Session settings |
| 17 | + |
| 18 | + - Send `SET setting=value` from the ClickHouse Cloud SQL console or |
| 19 | + `clickhouse client` in interactive mode. Similarly, you can use ClickHouse |
| 20 | + sessions in the HTTP protocol. To do this, you need to specify the |
| 21 | + `session_id` HTTP parameter. |
| 22 | + |
| 23 | +3. Query settings |
| 24 | + |
| 25 | + - When starting `clickhouse client` in non-interactive mode, set the startup |
| 26 | + parameter `--setting=value`. |
| 27 | + - When using the HTTP API, pass CGI parameters (`URL?setting_1=value&setting_2=value...`). |
| 28 | + - Define settings in the |
| 29 | + [SETTINGS](../../sql-reference/statements/select/index.md#settings-in-select-query) |
| 30 | + clause of the SELECT query. The setting value is applied only to that query |
| 31 | + and is reset to the default or previous value after the query is executed. |
| 32 | + |
| 33 | +## Examples |
| 34 | + |
| 35 | +These examples all set the value of the `async_insert` setting to `1`, and |
| 36 | +show how to examine the settings in a running system. |
| 37 | + |
| 38 | +### Using SQL to apply a setting to a user directly |
| 39 | + |
| 40 | +This creates the user `ingester` with the setting `async_inset = 1`: |
| 41 | + |
| 42 | +```sql |
| 43 | +CREATE USER ingester |
| 44 | +IDENTIFIED WITH sha256_hash BY '7e099f39b84ea79559b3e85ea046804e63725fd1f46b37f281276aae20f86dc3' |
| 45 | +# highlight-next-line |
| 46 | +SETTINGS async_insert = 1 |
| 47 | +``` |
| 48 | + |
| 49 | +#### Examine the settings profile and assignment |
| 50 | + |
| 51 | +```sql |
| 52 | +SHOW ACCESS |
| 53 | +``` |
| 54 | + |
| 55 | +```response |
| 56 | +┌─ACCESS─────────────────────────────────────────────────────────────────────────────┐ |
| 57 | +│ ... │ |
| 58 | +# highlight-next-line |
| 59 | +│ CREATE USER ingester IDENTIFIED WITH sha256_password SETTINGS async_insert = true │ |
| 60 | +│ ... │ |
| 61 | +└────────────────────────────────────────────────────────────────────────────────────┘ |
| 62 | +``` |
| 63 | +### Using SQL to create a settings profile and assign to a user |
| 64 | + |
| 65 | +This creates the profile `log_ingest` with the setting `async_inset = 1`: |
| 66 | + |
| 67 | +```sql |
| 68 | +CREATE |
| 69 | +SETTINGS PROFILE log_ingest SETTINGS async_insert = 1 |
| 70 | +``` |
| 71 | + |
| 72 | +This creates the user `ingester` and assigns the user the settings profile `log_ingest`: |
| 73 | + |
| 74 | +```sql |
| 75 | +CREATE USER ingester |
| 76 | +IDENTIFIED WITH sha256_hash BY '7e099f39b84ea79559b3e85ea046804e63725fd1f46b37f281276aae20f86dc3' |
| 77 | +# highlight-next-line |
| 78 | +SETTINGS PROFILE log_ingest |
| 79 | +``` |
| 80 | + |
| 81 | + |
| 82 | +### Using XML to create a settings profile and user |
| 83 | + |
| 84 | +```xml title=/etc/clickhouse-server/users.d/users.xml |
| 85 | +<clickhouse> |
| 86 | +# highlight-start |
| 87 | + <profiles> |
| 88 | + <log_ingest> |
| 89 | + <async_insert>1</async_insert> |
| 90 | + </log_ingest> |
| 91 | + </profiles> |
| 92 | +# highlight-end |
| 93 | + |
| 94 | + <users> |
| 95 | + <ingester> |
| 96 | + <password_sha256_hex>7e099f39b84ea79559b3e85ea046804e63725fd1f46b37f281276aae20f86dc3</password_sha256_hex> |
| 97 | +# highlight-start |
| 98 | + <profile>log_ingest</profile> |
| 99 | +# highlight-end |
| 100 | + </ingester> |
| 101 | + <default replace="true"> |
| 102 | + <password_sha256_hex>7e099f39b84ea79559b3e85ea046804e63725fd1f46b37f281276aae20f86dc3</password_sha256_hex> |
| 103 | + <access_management>1</access_management> |
| 104 | + <named_collection_control>1</named_collection_control> |
| 105 | + </default> |
| 106 | + </users> |
| 107 | +</clickhouse> |
| 108 | +``` |
| 109 | + |
| 110 | +#### Examine the settings profile and assignment |
| 111 | + |
| 112 | +```sql |
| 113 | +SHOW ACCESS |
| 114 | +``` |
| 115 | + |
| 116 | +```response |
| 117 | +┌─ACCESS─────────────────────────────────────────────────────────────────────────────┐ |
| 118 | +│ CREATE USER default IDENTIFIED WITH sha256_password │ |
| 119 | +# highlight-next-line |
| 120 | +│ CREATE USER ingester IDENTIFIED WITH sha256_password SETTINGS PROFILE log_ingest │ |
| 121 | +│ CREATE SETTINGS PROFILE default │ |
| 122 | +# highlight-next-line |
| 123 | +│ CREATE SETTINGS PROFILE log_ingest SETTINGS async_insert = true │ |
| 124 | +│ CREATE SETTINGS PROFILE readonly SETTINGS readonly = 1 │ |
| 125 | +│ ... │ |
| 126 | +└────────────────────────────────────────────────────────────────────────────────────┘ |
| 127 | +``` |
| 128 | + |
| 129 | +### Assign a setting to a session |
| 130 | + |
| 131 | +```sql |
| 132 | +SET async_insert =1; |
| 133 | +SELECT value FROM system.settings where name='async_insert'; |
| 134 | +``` |
| 135 | + |
| 136 | +```response |
| 137 | +┌─value──┐ |
| 138 | +│ 1 │ |
| 139 | +└────────┘ |
| 140 | +``` |
| 141 | + |
| 142 | +### Assign a setting during a query |
| 143 | + |
| 144 | +```sql |
| 145 | +INSERT INTO YourTable |
| 146 | +# highlight-next-line |
| 147 | +SETTINGS async_insert=1 |
| 148 | +VALUES (...) |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +## Converting a Setting to its Default Value |
| 153 | + |
| 154 | +If you change a setting and would like to revert it back to its default value, set the value to `DEFAULT`. The syntax looks like: |
| 155 | + |
| 156 | +```sql |
| 157 | +SET setting_name = DEFAULT |
| 158 | +``` |
| 159 | + |
| 160 | +For example, the default value of `async_insert` is `0`. Suppose you change its value to `1`: |
| 161 | + |
| 162 | +```sql |
| 163 | +SET async_insert = 1; |
| 164 | + |
| 165 | +SELECT value FROM system.settings where name='async_insert'; |
| 166 | +``` |
| 167 | + |
| 168 | +The response is: |
| 169 | + |
| 170 | +```response |
| 171 | +┌─value──┐ |
| 172 | +│ 1 │ |
| 173 | +└────────┘ |
| 174 | +``` |
| 175 | + |
| 176 | +The following command sets its value back to 0: |
| 177 | + |
| 178 | +```sql |
| 179 | +SET async_insert = DEFAULT; |
| 180 | + |
| 181 | +SELECT value FROM system.settings where name='async_insert'; |
| 182 | +``` |
| 183 | + |
| 184 | +The setting is now back to its default: |
| 185 | + |
| 186 | +```response |
| 187 | +┌─value───┐ |
| 188 | +│ 0 │ |
| 189 | +└─────────┘ |
| 190 | +``` |
| 191 | + |
| 192 | +## Custom Settings {#custom_settings} |
| 193 | + |
| 194 | +In addition to the common [settings](../../operations/settings/settings.md), users can define custom settings. |
| 195 | + |
| 196 | +A custom setting name must begin with one of predefined prefixes. The list of these prefixes must be declared in the [custom_settings_prefixes](../../operations/server-configuration-parameters/settings.md#custom_settings_prefixes) parameter in the server configuration file. |
| 197 | + |
| 198 | +```xml |
| 199 | +<custom_settings_prefixes>custom_</custom_settings_prefixes> |
| 200 | +``` |
| 201 | + |
| 202 | +To define a custom setting use `SET` command: |
| 203 | + |
| 204 | +```sql |
| 205 | +SET custom_a = 123; |
| 206 | +``` |
| 207 | + |
| 208 | +To get the current value of a custom setting use `getSetting()` function: |
| 209 | + |
| 210 | +```sql |
| 211 | +SELECT getSetting('custom_a'); |
| 212 | +``` |
| 213 | + |
| 214 | +**See Also** |
| 215 | + |
| 216 | +- View the [Settings](./settings.md) page for a description of the ClickHouse settings. |
| 217 | +- [Global server settings](../../operations/server-configuration-parameters/settings.md) |
0 commit comments