Open3

pg_duckdb v1.0のお試し

こばこば

まずはDockerで起動

 docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=duckdb pgduckdb/pgduckdb:17-v1.0.0

接続してExtensionなどを確認

postgres=# \dn
      List of schemas
  Name  |       Owner       
--------+-------------------
 duckdb | postgres
 public | pg_database_owner
(2 rows)

postgres=# \dx
                  List of installed extensions
   Name    | Version |   Schema   |         Description
-----------+---------+------------+------------------------------
 pg_duckdb | 1.0.0   | public     | DuckDB Embedded in Postgres
 plpgsql   | 1.0     | pg_catalog | PL/pgSQL procedural language
(2 rows)

postgres=# select * from pg_am;
  oid  | amname |      amhandler       | amtype
-------+--------+----------------------+--------
     2 | heap   | heap_tableam_handler | t
   403 | btree  | bthandler            | i
   405 | hash   | hashhandler          | i
   783 | gist   | gisthandler          | i
  2742 | gin    | ginhandler           | i
  4000 | spgist | spghandler           | i
  3580 | brin   | brinhandler          | i
 16416 | duckdb | duckdb._am_handler   | t
(8 rows)
こばこば

pg_duckdb v1.0の機能確認

https://motherduck.com/blog/pg-duckdb-release/

バージョン 1.0 では、MotherDuck の統合が強化され、より多くのデータ型のサポート、安定性の向上、並列テーブル スキャンを含むパフォーマンスの向上がもたらされます。

・Postgres テーブル値を並列に変換するためのサポートを追加します。これにより、大規模なスキャンを大幅に高速化できます。を使用して、使用するスレッドの数を構成できます。(#762duckdb.threads_for_postgres_scan)
・duckdbライブラリをpg_duckdb拡張機能に静的にコンパイルするためのサポートを追加します。(#618)
・型のサポートを追加します。(#532、#626、#627、#628、#636、#678、#689、#669DOMAINVARINTTIMETIMETZBITVARBITUNIONMAPSTRUCT)
・コミュニティ拡張機能のインストールのサポートを追加します。(#647)
・Postgres 18 リリース候補 1 のサポートを追加します。Postgres 18 にはまだ最終リリースがないため、これはまだ実験的な機能と見なされます。(#788)
・DuckDB テーブルへのサポートを追加しました。(#665COPY ... TOCOPY ... FROM)

・DuckDB へのシークレットの追加方法を変更します。新しいメソッドまたは関数を使用してシークレットを再作成する必要があります。内部的には、外部データラッパー用のシークレットが保存されるようになりました。(#697duckdb.create_simple_secretduckdb.create_azure_secretSERVERUSER MAPPINGduckdb)

そんなに大きな変更はない気がする。
並列テーブルスキャンがあるけど、これはどうやって使うんだ。

こばこば

DuckLakeも確認するためにDuckDB v1.4.0を準備

> ./duckdb
DuckDB v1.4.0 (Andium) b8a06e4a22
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.

D install ducklake;
100% ???????????????????????????????????????? (00:00:13.09 elapsed)     
D install postgres;
100% ???????????????????????????????????????? (00:00:19.71 elapsed)    

D SELECT extension_name, extension_version, installed_from, install_mode FROM duckdb_extensions();
┌──────────────────┬───────────────────┬────────────────┬───────────────────┐
│  extension_name  │ extension_version │ installed_from │   install_mode    │
│     varcharvarcharvarcharvarchar      │
├──────────────────┼───────────────────┼────────────────┼───────────────────┤
│ autocomplete     │ v1.4.0            │                │ STATICALLY_LINKED │
│ aws              │                   │                │ NOT_INSTALLED     │
│ azure            │                   │                │ NOT_INSTALLED     │
│ core_functions   │ v1.4.0            │                │ STATICALLY_LINKED │
│ delta            │                   │                │ NOT_INSTALLED     │
│ ducklake         │ 45788f0           │ core           │ REPOSITORY        │
│ encodings        │                   │                │ NOT_INSTALLED     │
│ excel            │                   │                │ NOT_INSTALLED     │
│ fts              │                   │                │ NOT_INSTALLED     │
│ httpfs           │                   │                │ NOT_INSTALLED     │
│ iceberg          │                   │                │ NOT_INSTALLED     │
│ icu              │ v1.4.0            │                │ STATICALLY_LINKED │
│ inet             │                   │                │ NOT_INSTALLED     │
│ jemalloc         │                   │                │ NOT_INSTALLED     │
│ json             │ v1.4.0            │                │ STATICALLY_LINKED │
│ motherduck       │                   │                │ NOT_INSTALLED     │
│ mysql_scanner    │                   │                │ NOT_INSTALLED     │
│ parquet          │ v1.4.0            │                │ STATICALLY_LINKED │
│ postgres_scanner │ f012a4f           │ core           │ REPOSITORY        │
│ shell            │ v1.4.0            │                │ STATICALLY_LINKED │
│ spatial          │                   │                │ NOT_INSTALLED     │
│ sqlite_scanner   │                   │                │ NOT_INSTALLED     │
│ tpcds            │                   │                │ NOT_INSTALLED     │
│ tpch             │                   │                │ NOT_INSTALLED     │
│ ui               │                   │                │ NOT_INSTALLED     │
│ vss              │                   │                │ NOT_INSTALLED     │
├──────────────────┴───────────────────┴────────────────┴───────────────────┤
│ 26 rows                                                         4 columns │
└───────────────────────────────────────────────────────────────────────────┘

DuckDBのExtensionのバージョンがちゃんと出ないのは何故?
DuckLakeのバージョンを確認したいのに。

DuckLakeの動作確認(ローカル)

今回はクラウドを使わずにローカルストレージにファイルを置くようにしてみる。

D ATTACH 'ducklake:postgres:dbname=local_ducklake_catalog user=postgres password=duckdb host=localhost' AS local_ducklake (DATA_PATH './data_files/');

D use local_ducklake;

D CREATE TABLE tokyo_koukinshunou AS SELECT * FROM read_csv_auto('https://www.opendata.metro.tokyo.lg.jp/kaikeikanri/koukinshuunou.csv',encoding='Shift_JIS');
D from tokyo_koukinshunou;
┌────────────────────────┬────────┬──────────────────────────────┬──────────────┬──────────────────────────────────────┐
│          区分          │ コード │          金融機関名          │ ペイジー対応 │                 備考                 │
│        varchar         │ int64  │           varcharvarcharvarchar                │
├────────────────────────┼────────┼──────────────────────────────┼──────────────┼──────────────────────────────────────┤
│ 都市銀行               │      1 │ みずほ銀行                   │ *           │ NULL                                 │
│ 都市銀行               │      9 │ 三井住友銀行                 │ *           │ NULL                                 │
│ 都市銀行               │      5 │ 三菱UFJ銀行               │ *           │ NULL                                 │
│ 都市銀行               │     10 │ りそな銀行                   │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    542 │ 愛知銀行                     │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    117 │ 青森銀行                     │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    119 │ 秋田銀行                     │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    129 │ 足利銀行                     │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    172 │ 阿波銀行                     │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    161 │ 池田泉州銀行                 │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    174 │ 伊予銀行                     │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    123 │ 岩手銀行                     │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    576 │ 愛媛銀行                     │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    183 │ 大分銀行                     │ NULLNULL                                 │
│ 地方銀行・第二地方銀行 │    152 │ 大垣共立銀行                 │ NULLNULL                                 │
│ 地方銀行・第二地方銀行 │    188 │ 沖縄銀行                     │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    185 │ 鹿児島銀行                   │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    554 │ 関西アーバン銀行             │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    191 │ 北九州銀行                   │ *           │ NULL                                 │
│ 地方銀行・第二地方銀行 │    509 │ 北日本銀行                   │ *           │ NULL                                 │
│    ・                   │     ・  │     ・                        │ ・            │  ・                                   │
│    ・                   │     ・  │     ・                        │ ・            │  ・                                   │
│    ・                   │     ・  │     ・                        │ ・            │  ・                                   │
│ 信用組合               │   2276 │ 東京都職員信用組合           │ NULLNULL                                 │
│ 信用組合               │   2210 │ 東浴信用組合                 │ NULLNULL                                 │
│ 信用組合               │   2235 │ 中ノ郷信用組合               │ *           │ NULL                                 │
│ 信用組合               │   2277 │ ハナ信用組合                 │ NULLNULL                                 │
│ 信用組合               │   2211 │ 文化産業信用組合             │ *           │ NULL                                 │
│ 農業協同組合           │   5039 │ 秋川農業協同組合             │ *           │ NULL                                 │
│ 農業協同組合           │   5095 │ 世田谷目黒農業協同組合       │ *           │ NULL                                 │
│ 農業協同組合           │   5097 │ 東京あおば農業協同組合       │ *           │ NULL                                 │
│ 農業協同組合           │   5100 │ 東京スマイル農業協同組合     │ *           │ NULL                                 │
│ 農業協同組合           │   5094 │ 東京中央農業協同組合         │ *           │ NULL                                 │
│ 農業協同組合           │   5072 │ 東京みどり農業協同組合       │ *           │ NULL                                 │
│ 農業協同組合           │   5055 │ 東京南農業協同組合           │ *           │ NULL                                 │
│ 農業協同組合           │   5077 │ 東京みらい農業協同組合       │ *           │ NULL                                 │
│ 農業協同組合           │   5087 │ 東京むさし農業協同組合       │ *           │ NULL                                 │
│ 農業協同組合           │   5037 │ 西多摩農業協同組合           │ *           │ NULL                                 │
│ 農業協同組合           │   5030 │ 西東京農業協同組合           │ *           │ NULL                                 │
│ 農業協同組合           │   5050 │ 八王子市農業協同組合         │ *           │ NULL                                 │
│ 農業協同組合           │   5070 │ マインズ農業協同組合         │ *           │ NULL                                 │
│ 農業協同組合           │   5060 │ 町田市農業協同組合           │ *           │ NULL                                 │
│ 農業協同組合           │   3013 │ 東京都信用農業協同組合連合会 │ *           │ 本店、八丈島代理店及び小笠原島代理店 │
├────────────────────────┴────────┴──────────────────────────────┴──────────────┴──────────────────────────────────────┤
│ 170 rows (40 shown)                                                                                        5 columns │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
D

PostgreSQLにも色々テーブルが出来ている

local_ducklake_catalog=# \d
                         List of relations
 Schema |                 Name                  | Type  |  Owner
--------+---------------------------------------+-------+----------
 public | ducklake_column                       | table | postgres
 public | ducklake_column_mapping               | table | postgres
 public | ducklake_column_tag                   | table | postgres
 public | ducklake_data_file                    | table | postgres
 public | ducklake_delete_file                  | table | postgres
 public | ducklake_file_column_stats            | table | postgres
 public | ducklake_file_partition_value         | table | postgres
 public | ducklake_files_scheduled_for_deletion | table | postgres
 public | ducklake_inlined_data_tables          | table | postgres
 public | ducklake_metadata                     | table | postgres
 public | ducklake_name_mapping                 | table | postgres
 public | ducklake_partition_column             | table | postgres
 public | ducklake_partition_info               | table | postgres
 public | ducklake_schema                       | table | postgres
 public | ducklake_schema_versions              | table | postgres
 public | ducklake_snapshot                     | table | postgres
 public | ducklake_snapshot_changes             | table | postgres
 public | ducklake_table                        | table | postgres
 public | ducklake_table_column_stats           | table | postgres
 public | ducklake_table_stats                  | table | postgres
 public | ducklake_tag                          | table | postgres
 public | ducklake_view                         | table | postgres
(22 rows)

local_ducklake_catalog=# select * from ducklake_data_file;
 data_file_id | table_id | begin_snapshot | end_snapshot | file_order |                         path                          | path_is_relative | file_format | record_count | file_size_bytes | footer_size | row_id_start | partition_id | encryption_key | partial_file_info | mapping_id
--------------+----------+----------------+--------------+------------+-------------------------------------------------------+------------------+-------------+--------------+-----------------+-------------+--------------+--------------+----------------+-------------------+------------
            0 |        1 |              1 |              |            | ducklake-01998464-3874-7d50-a1b9-340974a5a5ea.parquet | t                | parquet     |          170 |            4585 |        1114 |            0 |              |                |                   |
(1 row)

先ほど書き込んだParquetファイルも登録されている。