DO Blocks and Anonymous Code
Run ad-hoc PL/pgSQL with DO blocks, manage dollar-quoting, and use DO blocks for one-off migrations.
What Is a DO Block?
A DO block lets you run PL/pgSQL without defining a named function. Perfect for one-off migrations and ad-hoc scripts.
Basic DO Block
Minimal syntax:
DO $$
BEGIN
RAISE NOTICE 'Hello from PL/pgSQL';
END;
$$ LANGUAGE plpgsql;
-- The language defaults to plpgsql, so you can omit it:
DO $$ BEGIN RAISE NOTICE 'Hi'; END $$;All lessons in this course
- Trigger Anatomy: BEFORE/AFTER, FOR EACH ROW
- PL/pgSQL Function Basics
- DO Blocks and Anonymous Code
- Auditing Tables with Triggers