Discover how to run quick database fixes without the hassle of permanent functions!
Why DO blocks for anonymous code in PostgreSQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to run a quick set of commands in your database to fix some data or test a small logic piece without creating a full stored procedure.
You try to write and save a function every time, but it feels like too much work for a simple task.
Writing full functions for small tasks is slow and clutters your database with many one-time-use functions.
It's easy to make mistakes copying and pasting code, and cleaning up afterward is a hassle.
DO blocks let you write and run anonymous code directly in the database without saving it permanently.
This means you can quickly test or fix things with simple, reusable blocks that disappear after running.
CREATE FUNCTION temp_fix() RETURNS void AS $$ BEGIN UPDATE table SET col = val; END; $$ LANGUAGE plpgsql; SELECT temp_fix(); DROP FUNCTION temp_fix();
DO $$ BEGIN UPDATE table SET col = val; END; $$ LANGUAGE plpgsql;
You can run quick, temporary code snippets safely and efficiently without cluttering your database.
A developer needs to fix a few rows in a table immediately during debugging without creating permanent functions.
DO blocks run anonymous code without saving it.
They speed up quick fixes and tests.
They keep your database clean from temporary functions.
Practice
DO block in PostgreSQL?Solution
Step 1: Understand the role of DO blocks
DO blocks allow running procedural code immediately without saving it as a function.Step 2: Compare with other options
Creating tables or functions is done with other commands, and DO blocks do not return query results.Final Answer:
To run anonymous procedural code immediately without creating a permanent function -> Option DQuick Check:
DO blocks = anonymous immediate code execution [OK]
- Thinking DO blocks create permanent functions
- Confusing DO blocks with SELECT queries
- Assuming DO blocks create tables
Solution
Step 1: Recall the correct DO block syntax
The DO block uses dollar quoting $$ to enclose the code, with LANGUAGE plpgsql specified after the block.Step 2: Check each option
DO $$ BEGIN END $$ LANGUAGE plpgsql; correctly places $$ around BEGIN...END and specifies LANGUAGE plpgsql after the block.Final Answer:
DO $$ BEGIN END $$ LANGUAGE plpgsql; -> Option AQuick Check:
DO block syntax = DO $$ code $$ LANGUAGE plpgsql; [OK]
- Placing LANGUAGE plpgsql before BEGIN
- Not using dollar quoting $$
- Misordering keywords in the DO block
DO $$ BEGIN RAISE NOTICE 'Hello, PostgreSQL!'; END $$ LANGUAGE plpgsql;
Solution
Step 1: Understand RAISE NOTICE in DO blocks
RAISE NOTICE outputs a message to the client as an informational notice, not a query result.Step 2: Analyze the DO block behavior
The block runs immediately and prints the notice message but does not return rows or create functions.Final Answer:
It prints 'Hello, PostgreSQL!' as a notice message -> Option AQuick Check:
RAISE NOTICE outputs messages, not query results [OK]
- Expecting query result rows
- Thinking it creates a function
- Confusing notice with error
DO $$ BEGIN PERFORM 1/0; END $$ LANGUAGE plpgsql;
Solution
Step 1: Analyze the code inside DO block
The statement PERFORM 1/0 attempts to divide 1 by zero, which is not allowed.Step 2: Identify the error type
This causes a runtime error (division by zero), not a syntax error or missing keyword.Final Answer:
Division by zero runtime error -> Option BQuick Check:
1/0 causes runtime error, not syntax [OK]
- Confusing runtime error with syntax error
- Ignoring division by zero possibility
- Assuming PERFORM is invalid here
users to set active = false for all users who haven't logged in for over a year. Which DO block correctly performs this task?Solution
Step 1: Check the DO block structure and logic
DO $$ BEGIN UPDATE users SET active = false WHERE last_login < NOW() - INTERVAL '1 year'; END $$ LANGUAGE plpgsql; correctly uses BEGIN...END with an UPDATE statement and the right condition for last_login older than 1 year.Step 2: Verify other options
DO $$ BEGIN SELECT * FROM users WHERE last_login < NOW() - INTERVAL '1 year'; END $$ LANGUAGE plpgsql; only selects rows, no update. DO $$ UPDATE users SET active = false WHERE last_login < NOW() - INTERVAL '1 year'; END $$ LANGUAGE plpgsql; misses BEGIN...END block. DO $$ BEGIN UPDATE users SET active = false WHERE last_login > NOW() - INTERVAL '1 year'; END $$ LANGUAGE plpgsql; uses wrong condition (greater than instead of less than).Final Answer:
DO $$ BEGIN UPDATE users SET active = false WHERE last_login < NOW() - INTERVAL '1 year'; END $$ LANGUAGE plpgsql; -> Option CQuick Check:
Correct DO block with UPDATE and condition [OK]
- Omitting BEGIN...END block
- Using wrong comparison operator in WHERE
- Using SELECT instead of UPDATE
