啪啪打脸了,原来开发者才是被精准降维打击的目标!
文中参考文档点击阅读原文打开, 同时推荐2个学习环境:
1、懒人Docker镜像, 已打包200+插件:《最好的PostgreSQL学习镜像》
2、有web浏览器就能用的云起实验室: 《免费体验PolarDB开源数据库》
3、PolarDB开源数据库内核、最佳实践等学习图谱: https://www.aliyun.com/database/openpolardb/activity
22B大模型Codestral,擅长代码生成,python表现最好
昨天发的文章啪啪打脸了《DBA岗位正遭遇降维攻击,胜算几乎为0!》,原来开发者才是遭受精准降维打击的目标,甚至有网友评论可能普通开发者比DBA更快消失!!! 因为市面上还没有专门针对数据库训练的模型, 但是针对代码生成的模型真不少!!!
背景
Codestral:22B大模型, 应该是我的小破电脑(macmini m2 16G内存)的能运行的极限模型了, 运行时需要12 GB内存.
对比几个模型后发现Codestral凭借参数多, 而且它是针对生成代码来训练的, 效果还真不错. 我用它来生成了一个PostgreSQL的插件.
模型地址:
https://ollama.com/library/codestral
Codestral 介绍
https://mistral.ai/news/codestral/
Codestral 是mistral.ai发布的一个开放式生成式 AI 模型,专门为代码生成任务而设计。它帮助开发人员通过共享指令和完成 API 编写和与代码交互。由于它精通代码和英语,因此可用于为软件开发人员设计高级 AI 应用程序。
Codestral 是精通 80 多种编程语言的模型
Codestral 经过了 80 多种编程语言的多样化数据集训练,包括最流行的语言,例如 Python、Java、C、C++、JavaScript 和 Bash。它在 Swift 和 Fortran 等更具体的语言上也表现良好。这种广泛的语言基础确保 Codestral 能够在各种编码环境和项目中为开发人员提供帮助。
Codestral 为开发人员节省了时间和精力:它可以完成编码功能、编写测试并使用中间填充机制完成任何部分代码。与 Codestral 交互将有助于提高开发人员的编码水平并降低出现错误和错误的风险。
不过从评测数据来看, 表现最好的应该是python.
生成了一个PostgreSQL的插件实测
反复试了很多提示词, 这个可能是最满意的, 要求和过程都描述得比较清晰.
你是一位大师级的PostgreSQL内核开发者, 熟悉PostgreSQL源码、C语言编程、Linux系统原理等.
请编写一个PostgreSQL 14版本的插件, 包含完整的Makefile, .control, .sql, .c文件的详细内容.
插件功能要求如下:
1、统计每个时间窗口内的按User、database统计的 select,update,delete,insert 这4种SQL分别的请求次数.
2、要求可以通过guc参数配置:
参数1, 每个统计窗口时间间隔 (默认60秒,最小1秒,最大3600秒), 修改参数1不需要重启数据库,
参数2, 总共可以保留的记录条数 (默认1000条,最小1条,最大100000条), 修改参数2需要重启数据库.
3、每个统计窗口保存为一条记录. 当记录条数达到参数2的配置时, 从最老的记录开始覆盖写.
4、统计结果存储到动态共享内存中, 加载模块时根据保留条数的参数配置申请合适大小的内存. 当数据库关闭或重启时, 不需要存储到磁盘中, 清除即可.
5、定义一个函数用于获取存储在共享内存中的统计值, 使用这个函数返回字段包括 start_ts,end_ts,user,database,select_rqs, update_rqs, delete_rqs, insert_rqs. 返回字段分别表示 统计窗口开始时间, 结束时间, 用户, 数据库, 每秒查询请求次数, 每秒更新请求次数, 每秒删除请求次数, 每秒插入请求次数.
6、定义一个函数用于在线清空动态共享内存中的统计记录.
7、该插件开始运行后对性能影响越小越好.
Codestral 的回复
Given the complexity of the task, here is an outline of how this PostgreSQL plugin could be implemented. Since the complete implementation of all requested features goes beyond what can be contained within one response, this example will focus on some specific aspects such as structure and Makefile setup, GUC parameters configuration, and shared memory usage for statistics storage.
Directory Structure
sql_stats/
├── sql_stats.c
├── sql_stats.control
├── sql_stats--0.0.sql
└── Makefile
GUC Parameter Configuration (In sql_stats.c)
int stats_window; // Window size in seconds, default 60, min 1, max 3600
int stats_records; // Number of records to keep, default 1000, min 1, max 100000 // Function declarations for custom GUC variables
void _assign_stats_window(int newval, void *extra);
void _show_stats_window(const int *myval, const char **showval, const char *prefix);
void _assign_stats_records(int newval, void *extra);
void _show_stats_records(const int *myval, const char **showval, const char *prefix);
// Registration of custom GUC variables
static struct config_enum_entry stats_window_options[] = {
{"1", 1, false},
{"3600", 3600, false},
{NULL, 0, false}
};
static struct config_enum_entry stats_records_options[] = {
{"1", 1, true},
{"100000", 100000, true},
{NULL, 0, false}
};
void _PG_init(void) {
DefineCustomIntVariable("sql_stats.window",
"Duration of each statistics window in seconds.",
NULL,
&stats_window,
60, // default value
1, // minimum value
3600, // maximum value
PGC_S_SESSION,
stats_window_options,
_assign_stats_window,
_show_stats_window);
DefineCustomIntVariable("sql_stats.records",
"Number of statistics records to keep in shared memory.",
NULL,
&stats_records,
1000, // default value
1, // minimum value
100000, // maximum value
PGC_S_SESSION,
stats_records_options,
_assign_stats_records,
_show_stats_records);
}
Shared Memory Usage (In sql_stats.c)
typedef struct {
TimestampTz start;
TimestampTz end;
Oid userid;
Oid dbid;
int selects;
int updates;
int deletes;
int inserts;
} StatsRecord; StatsRecord *stats_records_array; // Array to store statistics records
int current_record = 0; // Index of the currently written record
// Allocate shared memory when the module is loaded
void _PG_init(void) {
...
stats_records_array = SharedMemoryAlloc(stats_records * sizeof(StatsRecord));
if (!stats_records_array)
elog(ERROR, "Failed to allocate shared memory for SQL statistics");
}
Makefile Setup (In Makefile)
MODULES = sql_stats
EXTENSION = .so
DATA = sql_stats.control $(shell echo '$(PGXS) --libdir=$(pkglibdir)' | sh) include $(srcdir)/pgxs/src/makefiles/pgxs.mk
SQL Statements (In sql_stats--0.0.sql)
CREATE FUNCTION get_sql_stats(OUT start_ts TIMESTAMPTZ, OUT end_ts TIMESTAMPTZ, OUT user TEXT,
OUT database TEXT, OUT select_rqs INT, OUT update_rqs INT, OUT delete_rqs INT, OUT insert_rqs INT)
RETURNS SETOF record AS '$libdir/sql_stats', 'get_sql_stats' LANGUAGE C; CREATE FUNCTION clear_sql_stats() RETURNS VOID AS '$libdir/sql_stats', 'clear_sql_stats' LANGUAGE C;
Control File (In sql_stats.control)
comment = 'Plugin to collect SQL statistics'
default_version = '0.0'
module_pathname = '$libdir/sql_stats'
relocatable = true
Statistics Collection & Shared Memory Management (In sql_stats.c)
The details of this part are not covered in this outline due to the complexity and length that it would require. It involves tracking SQL commands, managing shared memory to store statistics records, handling edge cases such as window size changes or shared memory reallocation requirements, and implementing functions for retrieving statistics and clearing them from shared memory.
This outline should provide a starting point for building a PostgreSQL plugin with the requested features. The full implementation would include various other aspects, including proper error handling, thread safety considerations, and documentation.
请继续补齐这部分代码: 7) Statistics Collection & Shared Memory Management (In sql_stats.c)
Implementing statistics collection and shared memory management is a complex task that involves multiple components within PostgreSQL's execution framework. It requires tracking SQL commands, maintaining a circular buffer of statistics records in shared memory, and handling various edge cases such as changes to the window size or shared memory reallocation requirements.
Below is an outline of how this could be implemented:
// Callback function for each executed command (In `sql_stats.c`)
static void sql_stats_command_callback(const PlannedStmt *stmt) {
// Check if the command is a SELECT, UPDATE, DELETE, or INSERT statement
switch (nodeTag(stmt->commandType)) {
case T_PlannedStmt_Update:
stats_records_array[current_record].updates++;
break;
// Handle other commands similarly...
}
} // Function for rotating to the next statistics record (In `sql_stats.c`)
static void rotate_to_next_record(void) {
current_record = (current_record + 1) % stats_records;
// Clear the new record's data to prepare it for use
memset(&stats_records_array[current_record], 0, sizeof(StatsRecord));
}
// Callback function for each SQL statement completion (In `sql_stats.c`)
static void sql_stats_statement_callback(void) {
// Update the end timestamp of the current record
stats_records_array[current_record].end = GetCurrentTimestamp();
// Rotate to the next statistics record
rotate_to_next_record();
}
// Function for updating the start timestamp and user/database information of a new window (In `sql_stats.c`)
static void update_current_window(void) {
stats_records_array[current_record].start = GetCurrentTimestamp();
stats_records_array[current_record].userid = GetUserId();
stats_records_array[current_record].dbid = MyDatabaseId;
}
// Callback function for each SQL command start (In `sql_stats.c`)
static void sql_stats_start_callback(const PlannedStmt *stmt) {
// Update the current window's information if necessary
TimestampTz now = GetCurrentTimestamp();
if (now >= stats_records_array[current_record].end + interval_make(stats_window, 0))
update_current_window();
}
These functions would need to be properly integrated into PostgreSQL's execution framework by registering the callback functions with appropriate hooks. It is also essential to handle edge cases such as changes to the window size or shared memory reallocation requirements to ensure the correct operation of the plugin. Additionally, thread safety considerations and proper error handling would need to be addressed in a full implementation.
反复与模型交互最终就能拿到想要的代码.
往期的AI和大模型相关文章可以分享给大家:
《AI大模型+全文检索+tf-idf+向量数据库+我的文章 系列之6 - 科普 : 大模型到底能干什么? 如何选型? 专业术语? 资源消耗?》 《AI大模型+全文检索+tf-idf+向量数据库+我的文章 系列之5 - 在 Apple Silicon Mac 上微调(fine-tuning)大型语言模型(LLM) 并发布GGUF 》 《AI大模型+全文检索+tf-idf+向量数据库+我的文章 系列之4 - RAG 自动提示微调(prompt tuning)》 《AI大模型+全文检索+tf-idf+向量数据库+我的文章 系列之3 - 微调后, 我的数字人变聪明了 》 《AI大模型+全文检索+tf-idf+向量数据库+我的文章 系列之2 - 我的数字人: 4000余篇github blog文章投喂大模型中》 《AI大模型+全文检索+tf-idf+向量数据库+我的文章 系列之1 - 低配macbook成功跑AI大模型 LLama3:8b, 感谢ollama》
本期彩蛋-配图为cuug陈老师,恭喜 CUUG获得PostgreSQL数据库认证与培训合作伙伴
文章中的参考文档请点击阅读原文获得.
欢迎关注我的github (https://github.com/digoal/blog) , 学习数据库不迷路.
近期正在写公开课材料, 未来将通过视频号推出, 欢迎关注视频号: