[Clickhouse] Engine: PostgreSQL
clickhouse๋ ๊ธฐ๋ณธ์ ์ผ๋ก 1์ฐจ ์ ์ฅ์๋ผ๊ธฐ๋ณด๋ค๋, ๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ๋ถ์ด์ ๋น ๋ฅด๊ฒ ๋ถ์ํ๊ธฐ ์ํ ๋ถ์์ฉ DB์ ๊ฐ๊น๋ค. ๊ทธ๋์ ๋ค๋ฅธ DB์์ ์์ค ํตํฉ์ ๊ฝค ์ ์ง์ํ๋ ํธ์ด๋ค. PostgreSQL ์์ค๋ฅผ ์์ง์ ํํ๋ก ์ ๊ณตํ๋ค.
๊ทธ๋ฅ ์ด๋ฐ ์์ผ๋ก ๋ง๋ค๋ฉด, clickhouse๊ฐ ์์์ ์๋ณธ DB์์ ๊ฐ์ ธ์์ ์ฟผ๋ฆฌ๋ฅผ ์์์ค๋ค.
CREATE TABLE default.postgresql_table
(
`float_nullable` Nullable(Float32),
`str` String,
`int_id` Int32
)
ENGINE = PostgreSQL('localhost', 'db๋ช
', '์๋ณธํ
์ด๋ธ', 'username', 'password');ํ๊ณ
์ด๊ฑด ์ค์ ๋ก clickhouse์ ๋ฐ์ดํฐ๋ฅผ ๋ถ์ด์ฃผ๋๊ฒ ์๋๋ผ, ๋์ ์ด์ฃผ๋ ๊ฒ์ ๋ถ๊ณผํ๋ค.
๊ทธ๋ฅ ์ค์๊ฐ์ผ๋ก PostgreSQL์ ์ฟผ๋ฆฌ๋ฅผ ์ด์ clickhouse ๋ด์์ ์ฐ์ฐ์ ํ๋ ๊ฒ์ด๋ผ์, PostgreSQL์ ์๋ณธ ๋ฐ์ดํฐ ํฌ๊ธฐ๊ฐ ํฌ๋ค๋ฉด ๋ฐ๋ผ์ ๋๋ ค์ง๋ค. ๋ด๋ถ์ ์ผ๋ก๋ COPY(SELECT ...)์ ์ฟผ๋ฆฌ๋ฅผ ๋ ๋ ค์ ๊ฐ์ ธ์จ๋ค.

PostgreSQL์ clickhouse๋ก ๊ฐ์ ธ์์ ๊ณ ์ฑ๋ฅ ์ฟผ๋ฆฌ๋ฅผ ๋ ๋ฆฌ๊ณ ์ถ๋ค๋ฉด, ์ง์ CDC๋ก ๊ฐ์ ธ์ค๊ฑฐ๋ ์คํ์ ๋์์ธ MaterializedPostgreSQL๋ฅผ ์จ์ผ ํ๋ค.
https://blog.naver.com/sssang97/223918168727
๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ
ํ๋ฒ ์จ๋ณด์.
์ด๋ฐ ๋จ์ํ key-valueํ ํ
์ด๋ธ์ด ์๋ค๊ณ ๊ฐ์ ํด๋ณด๊ฒ ๋ค.

CREATE TABLE public.global_state (
id bigserial NOT NULL,
created_at timestamptz NULL,
updated_at timestamptz NULL,
deleted_at timestamptz NULL,
"key" text NULL,
value text NULL,
CONSTRAINT global_state_pkey PRIMARY KEY (id)
);
์ ์ ์์ฒด๋ ๊ฐ๋จํ๋ค.
์ด๊ฑธ ๋ค์ clickhouse ํํ์ ๋ง์ถฐ์ ์ฌ์ ์ํด์ฃผ๊ณ
CREATE TABLE acloset_qa.global_state
(
id bigserial NOT NULL,
created_at timestamptz NULL,
updated_at timestamptz NULL,
deleted_at timestamptz NULL,
"key" text NULL,
value text NULL,
)
ENGINE = PostgreSQL('์๋ํฌ์ธํธ', 'db๋ช
', '์๋ณธํ
์ด๋ธ', 'username', 'password');
์์ง์๋ค๊ฐ DB ์ ์์ ๋ณด๋ง ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
๊ทธ๋ฌ๋ฉด clickhouse๊ฐ ์ ์ ๋ณด๋ก ์ ์ํด์ ์์์ ๊ฐ์ ธ์ค๋ ๊ฒ์ด๋ค.
๊ทธ๋ฌ๊ณ ๋ค์ด๊ฐ๋ณด๋ฉด

๋ฐ์ดํฐ๋ ๋ค ์ ๋ค์ด๊ฐ์๊ณ
๋ฐ์ดํฐ๋ฅผ ์๋ก ๋ฃ์ผ๋ฉด

๋ง์ถฐ์ ๋ฐ์๋๊ณ
์์ ํด๋

์์์ ๋ฐ์ํด์ค ๊ฒ์ด๋ค.
๊ทธ๋๋ก ์ ์ฐ๋ฉด ๋๋ค.
named collection ๊ธฐ๋ฅ
๊ทผ๋ฐ ์์์ ์ฌ์ฉํ ์์ฑ ๋ฐฉ๋ฒ์ ํ
์ด๋ธ์ ๋ง๋ค๋๋ง๋ค ์ ์์ ๋ณด๋ฅผ ๊ณ์ ๋ฐ๋ณต์ ์ผ๋ก, ์ค๋ณต๋๊ฒ ์
๋ ฅํด์ผ ํ๋ค๋ ๋จ์ ์ด ์๋ค.
named collection์ ์ฐ๋ฉด ์ ์์ ๋ณด๋ฅผ ๋ฏธ๋ฆฌ ์ ์ํด๋๊ณ ์ฌ์ฌ์ฉํ ์ ์๋ค.
/etc/clickhouse-server/config.d/named_collections.xml ๊ฐ์ ํ์ผ์ ๋ง๋ค๊ณ
<clickhouse>
<named_collections>
<mypg>
<host>localhost</host>
<port>5432</port>
<user>postgres</user>
<password>****</password>
<schema>schema1</schema>
</mypg>
</named_collections>
<clickhouse>
์ด๋ฐ ์์ผ๋ก ์
๋ ฅํด์ค ๋ค์, clickhouse ์๋ฒ๋ฅผ ์ฌ์์ํ๋ฉด ๋๋ค.
mypg๊ฐ ์ดํ์ ์ฌ์ฉํ ์ ์๋ ์ ์์ ๋ณด์ ์ด๋ฆ์ด๋ค.
๊ทธ๋ฌ๋ฉด ๊ทธ๋ฅ ๋ฐ๋ก postgresql ํจ์๋ก ํธ์ถํด์ ์ฌ์ฉํ ๋๋
SELECT * FROM postgresql(mypg, table='table1');
์ด๋ฐ ์์ผ๋ก ์ธ ์ ์๊ณ
ํ ์ด๋ธ์ ์์ฑํ ๋๋
CREATE TABLE mypgtable (
...
)
ENGINE = PostgreSQL(mypg, table = 'test', schema = 'public');
์ด๋ ๊ฒ ๊ฐ๋จํ๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค.
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์ฒด ๋ฏธ๋ฌ๋ง
ํ
์ด๋ธ์ ํ๋ํ๋ ์ ์ํด์ ์ฎ๊ธฐ๊ธฐ ๊ท์ฐฎ๋ค๋ฉด, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์ฒด๋ฅผ ๋ฏธ๋ฌ๋งํ๋ ๋ฐฉ๋ฒ๋ ์๋ค.
๊ทธ๋ฅ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ง๋ค๋ ์์ง ์ต์
์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
CREATE database DB๋ช
ENGINE = PostgreSQL('์๋ํฌ์ธํธ', 'DBNAME', 'user', 'password');
๊ทธ๋ผ ํด๋น ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฒด ํ ์ด๋ธ์ ์๋์ผ๋ก ์ฐ๊ฒฐํด์ค๋ค.
์ฐธ์กฐ
https://clickhouse.com/docs/integrations/postgresql
https://clickhouse.com/docs/operations/named-collections#example-of-using-named-collections-with-the-postgresql-function
https://clickhouse.com/docs/engines/table-engines/integrations/postgresql
https://clickhouse.com/docs/engines/database-engines/postgresql