<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=iso-8859-1"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.EmailStyle18
{mso-style-type:personal;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.EmailStyle19
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=FR link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Hi,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>I want to clone a schema, so I wrote the script below. My problem is that I can’t get sequences with pg_get_serial_sequence for the cloned primary keys.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>The sequence seems to be ok in the cloned schema :<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> idapp integer NOT NULL DEFAULT nextval('pr_1_1.ptbt_idapp_seq'::regclass),<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> CONSTRAINT adn_ptbt_pkey PRIMARY KEY (idapp)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>But if I run select pg_get_serial_sequence('pr_1_1.ptbt', 'idapp') it returns <NULL> <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Conversely, running pg_get_serial_sequence('source.ptbt', 'idapp') (from the source schema) returns the correct sequence.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB>I’m using 9.6, I saw that it could be a problem because column is not directly as “serial”, is this an answer without solution ?<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Any help will be appreciated !<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Thanks,<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>Olivier<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>CREATE OR REPLACE FUNCTION params.prm_colne_schema(source_schema text, dest_schema text) RETURNS boolean AS<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>$BODY$<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>DECLARE <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> object text;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> buffer text;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> default_ text;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> column_ text;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> sqls text;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>BEGIN<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> IF dest_schema <> 'public' THEN<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> EXECUTE 'DROP SCHEMA if exists ' || dest_schema || ' CASCADE';<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> EXECUTE 'CREATE SCHEMA ' || dest_schema;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> END IF; <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> -- copy sequences<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> FOR object IN<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> SELECT sequence_name::text FROM information_schema.SEQUENCES WHERE sequence_schema = source_schema<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> LOOP<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> EXECUTE 'CREATE SEQUENCE ' || dest_schema || '.' || object;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> END LOOP;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> -- copy tables without constraints<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> FOR object IN<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> SELECT TABLE_NAME::text FROM information_schema.TABLES WHERE table_schema = source_schema and substr(table_name,1,4) <> 'src_' <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> LOOP <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> buffer := dest_schema || '.' || object;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || source_schema || '.' || object || ' INCLUDING DEFAULTS)';<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> FOR column_, default_ IN<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> SELECT column_name::text, REPLACE(column_default::text, source_schema, dest_schema) FROM information_schema.COLUMNS WHERE table_schema = dest_schema AND TABLE_NAME = object<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> LOOP<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || column_ ||' DROP NOT NULL;';<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> END LOOP;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> END LOOP;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> -- copy tables with constraints<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> FOR object IN<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> SELECT TABLE_NAME::text FROM information_schema.TABLES WHERE table_schema = source_schema and substr(table_name,1,4) = 'src_' <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> LOOP <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> buffer := dest_schema || '.' || object;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || source_schema || '.' || object || ' INCLUDING ALL)';<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> -- copy sequences<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> FOR column_, default_ IN<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> SELECT column_name::text, REPLACE(column_default::text, source_schema, dest_schema) FROM information_schema.COLUMNS WHERE table_schema = dest_schema AND TABLE_NAME = object AND column_default LIKE 'nextval(%' || source_schema || '%::regclass)'<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> LOOP<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> sqls='ALTER TABLE ' || buffer || ' ALTER COLUMN ' || column_ || ' SET DEFAULT ' || default_;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> RAISE NOTICE 'default=%',sqls;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> EXECUTE (sqls);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> END LOOP;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> END LOOP;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> RETURN true;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>EXCEPTION<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> WHEN others THEN<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'> RETURN false;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>END;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>$BODY$<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='font-size:9.0pt;font-family:"Verdana",sans-serif'>LANGUAGE plpgsql VOLATILE;<o:p></o:p></span></p></div></body></html>