from experience i can say that one large delete is usually better. however, there can be some corner cases with IN, which might invalidate my statement but basically this is true.
Make sure your got enough work_mem around to allow PostgreSQL to nicely hash the IN.
To create a copy as close as possible you must use INCLUDING ALL
with CREATE TABLE .. (LIKE ..)
since there can be any number of columns with defaults that you obviously want to copy.
You just want serial
columns to get their own, independent sequence, which makes a lot of sense and probably should be the default behavior. For now, this should do the job:
Function to copy any given table
Copies any given table (must exist) with new given name and independent serial
columns (if any).
Data is not included, it's trivial to copy that, too.
CREATE OR REPLACE FUNCTION f_copy_tbl(_tbl regclass, _newtbl text)
RETURNS void AS
$func$
DECLARE
_sql text;
BEGIN
-- Copy table
EXECUTE format('CREATE TABLE %I (LIKE %s INCLUDING ALL);', _newtbl, _tbl);
-- Fix serial columns, if any
SELECT INTO _sql
string_agg('CREATE SEQUENCE ' || seq, E';\n') || E';\n'
|| string_agg(format('ALTER SEQUENCE %s OWNED BY %I.%I'
, seq, _newtbl, a.attname), E';\n') || E';\n'
|| 'ALTER TABLE ' || quote_ident(_newtbl) || E'\n '
|| string_agg(format($$ALTER %I SET DEFAULT nextval('%s'::regclass)$$
, a.attname, seq), E'\n, ')
FROM pg_attribute a
JOIN pg_attrdef ad ON ad.adrelid = a.attrelid
AND ad.adnum = a.attnum
, quote_ident(_newtbl || '_' || a.attname || '_seq') AS seq -- new seq name
WHERE a.attrelid = _tbl
AND a.attnum > 0
AND NOT a.attisdropped
AND a.atttypid = ANY ('{int,int8,int2}'::regtype[])
AND ad.adsrc = 'nextval('''
|| (pg_get_serial_sequence (a.attrelid::regclass::text, a.attname))::regclass
|| '''::regclass)'
;
IF _sql IS NOT NULL THEN
EXECUTE _sql;
END IF;
END
$func$ LANGUAGE plpgsql VOLATILE;
Call:
SELECT f_copy_tbl('tbl', 'tbl1');
Produces and executes SQL code of the form:
CREATE TABLE tbl1 (LIKE tbl INCLUDING ALL);
-- only if there are serial columns:
CREATE SEQUENCE tbl1_tbl_id_seq; -- one line per serial type ..
CREATE SEQUENCE "tbl1_Odd_COL_seq"; -- .. two in this example
ALTER SEQUENCE tbl1_tbl_id_seq OWNED BY tbl1.tbl_id;
ALTER SEQUENCE "tbl1_Odd_COL_seq" OWNED BY tbl1."Odd_COL";
ALTER TABLE tbl1
ALTER tbl_id SET DEFAULT nextval('tbl1_tbl_id_seq'::regclass)
, ALTER "Odd_COL" SET DEFAULT nextval('"tbl1_Odd_COL_seq"'::regclass);
Only serial
columns get their own sequence. Other column default are copied unchanged - including nextval()
from a sequence that is not owned by the column or differs in any way from a serial
.
The function is safe against SQL injection and should work with arbitrary table and column names.
SQL Fiddle.
It would be a useful option for the LIKE
feature to create separate sequences for serial
columns. You may want to post a friendly feature request to pgsql-general.
Best Answer
You would probably be better suited to do the whole operation inside an anonymous block..
DO
..SQL Fiddle