当前位置:   article > 正文

PgSQL - 内核特性 - 把DuckDB弄进来怎么样

PgSQL - 内核特性 - 把DuckDB弄进来怎么样

PgSQL - 内核特性 - 把DuckDB弄进来怎么样

DuckDB是一款高性能的分析型数据库系统,支持了基于Push-based pipeline的向量化执行引擎。这么好的一款数据库,有办法直接弄到PgSQL里面,以利用其优秀的列式存储、向量化执行引擎等优秀特性吗?Hydra团队开源了一款插件pg_quack,将duckdb以表访问方法的方式加到PgSQL中,为PgSQL提供了新的存储引擎以及执行引擎。

1、执行器及表访问方法hook接入

dea19d1414918b1b156b649818dc12d5.png

1)增加了一个配置项quack.data_dir,用于指定duckdb表的存访目录

2)quack_init_tableam初始化duckdb表访问方法的hook

3)quack_init_hooks初始化duckdb执行器的hook

2、接入执行器的hook

f9724b229387c80a714f9e8e4df14f7f.png

包括两个hook,一个用于DML等查询,一个用于DDL等操作。

3、DML的hook

通过duckdb api来进行操作。由于duckdb是嵌入式,所以只需要将duckdb的lib库和头文件加进来就可以使用。

  1. quack_executor_run->
  2. duckdb_database db = quack_open_database(MyDatabaseId, false);
  3. duckdb_connection connection = quack_open_connection(db);
  4. duckdb_query(connection, queryDesc->sourceText, &result)
  5. slot = MakeTupleTableSlot(queryDesc->tupDesc, &TTSOpsHeapTuple);
  6. row_count = duckdb_row_count(&result);//行数
  7. column_count = duckdb_column_count(&result);//列数
  8. for(idx_t row = 0; row < row_count; row++){
  9. ExecClearTuple(slot);
  10. for(idx_t col = 0; col < column_count; col++){
  11. if (duckdb_value_is_null(&result, col, row))
  12. slot->tts_isnull[col] = true;
  13. else{
  14. slot->tts_isnull[col] = false;
  15. quack_read_result(slot, &result, col, row);
  16. }
  17. }
  18. ExecStoreVirtualTuple(slot);
  19. dest->receiveSlot(slot, dest);//一行发送给用户
  20. 释放该行的所有列
  21. }
  22. ...

4、DDL的hook

  1. quack_process_utility->quack_execute_query:使用duckdb api执行
  2. //database目录quack_data_dir/"databaseOid".duckdb
  3. db = quack_open_database(MyDatabaseId, true);//创建/打开一个新的database
  4. duckdb_connect(db, &connection)//连接该database
  5. duckdb_query(connection, query, NULL);//执行
  6. duckdb_disconnect(&connection);//断开连接

主要利用duckdb的API进行操作。

5、duckdb表访问方法的hook

821f149c0b5ce6327c452a7789171f39.png

表访问方法如下:

  1. static const TableAmRoutine quack_am_methods = {
  2. .type = T_TableAmRoutine,
  3. .slot_callbacks = quack_slot_callbacks,
  4. .scan_begin = quack_begin_scan,
  5. .scan_end = quack_end_scan,
  6. .scan_rescan = quack_rescan,
  7. .scan_getnextslot = quack_getnextslot,
  8. .parallelscan_estimate = quack_parallelscan_estimate,
  9. .parallelscan_initialize = quack_parallelscan_initialize,
  10. .parallelscan_reinitialize = quack_parallelscan_reinitialize,
  11. .index_fetch_begin = quack_index_fetch_begin,
  12. .index_fetch_reset = quack_index_fetch_reset,
  13. .index_fetch_end = quack_index_fetch_end,
  14. .index_fetch_tuple = quack_index_fetch_tuple,
  15. .tuple_fetch_row_version = quack_fetch_row_version,
  16. .tuple_tid_valid = quack_tuple_tid_valid,
  17. .tuple_get_latest_tid = quack_get_latest_tid,
  18. .tuple_satisfies_snapshot = quack_tuple_satisfies_snapshot,
  19. .index_delete_tuples = quack_index_delete_tuples,
  20. .tuple_insert = quack_tuple_insert,
  21. .tuple_insert_speculative = quack_tuple_insert_speculative,
  22. .tuple_complete_speculative = quack_tuple_complete_speculative,
  23. .multi_insert = quack_multi_insert,
  24. .tuple_delete = quack_tuple_delete,
  25. .tuple_update = quack_tuple_update,
  26. .tuple_lock = quack_tuple_lock,
  27. .finish_bulk_insert = quack_finish_bulk_insert,
  28. .relation_set_new_filenode = quack_relation_set_new_filenode,
  29. .relation_nontransactional_truncate = quack_relation_nontransactional_truncate,
  30. .relation_copy_data = quack_relation_copy_data,
  31. .relation_copy_for_cluster = quack_relation_copy_for_cluster,
  32. .relation_vacuum = quack_vacuum_rel,
  33. .scan_analyze_next_block = quack_scan_analyze_next_block,
  34. .scan_analyze_next_tuple = quack_scan_analyze_next_tuple,
  35. .index_build_range_scan = quack_index_build_range_scan,
  36. .index_validate_scan = quack_index_validate_scan,
  37. .relation_size = quack_relation_size,
  38. .relation_needs_toast_table = quack_relation_needs_toast_table,
  39. .relation_estimate_size = quack_estimate_rel_size,
  40. .scan_bitmap_next_block = NULL,
  41. .scan_bitmap_next_tuple = NULL,
  42. .scan_sample_next_block = quack_scan_sample_next_block,
  43. .scan_sample_next_tuple = quack_scan_sample_next_tuple
  44. };

这样就将duckdb以表访问方法的方式加入PgSQL了

参考

https://github.com/hydradatabase/pg_quack

https://github.com/duckdb/duckdb/

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号