pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Cloning schemas
35+ messages / 5 participants
[nested] [flat]

* Cloning schemas
@ 2018-07-02 11:30 Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 12:24 ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-02 11:30 UTC (permalink / raw)
  To: pgsql-general

Hi,

i am trying to use postgresql clone schema function:

https://www.postgresql.org/message-id/CANu8FiwiBiAjYgdehYBkJcSRFd6ZFFnN5kDJE7TG4rad5BNXZQ%40mail.gma...

I created function clone_schema in public schema:

[image: image.png]


I am trying to use :

"select * from clone_schema('public','Version8',true) but i am getting
error:

"Column "max_value" does not exist.
LINE 1: SELECT last_value, max_value, start_value, increment_by, min...
HINT: Maybe you wanted to point to column " "audit_sq.last_value"?
QUERY: SELECT last_value, max_value, start_value, increment_by, min_value,
cache_value, log_cnt, is_cycled, is_called FROM public.audit_sq;

CONTEXT: function PL/pgSQL clone_schema(text,text,boolean), row 66 in
EXECUTE
SQL state: 42703
Can anyone help?

Best,
Jacek

Attachments:

image.png (image/png, 49.5K) download view image
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
@ 2018-07-02 12:24 ` Łukasz Jarych <jaryszek@gmail.com>
  1 sibling, 0 replies; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-02 12:24 UTC (permalink / raw)
  To: vnoagbodji@amplify-nation.com; pgsql-general

Hi ,

thank you !

You have right:

[image: image.png]

Hmm i thought that i am creating this table " audit_sq " within  clone
schema and this function is complete.

How can i fix this?

Best,
Jacek



pon., 2 lip 2018 o 13:51 Victor Noagbodji <vnoagbodji@amplify-nation.com>
napisał(a):

> Hello,
>
> Can you check if the table "audit_sq" has the column "max_value"?
>
> On Jul 2, 2018, at 7:30 AM, Łukasz Jarych <jaryszek@gmail.com> wrote:
>
> Hi,
>
> i am trying to use postgresql clone schema function:
>
>
> https://www.postgresql.org/message-id/CANu8FiwiBiAjYgdehYBkJcSRFd6ZFFnN5kDJE7TG4rad5BNXZQ%40mail.gma...
>
> I created function clone_schema in public schema:
>
> <image.png>
>
>
> I am trying to use :
>
> "select * from clone_schema('public','Version8',true) but i am getting
> error:
>
> "Column "max_value" does not exist.
> LINE 1: SELECT last_value, max_value, start_value, increment_by, min...
> HINT: Maybe you wanted to point to column " "audit_sq.last_value"?
> QUERY: SELECT last_value, max_value, start_value, increment_by, min_value,
> cache_value, log_cnt, is_cycled, is_called FROM public.audit_sq;
>
> CONTEXT: function PL/pgSQL clone_schema(text,text,boolean), row 66 in
> EXECUTE
> SQL state: 42703
> Can anyone help?
>
> Best,
> Jacek
>
>
>
>

Attachments:

image.png (image/png, 15.9K) download view image
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
@ 2018-07-02 13:30 ` Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  1 sibling, 1 reply; 35+ messages in thread

From: Tom Lane @ 2018-07-02 13:30 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; +Cc: pgsql-general

=?UTF-8?Q?=C5=81ukasz_Jarych?= <jaryszek@gmail.com> writes:
> I am trying to use :

> "select * from clone_schema('public','Version8',true) but i am getting
> error:

> "Column "max_value" does not exist.
> LINE 1: SELECT last_value, max_value, start_value, increment_by, min...
> HINT: Maybe you wanted to point to column " "audit_sq.last_value"?
> QUERY: SELECT last_value, max_value, start_value, increment_by, min_value,
> cache_value, log_cnt, is_cycled, is_called FROM public.audit_sq;
> CONTEXT: function PL/pgSQL clone_schema(text,text,boolean), row 66 in
> EXECUTE

I guess audit_sq is a sequence?  It looks to me like this function has
not been taught about the changes in sequence metadata in PG v10.
You need to update it, or talk to its author about an update.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-07-02 13:57   ` Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-02 13:57 UTC (permalink / raw)
  To: tgl@sss.pgh.pa.us; pgsql-general; vnoagbodji@amplify-nation.com

Hi,

"
Strange. "audit_sq" looks like an invalid sequence table. I went here,
https://www.postgresql.org/docs/9.6/static/sql-createsequence.html, and
checked all the way back to version 7.1 and "maxvalue" has been a column
since back then.

Maybe skip that table for now? It even says the last value is 1. You should
also check the other sequence tables. You can get them by doing:

select * from information_schema.sequences;"

Result of select:

[image: image.png]

Are you sure that I can skip " audit_sq" seq?

I wrote here because i am newbie and i can not update this. Hope for your
help Guys.

Best,
Jacek







pon., 2 lip 2018 o 15:30 Tom Lane <tgl@sss.pgh.pa.us> napisał(a):

> =?UTF-8?Q?=C5=81ukasz_Jarych?= <jaryszek@gmail.com> writes:
> > I am trying to use :
>
> > "select * from clone_schema('public','Version8',true) but i am getting
> > error:
>
> > "Column "max_value" does not exist.
> > LINE 1: SELECT last_value, max_value, start_value, increment_by, min...
> > HINT: Maybe you wanted to point to column " "audit_sq.last_value"?
> > QUERY: SELECT last_value, max_value, start_value, increment_by,
> min_value,
> > cache_value, log_cnt, is_cycled, is_called FROM public.audit_sq;
> > CONTEXT: function PL/pgSQL clone_schema(text,text,boolean), row 66 in
> > EXECUTE
>
> I guess audit_sq is a sequence?  It looks to me like this function has
> not been taught about the changes in sequence metadata in PG v10.
> You need to update it, or talk to its author about an update.
>
>                         regards, tom lane
>

Attachments:

image.png (image/png, 66.5K) download view image
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
@ 2018-07-02 14:03     ` Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Adrian Klaver @ 2018-07-02 14:03 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; tgl@sss.pgh.pa.us; pgsql-general; vnoagbodji@amplify-nation.com

On 07/02/2018 06:57 AM, Łukasz Jarych wrote:
> Hi,
> 
> "
> Strange. "audit_sq" looks like an invalid sequence table. I went 
> here,https://www.postgresql.org/docs/9.6/static/sql-createsequence.html, 
> and checked all the way back to version 7.1 and "maxvalue" has been a 
> column since back then.

What version of Postgres are you actually doing the cloning in?

Per Tom's post:

https://www.postgresql.org/docs/10/static/release-10.html

"
Move sequences' metadata fields into a new pg_sequence system catalog 
(Peter Eisentraut)

...

A sequence relation now stores only the fields that can be modified by 
nextval(), that is last_value, log_cnt, and is_called.

...

The main incompatibility introduced by this change is that selecting 
from a sequence relation now returns only the three fields named above. 
To obtain the sequence's other properties, applications must look into 
pg_sequence. The new system view pg_sequences can also be used for this 
purpose; it provides column names that are more compatible with existing 
code.
"

> 
> Maybe skip that table for now? It even says the last value is 1. You 
> should also check the other sequence tables. You can get them by doing:
> 
> select * from information_schema.sequences;"
> 
> Result of select:
> 
> image.png
> 
> Are you sure that I can skip " audit_sq" seq?
> 
> I wrote here because i am newbie and i can not update this. Hope for 
> your help Guys.
> 
> Best,
> Jacek
> 
> 
> 
> 
> 
> 
> 
> pon., 2 lip 2018 o 15:30 Tom Lane <tgl@sss.pgh.pa.us 
> <mailto:tgl@sss.pgh.pa.us>> napisał(a):
> 
>     =?UTF-8?Q?=C5=81ukasz_Jarych?= <jaryszek@gmail.com
>     <mailto:jaryszek@gmail.com>> writes:
>      > I am trying to use :
> 
>      > "select * from clone_schema('public','Version8',true) but i am
>     getting
>      > error:
> 
>      > "Column "max_value" does not exist.
>      > LINE 1: SELECT last_value, max_value, start_value, increment_by,
>     min...
>      > HINT: Maybe you wanted to point to column " "audit_sq.last_value"?
>      > QUERY: SELECT last_value, max_value, start_value, increment_by,
>     min_value,
>      > cache_value, log_cnt, is_cycled, is_called FROM public.audit_sq;
>      > CONTEXT: function PL/pgSQL clone_schema(text,text,boolean), row 66 in
>      > EXECUTE
> 
>     I guess audit_sq is a sequence?  It looks to me like this function has
>     not been taught about the changes in sequence metadata in PG v10.
>     You need to update it, or talk to its author about an update.
> 
>                              regards, tom lane
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-07-02 14:14       ` Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-02 14:14 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

Hi,

i see. thank you

I am using:

[image: image.png]

Best,
Jacek

pon., 2 lip 2018 o 16:03 Adrian Klaver <adrian.klaver@aklaver.com>
napisał(a):

> On 07/02/2018 06:57 AM, Łukasz Jarych wrote:
> > Hi,
> >
> > "
> > Strange. "audit_sq" looks like an invalid sequence table. I went
> > here,https://www.postgresql.org/docs/9.6/static/sql-createsequence.html,
>
> > and checked all the way back to version 7.1 and "maxvalue" has been a
> > column since back then.
>
> What version of Postgres are you actually doing the cloning in?
>
> Per Tom's post:
>
> https://www.postgresql.org/docs/10/static/release-10.html
>
> "
> Move sequences' metadata fields into a new pg_sequence system catalog
> (Peter Eisentraut)
>
> ...
>
> A sequence relation now stores only the fields that can be modified by
> nextval(), that is last_value, log_cnt, and is_called.
>
> ...
>
> The main incompatibility introduced by this change is that selecting
> from a sequence relation now returns only the three fields named above.
> To obtain the sequence's other properties, applications must look into
> pg_sequence. The new system view pg_sequences can also be used for this
> purpose; it provides column names that are more compatible with existing
> code.
> "
>
> >
> > Maybe skip that table for now? It even says the last value is 1. You
> > should also check the other sequence tables. You can get them by doing:
> >
> > select * from information_schema.sequences;"
> >
> > Result of select:
> >
> > image.png
> >
> > Are you sure that I can skip " audit_sq" seq?
> >
> > I wrote here because i am newbie and i can not update this. Hope for
> > your help Guys.
> >
> > Best,
> > Jacek
> >
> >
> >
> >
> >
> >
> >
> > pon., 2 lip 2018 o 15:30 Tom Lane <tgl@sss.pgh.pa.us
> > <mailto:tgl@sss.pgh.pa.us>> napisał(a):
> >
> >     =?UTF-8?Q?=C5=81ukasz_Jarych?= <jaryszek@gmail.com
> >     <mailto:jaryszek@gmail.com>> writes:
> >      > I am trying to use :
> >
> >      > "select * from clone_schema('public','Version8',true) but i am
> >     getting
> >      > error:
> >
> >      > "Column "max_value" does not exist.
> >      > LINE 1: SELECT last_value, max_value, start_value, increment_by,
> >     min...
> >      > HINT: Maybe you wanted to point to column " "audit_sq.last_value"?
> >      > QUERY: SELECT last_value, max_value, start_value, increment_by,
> >     min_value,
> >      > cache_value, log_cnt, is_cycled, is_called FROM public.audit_sq;
> >      > CONTEXT: function PL/pgSQL clone_schema(text,text,boolean), row
> 66 in
> >      > EXECUTE
> >
> >     I guess audit_sq is a sequence?  It looks to me like this function
> has
> >     not been taught about the changes in sequence metadata in PG v10.
> >     You need to update it, or talk to its author about an update.
> >
> >                              regards, tom lane
> >
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>

Attachments:

image.png (image/png, 19.6K) download view image
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
@ 2018-07-02 14:21         ` Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Melvin Davidson @ 2018-07-02 14:21 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

On Mon, Jul 2, 2018 at 10:14 AM, Łukasz Jarych <jaryszek@gmail.com> wrote:

>
> Hi,
>
> i see. thank you
>
> I am using:
>
> [image: image.png]
>
> Best,
> Jacek
>
> pon., 2 lip 2018 o 16:03 Adrian Klaver <adrian.klaver@aklaver.com>
> napisał(a):
>
>> On 07/02/2018 06:57 AM, Łukasz Jarych wrote:
>> > Hi,
>> >
>> > "
>> > Strange. "audit_sq" looks like an invalid sequence table. I went
>> > here,https://www.postgresql.org/docs/9.6/static/sql-createsequence.html,
>>
>> > and checked all the way back to version 7.1 and "maxvalue" has been a
>> > column since back then.
>>
>> What version of Postgres are you actually doing the cloning in?
>>
>> Per Tom's post:
>>
>> https://www.postgresql.org/docs/10/static/release-10.html
>>
>> "
>> Move sequences' metadata fields into a new pg_sequence system catalog
>> (Peter Eisentraut)
>>
>> ...
>>
>> A sequence relation now stores only the fields that can be modified by
>> nextval(), that is last_value, log_cnt, and is_called.
>>
>> ...
>>
>> The main incompatibility introduced by this change is that selecting
>> from a sequence relation now returns only the three fields named above.
>> To obtain the sequence's other properties, applications must look into
>> pg_sequence. The new system view pg_sequences can also be used for this
>> purpose; it provides column names that are more compatible with existing
>> code.
>> "
>>
>> >
>> > Maybe skip that table for now? It even says the last value is 1. You
>> > should also check the other sequence tables. You can get them by doing:
>> >
>> > select * from information_schema.sequences;"
>> >
>> > Result of select:
>> >
>> > image.png
>> >
>> > Are you sure that I can skip " audit_sq" seq?
>> >
>> > I wrote here because i am newbie and i can not update this. Hope for
>> > your help Guys.
>> >
>> > Best,
>> > Jacek
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> > pon., 2 lip 2018 o 15:30 Tom Lane <tgl@sss.pgh.pa.us
>> > <mailto:tgl@sss.pgh.pa.us>> napisał(a):
>> >
>> >     =?UTF-8?Q?=C5=81ukasz_Jarych?= <jaryszek@gmail.com
>> >     <mailto:jaryszek@gmail.com>> writes:
>> >      > I am trying to use :
>> >
>> >      > "select * from clone_schema('public','Version8',true) but i am
>> >     getting
>> >      > error:
>> >
>> >      > "Column "max_value" does not exist.
>> >      > LINE 1: SELECT last_value, max_value, start_value, increment_by,
>> >     min...
>> >      > HINT: Maybe you wanted to point to column "
>> "audit_sq.last_value"?
>> >      > QUERY: SELECT last_value, max_value, start_value, increment_by,
>> >     min_value,
>> >      > cache_value, log_cnt, is_cycled, is_called FROM public.audit_sq;
>> >      > CONTEXT: function PL/pgSQL clone_schema(text,text,boolean), row
>> 66 in
>> >      > EXECUTE
>> >
>> >     I guess audit_sq is a sequence?  It looks to me like this function
>> has
>> >     not been taught about the changes in sequence metadata in PG v10.
>> >     You need to update it, or talk to its author about an update.
>> >
>> >                              regards, tom lane
>> >
>>
>>
>> --
>> Adrian Klaver
>> adrian.klaver@aklaver.com
>>
>
Lukas,

Once again, in Version 10, the developers have changed the system catalogs.
Please use the attached clone_schema_10.sql which
has been adjusted for PostgreSQL 10.



-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

Attachments:

image.png (image/png, 19.6K) download view image
clone_schema_10.sql (text/plain, 13.8K) download  -- Function: public.clone_schema(text, text, boolean) -- DROP FUNCTION public.clone_schema(text, text, boolean); CREATE OR REPLACE FUNCTION public.clone_schema( source_schema text, dest_schema text, include_recs boolean) RETURNS void AS $BODY$ -- Initial code by Emanuel '3manuek' -- Last revision 2017-04-17 by Melvin Davidson -- Added SELECT REPLACE for schema views -- -- This function will clone all sequences, tables, indexes, rules, triggers, -- data(optional), views & functions from any existing schema to a new schema -- SAMPLE CALL: -- SELECT clone_schema('public', 'new_schema', TRUE); DECLARE src_oid oid; tbl_oid oid; seq_oid oid; func_oid oid; owner_id oid; con_oid oid; v_path text; v_func text; v_args text; v_conname text; v_rule text; v_trig text; object text; buffer text; srctbl text; default_ text; v_column text; qry text; dest_qry text; v_def text; v_stat integer; seqval bigint; seq_name name; sq_type oid; sq_last_value bigint; sq_max_value bigint; sq_start_value bigint; sq_increment_by bigint; sq_min_value bigint; sq_cache_value bigint; sq_log_cnt bigint; -- sq_is_called boolean; sq_is_cycled boolean; sq_typname name; seq_cycled text; seq_owner name; -- sq_cycled char(10); BEGIN -- Check that source_schema exists SELECT oid INTO src_oid FROM pg_namespace WHERE nspname = quote_ident(source_schema); IF NOT FOUND THEN RAISE NOTICE 'source schema % does not exist!', source_schema; RETURN ; END IF; -- Check that dest_schema does not yet exist PERFORM nspname FROM pg_namespace WHERE nspname = quote_ident(dest_schema); IF FOUND THEN RAISE NOTICE 'dest schema % already exists!', dest_schema; RETURN ; END IF; EXECUTE 'CREATE SCHEMA ' || quote_ident(dest_schema) ; -- Add schema comment SELECT description INTO v_def FROM pg_description WHERE objoid = src_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SCHEMA ' || quote_ident(dest_schema) || ' IS ' || quote_literal(v_def); END IF; SET search_path TO dest_schema, source_schema, 'public', pg_catalog; SELECT current_schemas(TRUE) INTO v_path; RAISE NOTICE 'search path = %', v_path; -- Create sequences -- TODO: Find a way to make this sequence's owner is the correct table. FOR object IN SELECT sequence_name::text FROM information_schema.sequences WHERE sequence_schema = quote_ident(source_schema) LOOP SELECT oid, relowner into seq_oid, owner_id FROM pg_class WHERE relname = quote_ident(object) AND relkind = 'S'; SELECT seqtypid, seqstart, seqincrement, seqmax, seqmin, seqcache, seqcycle INTO sq_type, sq_start_value, sq_increment_by, sq_max_value, sq_min_value, sq_cache_value, sq_is_cycled FROM pg_sequence WHERE seqrelid = seq_oid; SELECT typname INTO sq_typname FROM pg_type WHERE oid = sq_type; SELECT rolname INTO seq_owner FROM pg_authid WHERE oid = owner_id; -- EXECUTE 'CREATE SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object); -- srctbl := quote_ident(source_schema) || '.' || quote_ident(object); IF sq_is_cycled THEN seq_cycled = ' CYCLE'; ELSE seq_cycled = ' NO CYCLE'; END IF; EXECUTE 'CREATE SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' AS ' || sq_typname || ' INCREMENT ' || sq_increment_by || ' MINVALUE ' || sq_min_value || ' MAXVALUE ' || sq_max_value || ' START WITH ' || sq_start_value || ' CACHE ' || sq_cache_value || seq_cycled; -- || ' OWNED BY ' || seq_owner; /* buffer := quote_ident(dest_schema) || '.' || quote_ident(object); IF include_recs THEN EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_last_value || ', ' || sq_is_called || ');' ; ELSE EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_start_value || ', ' || sq_is_called || ');' ; END IF; */ -- add sequence comments SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'S' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create tables FOR object IN SELECT TABLE_NAME::text FROM information_schema.tables WHERE table_schema = quote_ident(source_schema) AND table_type = 'BASE TABLE' LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || quote_ident(source_schema) || '.' || quote_ident(object) || ' INCLUDING ALL)'; -- Add table comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; IF include_recs THEN -- Insert records from source table EXECUTE 'INSERT INTO ' || buffer || ' SELECT * FROM ' || quote_ident(source_schema) || '.' || quote_ident(object) || ';'; END IF; FOR v_column, default_ IN SELECT column_name::text, REPLACE(column_default::text, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.' ) FROM information_schema.COLUMNS WHERE table_schema = dest_schema AND TABLE_NAME = object AND column_default LIKE 'nextval(%' || quote_ident(source_schema) || '%::regclass)' LOOP EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || v_column || ' SET DEFAULT ' || default_; END LOOP; END LOOP; -- set column statistics FOR tbl_oid, srctbl IN SELECT oid, relname FROM pg_class WHERE relnamespace = src_oid AND relkind = 'r' LOOP FOR v_column, v_stat IN SELECT attname, attstattarget FROM pg_attribute WHERE attrelid = tbl_oid AND attnum > 0 LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(srctbl); -- RAISE EXCEPTION 'ALTER TABLE % ALTER COLUMN % SET STATISTICS %', buffer, v_column, v_stat::text; EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || quote_ident(v_column) || ' SET STATISTICS ' || v_stat || ';'; END LOOP; END LOOP; -- add FK constraint FOR qry IN SELECT 'ALTER TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(rn.relname) || ' ADD CONSTRAINT ' || quote_ident(ct.conname) || ' ' || replace(pg_get_constraintdef(ct.oid), quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') || ';' FROM pg_constraint ct JOIN pg_class rn ON rn.oid = ct.conrelid WHERE connamespace = src_oid AND rn.relkind = 'r' AND ct.contype = 'f' LOOP EXECUTE qry; END LOOP; -- Add constraint comment FOR con_oid IN SELECT oid FROM pg_constraint WHERE conrelid = tbl_oid LOOP SELECT conname INTO v_conname FROM pg_constraint WHERE oid = con_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid; IF FOUND THEN EXECUTE 'COMMENT ON CONSTRAINT ' || v_conname || ' ON ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create views FOR object IN SELECT table_name::text, view_definition FROM information_schema.views WHERE table_schema = quote_ident(source_schema) LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); SELECT view_definition INTO v_def FROM information_schema.views WHERE table_schema = quote_ident(source_schema) AND table_name = quote_ident(object); SELECT REPLACE(v_def, source_schema, dest_schema) INTO v_def; -- RAISE NOTICE 'view def, % , source % , dest % ', v_def, source_schema, dest_schema; EXECUTE 'CREATE OR REPLACE VIEW ' || buffer || ' AS ' || v_def || ';' ; -- Add comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'v' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON VIEW ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- Create functions FOR func_oid IN SELECT oid, proargnames FROM pg_proc WHERE pronamespace = src_oid LOOP SELECT pg_get_functiondef(func_oid) INTO qry; SELECT proname, oidvectortypes(proargtypes) INTO v_func, v_args FROM pg_proc WHERE oid = func_oid; SELECT replace(qry, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') INTO dest_qry; EXECUTE dest_qry; -- Add function comment SELECT description INTO v_def FROM pg_description WHERE objoid = func_oid AND objsubid = 0; IF FOUND THEN -- RAISE NOTICE 'func_oid %, object %, v_args %', func_oid::text, quote_ident(object), v_args; EXECUTE 'COMMENT ON FUNCTION ' || quote_ident(dest_schema) || '.' || quote_ident(v_func) || '(' || v_args || ')' || ' IS ' || quote_literal(v_def) ||';' ; END IF; END LOOP; -- add Rules SET search_path TO dest_schema; FOR v_def IN SELECT definition FROM pg_rules WHERE schemaname = quote_ident(source_schema) LOOP IF v_def IS NOT NULL THEN SELECT replace(v_def, source_schema || '.', dest_schema || '.') INTO v_def; EXECUTE ' ' || v_def; END IF; END LOOP; -- add triggers FOR v_def IN SELECT pg_get_triggerdef(oid) FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT replace(v_def, source_schema || '.', dest_schema || '.') INTO dest_qry; EXECUTE dest_qry; END LOOP; -- Disable inactive triggers -- D = disabled FOR tbl_oid IN SELECT oid FROM pg_trigger WHERE tgenabled = 'D' AND tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT t.tgname, c.relname INTO object, srctbl FROM pg_trigger t JOIN pg_class c ON c.oid = t.tgrelid WHERE t.oid = tbl_oid; IF FOUND THEN EXECUTE 'ALTER TABLE ' || dest_schema || '.' || srctbl || ' DISABLE TRIGGER ' || object || ';'; END IF; END LOOP; -- Add index comment FOR tbl_oid IN SELECT oid FROM pg_class WHERE relkind = 'i' AND relnamespace = src_oid LOOP SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON INDEX ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- add rule comments FOR con_oid IN SELECT oid, * FROM pg_rewrite WHERE rulename <> '_RETURN'::name LOOP SELECT rulename, ev_class INTO v_rule, tbl_oid FROM pg_rewrite WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON RULE ' || v_rule || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- add trigger comments FOR con_oid IN SELECT oid, * FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' LOOP SELECT tgname, tgrelid INTO v_trig, tbl_oid FROM pg_trigger WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TRIGGER ' || v_trig || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public.clone_schema(text, text, boolean) OWNER TO postgres; COMMENT ON FUNCTION public.clone_schema(text, text, boolean) IS 'Duplicates sequences, tables, indexes, rules, triggers, data(optional), views & functions from the source schema to the destination schema';
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-03 08:11           ` Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-03 08:11 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

Hi Melvin,

I understand this but i can not update function by myself. Thnak you very
much !

Still errors here.

Something like:

ERROR: BŁĄD: you cannot add to column "TopoToVersion_ID" DETAIL: Column
"TopoToVersion_ID" is an identity column defined as GENERATED ALWAYS. HINT:
Use OVERRIDING SYSTEM VALUE to override. CONTEXT: wyrażenie SQL "INSERT
INTO version4.t_topotoversions SELECT * FROM public.t_topotoversions;"
funkcja PL/pgSQL clone_schema(text,text,boolean), wiersz 212 w EXECUTE SQL
state: 428C9

pon., 2 lip 2018 o 16:22 Melvin Davidson <melvin6925@gmail.com> napisał(a):

>
>
> On Mon, Jul 2, 2018 at 10:14 AM, Łukasz Jarych <jaryszek@gmail.com> wrote:
>
>>
>> Hi,
>>
>> i see. thank you
>>
>> I am using:
>>
>> [image: image.png]
>>
>> Best,
>> Jacek
>>
>> pon., 2 lip 2018 o 16:03 Adrian Klaver <adrian.klaver@aklaver.com>
>> napisał(a):
>>
>>> On 07/02/2018 06:57 AM, Łukasz Jarych wrote:
>>> > Hi,
>>> >
>>> > "
>>> > Strange. "audit_sq" looks like an invalid sequence table. I went
>>> > here,
>>> https://www.postgresql.org/docs/9.6/static/sql-createsequence.html,
>>> > and checked all the way back to version 7.1 and "maxvalue" has been a
>>> > column since back then.
>>>
>>> What version of Postgres are you actually doing the cloning in?
>>>
>>> Per Tom's post:
>>>
>>> https://www.postgresql.org/docs/10/static/release-10.html
>>>
>>> "
>>> Move sequences' metadata fields into a new pg_sequence system catalog
>>> (Peter Eisentraut)
>>>
>>> ...
>>>
>>> A sequence relation now stores only the fields that can be modified by
>>> nextval(), that is last_value, log_cnt, and is_called.
>>>
>>> ...
>>>
>>> The main incompatibility introduced by this change is that selecting
>>> from a sequence relation now returns only the three fields named above.
>>> To obtain the sequence's other properties, applications must look into
>>> pg_sequence. The new system view pg_sequences can also be used for this
>>> purpose; it provides column names that are more compatible with existing
>>> code.
>>> "
>>>
>>> >
>>> > Maybe skip that table for now? It even says the last value is 1. You
>>> > should also check the other sequence tables. You can get them by doing:
>>> >
>>> > select * from information_schema.sequences;"
>>> >
>>> > Result of select:
>>> >
>>> > image.png
>>> >
>>> > Are you sure that I can skip " audit_sq" seq?
>>> >
>>> > I wrote here because i am newbie and i can not update this. Hope for
>>> > your help Guys.
>>> >
>>> > Best,
>>> > Jacek
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> >
>>> > pon., 2 lip 2018 o 15:30 Tom Lane <tgl@sss.pgh.pa.us
>>> > <mailto:tgl@sss.pgh.pa.us>> napisał(a):
>>> >
>>> >     =?UTF-8?Q?=C5=81ukasz_Jarych?= <jaryszek@gmail.com
>>> >     <mailto:jaryszek@gmail.com>> writes:
>>> >      > I am trying to use :
>>> >
>>> >      > "select * from clone_schema('public','Version8',true) but i am
>>> >     getting
>>> >      > error:
>>> >
>>> >      > "Column "max_value" does not exist.
>>> >      > LINE 1: SELECT last_value, max_value, start_value, increment_by,
>>> >     min...
>>> >      > HINT: Maybe you wanted to point to column "
>>> "audit_sq.last_value"?
>>> >      > QUERY: SELECT last_value, max_value, start_value, increment_by,
>>> >     min_value,
>>> >      > cache_value, log_cnt, is_cycled, is_called FROM public.audit_sq;
>>> >      > CONTEXT: function PL/pgSQL clone_schema(text,text,boolean), row
>>> 66 in
>>> >      > EXECUTE
>>> >
>>> >     I guess audit_sq is a sequence?  It looks to me like this function
>>> has
>>> >     not been taught about the changes in sequence metadata in PG v10.
>>> >     You need to update it, or talk to its author about an update.
>>> >
>>> >                              regards, tom lane
>>> >
>>>
>>>
>>> --
>>> Adrian Klaver
>>> adrian.klaver@aklaver.com
>>>
>>
> Lukas,
>
> Once again, in Version 10, the developers have changed the system
> catalogs. Please use the attached clone_schema_10.sql which
> has been adjusted for PostgreSQL 10.
>
>
>
> --
> *Melvin Davidson*
> *Maj. Database & Exploration Specialist*
> *Universe Exploration Command – UXC*
> Employment by invitation only!
>

Attachments:

image.png (image/png, 19.6K) download view image
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
@ 2018-07-03 13:34             ` Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:27               ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  0 siblings, 2 replies; 35+ messages in thread

From: Melvin Davidson @ 2018-07-03 13:34 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

> ERROR: BŁĄD: you cannot add to column "TopoToVersion_ID" DETAIL: Column
> "TopoToVersion_ID" is an identity column defined as GENERATED ALWAYS. HINT:
> Use OVERRIDING SYSTEM VALUE to override. CONTEXT: wyrażenie SQL "INSERT
> INTO version4.t_topotoversions SELECT * FROM public.t_topotoversions;"
> funkcja PL/pgSQL clone_schema(text,text,boolean), wiersz 212 w EXECUTE
>

Lukasz,
That ERROR is occuring because you choose to copy the data (include_recs =
TRUE).
I have added OVERRIDING SYSTEM VALUE to the insert statement and attached
revised version.
CAUTION: The value of TopoToVersion_ID and any other IDENTITY columns may
be changed.

The revised version is attached.

Attachments:

clone_schema_10.sql (text/plain, 13.8K) download  -- Function: public.clone_schema(text, text, boolean) -- DROP FUNCTION public.clone_schema(text, text, boolean); CREATE OR REPLACE FUNCTION public.clone_schema( source_schema text, dest_schema text, include_recs boolean) RETURNS void AS $BODY$ -- Initial code by Emanuel '3manuek' -- Last revision 2018-07-03 by Melvin Davidson -- Added ' OVERRIDING SYSTEM VALUE' for INSERT records -- -- This function will clone all sequences, tables, indexes, rules, triggers, -- data(optional), views & functions from any existing schema to a new schema -- SAMPLE CALL: -- SELECT clone_schema('public', 'new_schema', TRUE); DECLARE src_oid oid; tbl_oid oid; seq_oid oid; func_oid oid; owner_id oid; con_oid oid; v_path text; v_func text; v_args text; v_conname text; v_rule text; v_trig text; object text; buffer text; srctbl text; default_ text; v_column text; qry text; dest_qry text; v_def text; v_stat integer; seqval bigint; seq_name name; sq_type oid; sq_last_value bigint; sq_max_value bigint; sq_start_value bigint; sq_increment_by bigint; sq_min_value bigint; sq_cache_value bigint; sq_log_cnt bigint; -- sq_is_called boolean; sq_is_cycled boolean; sq_typname name; seq_cycled text; seq_owner name; -- sq_cycled char(10); BEGIN -- Check that source_schema exists SELECT oid INTO src_oid FROM pg_namespace WHERE nspname = quote_ident(source_schema); IF NOT FOUND THEN RAISE NOTICE 'source schema % does not exist!', source_schema; RETURN ; END IF; -- Check that dest_schema does not yet exist PERFORM nspname FROM pg_namespace WHERE nspname = quote_ident(dest_schema); IF FOUND THEN RAISE NOTICE 'dest schema % already exists!', dest_schema; RETURN ; END IF; EXECUTE 'CREATE SCHEMA ' || quote_ident(dest_schema) ; -- Add schema comment SELECT description INTO v_def FROM pg_description WHERE objoid = src_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SCHEMA ' || quote_ident(dest_schema) || ' IS ' || quote_literal(v_def); END IF; SET search_path TO dest_schema, source_schema, 'public', pg_catalog; SELECT current_schemas(TRUE) INTO v_path; RAISE NOTICE 'search path = %', v_path; -- Create sequences -- TODO: Find a way to make this sequence's owner is the correct table. FOR object IN SELECT sequence_name::text FROM information_schema.sequences WHERE sequence_schema = quote_ident(source_schema) LOOP SELECT oid, relowner into seq_oid, owner_id FROM pg_class WHERE relname = quote_ident(object) AND relkind = 'S'; SELECT seqtypid, seqstart, seqincrement, seqmax, seqmin, seqcache, seqcycle INTO sq_type, sq_start_value, sq_increment_by, sq_max_value, sq_min_value, sq_cache_value, sq_is_cycled FROM pg_sequence WHERE seqrelid = seq_oid; SELECT typname INTO sq_typname FROM pg_type WHERE oid = sq_type; SELECT rolname INTO seq_owner FROM pg_authid WHERE oid = owner_id; -- EXECUTE 'CREATE SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object); -- srctbl := quote_ident(source_schema) || '.' || quote_ident(object); IF sq_is_cycled THEN seq_cycled = ' CYCLE'; ELSE seq_cycled = ' NO CYCLE'; END IF; EXECUTE 'CREATE SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' AS ' || sq_typname || ' INCREMENT ' || sq_increment_by || ' MINVALUE ' || sq_min_value || ' MAXVALUE ' || sq_max_value || ' START WITH ' || sq_start_value || ' CACHE ' || sq_cache_value || seq_cycled; -- || ' OWNED BY ' || seq_owner; /* buffer := quote_ident(dest_schema) || '.' || quote_ident(object); IF include_recs THEN EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_last_value || ', ' || sq_is_called || ');' ; ELSE EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_start_value || ', ' || sq_is_called || ');' ; END IF; */ -- add sequence comments SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'S' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create tables FOR object IN SELECT TABLE_NAME::text FROM information_schema.tables WHERE table_schema = quote_ident(source_schema) AND table_type = 'BASE TABLE' LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || quote_ident(source_schema) || '.' || quote_ident(object) || ' INCLUDING ALL)'; -- Add table comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; IF include_recs THEN -- Insert records from source table EXECUTE 'INSERT INTO ' || buffer || ' SELECT * FROM ' || quote_ident(source_schema) || '.' || quote_ident(object) || ' OVERRIDING SYSTEM VALUE;'; END IF; FOR v_column, default_ IN SELECT column_name::text, REPLACE(column_default::text, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.' ) FROM information_schema.COLUMNS WHERE table_schema = dest_schema AND TABLE_NAME = object AND column_default LIKE 'nextval(%' || quote_ident(source_schema) || '%::regclass)' LOOP EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || v_column || ' SET DEFAULT ' || default_; END LOOP; END LOOP; -- set column statistics FOR tbl_oid, srctbl IN SELECT oid, relname FROM pg_class WHERE relnamespace = src_oid AND relkind = 'r' LOOP FOR v_column, v_stat IN SELECT attname, attstattarget FROM pg_attribute WHERE attrelid = tbl_oid AND attnum > 0 LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(srctbl); -- RAISE EXCEPTION 'ALTER TABLE % ALTER COLUMN % SET STATISTICS %', buffer, v_column, v_stat::text; EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || quote_ident(v_column) || ' SET STATISTICS ' || v_stat || ';'; END LOOP; END LOOP; -- add FK constraint FOR qry IN SELECT 'ALTER TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(rn.relname) || ' ADD CONSTRAINT ' || quote_ident(ct.conname) || ' ' || replace(pg_get_constraintdef(ct.oid), quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') || ';' FROM pg_constraint ct JOIN pg_class rn ON rn.oid = ct.conrelid WHERE connamespace = src_oid AND rn.relkind = 'r' AND ct.contype = 'f' LOOP EXECUTE qry; END LOOP; -- Add constraint comment FOR con_oid IN SELECT oid FROM pg_constraint WHERE conrelid = tbl_oid LOOP SELECT conname INTO v_conname FROM pg_constraint WHERE oid = con_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid; IF FOUND THEN EXECUTE 'COMMENT ON CONSTRAINT ' || v_conname || ' ON ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create views FOR object IN SELECT table_name::text, view_definition FROM information_schema.views WHERE table_schema = quote_ident(source_schema) LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); SELECT view_definition INTO v_def FROM information_schema.views WHERE table_schema = quote_ident(source_schema) AND table_name = quote_ident(object); SELECT REPLACE(v_def, source_schema, dest_schema) INTO v_def; -- RAISE NOTICE 'view def, % , source % , dest % ', v_def, source_schema, dest_schema; EXECUTE 'CREATE OR REPLACE VIEW ' || buffer || ' AS ' || v_def || ';' ; -- Add comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'v' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON VIEW ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- Create functions FOR func_oid IN SELECT oid, proargnames FROM pg_proc WHERE pronamespace = src_oid LOOP SELECT pg_get_functiondef(func_oid) INTO qry; SELECT proname, oidvectortypes(proargtypes) INTO v_func, v_args FROM pg_proc WHERE oid = func_oid; SELECT replace(qry, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') INTO dest_qry; EXECUTE dest_qry; -- Add function comment SELECT description INTO v_def FROM pg_description WHERE objoid = func_oid AND objsubid = 0; IF FOUND THEN -- RAISE NOTICE 'func_oid %, object %, v_args %', func_oid::text, quote_ident(object), v_args; EXECUTE 'COMMENT ON FUNCTION ' || quote_ident(dest_schema) || '.' || quote_ident(v_func) || '(' || v_args || ')' || ' IS ' || quote_literal(v_def) ||';' ; END IF; END LOOP; -- add Rules SET search_path TO dest_schema; FOR v_def IN SELECT definition FROM pg_rules WHERE schemaname = quote_ident(source_schema) LOOP IF v_def IS NOT NULL THEN SELECT replace(v_def, source_schema || '.', dest_schema || '.') INTO v_def; EXECUTE ' ' || v_def; END IF; END LOOP; -- add triggers FOR v_def IN SELECT pg_get_triggerdef(oid) FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT replace(v_def, source_schema || '.', dest_schema || '.') INTO dest_qry; EXECUTE dest_qry; END LOOP; -- Disable inactive triggers -- D = disabled FOR tbl_oid IN SELECT oid FROM pg_trigger WHERE tgenabled = 'D' AND tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT t.tgname, c.relname INTO object, srctbl FROM pg_trigger t JOIN pg_class c ON c.oid = t.tgrelid WHERE t.oid = tbl_oid; IF FOUND THEN EXECUTE 'ALTER TABLE ' || dest_schema || '.' || srctbl || ' DISABLE TRIGGER ' || object || ';'; END IF; END LOOP; -- Add index comment FOR tbl_oid IN SELECT oid FROM pg_class WHERE relkind = 'i' AND relnamespace = src_oid LOOP SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON INDEX ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- add rule comments FOR con_oid IN SELECT oid, * FROM pg_rewrite WHERE rulename <> '_RETURN'::name LOOP SELECT rulename, ev_class INTO v_rule, tbl_oid FROM pg_rewrite WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON RULE ' || v_rule || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- add trigger comments FOR con_oid IN SELECT oid, * FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' LOOP SELECT tgname, tgrelid INTO v_trig, tbl_oid FROM pg_trigger WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TRIGGER ' || v_trig || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public.clone_schema(text, text, boolean) OWNER TO postgres; COMMENT ON FUNCTION public.clone_schema(text, text, boolean) IS 'Duplicates sequences, tables, indexes, rules, triggers, data(optional), views & functions from the source schema to the destination schema';
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-04 18:27               ` Łukasz Jarych <jaryszek@gmail.com>
  2018-07-04 18:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  1 sibling, 1 reply; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-04 18:27 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; pgsql-general

Hi Melvin,

thank you very much. Awesome!!!

Best,
Jacek


wt., 3 lip 2018 o 15:34 Melvin Davidson <melvin6925@gmail.com> napisał(a):

>
>
>
>> ERROR: BŁĄD: you cannot add to column "TopoToVersion_ID" DETAIL: Column
>> "TopoToVersion_ID" is an identity column defined as GENERATED ALWAYS. HINT:
>> Use OVERRIDING SYSTEM VALUE to override. CONTEXT: wyrażenie SQL "INSERT
>> INTO version4.t_topotoversions SELECT * FROM public.t_topotoversions;"
>> funkcja PL/pgSQL clone_schema(text,text,boolean), wiersz 212 w EXECUTE
>>
>
> Lukasz,
> That ERROR is occuring because you choose to copy the data (include_recs =
> TRUE).
> I have added OVERRIDING SYSTEM VALUE to the insert statement and attached
> revised version.
> CAUTION: The value of TopoToVersion_ID and any other IDENTITY columns may
> be changed.
>
> The revised version is attached.
>
>

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:27               ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
@ 2018-07-04 18:38                 ` Melvin Davidson <melvin6925@gmail.com>
  0 siblings, 0 replies; 35+ messages in thread

From: Melvin Davidson @ 2018-07-04 18:38 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; +Cc: pgsql-general

On Wed, Jul 4, 2018 at 2:27 PM, Łukasz Jarych <jaryszek@gmail.com> wrote:

> Hi Melvin,
>
>
>
> Best,
> Jacek
>
>
> wt., 3 lip 2018 o 15:34 Melvin Davidson <melvin6925@gmail.com> napisał(a):
>
>>
>>
>>
>>> ERROR: BŁĄD: you cannot add to column "TopoToVersion_ID" DETAIL: Column
>>> "TopoToVersion_ID" is an identity column defined as GENERATED ALWAYS. HINT:
>>> Use OVERRIDING SYSTEM VALUE to override. CONTEXT: wyrażenie SQL "INSERT
>>> INTO version4.t_topotoversions SELECT * FROM public.t_topotoversions;"
>>> funkcja PL/pgSQL clone_schema(text,text,boolean), wiersz 212 w EXECUTE
>>>
>>
>> Lukasz,
>> That ERROR is occuring because you choose to copy the data (include_recs
>> = TRUE).
>> I have added OVERRIDING SYSTEM VALUE to the insert statement and
>> attached revised version.
>> CAUTION: The value of TopoToVersion_ID and any other IDENTITY columns
>> may be changed.
>>
>> The revised version is attached.
>>
>>
> thank you very much. Awesome!!!

Jacek,
You are quite welcome.



-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-04 18:48               ` DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  1 sibling, 1 reply; 35+ messages in thread

From: DiasCosta @ 2018-07-04 18:48 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; Łukasz Jarych <jaryszek@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

Hi Melvin,

I'm new to clone_schema.
Can I use it on PostgreSQL 9.6?

TIA
DCosta



On 03-07-2018 14:34, Melvin Davidson wrote:
>
>
>
>     ERROR: BŁĄD: you cannot add to column "TopoToVersion_ID" DETAIL:
>     Column "TopoToVersion_ID" is an identity column defined as
>     GENERATED ALWAYS. HINT: Use OVERRIDING SYSTEM VALUE to override.
>     CONTEXT: wyrażenie SQL "INSERT INTO version4.t_topotoversions
>     SELECT * FROM public.t_topotoversions;" funkcja PL/pgSQL
>     clone_schema(text,text,boolean), wiersz 212 w EXECUTE
>
>
> Lukasz,
> That ERROR is occuring because you choose to copy the data 
> (include_recs = TRUE).
> I have added OVERRIDING SYSTEM VALUE to the insert statement and 
> attached revised version.
> CAUTION: The value of TopoToVersion_ID and any other IDENTITY columns 
> may be changed.
>
> The revised version is attached.
>

-- 
J. M. Dias Costa
Telef. 214026948     Telem. 939307421

Se divulgar esta mensagem por terceiros, por favor:
1. Apague o meu endereço de correio electrónico e o meu nome.
2. Apague também os endereços dos seus amigos antes de distribuir.
3. Enderece como cópia oculta (Cc ou Bcc) para os SEUS destinatários.
Agindo deste modo, dificultará a disseminação de "vírus", "spams" e
"banners" e contribuirá para manter a privacidade de todos e cada um.
Obrigado.

Nota: Não se deverá ao acaso a ocorrência de palavras na minha escrita que não respeitem o
malfadado acordo ortográfico.

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
@ 2018-07-04 22:38                 ` Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 22:48                   ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  0 siblings, 2 replies; 35+ messages in thread

From: Melvin Davidson @ 2018-07-04 22:38 UTC (permalink / raw)
  To: DiasCosta <diascosta@diascosta.org>; +Cc: Łukasz Jarych <jaryszek@gmail.com>; Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

On Wed, Jul 4, 2018 at 2:48 PM, DiasCosta <diascosta@diascosta.org> wrote:

> Hi Melvin,
>
> I'm new to clone_schema.
> Can I use it on PostgreSQL 9.6?
>
> TIA
> DCostaployment by invitation only!
>

> Can I use it on PostgreSQL 9.6?
Yes, but because the developer(s) once again monkeyed with the system
catalogs, there are now
two versions. One for 10 and one for 9.6 and below. I've attached the 9.6
version for you.

Attachments:

clone_schema.sql (text/plain, 13.9K) download -- Function: public.clone_schema(text, text, boolean) -- DROP FUNCTION public.clone_schema(text, text, boolean); CREATE OR REPLACE FUNCTION public.clone_schema( source_schema text, dest_schema text, include_recs boolean) RETURNS void AS $BODY$ -- Initial code by Emanuel '3manuek' -- Last revision 2017-04-17 by Melvin Davidson -- Added SELECT REPLACE for schema views -- -- This function will clone all sequences, tables, indexes, rules, triggers, -- data(optional), views & functions from any existing schema to a new schema -- SAMPLE CALL: -- SELECT clone_schema('public', 'new_schema', TRUE); DECLARE src_oid oid; tbl_oid oid; func_oid oid; con_oid oid; v_path text; v_func text; v_args text; v_conname text; v_rule text; v_trig text; object text; buffer text; srctbl text; default_ text; v_column text; qry text; dest_qry text; v_def text; v_stat integer; seqval bigint; sq_last_value bigint; sq_max_value bigint; sq_start_value bigint; sq_increment_by bigint; sq_min_value bigint; sq_cache_value bigint; sq_log_cnt bigint; sq_is_called boolean; sq_is_cycled boolean; sq_cycled char(10); BEGIN -- Check that source_schema exists SELECT oid INTO src_oid FROM pg_namespace WHERE nspname = quote_ident(source_schema); IF NOT FOUND THEN RAISE NOTICE 'source schema % does not exist!', source_schema; RETURN ; END IF; -- Check that dest_schema does not yet exist PERFORM nspname FROM pg_namespace WHERE nspname = quote_ident(dest_schema); IF FOUND THEN RAISE NOTICE 'dest schema % already exists!', dest_schema; RETURN ; END IF; EXECUTE 'CREATE SCHEMA ' || quote_ident(dest_schema) ; -- Add schema comment SELECT description INTO v_def FROM pg_description WHERE objoid = src_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SCHEMA ' || quote_ident(dest_schema) || ' IS ' || quote_literal(v_def); END IF; SET search_path TO dest_schema, source_schema, 'public', pg_catalog; SELECT current_schemas(TRUE) INTO v_path; RAISE NOTICE 'search path = %', v_path; -- Create sequences -- TODO: Find a way to make this sequence's owner is the correct table. FOR object IN SELECT sequence_name::text FROM information_schema.sequences WHERE sequence_schema = quote_ident(source_schema) LOOP EXECUTE 'CREATE SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object); srctbl := quote_ident(source_schema) || '.' || quote_ident(object); EXECUTE 'SELECT last_value, max_value, start_value, increment_by, min_value, cache_value, log_cnt, is_cycled, is_called FROM ' || quote_ident(source_schema) || '.' || quote_ident(object) || ';' INTO sq_last_value, sq_max_value, sq_start_value, sq_increment_by, sq_min_value, sq_cache_value, sq_log_cnt, sq_is_cycled, sq_is_called ; IF sq_is_cycled THEN sq_cycled := 'CYCLE'; ELSE sq_cycled := 'NO CYCLE'; END IF; EXECUTE 'ALTER SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' INCREMENT BY ' || sq_increment_by || ' MINVALUE ' || sq_min_value || ' MAXVALUE ' || sq_max_value || ' START WITH ' || sq_start_value || ' RESTART ' || sq_min_value || ' CACHE ' || sq_cache_value || sq_cycled || ' ;' ; buffer := quote_ident(dest_schema) || '.' || quote_ident(object); IF include_recs THEN EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_last_value || ', ' || sq_is_called || ');' ; ELSE EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_start_value || ', ' || sq_is_called || ');' ; END IF; -- add sequence comments SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'S' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create tables FOR object IN SELECT TABLE_NAME::text FROM information_schema.tables WHERE table_schema = quote_ident(source_schema) AND table_type = 'BASE TABLE' LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || quote_ident(source_schema) || '.' || quote_ident(object) || ' INCLUDING ALL)'; -- Add table comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; IF include_recs THEN -- Insert records from source table EXECUTE 'INSERT INTO ' || buffer || ' SELECT * FROM ' || quote_ident(source_schema) || '.' || quote_ident(object) || ';'; END IF; FOR v_column, default_ IN SELECT column_name::text, REPLACE(column_default::text, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.' ) FROM information_schema.COLUMNS WHERE table_schema = dest_schema AND TABLE_NAME = object AND column_default LIKE 'nextval(%' || quote_ident(source_schema) || '%::regclass)' LOOP EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || v_column || ' SET DEFAULT ' || default_; END LOOP; END LOOP; -- set column statistics FOR tbl_oid, srctbl IN SELECT oid, relname FROM pg_class WHERE relnamespace = src_oid AND relkind = 'r' LOOP FOR v_column, v_stat IN SELECT attname, attstattarget FROM pg_attribute WHERE attrelid = tbl_oid AND attnum > 0 LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(srctbl); -- RAISE EXCEPTION 'ALTER TABLE % ALTER COLUMN % SET STATISTICS %', buffer, v_column, v_stat::text; EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || quote_ident(v_column) || ' SET STATISTICS ' || v_stat || ';'; END LOOP; END LOOP; -- add FK constraint FOR qry IN SELECT 'ALTER TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(rn.relname) || ' ADD CONSTRAINT ' || quote_ident(ct.conname) || ' ' || replace(pg_get_constraintdef(ct.oid), quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') || ';' FROM pg_constraint ct JOIN pg_class rn ON rn.oid = ct.conrelid WHERE connamespace = src_oid AND rn.relkind = 'r' AND ct.contype = 'f' LOOP EXECUTE qry; END LOOP; -- Add constraint comment FOR con_oid IN SELECT oid FROM pg_constraint WHERE conrelid = tbl_oid LOOP SELECT conname INTO v_conname FROM pg_constraint WHERE oid = con_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid; IF FOUND THEN EXECUTE 'COMMENT ON CONSTRAINT ' || v_conname || ' ON ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create views FOR object IN SELECT table_name::text, view_definition FROM information_schema.views WHERE table_schema = quote_ident(source_schema) LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); SELECT view_definition INTO v_def FROM information_schema.views WHERE table_schema = quote_ident(source_schema) AND table_name = quote_ident(object); SELECT REPLACE(v_def, source_schema, dest_schema) INTO v_def; -- RAISE NOTICE 'view def, % , source % , dest % ', v_def, source_schema, dest_schema; EXECUTE 'CREATE OR REPLACE VIEW ' || buffer || ' AS ' || v_def || ';' ; -- Add comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'v' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON VIEW ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- Create functions FOR func_oid IN SELECT oid, proargnames FROM pg_proc WHERE pronamespace = src_oid LOOP SELECT pg_get_functiondef(func_oid) INTO qry; SELECT proname, oidvectortypes(proargtypes) INTO v_func, v_args FROM pg_proc WHERE oid = func_oid; SELECT replace(qry, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') INTO dest_qry; EXECUTE dest_qry; -- Add function comment SELECT description INTO v_def FROM pg_description WHERE objoid = func_oid AND objsubid = 0; IF FOUND THEN -- RAISE NOTICE 'func_oid %, object %, v_args %', func_oid::text, quote_ident(object), v_args; EXECUTE 'COMMENT ON FUNCTION ' || quote_ident(dest_schema) || '.' || quote_ident(v_func) || '(' || v_args || ')' || ' IS ' || quote_literal(v_def) ||';' ; END IF; END LOOP; -- add Rules SET search_path TO dest_schema; FOR v_def IN SELECT definition FROM pg_rules WHERE schemaname = quote_ident(source_schema) LOOP IF v_def IS NOT NULL THEN SELECT replace(v_def, source_schema || '.', dest_schema || '.') INTO v_def; RAISE NOTICE 'new rule %', v_def; EXECUTE ' ' || v_def; END IF; END LOOP; -- add triggers FOR v_def IN SELECT pg_get_triggerdef(oid) FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT replace(v_def, source_schema || '.', dest_schema || '.') INTO dest_qry; EXECUTE dest_qry; END LOOP; -- Disable inactive triggers -- D = disabled FOR tbl_oid IN SELECT oid FROM pg_trigger WHERE tgenabled = 'D' AND tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT t.tgname, c.relname INTO object, srctbl FROM pg_trigger t JOIN pg_class c ON c.oid = t.tgrelid WHERE t.oid = tbl_oid; IF FOUND THEN EXECUTE 'ALTER TABLE ' || dest_schema || '.' || srctbl || ' DISABLE TRIGGER ' || object || ';'; END IF; END LOOP; -- Add index comment FOR tbl_oid IN SELECT oid FROM pg_class WHERE relkind = 'i' AND relnamespace = src_oid LOOP SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON INDEX ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- add rule comments FOR con_oid IN SELECT oid, * FROM pg_rewrite WHERE rulename <> '_RETURN'::name LOOP SELECT rulename, ev_class INTO v_rule, tbl_oid FROM pg_rewrite WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON RULE ' || v_rule || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- add trigger comments FOR con_oid IN SELECT oid, * FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' LOOP SELECT tgname, tgrelid INTO v_trig, tbl_oid FROM pg_trigger WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TRIGGER ' || v_trig || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public.clone_schema(text, text, boolean) OWNER TO postgres; COMMENT ON FUNCTION public.clone_schema(text, text, boolean) IS 'Duplicates sequences, tables, indexes, rules, triggers, data(optional), views & functions from the source schema to the destination schema';
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-04 22:48                   ` Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 22:53                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  1 sibling, 1 reply; 35+ messages in thread

From: Adrian Klaver @ 2018-07-04 22:48 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; DiasCosta <diascosta@diascosta.org>; +Cc: Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

On 07/04/2018 03:38 PM, Melvin Davidson wrote:
> 
> 
> On Wed, Jul 4, 2018 at 2:48 PM, DiasCosta <diascosta@diascosta.org 
> <mailto:diascosta@diascosta.org>> wrote:
> 
>     Hi Melvin,
> 
>     I'm new to clone_schema.
>     Can I use it on PostgreSQL 9.6?
> 
>     TIA
>     DCostaployment by invitation only!
> 
> 
>  > Can I use it on PostgreSQL 9.6?
> 
> Yes, but because the developer(s) once again monkeyed with the system 
> catalogs, there are now

Well that is one of the things that distinguish a major release so it 
should be no surprise.

> two versions. One for 10 and one for 9.6 and below. I've attached the 
> 9.6 version for you.
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 22:48                   ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-07-04 22:53                     ` Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 23:00                       ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Melvin Davidson @ 2018-07-04 22:53 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: DiasCosta <diascosta@diascosta.org>; Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

On Wed, Jul 4, 2018 at 6:48 PM, Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> On 07/04/2018 03:38 PM, Melvin Davidson wrote:
>
>>
>>
>> On Wed, Jul 4, 2018 at 2:48 PM, DiasCosta <diascosta@diascosta.org
>> <mailto:diascosta@diascosta.org>> wrote:
>>
>>     Hi Melvin,
>>
>>     I'm new to clone_schema.
>>     Can I use it on PostgreSQL 9.6?
>>
>>     TIA
>>     DCostaployment by invitation only!
>>
>>
>>  > Can I use it on PostgreSQL 9.6?
>>
>> Yes, but because the developer(s) once again monkeyed with the system
>> catalogs, there are now
>>
>
> Well that is one of the things that distinguish a major release so it
> should be no surprise.
>
>
>
The problem is, AFAICS, none of the changes induced were really necessary
or increased performance.


*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 22:48                   ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 22:53                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-04 23:00                       ` Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 23:08                         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Adrian Klaver @ 2018-07-04 23:00 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; +Cc: DiasCosta <diascosta@diascosta.org>; Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

On 07/04/2018 03:53 PM, Melvin Davidson wrote:
> 
> 

> 
> 
> The problem is, AFAICS, none of the changes induced were really 
> necessary or increased performance.

The folks that wanted transactional ALTER SEQUENCE might disagree:):

https://www.postgresql.org/docs/10/static/release-10.html
"Move sequences' metadata fields into a new pg_sequence system catalog 
(Peter Eisentraut)

A sequence relation now stores only the fields that can be modified by 
nextval(), that is last_value, log_cnt, and is_called. Other sequence 
properties, such as the starting value and increment, are kept in a 
corresponding row of the pg_sequence catalog. ALTER SEQUENCE updates are 
now fully transactional, implying that the sequence is locked until 
commit. The nextval() and setval() functions remain nontransactional.

...
"

> 
> 
> *Melvin Davidson**
> Maj. Database & Exploration Specialist**
> Universe Exploration Command – UXC***
> Employment by invitation only!


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 22:48                   ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 22:53                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 23:00                       ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-07-04 23:08                         ` Melvin Davidson <melvin6925@gmail.com>
  2018-07-05 05:49                           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Melvin Davidson @ 2018-07-04 23:08 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: DiasCosta <diascosta@diascosta.org>; Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

>The folks that wanted transactional ALTER SEQUENCE might disagree:):
Ah, so you mean the previous version was not working or sufficient?

https://www.postgresql.org/docs/9.6/static/sql-altersequence.html

-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 22:48                   ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 22:53                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 23:00                       ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 23:08                         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-05 05:49                           ` Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 05:49                             ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-05 05:49 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; diascosta@diascosta.org; pgsql-general

Hi Melvin.

folks wrote only that it is old version and didnt care :)

Tahnk you very much,
Best,
Jacek

czw., 5 lip 2018 o 01:09 Melvin Davidson <melvin6925@gmail.com> napisał(a):

>
> >The folks that wanted transactional ALTER SEQUENCE might disagree:):
> Ah, so you mean the previous version was not working or sufficient?
>
> https://www.postgresql.org/docs/9.6/static/sql-altersequence.html
>
> --
> *Melvin Davidson*
> *Maj. Database & Exploration Specialist*
> *Universe Exploration Command – UXC*
> Employment by invitation only!
>

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 22:48                   ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 22:53                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 23:00                       ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 23:08                         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-05 05:49                           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
@ 2018-07-05 05:49                             ` Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 14:01                               ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-05 05:49 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; diascosta@diascosta.org; pgsql-general



^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 22:48                   ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 22:53                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 23:00                       ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 23:08                         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-05 05:49                           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 05:49                             ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
@ 2018-07-05 14:01                               ` Melvin Davidson <melvin6925@gmail.com>
  2018-07-05 14:38                                 ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Melvin Davidson @ 2018-07-05 14:01 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; DiasCosta <diascosta@diascosta.org>; pgsql-general

On Thu, Jul 5, 2018 at 1:49 AM, Łukasz Jarych <jaryszek@gmail.com> wrote:

> From link function is not working.
>

There is no " From link" in PostgreSQL,
Would you please be more specific. Please provide a working example.

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 22:48                   ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 22:53                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 23:00                       ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 23:08                         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-05 05:49                           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 05:49                             ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 14:01                               ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-05 14:38                                 ` Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 14:52                                   ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-05 14:38 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; pgsql-general

You gave me working example.

the function from here is not working:

https://www.postgresql.org/message-id/CANu8FiyJtt-0q%3DbkUxyra66tHi6FFzgU8TqVR2aahseCBDDntA%40mail.g...

Best,
Jacek

czw., 5 lip 2018 o 16:02 Melvin Davidson <melvin6925@gmail.com> napisał(a):

>
>
> On Thu, Jul 5, 2018 at 1:49 AM, Łukasz Jarych <jaryszek@gmail.com> wrote:
>
>> From link function is not working.
>>
>
> There is no " From link" in PostgreSQL,
> Would you please be more specific. Please provide a working example.
>
>

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 22:48                   ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 22:53                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 23:00                       ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 23:08                         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-05 05:49                           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 05:49                             ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 14:01                               ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-05 14:38                                 ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
@ 2018-07-05 14:52                                   ` Melvin Davidson <melvin6925@gmail.com>
  2018-07-05 14:58                                     ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Melvin Davidson @ 2018-07-05 14:52 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; +Cc: pgsql-general

On Thu, Jul 5, 2018 at 10:38 AM, Łukasz Jarych <jaryszek@gmail.com> wrote:

> You gave me working example.
>
> the function from here is not working:
>
> https://www.postgresql.org/message-id/CANu8FiyJtt-0q%
> 3DbkUxyra66tHi6FFzgU8TqVR2aahseCBDDntA%40mail.gmail.com
>
> Best,
> Jacek
>
> czw., 5 lip 2018 o 16:02 Melvin Davidson <melvin6925@gmail.com>
> napisał(a):
>
>>
>>
>> On Thu, Jul 5, 2018 at 1:49 AM, Łukasz Jarych <jaryszek@gmail.com> wrote:
>>
>>> From link function is not working.
>>>
>>
>> There is no " From link" in PostgreSQL,
>> Would you please be more specific. Please provide a working example.
>>
>>
>the function from here is not working:

>https://www.postgresql.org/message-id/CANu8FiyJtt-0q%
3DbkUxyra66tHi6FFzgU8TqVR2aahseCBDDntA%40mail.gmail.com
<https://www.postgresql.org/message-id/CANu8FiyJtt-0q%3DbkUxyra66tHi6FFzgU8TqVR2aahseCBDDntA%40mail.g...;


Jacek,

That is NOT the version for PostgreSQL 10. It is for 9.6 and below only!
I attached the working function for 10 and it is attached again to this
response.


-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 22:48                   ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 22:53                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 23:00                       ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-04 23:08                         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-05 05:49                           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 05:49                             ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 14:01                               ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-05 14:38                                 ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-05 14:52                                   ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-05 14:58                                     ` Łukasz Jarych <jaryszek@gmail.com>
  0 siblings, 0 replies; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-05 14:58 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; +Cc: pgsql-general

Melvin,

thank you once again ! Yes and this is working like a charm,
I love your function and file !

Best,
Jacek

czw., 5 lip 2018 o 16:53 Melvin Davidson <melvin6925@gmail.com> napisał(a):

>
>
>
> On Thu, Jul 5, 2018 at 10:38 AM, Łukasz Jarych <jaryszek@gmail.com> wrote:
>
>> You gave me working example.
>>
>> the function from here is not working:
>>
>>
>> https://www.postgresql.org/message-id/CANu8FiyJtt-0q%3DbkUxyra66tHi6FFzgU8TqVR2aahseCBDDntA%40mail.g...
>>
>> Best,
>> Jacek
>>
>> czw., 5 lip 2018 o 16:02 Melvin Davidson <melvin6925@gmail.com>
>> napisał(a):
>>
>>>
>>>
>>> On Thu, Jul 5, 2018 at 1:49 AM, Łukasz Jarych <jaryszek@gmail.com>
>>> wrote:
>>>
>>>> From link function is not working.
>>>>
>>>
>>> There is no " From link" in PostgreSQL,
>>> Would you please be more specific. Please provide a working example.
>>>
>>>
> >the function from here is not working:
>
>
> >https://www.postgresql.org/message-id/CANu8FiyJtt-0q%3DbkUxyra66tHi6FFzgU8TqVR2aahseCBDDntA%40mail.g...
> <https://www.postgresql.org/message-id/CANu8FiyJtt-0q%3DbkUxyra66tHi6FFzgU8TqVR2aahseCBDDntA%40mail.g...;
>
>
> Jacek,
>
> That is NOT the version for PostgreSQL 10. It is for 9.6 and below only!
> I attached the working function for 10 and it is attached again to this
> response.
>
>
> --
> *Melvin Davidson*
> *Maj. Database & Exploration Specialist*
> *Universe Exploration Command – UXC*
> Employment by invitation only!
>

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-07 08:32                   ` DiasCosta <diascosta@diascosta.org>
  2018-07-07 20:19                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 16:32                     ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  1 sibling, 2 replies; 35+ messages in thread

From: DiasCosta @ 2018-07-07 08:32 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; +Cc: Łukasz Jarych <jaryszek@gmail.com>; Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

Hi Melvin,

Thank you.

Dias Costa

On 04-07-2018 23:38, Melvin Davidson wrote:
>
>
> On Wed, Jul 4, 2018 at 2:48 PM, DiasCosta <diascosta@diascosta.org 
> <mailto:diascosta@diascosta.org>> wrote:
>
>     Hi Melvin,
>
>     I'm new to clone_schema.
>     Can I use it on PostgreSQL 9.6?
>
>     TIA
>     DCostaployment by invitation only!
>
>
> > Can I use it on PostgreSQL 9.6?
>
> Yes, but because the developer(s) once again monkeyed with the system 
> catalogs, there are now
> two versions. One for 10 and one for 9.6 and below. I've attached the 
> 9.6 version for you.
>

-- 
J. M. Dias Costa
Telef. 214026948     Telem. 939307421

Se divulgar esta mensagem por terceiros, por favor:
1. Apague o meu endereço de correio electrónico e o meu nome.
2. Apague também os endereços dos seus amigos antes de distribuir.
3. Enderece como cópia oculta (Cc ou Bcc) para os SEUS destinatários.
Agindo deste modo, dificultará a disseminação de "vírus", "spams" e
"banners" e contribuirá para manter a privacidade de todos e cada um.
Obrigado.

Nota: Não se deverá ao acaso a ocorrência de palavras na minha escrita que não respeitem o
malfadado acordo ortográfico.

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
@ 2018-07-07 20:19                     ` Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 09:14                       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  1 sibling, 1 reply; 35+ messages in thread

From: Melvin Davidson @ 2018-07-07 20:19 UTC (permalink / raw)
  To: DiasCosta <diascosta@diascosta.org>; +Cc: Łukasz Jarych <jaryszek@gmail.com>; Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

2018-07-07 4:32 GMT-04:00 DiasCosta <diascosta@diascosta.org>:

> Hi Melvin,
>
> Thank you.
>
> Dias Costa
>
> On 04-07-2018 23:38, Melvin Davidson wrote:
>
>
>
> On Wed, Jul 4, 2018 at 2:48 PM, DiasCosta <diascosta@diascosta.org> wrote:
>
>> Hi Melvin,
>>
>> I'm new to clone_schema.
>> Can I use it on PostgreSQL 9.6?
>>
>> TIA
>> DCostaployment by invitation only!
>>
>
> > Can I use it on PostgreSQL 9.6?
> Yes, but because the developer(s) once again monkeyed with the system
> catalogs, there are now
> two versions. One for 10 and one for 9.6 and below. I've attached the 9.6
> version for you.
>
>
>
>
> --
> J. M. Dias Costa
> Telef. 214026948     Telem. 939307421
>
> Se divulgar esta mensagem por terceiros, por favor:
> 1. Apague o meu endereço de correio electrónico e o meu nome.
> 2. Apague também os endereços dos seus amigos antes de distribuir.
> 3. Enderece como cópia oculta (Cc ou Bcc) para os SEUS destinatários.
> Agindo deste modo, dificultará a disseminação de "vírus", "spams" e
> "banners" e contribuirá para manter a privacidade de todos e cada um.
> Obrigado.
>
> Nota: Não se deverá ao acaso a ocorrência de palavras na minha escrita que não respeitem o
> malfadado acordo ortográfico.
>
>
You are welcome Dias!
Good to have positive feedback.


-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-07 20:19                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-09 09:14                       ` Łukasz Jarych <jaryszek@gmail.com>
  2018-07-09 13:04                         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Łukasz Jarych @ 2018-07-09 09:14 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; +Cc: diascosta@diascosta.org; Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

Hi Melvin,

i am trying to run postgresql 10 cloning schema function but still i am
getting error...

[image: image.png]

Error: Error in syntax near "SYSTEM"
Context: Function PL/pgSQL, row 212 in EXECUTE

What is happening?

Best,
Jacek


sob., 7 lip 2018 o 22:20 Melvin Davidson <melvin6925@gmail.com> napisał(a):

>
>
> 2018-07-07 4:32 GMT-04:00 DiasCosta <diascosta@diascosta.org>:
>
>> Hi Melvin,
>>
>> Thank you.
>>
>> Dias Costa
>>
>> On 04-07-2018 23:38, Melvin Davidson wrote:
>>
>>
>>
>> On Wed, Jul 4, 2018 at 2:48 PM, DiasCosta <diascosta@diascosta.org>
>> wrote:
>>
>>> Hi Melvin,
>>>
>>> I'm new to clone_schema.
>>> Can I use it on PostgreSQL 9.6?
>>>
>>> TIA
>>> DCostaployment by invitation only!
>>>
>>
>> > Can I use it on PostgreSQL 9.6?
>> Yes, but because the developer(s) once again monkeyed with the system
>> catalogs, there are now
>> two versions. One for 10 and one for 9.6 and below. I've attached the 9.6
>> version for you.
>>
>>
>>
>>
>> --
>> J. M. Dias Costa
>> Telef. 214026948     Telem. 939307421
>>
>> Se divulgar esta mensagem por terceiros, por favor:
>> 1. Apague o meu endereço de correio electrónico e o meu nome.
>> 2. Apague também os endereços dos seus amigos antes de distribuir.
>> 3. Enderece como cópia oculta (Cc ou Bcc) para os SEUS destinatários.
>> Agindo deste modo, dificultará a disseminação de "vírus", "spams" e
>> "banners" e contribuirá para manter a privacidade de todos e cada um.
>> Obrigado.
>>
>> Nota: Não se deverá ao acaso a ocorrência de palavras na minha escrita que não respeitem o
>> malfadado acordo ortográfico.
>>
>>
> You are welcome Dias!
> Good to have positive feedback.
>
>
> --
> *Melvin Davidson*
> *Maj. Database & Exploration Specialist*
> *Universe Exploration Command – UXC*
> Employment by invitation only!
>

Attachments:

image.png (image/png, 30.9K) download view image
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-07 20:19                     ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 09:14                       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
@ 2018-07-09 13:04                         ` Melvin Davidson <melvin6925@gmail.com>
  0 siblings, 0 replies; 35+ messages in thread

From: Melvin Davidson @ 2018-07-09 13:04 UTC (permalink / raw)
  To: Łukasz Jarych <jaryszek@gmail.com>; +Cc: DiasCosta <diascosta@diascosta.org>; Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

On Mon, Jul 9, 2018 at 5:14 AM, Łukasz Jarych <jaryszek@gmail.com> wrote:

> Hi Melvin,
>
> i am trying to run postgresql 10 cloning schema function but still i am
> getting error...
>
> [image: image.png]
>
> Error: Error in syntax near "SYSTEM"
> Context: Function PL/pgSQL, row 212 in EXECUTE
>
> What is happening?
>
> Best,
> Jacek
>
> > Error: Error in syntax near "SYSTEM"

Jacek,
I have changed the code from OVERRIDING SYSTEM VALUE to OVERRIDING USER
VALUE
and attached the revised version below.

If that does not fix the problem, then I will need you to do a
pgdump -F p -t public.t_cpuinfo

and send the output to me.

-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

Attachments:

image.png (image/png, 30.9K) download view image
clone_schema_10.sql (text/plain, 13.8K) download  -- Function: public.clone_schema(text, text, boolean) -- DROP FUNCTION public.clone_schema(text, text, boolean); CREATE OR REPLACE FUNCTION public.clone_schema( source_schema text, dest_schema text, include_recs boolean) RETURNS void AS $BODY$ -- Initial code by Emanuel '3manuek' -- Last revision 2018-07-03 by Melvin Davidson -- Added ' OVERRIDING USER VALUE' for INSERT records -- -- This function will clone all sequences, tables, indexes, rules, triggers, -- data(optional), views & functions from any existing schema to a new schema -- SAMPLE CALL: -- SELECT clone_schema('public', 'new_schema', TRUE); DECLARE src_oid oid; tbl_oid oid; seq_oid oid; func_oid oid; owner_id oid; con_oid oid; v_path text; v_func text; v_args text; v_conname text; v_rule text; v_trig text; object text; buffer text; srctbl text; default_ text; v_column text; qry text; dest_qry text; v_def text; v_stat integer; seqval bigint; seq_name name; sq_type oid; sq_last_value bigint; sq_max_value bigint; sq_start_value bigint; sq_increment_by bigint; sq_min_value bigint; sq_cache_value bigint; sq_log_cnt bigint; -- sq_is_called boolean; sq_is_cycled boolean; sq_typname name; seq_cycled text; seq_owner name; -- sq_cycled char(10); BEGIN -- Check that source_schema exists SELECT oid INTO src_oid FROM pg_namespace WHERE nspname = quote_ident(source_schema); IF NOT FOUND THEN RAISE NOTICE 'source schema % does not exist!', source_schema; RETURN ; END IF; -- Check that dest_schema does not yet exist PERFORM nspname FROM pg_namespace WHERE nspname = quote_ident(dest_schema); IF FOUND THEN RAISE NOTICE 'dest schema % already exists!', dest_schema; RETURN ; END IF; EXECUTE 'CREATE SCHEMA ' || quote_ident(dest_schema) ; -- Add schema comment SELECT description INTO v_def FROM pg_description WHERE objoid = src_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SCHEMA ' || quote_ident(dest_schema) || ' IS ' || quote_literal(v_def); END IF; SET search_path TO dest_schema, source_schema, 'public', pg_catalog; SELECT current_schemas(TRUE) INTO v_path; RAISE NOTICE 'search path = %', v_path; -- Create sequences -- TODO: Find a way to make this sequence's owner is the correct table. FOR object IN SELECT sequence_name::text FROM information_schema.sequences WHERE sequence_schema = quote_ident(source_schema) LOOP SELECT oid, relowner into seq_oid, owner_id FROM pg_class WHERE relname = quote_ident(object) AND relkind = 'S'; SELECT seqtypid, seqstart, seqincrement, seqmax, seqmin, seqcache, seqcycle INTO sq_type, sq_start_value, sq_increment_by, sq_max_value, sq_min_value, sq_cache_value, sq_is_cycled FROM pg_sequence WHERE seqrelid = seq_oid; SELECT typname INTO sq_typname FROM pg_type WHERE oid = sq_type; SELECT rolname INTO seq_owner FROM pg_authid WHERE oid = owner_id; -- EXECUTE 'CREATE SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object); -- srctbl := quote_ident(source_schema) || '.' || quote_ident(object); IF sq_is_cycled THEN seq_cycled = ' CYCLE'; ELSE seq_cycled = ' NO CYCLE'; END IF; EXECUTE 'CREATE SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' AS ' || sq_typname || ' INCREMENT ' || sq_increment_by || ' MINVALUE ' || sq_min_value || ' MAXVALUE ' || sq_max_value || ' START WITH ' || sq_start_value || ' CACHE ' || sq_cache_value || seq_cycled; -- || ' OWNED BY ' || seq_owner; /* buffer := quote_ident(dest_schema) || '.' || quote_ident(object); IF include_recs THEN EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_last_value || ', ' || sq_is_called || ');' ; ELSE EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_start_value || ', ' || sq_is_called || ');' ; END IF; */ -- add sequence comments SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'S' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create tables FOR object IN SELECT TABLE_NAME::text FROM information_schema.tables WHERE table_schema = quote_ident(source_schema) AND table_type = 'BASE TABLE' LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || quote_ident(source_schema) || '.' || quote_ident(object) || ' INCLUDING ALL)'; -- Add table comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; IF include_recs THEN -- Insert records from source table EXECUTE 'INSERT INTO ' || buffer || ' SELECT * FROM ' || quote_ident(source_schema) || '.' || quote_ident(object) || ' OVERRIDING USER VALUE;'; END IF; FOR v_column, default_ IN SELECT column_name::text, REPLACE(column_default::text, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.' ) FROM information_schema.COLUMNS WHERE table_schema = dest_schema AND TABLE_NAME = object AND column_default LIKE 'nextval(%' || quote_ident(source_schema) || '%::regclass)' LOOP EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || v_column || ' SET DEFAULT ' || default_; END LOOP; END LOOP; -- set column statistics FOR tbl_oid, srctbl IN SELECT oid, relname FROM pg_class WHERE relnamespace = src_oid AND relkind = 'r' LOOP FOR v_column, v_stat IN SELECT attname, attstattarget FROM pg_attribute WHERE attrelid = tbl_oid AND attnum > 0 LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(srctbl); -- RAISE EXCEPTION 'ALTER TABLE % ALTER COLUMN % SET STATISTICS %', buffer, v_column, v_stat::text; EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || quote_ident(v_column) || ' SET STATISTICS ' || v_stat || ';'; END LOOP; END LOOP; -- add FK constraint FOR qry IN SELECT 'ALTER TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(rn.relname) || ' ADD CONSTRAINT ' || quote_ident(ct.conname) || ' ' || replace(pg_get_constraintdef(ct.oid), quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') || ';' FROM pg_constraint ct JOIN pg_class rn ON rn.oid = ct.conrelid WHERE connamespace = src_oid AND rn.relkind = 'r' AND ct.contype = 'f' LOOP EXECUTE qry; END LOOP; -- Add constraint comment FOR con_oid IN SELECT oid FROM pg_constraint WHERE conrelid = tbl_oid LOOP SELECT conname INTO v_conname FROM pg_constraint WHERE oid = con_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid; IF FOUND THEN EXECUTE 'COMMENT ON CONSTRAINT ' || v_conname || ' ON ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create views FOR object IN SELECT table_name::text, view_definition FROM information_schema.views WHERE table_schema = quote_ident(source_schema) LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); SELECT view_definition INTO v_def FROM information_schema.views WHERE table_schema = quote_ident(source_schema) AND table_name = quote_ident(object); SELECT REPLACE(v_def, source_schema, dest_schema) INTO v_def; -- RAISE NOTICE 'view def, % , source % , dest % ', v_def, source_schema, dest_schema; EXECUTE 'CREATE OR REPLACE VIEW ' || buffer || ' AS ' || v_def || ';' ; -- Add comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'v' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON VIEW ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- Create functions FOR func_oid IN SELECT oid, proargnames FROM pg_proc WHERE pronamespace = src_oid LOOP SELECT pg_get_functiondef(func_oid) INTO qry; SELECT proname, oidvectortypes(proargtypes) INTO v_func, v_args FROM pg_proc WHERE oid = func_oid; SELECT replace(qry, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') INTO dest_qry; EXECUTE dest_qry; -- Add function comment SELECT description INTO v_def FROM pg_description WHERE objoid = func_oid AND objsubid = 0; IF FOUND THEN -- RAISE NOTICE 'func_oid %, object %, v_args %', func_oid::text, quote_ident(object), v_args; EXECUTE 'COMMENT ON FUNCTION ' || quote_ident(dest_schema) || '.' || quote_ident(v_func) || '(' || v_args || ')' || ' IS ' || quote_literal(v_def) ||';' ; END IF; END LOOP; -- add Rules SET search_path TO dest_schema; FOR v_def IN SELECT definition FROM pg_rules WHERE schemaname = quote_ident(source_schema) LOOP IF v_def IS NOT NULL THEN SELECT replace(v_def, source_schema || '.', dest_schema || '.') INTO v_def; EXECUTE ' ' || v_def; END IF; END LOOP; -- add triggers FOR v_def IN SELECT pg_get_triggerdef(oid) FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT replace(v_def, source_schema || '.', dest_schema || '.') INTO dest_qry; EXECUTE dest_qry; END LOOP; -- Disable inactive triggers -- D = disabled FOR tbl_oid IN SELECT oid FROM pg_trigger WHERE tgenabled = 'D' AND tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT t.tgname, c.relname INTO object, srctbl FROM pg_trigger t JOIN pg_class c ON c.oid = t.tgrelid WHERE t.oid = tbl_oid; IF FOUND THEN EXECUTE 'ALTER TABLE ' || dest_schema || '.' || srctbl || ' DISABLE TRIGGER ' || object || ';'; END IF; END LOOP; -- Add index comment FOR tbl_oid IN SELECT oid FROM pg_class WHERE relkind = 'i' AND relnamespace = src_oid LOOP SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON INDEX ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- add rule comments FOR con_oid IN SELECT oid, * FROM pg_rewrite WHERE rulename <> '_RETURN'::name LOOP SELECT rulename, ev_class INTO v_rule, tbl_oid FROM pg_rewrite WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON RULE ' || v_rule || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- add trigger comments FOR con_oid IN SELECT oid, * FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' LOOP SELECT tgname, tgrelid INTO v_trig, tbl_oid FROM pg_trigger WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TRIGGER ' || v_trig || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public.clone_schema(text, text, boolean) OWNER TO postgres; COMMENT ON FUNCTION public.clone_schema(text, text, boolean) IS 'Duplicates sequences, tables, indexes, rules, triggers, data(optional), views & functions from the source schema to the destination schema';
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
@ 2018-07-09 16:32                     ` DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:49                       ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  1 sibling, 1 reply; 35+ messages in thread

From: DiasCosta @ 2018-07-09 16:32 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; +Cc: Łukasz Jarych <jaryszek@gmail.com>; Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

Hi Melvin,

Trying run 9.6 clone_schema on a different schema and I get the 
following error:

NOTICE:  search path = {public,pg_catalog}
CONTEXT:  PL/pgSQL function clone_schema(text,text,boolean) line 79 at RAISE
ERROR:  relation "bilhetica_logic_schema.idx_unq_cod_operador_aml" does 
not exist
CONTEXT:  SQL statement "COMMENT ON INDEX 
bilhetica_logic_schema.idx_unq_cod_operador_aml IS 'garante unicidade do 
Cod_Operador_AML';"
PL/pgSQL function clone_schema(text,text,boolean) line 401 at EXECUTE
********** Error **********

ERROR: relation "bilhetica_logic_schema.idx_unq_cod_operador_aml" does 
not exist
SQL state: 42P01
Context: SQL statement "COMMENT ON INDEX 
bilhetica_logic_schema.idx_unq_cod_operador_aml IS 'garante unicidade do 
Cod_Operador_AML';"
PL/pgSQL function clone_schema(text,text,boolean) line 401 at EXECUTE


Can you help me, please?
Thanks in advance
Dias Costa




On 07-07-2018 09:32, DiasCosta wrote:
> Hi Melvin,
>
> Thank you.
>
> Dias Costa
>
> On 04-07-2018 23:38, Melvin Davidson wrote:
>>
>>
>> On Wed, Jul 4, 2018 at 2:48 PM, DiasCosta <diascosta@diascosta.org 
>> <mailto:diascosta@diascosta.org>> wrote:
>>
>>     Hi Melvin,
>>
>>     I'm new to clone_schema.
>>     Can I use it on PostgreSQL 9.6?
>>
>>     TIA
>>     DCostaployment by invitation only!
>>
>>
>> > Can I use it on PostgreSQL 9.6?
>>
>> Yes, but because the developer(s) once again monkeyed with the system 
>> catalogs, there are now
>> two versions. One for 10 and one for 9.6 and below. I've attached the 
>> 9.6 version for you.
>>
>
> -- 
> J. M. Dias Costa
> Telef. 214026948     Telem. 939307421
>
> Se divulgar esta mensagem por terceiros, por favor:
> 1. Apague o meu endereço de correio electrónico e o meu nome.
> 2. Apague também os endereços dos seus amigos antes de distribuir.
> 3. Enderece como cópia oculta (Cc ou Bcc) para os SEUS destinatários.
> Agindo deste modo, dificultará a disseminação de "vírus", "spams" e
> "banners" e contribuirá para manter a privacidade de todos e cada um.
> Obrigado.
>
> Nota: Não se deverá ao acaso a ocorrência de palavras na minha escrita que não respeitem o
> malfadado acordo ortográfico.


-- 
J. M. Dias Costa
Telef. 214026948     Telem. 939307421

Se divulgar esta mensagem por terceiros, por favor:
1. Apague o meu endereço de correio electrónico e o meu nome.
2. Apague também os endereços dos seus amigos antes de distribuir.
3. Enderece como cópia oculta (Cc ou Bcc) para os SEUS destinatários.
Agindo deste modo, dificultará a disseminação de "vírus", "spams" e
"banners" e contribuirá para manter a privacidade de todos e cada um.
Obrigado.

Nota: Não se deverá ao acaso a ocorrência de palavras na minha escrita que não respeitem o
malfadado acordo ortográfico.

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:32                     ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
@ 2018-07-09 16:49                       ` Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 18:56                         ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Melvin Davidson @ 2018-07-09 16:49 UTC (permalink / raw)
  To: DiasCosta <diascosta@diascosta.org>; +Cc: Łukasz Jarych <jaryszek@gmail.com>; Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general

On Mon, Jul 9, 2018 at 12:32 PM, DiasCosta <diascosta@diascosta.org> wrote:

> Hi Melvin,
>
> Trying run 9.6 clone_schema on a different schema and I get the following
> error:
>
> NOTICE:  search path = {public,pg_catalog}
> CONTEXT:  PL/pgSQL function clone_schema(text,text,boolean) line 79 at
> RAISE
> ERROR:  relation "bilhetica_logic_schema.idx_unq_cod_operador_aml" does
> not exist
> CONTEXT:  SQL statement "COMMENT ON INDEX bilhetica_logic_schema.idx_unq_cod_operador_aml
> IS 'garante unicidade do Cod_Operador_AML';"
> PL/pgSQL function clone_schema(text,text,boolean) line 401 at EXECUTE
> ********** Error **********
>
> ERROR: relation "bilhetica_logic_schema.idx_unq_cod_operador_aml" does
> not exist
> SQL state: 42P01
> Context: SQL statement "COMMENT ON INDEX bilhetica_logic_schema.idx_unq_cod_operador_aml
> IS 'garante unicidade do Cod_Operador_AML';"
> PL/pgSQL function clone_schema(text,text,boolean) line 401 at EXECUTE
>
>
> Can you help me, please?
> Thanks in advance
> Dias Costa
>
>
Dias
> NOTICE:  search path = {public,pg_catalog}
>ERROR: relation "bilhetica_logic_schema.idx_unq_cod_operador_aml" does not
exist

This is not related to the clone_schema function. It looks like you may
have corruption in your syste catalogs,
Try reindexing your system_catalogs.

REINDEX VERBOSE SYSTEM  <your_database_name>;



-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:32                     ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:49                       ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-09 18:56                         ` Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-09 21:50                           ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  0 siblings, 1 reply; 35+ messages in thread

From: Adrian Klaver @ 2018-07-09 18:56 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; DiasCosta <diascosta@diascosta.org>; +Cc: Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

On 07/09/2018 09:49 AM, Melvin Davidson wrote:
> 
> 
> On Mon, Jul 9, 2018 at 12:32 PM, DiasCosta <diascosta@diascosta.org 
> <mailto:diascosta@diascosta.org>> wrote:
> 
>     Hi Melvin,
> 
>     Trying run 9.6 clone_schema on a different schema and I get the
>     following error:
> 
>     NOTICE:  search path = {public,pg_catalog}
>     CONTEXT:  PL/pgSQL function clone_schema(text,text,boolean) line 79
>     at RAISE
>     ERROR:  relation "bilhetica_logic_schema.idx_unq_cod_operador_aml"
>     does not exist
>     CONTEXT:  SQL statement "COMMENT ON INDEX
>     bilhetica_logic_schema.idx_unq_cod_operador_aml IS 'garante
>     unicidade do Cod_Operador_AML';"
>     PL/pgSQL function clone_schema(text,text,boolean) line 401 at EXECUTE
>     ********** Error **********
> 
>     ERROR: relation "bilhetica_logic_schema.idx_unq_cod_operador_aml"
>     does not exist
>     SQL state: 42P01
>     Context: SQL statement "COMMENT ON INDEX
>     bilhetica_logic_schema.idx_unq_cod_operador_aml IS 'garante
>     unicidade do Cod_Operador_AML';"
>     PL/pgSQL function clone_schema(text,text,boolean) line 401 at EXECUTE
> 
> 
>     Can you help me, please?
>     Thanks in advance
>     Dias Costa
> 
> 
> Dias
>  > NOTICE:  search path = {public,pg_catalog}
>  >ERROR: relation "bilhetica_logic_schema.idx_unq_cod_operador_aml" does 
> not exist
> 
> This is not related to the clone_schema function. It looks like you may 
> have corruption in your syste catalogs,
> Try reindexing your system_catalogs.

Or from clone_schema.sql:

EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || 
quote_ident(source_schema) || '.' || quote_ident(object)
         || ' INCLUDING ALL)';

https://www.postgresql.org/docs/10/static/sql-createtable.html

"LIKE source_table [ like_option ... ]

...

Indexes, PRIMARY KEY, UNIQUE, and EXCLUDE constraints on the original 
table will be created on the new table only if INCLUDING INDEXES is 
specified. <*/Names for the new indexes and constraints are chosen 
according to the default rules, regardless of how the originals were 
named. (This behavior avoids possible duplicate-name failures for the 
new indexes.)/*>

...
INCLUDING ALL is an abbreviated form of INCLUDING COMMENTS INCLUDING 
CONSTRAINTS INCLUDING DEFAULTS INCLUDING IDENTITY INCLUDING INDEXES 
INCLUDING STATISTICS INCLUDING STORAGE.

..."

See tagged part(<*/ /*>) part above. I could see where the indexes in 
the new schema have new names while the index comments in the old schema 
refer to the old name. Then you would get the error the OP showed.

> 
> REINDEX VERBOSE SYSTEM  <your_database_name>;
> 
> 
> 
> -- 
> *Melvin Davidson**
> Maj. Database & Exploration Specialist**
> Universe Exploration Command – UXC***
> Employment by invitation only!


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:32                     ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:49                       ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 18:56                         ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-07-09 21:50                           ` Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 22:07                             ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 22:23                             ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 2 replies; 35+ messages in thread

From: Melvin Davidson @ 2018-07-09 21:50 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: DiasCosta <diascosta@diascosta.org>; Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

Adrian,
The code that CREATES the TABLE is

EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' ||
quote_ident(source_schema) || '.' || quote_ident(object)
        || ' INCLUDING ALL)';

The schema names are supposed to be changed!

This function HAS been tested and does WORK. Please do not muddle the
problem without testing yourself.


On Mon, Jul 9, 2018 at 2:56 PM, Adrian Klaver <adrian.klaver@aklaver.com>
wrote:

> On 07/09/2018 09:49 AM, Melvin Davidson wrote:
>
>
>>
>> On Mon, Jul 9, 2018 at 12:32 PM, DiasCosta <diascosta@diascosta.org
>> <mailto:diascosta@diascosta.org>> wrote:
>>
>>     Hi Melvin,
>>
>>     Trying run 9.6 clone_schema on a different schema and I get the
>>     following error:
>>
>>     NOTICE:  search path = {public,pg_catalog}
>>     CONTEXT:  PL/pgSQL function clone_schema(text,text,boolean) line 79
>>     at RAISE
>>     ERROR:  relation "bilhetica_logic_schema.idx_unq_cod_operador_aml"
>>     does not exist
>>     CONTEXT:  SQL statement "COMMENT ON INDEX
>>     bilhetica_logic_schema.idx_unq_cod_operador_aml IS 'garante
>>     unicidade do Cod_Operador_AML';"
>>     PL/pgSQL function clone_schema(text,text,boolean) line 401 at EXECUTE
>>     ********** Error **********
>>
>>     ERROR: relation "bilhetica_logic_schema.idx_unq_cod_operador_aml"
>>     does not exist
>>     SQL state: 42P01
>>     Context: SQL statement "COMMENT ON INDEX
>>     bilhetica_logic_schema.idx_unq_cod_operador_aml IS 'garante
>>     unicidade do Cod_Operador_AML';"
>>     PL/pgSQL function clone_schema(text,text,boolean) line 401 at EXECUTE
>>
>>
>>     Can you help me, please?
>>     Thanks in advance
>>     Dias Costa
>>
>>
>> Dias
>>  > NOTICE:  search path = {public,pg_catalog}
>>  >ERROR: relation "bilhetica_logic_schema.idx_unq_cod_operador_aml" does
>> not exist
>>
>> This is not related to the clone_schema function. It looks like you may
>> have corruption in your syste catalogs,
>> Try reindexing your system_catalogs.
>>
>
> Or from clone_schema.sql:
>
> EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' ||
> quote_ident(source_schema) || '.' || quote_ident(object)
>         || ' INCLUDING ALL)';
>
> https://www.postgresql.org/docs/10/static/sql-createtable.html
>
> "LIKE source_table [ like_option ... ]
>
> ...
>
> Indexes, PRIMARY KEY, UNIQUE, and EXCLUDE constraints on the original
> table will be created on the new table only if INCLUDING INDEXES is
> specified. <*/Names for the new indexes and constraints are chosen
> according to the default rules, regardless of how the originals were named.
> (This behavior avoids possible duplicate-name failures for the new
> indexes.)/*>
>
> ...
> INCLUDING ALL is an abbreviated form of INCLUDING COMMENTS INCLUDING
> CONSTRAINTS INCLUDING DEFAULTS INCLUDING IDENTITY INCLUDING INDEXES
> INCLUDING STATISTICS INCLUDING STORAGE.
>
> ..."
>
> See tagged part(<*/ /*>) part above. I could see where the indexes in the
> new schema have new names while the index comments in the old schema refer
> to the old name. Then you would get the error the OP showed.
>
>
>> REINDEX VERBOSE SYSTEM  <your_database_name>;
>>
>>
>>
>> --
>> *Melvin Davidson**
>> Maj. Database & Exploration Specialist**
>> Universe Exploration Command – UXC***
>> Employment by invitation only!
>>
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>



-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:32                     ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:49                       ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 18:56                         ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-09 21:50                           ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-09 22:07                             ` DiasCosta <diascosta@diascosta.org>
  1 sibling, 0 replies; 35+ messages in thread

From: DiasCosta @ 2018-07-09 22:07 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

Hi Melvin,

I followed your recommendation and it did not work.

Since I was in a rush I did try to understand where the function crashed 
and commenting in the function the creation of
comments for indexes, as follows, was sufficient for the function work.

     IF FOUND
       THEN
--        EXECUTE 'COMMENT ON INDEX ' || quote_ident(dest_schema) || '.' 
|| quote_ident(object)
--                 || ' IS ''' || v_def || ''';';

Problem is I don't usually comment indexes.

Thanks and
Keep good working
Dias Costa









On 09-07-2018 22:50, Melvin Davidson wrote:
>
> Adrian,
> The code that CREATES the TABLE is
>
> EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || 
> quote_ident(source_schema) || '.' || quote_ident(object)
>         || ' INCLUDING ALL)';
>
> The schema names are supposed to be changed!
>
> This function HAS been tested and does WORK. Please do not muddle the 
> problem without testing yourself.
>
>
> On Mon, Jul 9, 2018 at 2:56 PM, Adrian Klaver 
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
>
>     On 07/09/2018 09:49 AM, Melvin Davidson wrote:
>
>
>
>         On Mon, Jul 9, 2018 at 12:32 PM, DiasCosta
>         <diascosta@diascosta.org <mailto:diascosta@diascosta.org>
>         <mailto:diascosta@diascosta.org
>         <mailto:diascosta@diascosta.org>>> wrote:
>
>             Hi Melvin,
>
>             Trying run 9.6 clone_schema on a different schema and I
>         get the
>             following error:
>
>             NOTICE:  search path = {public,pg_catalog}
>             CONTEXT:  PL/pgSQL function
>         clone_schema(text,text,boolean) line 79
>             at RAISE
>             ERROR:  relation
>         "bilhetica_logic_schema.idx_unq_cod_operador_aml"
>             does not exist
>             CONTEXT:  SQL statement "COMMENT ON INDEX
>             bilhetica_logic_schema.idx_unq_cod_operador_aml IS 'garante
>             unicidade do Cod_Operador_AML';"
>             PL/pgSQL function clone_schema(text,text,boolean) line 401
>         at EXECUTE
>             ********** Error **********
>
>             ERROR: relation
>         "bilhetica_logic_schema.idx_unq_cod_operador_aml"
>             does not exist
>             SQL state: 42P01
>             Context: SQL statement "COMMENT ON INDEX
>             bilhetica_logic_schema.idx_unq_cod_operador_aml IS 'garante
>             unicidade do Cod_Operador_AML';"
>             PL/pgSQL function clone_schema(text,text,boolean) line 401
>         at EXECUTE
>
>
>             Can you help me, please?
>             Thanks in advance
>             Dias Costa
>
>
>         Dias
>          > NOTICE:  search path = {public,pg_catalog}
>          >ERROR: relation
>         "bilhetica_logic_schema.idx_unq_cod_operador_aml" does not exist
>
>         This is not related to the clone_schema function. It looks
>         like you may have corruption in your syste catalogs,
>         Try reindexing your system_catalogs.
>
>
>     Or from clone_schema.sql:
>
>     EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' ||
>     quote_ident(source_schema) || '.' || quote_ident(object)
>             || ' INCLUDING ALL)';
>
>     https://www.postgresql.org/docs/10/static/sql-createtable.html
>     <https://www.postgresql.org/docs/10/static/sql-createtable.html;
>
>     "LIKE source_table [ like_option ... ]
>
>     ...
>
>     Indexes, PRIMARY KEY, UNIQUE, and EXCLUDE constraints on the
>     original table will be created on the new table only if INCLUDING
>     INDEXES is specified. <*/Names for the new indexes and constraints
>     are chosen according to the default rules, regardless of how the
>     originals were named. (This behavior avoids possible
>     duplicate-name failures for the new indexes.)/*>
>
>     ...
>     INCLUDING ALL is an abbreviated form of INCLUDING COMMENTS
>     INCLUDING CONSTRAINTS INCLUDING DEFAULTS INCLUDING IDENTITY
>     INCLUDING INDEXES INCLUDING STATISTICS INCLUDING STORAGE.
>
>     ..."
>
>     See tagged part(<*/ /*>) part above. I could see where the indexes
>     in the new schema have new names while the index comments in the
>     old schema refer to the old name. Then you would get the error the
>     OP showed.
>
>
>         REINDEX VERBOSE SYSTEM  <your_database_name>;
>
>
>
>         -- 
>         *Melvin Davidson**
>         Maj. Database & Exploration Specialist**
>         Universe Exploration Command – UXC***
>         Employment by invitation only!
>
>
>
>     -- 
>     Adrian Klaver
>     adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
>
>
>
>
> -- 
> *Melvin Davidson**
> Maj. Database & Exploration Specialist**
> Universe Exploration Command – UXC*
> Employment by invitation only!


-- 
J. M. Dias Costa
Telef. 214026948     Telem. 939307421

Se divulgar esta mensagem por terceiros, por favor:
1. Apague o meu endereço de correio electrónico e o meu nome.
2. Apague também os endereços dos seus amigos antes de distribuir.
3. Enderece como cópia oculta (Cc ou Bcc) para os SEUS destinatários.
Agindo deste modo, dificultará a disseminação de "vírus", "spams" e
"banners" e contribuirá para manter a privacidade de todos e cada um.
Obrigado.

Nota: Não se deverá ao acaso a ocorrência de palavras na minha escrita que não respeitem o
malfadado acordo ortográfico.

^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:32                     ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:49                       ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 18:56                         ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-09 21:50                           ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
@ 2018-07-09 22:23                             ` Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-09 23:52                               ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 23:53                               ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 2 replies; 35+ messages in thread

From: Adrian Klaver @ 2018-07-09 22:23 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; +Cc: DiasCosta <diascosta@diascosta.org>; Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

On 07/09/2018 02:50 PM, Melvin Davidson wrote:
> 
> Adrian,
> The code that CREATES the TABLE is
> 
> EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || 
> quote_ident(source_schema) || '.' || quote_ident(object)
>          || ' INCLUDING ALL)';
> 
> The schema names are supposed to be changed!
> 
> This function HAS been tested and does WORK. Please do not muddle the 
> problem without testing yourself.
> 

create table public.idx_test (id int, fld_1 varchar);
create index test_idx on idx_test (id);

test_(postgres)# \d idx_test
                    Table "public.idx_test"
  Column |       Type        | Collation | Nullable | Default
--------+-------------------+-----------+----------+---------
  id     | integer           |           |          |
  fld_1  | character varying |           |          |
Indexes:
     "test_idx" btree (id)

create table sch_test.idx_test (like public.idx_test including all);

test_(postgres)# \d sch_test.idx_test
                   Table "sch_test.idx_test"
  Column |       Type        | Collation | Nullable | Default
--------+-------------------+-----------+----------+---------
  id     | integer           |           |          |
  fld_1  | character varying |           |          |
Indexes:
     "idx_test_id_idx" btree (id)

When you look up the comments you do:

  SELECT oid
       FROM pg_class
      WHERE relkind = 'i'
        AND relnamespace = src_oid

Where src_oid is the source namespace/schema. So in this case:

test_(postgres)# SELECT oid, relname
       FROM pg_class
      WHERE relkind = 'i'
        AND relnamespace = 'public'::regnamespace AND oid=2089851;
    oid   | relname
---------+----------
  2089851 | test_idx

You then do:

  SELECT relname INTO object ..

EXECUTE 'COMMENT ON INDEX ' || quote_ident(dest_schema) || '.' || 
quote_ident(object)
                  || ' IS ''' || v_def || ''';';

The problem is that the relname/object has changed in the new schema. In 
this case from text_idx --> idx_test_id_idx. So this happens:

test_(postgres)# comment on index sch_test.test_idx is 'test';
ERROR:  relation "sch_test.test_idx" does not exist


Just some muddling do with it what you will:)


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:32                     ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:49                       ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 18:56                         ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-09 21:50                           ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 22:23                             ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-07-09 23:52                               ` Melvin Davidson <melvin6925@gmail.com>
  1 sibling, 0 replies; 35+ messages in thread

From: Melvin Davidson @ 2018-07-09 23:52 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: DiasCosta <diascosta@diascosta.org>; Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

Dias,

As an experiment, I commented out the code that creates the comment on
indexes and it still works flawlessly, so that part is redundant.
I have attached the modified function below,
Please retry and see if the problem still exists.
If it does, then please do a schema only pg_dump of the source schema abd
send me the call to clone schema that you are using.

-- 
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!

Attachments:

clone_schema.sql (text/plain, 14.0K) download -- Function: public.clone_schema(text, text, boolean) -- DROP FUNCTION public.clone_schema(text, text, boolean); CREATE OR REPLACE FUNCTION public.clone_schema( source_schema text, dest_schema text, include_recs boolean) RETURNS void AS $BODY$ -- Initial code by Emanuel '3manuek' -- Last revision 2017-04-17 by Melvin Davidson -- Added SELECT REPLACE for schema views -- -- This function will clone all sequences, tables, indexes, rules, triggers, -- data(optional), views & functions from any existing schema to a new schema -- SAMPLE CALL: -- SELECT clone_schema('public', 'new_schema', TRUE); DECLARE src_oid oid; tbl_oid oid; func_oid oid; con_oid oid; v_path text; v_func text; v_args text; v_conname text; v_rule text; v_trig text; object text; buffer text; srctbl text; default_ text; v_column text; qry text; dest_qry text; v_def text; v_stat integer; seqval bigint; sq_last_value bigint; sq_max_value bigint; sq_start_value bigint; sq_increment_by bigint; sq_min_value bigint; sq_cache_value bigint; sq_log_cnt bigint; sq_is_called boolean; sq_is_cycled boolean; sq_cycled char(10); BEGIN -- Check that source_schema exists SELECT oid INTO src_oid FROM pg_namespace WHERE nspname = quote_ident(source_schema); IF NOT FOUND THEN RAISE NOTICE 'source schema % does not exist!', source_schema; RETURN ; END IF; -- Check that dest_schema does not yet exist PERFORM nspname FROM pg_namespace WHERE nspname = quote_ident(dest_schema); IF FOUND THEN RAISE NOTICE 'dest schema % already exists!', dest_schema; RETURN ; END IF; EXECUTE 'CREATE SCHEMA ' || quote_ident(dest_schema) ; -- Add schema comment SELECT description INTO v_def FROM pg_description WHERE objoid = src_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SCHEMA ' || quote_ident(dest_schema) || ' IS ' || quote_literal(v_def); END IF; SET search_path TO dest_schema, source_schema, 'public', pg_catalog; SELECT current_schemas(TRUE) INTO v_path; RAISE NOTICE 'search path = %', v_path; -- Create sequences -- TODO: Find a way to make this sequence's owner is the correct table. FOR object IN SELECT sequence_name::text FROM information_schema.sequences WHERE sequence_schema = quote_ident(source_schema) LOOP EXECUTE 'CREATE SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object); srctbl := quote_ident(source_schema) || '.' || quote_ident(object); EXECUTE 'SELECT last_value, max_value, start_value, increment_by, min_value, cache_value, log_cnt, is_cycled, is_called FROM ' || quote_ident(source_schema) || '.' || quote_ident(object) || ';' INTO sq_last_value, sq_max_value, sq_start_value, sq_increment_by, sq_min_value, sq_cache_value, sq_log_cnt, sq_is_cycled, sq_is_called ; IF sq_is_cycled THEN sq_cycled := 'CYCLE'; ELSE sq_cycled := 'NO CYCLE'; END IF; EXECUTE 'ALTER SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' INCREMENT BY ' || sq_increment_by || ' MINVALUE ' || sq_min_value || ' MAXVALUE ' || sq_max_value || ' START WITH ' || sq_start_value || ' RESTART ' || sq_min_value || ' CACHE ' || sq_cache_value || sq_cycled || ' ;' ; buffer := quote_ident(dest_schema) || '.' || quote_ident(object); IF include_recs THEN EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_last_value || ', ' || sq_is_called || ');' ; ELSE EXECUTE 'SELECT setval( ''' || buffer || ''', ' || sq_start_value || ', ' || sq_is_called || ');' ; END IF; -- add sequence comments SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'S' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON SEQUENCE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create tables FOR object IN SELECT TABLE_NAME::text FROM information_schema.tables WHERE table_schema = quote_ident(source_schema) AND table_type = 'BASE TABLE' LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || quote_ident(source_schema) || '.' || quote_ident(object) || ' INCLUDING ALL)'; -- Add table comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; IF include_recs THEN -- Insert records from source table EXECUTE 'INSERT INTO ' || buffer || ' SELECT * FROM ' || quote_ident(source_schema) || '.' || quote_ident(object) || ';'; END IF; FOR v_column, default_ IN SELECT column_name::text, REPLACE(column_default::text, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.' ) FROM information_schema.COLUMNS WHERE table_schema = dest_schema AND TABLE_NAME = object AND column_default LIKE 'nextval(%' || quote_ident(source_schema) || '%::regclass)' LOOP EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || v_column || ' SET DEFAULT ' || default_; END LOOP; END LOOP; -- set column statistics FOR tbl_oid, srctbl IN SELECT oid, relname FROM pg_class WHERE relnamespace = src_oid AND relkind = 'r' LOOP FOR v_column, v_stat IN SELECT attname, attstattarget FROM pg_attribute WHERE attrelid = tbl_oid AND attnum > 0 LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(srctbl); -- RAISE EXCEPTION 'ALTER TABLE % ALTER COLUMN % SET STATISTICS %', buffer, v_column, v_stat::text; EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || quote_ident(v_column) || ' SET STATISTICS ' || v_stat || ';'; END LOOP; END LOOP; -- add FK constraint FOR qry IN SELECT 'ALTER TABLE ' || quote_ident(dest_schema) || '.' || quote_ident(rn.relname) || ' ADD CONSTRAINT ' || quote_ident(ct.conname) || ' ' || replace(pg_get_constraintdef(ct.oid), quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') || ';' FROM pg_constraint ct JOIN pg_class rn ON rn.oid = ct.conrelid WHERE connamespace = src_oid AND rn.relkind = 'r' AND ct.contype = 'f' LOOP EXECUTE qry; END LOOP; -- Add constraint comment FOR con_oid IN SELECT oid FROM pg_constraint WHERE conrelid = tbl_oid LOOP SELECT conname INTO v_conname FROM pg_constraint WHERE oid = con_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid; IF FOUND THEN EXECUTE 'COMMENT ON CONSTRAINT ' || v_conname || ' ON ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; END LOOP; -- Create views FOR object IN SELECT table_name::text, view_definition FROM information_schema.views WHERE table_schema = quote_ident(source_schema) LOOP buffer := quote_ident(dest_schema) || '.' || quote_ident(object); SELECT view_definition INTO v_def FROM information_schema.views WHERE table_schema = quote_ident(source_schema) AND table_name = quote_ident(object); SELECT REPLACE(v_def, source_schema, dest_schema) INTO v_def; -- RAISE NOTICE 'view def, % , source % , dest % ', v_def, source_schema, dest_schema; EXECUTE 'CREATE OR REPLACE VIEW ' || buffer || ' AS ' || v_def || ';' ; -- Add comment SELECT oid INTO tbl_oid FROM pg_class WHERE relkind = 'v' AND relnamespace = src_oid AND relname = quote_ident(object); SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON VIEW ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- Create functions FOR func_oid IN SELECT oid, proargnames FROM pg_proc WHERE pronamespace = src_oid LOOP SELECT pg_get_functiondef(func_oid) INTO qry; SELECT proname, oidvectortypes(proargtypes) INTO v_func, v_args FROM pg_proc WHERE oid = func_oid; SELECT replace(qry, quote_ident(source_schema) || '.', quote_ident(dest_schema) || '.') INTO dest_qry; EXECUTE dest_qry; -- Add function comment SELECT description INTO v_def FROM pg_description WHERE objoid = func_oid AND objsubid = 0; IF FOUND THEN -- RAISE NOTICE 'func_oid %, object %, v_args %', func_oid::text, quote_ident(object), v_args; EXECUTE 'COMMENT ON FUNCTION ' || quote_ident(dest_schema) || '.' || quote_ident(v_func) || '(' || v_args || ')' || ' IS ' || quote_literal(v_def) ||';' ; END IF; END LOOP; -- add Rules SET search_path TO dest_schema; FOR v_def IN SELECT definition FROM pg_rules WHERE schemaname = quote_ident(source_schema) LOOP IF v_def IS NOT NULL THEN SELECT replace(v_def, source_schema || '.', dest_schema || '.') INTO v_def; RAISE NOTICE 'new rule %', v_def; EXECUTE ' ' || v_def; END IF; END LOOP; -- add triggers FOR v_def IN SELECT pg_get_triggerdef(oid) FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT replace(v_def, source_schema || '.', dest_schema || '.') INTO dest_qry; EXECUTE dest_qry; END LOOP; -- Disable inactive triggers -- D = disabled FOR tbl_oid IN SELECT oid FROM pg_trigger WHERE tgenabled = 'D' AND tgname NOT LIKE 'RI_%' AND tgrelid IN (SELECT oid FROM pg_class WHERE relkind = 'r' AND relnamespace = src_oid) LOOP SELECT t.tgname, c.relname INTO object, srctbl FROM pg_trigger t JOIN pg_class c ON c.oid = t.tgrelid WHERE t.oid = tbl_oid; IF FOUND THEN EXECUTE 'ALTER TABLE ' || dest_schema || '.' || srctbl || ' DISABLE TRIGGER ' || object || ';'; END IF; END LOOP; -- Add index comment FOR tbl_oid IN SELECT oid FROM pg_class WHERE relkind = 'i' AND relnamespace = src_oid LOOP SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid; SELECT description INTO v_def FROM pg_description WHERE objoid = tbl_oid AND objsubid = 0; /* IF FOUND THEN EXECUTE 'COMMENT ON INDEX ' || quote_ident(dest_schema) || '.' || quote_ident(object) || ' IS ''' || v_def || ''';'; END IF; */ END LOOP; -- add rule comments FOR con_oid IN SELECT oid, * FROM pg_rewrite WHERE rulename <> '_RETURN'::name LOOP SELECT rulename, ev_class INTO v_rule, tbl_oid FROM pg_rewrite WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON RULE ' || v_rule || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; -- add trigger comments FOR con_oid IN SELECT oid, * FROM pg_trigger WHERE tgname NOT LIKE 'RI_%' LOOP SELECT tgname, tgrelid INTO v_trig, tbl_oid FROM pg_trigger WHERE oid = con_oid; SELECT relname INTO object FROM pg_class WHERE oid = tbl_oid AND relkind = 'r'; SELECT description INTO v_def FROM pg_description WHERE objoid = con_oid AND objsubid = 0; IF FOUND THEN EXECUTE 'COMMENT ON TRIGGER ' || v_trig || ' ON ' || quote_ident(dest_schema) || '.' || object || ' IS ' || quote_literal(v_def); END IF; END LOOP; RETURN; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public.clone_schema(text, text, boolean) OWNER TO postgres; COMMENT ON FUNCTION public.clone_schema(text, text, boolean) IS 'Duplicates sequences, tables, indexes, rules, triggers, data(optional), views & functions from the source schema to the destination schema'; SELECT clone_schema('public', 'new_aud', false);
^ permalink raw reply [nested|flat] 35+ messages in thread

* Re: Cloning schemas
  2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 13:30 ` Re: Cloning schemas Tom Lane <tgl@sss.pgh.pa.us>
  2018-07-02 13:57   ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:03     ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-02 14:14       ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-02 14:21         ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-03 08:11           ` Re: Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
  2018-07-03 13:34             ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-04 18:48               ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-04 22:38                 ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-07 08:32                   ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:32                     ` Re: Cloning schemas DiasCosta <diascosta@diascosta.org>
  2018-07-09 16:49                       ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 18:56                         ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
  2018-07-09 21:50                           ` Re: Cloning schemas Melvin Davidson <melvin6925@gmail.com>
  2018-07-09 22:23                             ` Re: Cloning schemas Adrian Klaver <adrian.klaver@aklaver.com>
@ 2018-07-09 23:53                               ` Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 0 replies; 35+ messages in thread

From: Adrian Klaver @ 2018-07-09 23:53 UTC (permalink / raw)
  To: Melvin Davidson <melvin6925@gmail.com>; +Cc: DiasCosta <diascosta@diascosta.org>; Łukasz Jarych <jaryszek@gmail.com>; pgsql-general

On 07/09/2018 03:23 PM, Adrian Klaver wrote:
> On 07/09/2018 02:50 PM, Melvin Davidson wrote:
>>
>> Adrian,

> 
> The problem is that the relname/object has changed in the new schema. In 
> this case from text_idx --> idx_test_id_idx. So this happens:
> 
> test_(postgres)# comment on index sch_test.test_idx is 'test';
> ERROR:  relation "sch_test.test_idx" does not exist
> 
> 
> Just some muddling do with it what you will:)
> 

Should have added to the above that INCLUDING ALL encompasses INCLUDING 
COMMENTS:

https://www.postgresql.org/docs/10/static/sql-createtable.html

"Comments for the copied columns, constraints, and indexes will be 
copied only if INCLUDING COMMENTS is specified. The default behavior is 
to exclude comments, resulting in the copied columns and constraints in 
the new table having no comments."

So the COMMENT ON INDEX code is redundant anyway.

-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 35+ messages in thread


end of thread, other threads:[~2018-07-09 23:53 UTC | newest]

Thread overview: 35+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-07-02 11:30 Cloning schemas Łukasz Jarych <jaryszek@gmail.com>
2018-07-02 12:24 ` Łukasz Jarych <jaryszek@gmail.com>
2018-07-02 13:30 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-07-02 13:57   ` Łukasz Jarych <jaryszek@gmail.com>
2018-07-02 14:03     ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-07-02 14:14       ` Łukasz Jarych <jaryszek@gmail.com>
2018-07-02 14:21         ` Melvin Davidson <melvin6925@gmail.com>
2018-07-03 08:11           ` Łukasz Jarych <jaryszek@gmail.com>
2018-07-03 13:34             ` Melvin Davidson <melvin6925@gmail.com>
2018-07-04 18:27               ` Łukasz Jarych <jaryszek@gmail.com>
2018-07-04 18:38                 ` Melvin Davidson <melvin6925@gmail.com>
2018-07-04 18:48               ` DiasCosta <diascosta@diascosta.org>
2018-07-04 22:38                 ` Melvin Davidson <melvin6925@gmail.com>
2018-07-04 22:48                   ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-07-04 22:53                     ` Melvin Davidson <melvin6925@gmail.com>
2018-07-04 23:00                       ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-07-04 23:08                         ` Melvin Davidson <melvin6925@gmail.com>
2018-07-05 05:49                           ` Łukasz Jarych <jaryszek@gmail.com>
2018-07-05 05:49                             ` Łukasz Jarych <jaryszek@gmail.com>
2018-07-05 14:01                               ` Melvin Davidson <melvin6925@gmail.com>
2018-07-05 14:38                                 ` Łukasz Jarych <jaryszek@gmail.com>
2018-07-05 14:52                                   ` Melvin Davidson <melvin6925@gmail.com>
2018-07-05 14:58                                     ` Łukasz Jarych <jaryszek@gmail.com>
2018-07-07 08:32                   ` DiasCosta <diascosta@diascosta.org>
2018-07-07 20:19                     ` Melvin Davidson <melvin6925@gmail.com>
2018-07-09 09:14                       ` Łukasz Jarych <jaryszek@gmail.com>
2018-07-09 13:04                         ` Melvin Davidson <melvin6925@gmail.com>
2018-07-09 16:32                     ` DiasCosta <diascosta@diascosta.org>
2018-07-09 16:49                       ` Melvin Davidson <melvin6925@gmail.com>
2018-07-09 18:56                         ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-07-09 21:50                           ` Melvin Davidson <melvin6925@gmail.com>
2018-07-09 22:07                             ` DiasCosta <diascosta@diascosta.org>
2018-07-09 22:23                             ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-07-09 23:52                               ` Melvin Davidson <melvin6925@gmail.com>
2018-07-09 23:53                               ` Adrian Klaver <adrian.klaver@aklaver.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox