
About
Tetris is a puzzle video game created by Alexey Pajitnov in the mid-1980s, in which falling four-block tetrominoes are stacked and cleared by filling complete horizontal lines. Tetris-SQL is a complete implementation of Tetris written as a single PostgreSQL query, using recursive common table expressions to drive the game loop, the `dblink` extension to read live player input, and `RAISE NOTICE` output to render the board in a terminal. It requires a PostgreSQL server (tested on versions 10 through 18) and a small Python helper script to relay player input.
How it differs from the original
Unlike a typical Tetris implementation, the entire game runs as a single SQL query inside the database engine — a separate Python script only relays player input into the database — so it's playable only via a terminal with a running Postgres server, not as a standalone program.
Getting started
Run a PostgreSQL server (Docker setup steps are provided in the repo) with the `dblink` extension enabled, start the input-relay script in one terminal, and execute the Tetris SQL query in a second terminal to play.
History
The project's implementation techniques were presented as a talk at PGConf.EU 2025.
Additional notes
- Technical note
- Postgres keeps all intermediate rows generated by the game's recursive query, so memory and disk usage grow continuously the longer a session runs.