PostgreSQL: generate_series

If you want to dummy up a bunch of data in PostgreSQL you can use this neat little trick

create table test (a int);
insert into test (select generate_series(0,999,1));
INSERT 0 1000

HowTo: Add Single Quotes to results in dynamic SQL

I frequently find the need to have single quoted output when i generate dynamic SQL.

It's always a pain to remember the exact number of ticks needed to get the quoted output.

Here is a reminder on how to do it:

select ''''||schemaname||'.'||tablename||'''' from pg_tables

4 quotes when there's no text, or 3 quotes on the outside and one on the inside when there is text.

select '''public.'||tablename||''';' from pg_tables
select 'select pg_size_pretty(pg_relation_size('''||tablename||'''));' from pg_tables where schemaname = 'bla';

About

Random Database, OS or otherwise interesting tips and tricks.

User


Clicky Web Analytics