Building Tetris in a SQL Query!
summary
A fully working Tetris is implemented as a single PostgreSQL query using recursive CTEs for the game loop, one-dimensional boolean arrays for the board, and nested recursive CTEs for hard drop and collision checks; output uses a tiny psql notify function and input is read via dblink to open a new session and see new rows. Piece generation follows an NES-style bag, collision and line clears run as SQL joins, and the demo runs—though snapshot isolation, memory growth and lack of parallelism limit practicality. Tetris implemented as a single recursive CTE
AI-written summary · about
chapters (12)
- 0:00Introduction
- 1:49Recursive CTEs basics
- 4:37Why use recursive CTEs
- 8:09Limitations of recursive CTEs
- 13:51When not to use recursive CTEs
- 14:33Building Tetris in SQL
- 18:27Game data and board encoding
- 20:12Game logic and movement
- 22:59Dropping and clearing lines
- 23:43Rendering the game
- 24:05Live demo
- 25:28Conclusion and Q&A








