使用纯SQL在PostgreSQL中生成二维码
Generate QR Codes with Pure SQL in PostgreSQL

原始链接: https://tanelpoder.com/posts/generate-qr-code-with-pure-sql-in-postgres/

塔内尔·波德尔为PostgreSQL创建了一个纯SQL二维码生成器,名为`pqr.sql`,作为个人学习项目。该生成器不需要外部库或扩展——它只是一个SQL语句!虽然不打算用于生产环境(但优化是一个潜在的学习练习),但它成功地将数据编码成二维码,直接在数据库内完成。 一个更安全的版本,`pqrsafe.sql`,可以防止编码的数据超过二维码的容量,避免生成无法读取的二维码。使用方法很简单,通过命令行工具`psql`和一个有效载荷变量即可。 波德尔指出,PostgreSQL 17相比16有了显著的性能提升。他幽默地反思,像ChatGPT这样的人工智能工具极大地提高了开发效率,并戏称自己现在是“11倍工程师”,相比于20年前完成类似任务时的效率。

一篇 Hacker News 帖子详细介绍了一个项目,用户 (tanelpoder) 成功地使用纯 SQL 在 PostgreSQL 中完全生成了二维码,未使用任何外部库或 PL/SQL。作者利用 Codex AI 构建了生成器,最初引导它完成二维码创建的必要步骤。 该过程涉及迭代改进,克服了诸如不正确的矩阵运算和无法读取的代码等挑战。一个关键步骤是创建一个测试套件,将 SQL 输出与标准的 "qr" 工具进行比较,以确保每次 Codex 迭代的准确性。整个项目大约花费了一个小时。 评论者赞扬了代码的可读性(对于熟悉二维码生成的人来说)和项目的概念优雅,将其与 Haskell 等函数式编程语言相提并论。虽然承认由于可能的性能问题而不适用于生产环境,但该帖子引发了关于潜在用例和 SQL 令人惊讶能力的讨论。作者鼓励在 PostgreSQL 版本 (16、17、18) 上进行性能测试,并欢迎相关分析的链接。
相关文章

原文
pqr.sql: Generate QR Codes with Pure SQL in PostgreSQL | Tanel Poder Blog

As my cat woke me up way too early for a Saturday morning, I decided to put all this extra time (and my ChatGPT Pro subscription) into good use and generated a QR-code generator for PostgreSQL, written as a single pure SQL statement. No external libraries or Postgres extension installation needed!

I did this purely for fun and learning, I don’t think this is going to be the best way for generating QR codes in production. But optimizing this SQL to run faster should be a fun learning exercise. PostgreSQL 17 ran it much faster than 16 in my lab, haven’t gotten to compare the execution plans yet.

The pqr.sql example usage is simple:

$ psql -qf pqr.sql -v payload='Hello, World!'

An example QR code created with SQL

There’s also a bit more complex version pqrsafe.sql that raises an error if you’re trying to encode a too long payload into the QR code (the basic version would just generate a QR code that doesn’t “read”).

$ psql -qf pqrsafe.sql -v payload='Hello, World!!!'
Version 1-M (ECC M) cannot encode byte payloads that long. Got 15 characters (15 bytes).

Fun fact: about 20 years ago, I spent my entire Sunday manually writing an important production fish generator utility for large-scale Oracle databases. Today I got all the QR code work done with just an hour of talking to OpenAI Codex.

A SQL generated image of a fish

I am an 11x engineer now!



联系我们 contact @ memedata.com