Skip to content

Instantly share code, notes, and snippets.

@qnkhuat
Created August 2, 2024 10:48
Show Gist options
  • Save qnkhuat/1dc6af93365875ec372ce01936340104 to your computer and use it in GitHub Desktop.
Save qnkhuat/1dc6af93365875ec372ce01936340104 to your computer and use it in GitHub Desktop.
;; create 100 tables, each with 100 columns of type: timestamp, int, float, text
;; each table has 100 rows
DO
$$
DECLARE
table_idx INT;
column_idx INT;
col_type_idx INT;
col_types TEXT[] := ARRAY['INT', 'TIMESTAMP', 'FLOAT', 'TEXT'];
col_type TEXT;
insert_stmt TEXT;
BEGIN
FOR table_idx IN 1..100 LOOP
EXECUTE format('CREATE TABLE table_%s (', table_idx) ||
array_to_string(ARRAY(
SELECT format('col_%s_%s %s', table_idx, col_idx, col_types[(col_idx % 4) + 1])
FROM generate_series(1, 100) AS col_idx
), ', ') ||
');';
insert_stmt := '';
FOR column_idx IN 1..100 LOOP
col_type_idx := (column_idx % 4) + 1;
col_type := col_types[col_type_idx];
IF col_type = 'INT' THEN
insert_stmt := insert_stmt || format('%s', floor(random())::int) || ', ';
ELSIF col_type = 'TIMESTAMP' THEN
insert_stmt := insert_stmt || 'current_timestamp, ';
ELSIF col_type = 'FLOAT' THEN
insert_stmt := insert_stmt || 'random(), ';
ELSE
insert_stmt := insert_stmt || format('''sample_text_%s''', column_idx) || ', ';
END IF;
END LOOP;
insert_stmt := left(insert_stmt, -2); -- Remove last comma and space
FOR row_idx IN 1..100 LOOP
EXECUTE format('INSERT INTO table_%s VALUES (%s);', table_idx, insert_stmt);
END LOOP;
END LOOP;
END
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment