I find the do block a nice enhancement; for example, it allows me to do many administration tasks quickly without adding a procedure to the database. Imagine that I need to truncate all the tables in a schema for development purposes in order to fill it with test data. I could do like this
DO $$
DECLARE
table_name text;
BEGIN
FOR table_name IN SELECT tablename FROM pg_tables WHERE schemaname = 'development' LOOP
EXECUTE 'TRUNCATE ' || table_name ||' CASCADE ;' ;
END LOOP;
END;
$$;
Regards
From: Chris Travers <chris.travers@gmail.com>
To: Rob Richardson <RDRichardson@rad-con.com>
Cc: "pgsql-general@postgresql.org" <pgsql-general@postgresql.org>
Sent: Thursday, May 23, 2013 3:04 PM
Subject: Re: [GENERAL] What is a DO block for?