DEV.co
Open-Source Databases · nuno-faria

tetris-sql

Tetris-SQL is a demonstration of Turing completeness in PostgreSQL, implementing a fully functional Tetris game using only SQL queries with recursive CTEs. It requires PostgreSQL 10+, Python 3 with psycopg2, and a compatible terminal for rendering.

Source: GitHub — github.com/nuno-faria/tetris-sql
1k
GitHub stars
43
Forks
PLpgSQL
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositorynuno-faria/tetris-sql
Ownernuno-faria
Primary languagePLpgSQL
LicenseMIT — OSI-approved
Stars1k
Forks43
Open issues1
Latest releaseUnknown
Last updated2025-10-26
Sourcehttps://github.com/nuno-faria/tetris-sql

What tetris-sql is

The implementation uses recursive Common Table Expressions (CTEs) to implement a game loop, RAISE NOTICE for rendering output, and the dblink extension to read fresh input snapshots during query execution. Game state and logic are maintained entirely within SQL, with a Python helper script converting terminal input into database writes.

Quickstart

Get the tetris-sql source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/nuno-faria/tetris-sql.gitcd tetris-sql# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Educational demonstration of SQL capabilities

Excellent resource for teaching recursive CTEs, query optimization, and the theoretical limits of SQL as a Turing-complete language to developers and database engineers.

Proof-of-concept for complex SQL logic

Reference implementation for teams exploring what is technically possible within SQL constraints, particularly for game loops or state machines in database-driven systems.

PostgreSQL conference talks and technical writing

The project has been presented at PGConf.EU 2025 and serves as compelling material for technical presentations on advanced SQL features and query optimization.

Implementation considerations

  • Requires PostgreSQL 10+ with dblink extension enabled; tested on versions 10 through 18.
  • Python 3 with psycopg2 is mandatory for the input handler; Docker Compose setup is recommended for isolated environment management.
  • Terminal compatibility is critical; Windows Terminal ≥v1.22, GNOME Terminal, iTerm2, and macOS Terminal are documented as tested, but other terminals may exhibit rendering flicker.
  • Initial query execution includes JIT compilation overhead in PostgreSQL; disabling JIT (`SET jit = off`) may improve perceived responsiveness but is not recommended for production workloads.
  • The game loop depends on polling the input table via dblink with a synthetic counter appended to bypass query result caching; this is a workaround specific to PostgreSQL's snapshot isolation.

When to avoid it — and what to weigh

  • Production game or interactive application — This is not suitable for any production use case. SQL is not designed for general-purpose programming, and game performance will be severely limited by database round-trips and query overhead.
  • Real-time or high-performance requirements — The rendering relies on RAISE NOTICE output and dblink polling, introducing latency and rendering artifacts. Initial JIT compilation adds significant startup delay.
  • Scenarios requiring modern game development tools — If you need a functional Tetris game, use established game engines (Unity, Godot) or JavaScript libraries. This project explicitly trades practicality for demonstrating SQL's theoretical capabilities.
  • Systems without direct PostgreSQL access or without Python support — The implementation requires both a PostgreSQL server (versions 10–18) and Python 3 with psycopg2, plus dblink extension enabled, creating a specific operational footprint.

License & commercial use

MIT License. Permits unrestricted use, modification, and distribution in both open-source and proprietary contexts, provided the license and copyright notice are included.

The MIT License permits commercial use without restriction. However, this is a demonstration project explicitly unsuitable for production use; commercial interest should focus on the educational or reference value, not operational deployment of the game itself.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationStrong
License clarityClear
Deployment complexityModerate
DEV.co fitPossible
Assessment confidenceHigh
Security considerations

dblink connections to the local database use default credentials specified on the command line, creating potential exposure if credentials appear in process lists. The input table mechanism assumes only the input.py script writes to it; unauthorized writes could alter game state. SQL injection is not a concern in the game context, but the reliance on string interpolation in dblink queries (`'--' || i`) should be reviewed if adapted to other use cases. No encryption or authentication layer is described.

Alternatives to consider

JavaScript/Canvas Tetris implementations

Vastly more practical for actual gameplay. Numerous open-source examples exist (e.g., GitHub search for 'tetris-js'), with proper input handling, rendering performance, and no database dependency.

PostgreSQL procedural language (PL/pgSQL) stored procedures

Better suited than recursive CTEs for complex game logic, offering imperative control flow, easier debugging, and more idiomatic database programming; dblink polling would still be required for input.

Other Turing-completeness demonstrations (SQL 3D renderer, SQL ray tracer, SQL GPT implementations)

If the goal is to explore SQL's theoretical power, these projects (mentioned in the README) offer different application domains and may provide clearer insights depending on your learning objective.

Software development agency

Build on tetris-sql with DEV.co software developers

If you're considering tetris-sql for education, proof-of-concept work, or technical presentations, our engineering team can help you set up, customize, and integrate it into your development workflow. We specialize in PostgreSQL optimization and advanced SQL patterns.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

tetris-sql FAQ

Can I play this on any database (MySQL, SQLite, etc.)?
No. The implementation relies on PostgreSQL-specific features: recursive CTEs (standard SQL), RAISE NOTICE, and especially the dblink extension. MySQL and SQLite do not have these capabilities or have different syntax.
Why does the game take so long to start?
PostgreSQL's JIT compiler optimizes the recursive CTE query plan on first execution. This is normal. You can disable JIT with `SET jit = off` in the query, though this is not recommended for other workloads.
Is this suitable for learning real SQL optimization?
It teaches recursive CTEs and query plan inspection, which are valuable. However, real SQL workloads prioritize data retrieval and transaction processing, not imperative control flow, so treat this as a specialized learning tool.
Can I modify the game rules or add features?
Yes, the code is MIT-licensed and fully open. However, the SQL-based design makes changes to game logic difficult; consider your effort-to-benefit ratio before attempting major modifications.

Work with a software development agency

From first prototype to production, DEV.co delivers software development services around tools like tetris-sql. Our software development agency staffs experienced software developers and web developers for custom software development, web development, integrations, and ongoing support across open-source databases and beyond.

Explore tetris-sql for your team

If you're considering tetris-sql for education, proof-of-concept work, or technical presentations, our engineering team can help you set up, customize, and integrate it into your development workflow. We specialize in PostgreSQL optimization and advanced SQL patterns.