설정에 대한 제약(Constraints on Settings)

설정에 대한 제약(Constraints on Settings)

ClickHouse의 "설정 제약"은 설정에 부여할 수 있는 제한과 규칙을 말하며, 데이터베이스의 안정성과 보안, 예측 가능한 동작을 유지하는 데 사용됩니다. 이 문서에서는 user.xml에서 제약을 정의하는 방법과 각 제약 유형을 설명할게요.

출처: 문서

본문

개요

ClickHouse에서 "설정에 대한 제약(constraints on settings)"은 설정에 부여할 수 있는 제한과 규칙을 말합니다. 이러한 제약은 데이터베이스의 안정성, 보안, 예측 가능한 동작을 유지하기 위해 적용할 수 있습니다.

제약 정의하기

설정에 대한 제약은 user.xml 설정 파일의 profiles 섹션에서 정의할 수 있습니다. 이들은 사용자가 SET 문으로 일부 설정을 변경하는 것을 금지합니다. 제약은 다음과 같이 정의됩니다:

<profiles>
  <user_name>
    <constraints>
      <setting_name_1>
        <min>lower_boundary</min>
      </setting_name_1>
      <setting_name_2>
        <max>upper_boundary</max>
      </setting_name_2>
      <setting_name_3>
        <min>lower_boundary</min>
        <max>upper_boundary</max>
      </setting_name_3>
      <setting_name_4>
        <readonly/>
      </setting_name_4>
      <setting_name_5>
        <min>lower_boundary</min>
        <max>upper_boundary</max>
        <changeable_in_readonly/>
      </setting_name_5>
      <setting_name_6>
        <min>lower_boundary</min>
        <max>upper_boundary</max>
        <disallowed>value1</disallowed>
        <disallowed>value2</disallowed>
        <disallowed>value3</disallowed>
        <changeable_in_readonly/>
      </setting_name_6>
    </constraints>
  </user_name>
</profiles>

사용자가 제약을 위반하려 하면 예외가 발생하고 설정은 변경되지 않습니다.

제약 유형

ClickHouse에서 지원하는 제약 유형은 몇 가지입니다:

  • min
  • max
  • disallowed
  • readonly (별칭 const)
  • changeable_in_readonly

minmax 제약은 숫자 설정에 대한 상한과 하한을 지정하며 서로 함께 사용할 수 있습니다. disallowed 제약은 특정 설정에 대해 허용되지 않아야 하는 특정 값(들)을 지정하는 데 사용됩니다. readonly 또는 const 제약은 사용자가 해당 설정을 전혀 변경할 수 없다는 것을 지정합니다. changeable_in_readonly 제약 유형은 readonly 설정이 1로 설정되어 있어도 사용자가 min/max 범위 내에서 설정을 변경할 수 있게 하며, 그렇지 않으면 readonly=1 모드에서는 설정 변경이 허용되지 않습니다.

changeable_in_readonlysettings_constraints_replace_previous가 활성화된 경우에만 지원됩니다:

<access_control_improvements>
  <settings_constraints_replace_previous>true</settings_constraints_replace_previous>
</access_control_improvements>

읽기 전용 모드에서 readonly를 변경 가능하게 만들지 마세요

모든 프로필에서 readonlychangeable_in_readonly 목록에서 제외시키고, 프로필 제한이 사용자별이 아니라 시스템 전체에 적용된다는 점을 기억하세요.

readonly 자체를 changeable_in_readonly로 표시하지 마세요. 그렇게 하면 readonly = 1로 시작한 세션이 SET readonly = 0을 실행하여 기존 권한이 허용하는 쓰기 쿼리를 다시 실행할 수 있게 됩니다. 이것은 사용자에게 할당된 프로필뿐만 아니라 어디서든 중요합니다:

  • SET profile을 사용해 어떤 세션이든 임의의 프로필로 전환할 수 있습니다. 프로필 선택은 접근 검사되지 않기 때문입니다(이미 활성화된 설정 제약에 의해서만 제한됩니다).
  • HTTP에서 GET 요청은 유효 값이 그렇지 않으면 0이 될 때만 readonly = 2로 강제됩니다. 따라서 readonly = 1을 설정하면서 readonly를 읽기 전용 모드에서 변경 가능하게 하는 프로필은 그 보호를 무력화합니다 — GET 요청이 그것을 0으로 뒤집고 쓰기를 수행할 수 있습니다.

여러 제약 프로필

사용자에 대해 여러 프로필이 활성화되어 있으면 제약이 병합됩니다. 병합 과정은 settings_constraints_replace_previous에 따라 달라집니다:

  • true (권장): 병합 중 같은 설정에 대한 제약이 교체되어 마지막 제약이 사용되고 이전 제약은 모두 무시됩니다. 새 제약에 설정되지 않은 필드도 포함됩니다.
  • false (기본): 같은 설정에 대한 제약이, 설정되지 않은 제약 유형은 이전 프로필에서 가져오고 설정된 제약 유형은 새 프로필의 값으로 교체되는 방식으로 병합됩니다.

읽기 전용 모드

읽기 전용 모드는 readonly 설정에 의해 활성화됩니다. 이것은 readonly 제약 유형과 혼동해서는 안 됩니다. readonly = 1일 때, 그렇지 않으면 거부되는 설정이라도 changeable_in_readonly 제약이 허용하면 변경될 수 있습니다. 설정 값의 의미에 대해서는 설정 참조를 참고하고, 각 값이 허용하는 쿼리 클래스와 HTTP 인터페이스가 어떻게 설정하는지에 대해서는 쿼리에 대한 권한을 참고하세요.

예제

users.xml에 다음 줄이 포함되어 있다고 가정해 보겠습니다:

<profiles>
  <default>
    <max_memory_usage>10000000000</max_memory_usage>
    <force_index_by_date>0</force_index_by_date>
    ...
    <constraints>
      <max_memory_usage>
        <min>5000000000</min>
        <max>20000000000</max>
      </max_memory_usage>
      <force_index_by_date>
        <readonly/>
      </force_index_by_date>
    </constraints>
  </default>
</profiles>

다음 쿼리들은 모두 예외를 발생시킵니다:

SET max_memory_usage=20000000001;
SET max_memory_usage=4999999999;
SET force_index_by_date=1;
Code: 452, e.displayText() = DB::Exception: Setting max_memory_usage should not be greater than 20000000000.
Code: 452, e.displayText() = DB::Exception: Setting max_memory_usage should not be less than 5000000000.
Code: 452, e.displayText() = DB::Exception: Setting force_index_by_date should not be changed.

default 프로필은 특별하게 처리됩니다. default 프로필에 정의된 모든 제약이 기본 제약이 되어, 해당 사용자에게 명시적으로 재정의하기 전까지 모든 사용자를 제한합니다.

MergeTree 설정에 대한 제약

머지 트리 설정(merge tree settings)에 대한 제약을 설정하는 것도 가능합니다. 이 제약들은 MergeTree 엔진이 있는 테이블이 생성되거나 스토리지 설정이 변경될 때 적용됩니다. <constraints> 섹션에서 참조할 때 머지 트리 설정의 이름은 merge_tree_ 접두사로 시작해야 합니다.

예제

storage_policy를 명시적으로 지정한 새 테이블 생성을 금지할 수 있습니다:

<profiles>
  <default>
    <constraints>
      <merge_tree_storage_policy>
        <const/>
      </merge_tree_storage_policy>
    </constraints>
  </default>
</profiles>

더 알아보기 (Learn more)