EDB clone schema v15
EDB Clone Schema is an extension module for EDB Postgres Advanced Server that allows you to copy a schema and its database objects from a local or remote database (the source database) to a receiving database (the target database).
The source and target databases can be either:
- The same physical database
- Different databases in the same database cluster
- Separate databases running under different database clusters on separate database server hosts
Use the following functions with EDB Clone Schema:
localcopyschema
. This function copies a schema and its database objects from a source database into the same database (the target) but with a different schema name from the original. Use this function when the source schema and the copy will reside within the same database. See localcopyschema for more information.localcopyschema_nb
. This function performs the same purpose aslocalcopyschema
but as a background job, which frees up the terminal from which the function was initiated. This function is referred to as a non-blocking function. See localcopyschema_nb for more information.remotecopyschema
. This function copies a schema and its database objects from a source database to a different target database. Use this function when the source schema and the copy will reside in separate databases. The separate databases can reside in the same EDB Postgres Advanced Server database clusters or in different ones. See remotecopyschema for more information.remotecopyschema_nb
. This function performs the same purpose asremotecopyschema
but as a background job, which frees up the terminal from which the function was initiated. This function is a non-blocking function. See remotecopyschema_nb for more information.process_status_from_log
. This function displays the status of the cloning functions. The information is obtained from a log file you specify when invoking a cloning function. See process_status_from_log for more information.remove_log_file_and_job
. This function deletes the log file created by a cloning function. You can also use this function to delete a job created by the non-blocking form of the function. See remove_log_file_and_job for more information.
You can clone these database objects from one schema to another:
- Data types
- Tables including partitioned tables, excluding foreign tables
- Indexes
- Constraints
- Sequences
- View definitions
- Materialized views
- Private synonyms
- Table triggers, but excluding event triggers
- Rules
- Functions
- Procedures
- Packages
- Comments for all supported object types
- Access control lists (ACLs) for all supported object types
You can't clone the following database objects:
- Large objects (Postgres
LOBs
andBFILEs
) - Logical replication attributes for a table
- Database links
- Foreign data wrappers
- Foreign tables
- Event triggers
- Extensions
For cloning objects that rely on extensions, see the limitations that follow.
- Row-level security
- Policies
- Operator class
The following limitations apply:
- EDB Clone Schema is supported on EDB Postgres Advanced Server when you specify a dialect of Compatible with Oracle on the EDB Postgres Advanced Server Dialect dialog box during installation. It's also supported when you include the
--redwood-like
keywords during a text-mode installation or cluster initialization. - The source code in functions, procedures, triggers, packages, and so on, aren't modified after being copied to the target schema. If such programs contain coded references to objects with schema names, the programs might fail when invoked in the target schema if such schema names are no longer consistent in the target schema.
- Cross-schema object dependencies aren't resolved. If an object in the target schema depends on an object in another schema, this dependency isn't resolved by the cloning functions.
- For remote cloning, if an object in the source schema depends on an extension, then you must create this extension in the public schema of the remote database before invoking the remote cloning function.
- At most, 16 copy jobs can run in parallel to clone schemas. Each job can have at most 16 worker processes to copy table data in parallel.
- You can't cancel queries run by background workers.
Setup process
You must install several extensions along with the PL/Perl language on any database used as the source or target database by an EDB Clone Schema function.
In addition, it might help to modify some configuration parameters in the postgresql.conf
file of the database servers.
Installing extensions and PL/Perl
Perform this installation on any database to be used as the source or target database by an EDB Clone Schema function.
- Install the following extensions on the database:
postgres_fdw
dblink
adminpack
pgagent
Ensure that pgAgent is installed before creating the
pgagent
extension. On Linux, you can use theedb-asxx-pgagent
RPM package, wherexx
is the EDB Postgres Advanced Server version number to install pgAgent. On Windows, use StackBuilder Plus to download and install pgAgent.You can install the extensions using the following commands:
For more information about using the
CREATE EXTENSION
command, see the PostgreSQL core documentation.
Modify the
postgresql.conf
file by adding$libdir/parallel_clone
to theshared_preload_libraries
configuration parameter as shown in this example:Install the Perl procedural language (PL/Perl) on the database, and run the
CREATE TRUSTED LANGUAGE plperl
command. For Linux, install PL/Perl using theedb-asxx-server-plperl
RPM package, wherexx
is the EDB Postgres Advanced Server version number. For Windows, use the EDB Postgres Language Pack. For information on EDB Language Pack, see the EDB Postgres Language Pack.Connect to the database as a superuser and run the following command:
For more information about using the CREATE LANGUAGE
command, see the PostgreSQL core documentation.
Setting configuration parameters
You might need to modify configuration parameters in the postgresql.conf
file.
Performance configuration parameters
You might need to tune the system for copying a large schema as part of one transaction. Tuning of configuration parameters is for the source database server referenced in a cloning function.
You might need to tune the following configuration parameters in the postgresql.conf
file:
work_mem
. Specifies the amount of memory for internal sort operations and hash tables to use before writing to temporary disk files.maintenance_work_mem
. Specifies the maximum amount of memory for maintenance operations such asVACUUM
,CREATE INDEX
, andALTER TABLE ADD FOREIGN KEY
to use.max_worker_processes
. Sets the maximum number of background processes that the system can support.checkpoint_timeout
. Maximum time between automatic WAL checkpoints, in seconds.checkpoint_completion_target
. Specifies the target of checkpoint completion as a fraction of total time between checkpoints.checkpoint_flush_after
. Whenever more thancheckpoint_flush_after
bytes are written while performing a checkpoint, attempt to force the OS to issue these writes to the underlying storage.max_wal_size
. Maximum size to let the WAL grow to between automatic WAL checkpoints.max_locks_per_transaction
. Controls the average number of object locks allocated for each transaction. Individual transactions can lock more objects as long as the locks of all transactions fit in the lock table.
For information about the configuration parameters, see the PostgreSQL core documentation.
Status logging
Status logging by the cloning functions creates log files in the directory specified by the log_directory
parameter in the postgresql.conf
file for the database server to which you're connected when invoking the cloning function.
The default location is PGDATA/log
, as shown by the following:
This directory must exist before running a cloning function.
The name of the log file is determined by what you specify in the parameter list when invoking the cloning function.
To display the status from a log file, use the process_status_from_log
function.
To delete a log file, use the remove_log_file_and_job
function, or delete it manually from the log directory.
Installing EDB Clone Schema
Install the EDB Clone Schema on any database to be used as the source or target database by an EDB Clone Schema function.
- If you previously installed an older version of the
edb_cloneschema
extension, run the following command:
Step 1: If you had previously installed an older version of the edb_cloneschema
extension, then you must run the following command:
This command also drops the edb_cloneschema
extension.
Install the extensions. Make sure that you create the
parallel_clone
extension before creating theedb_cloneschema
extension.
Creating the foreign servers and user mappings
When using one of the local cloning functions localcopyschema
or localcopyschema_nb
, one of the required parameters includes a single, foreign server. This server is for identifying the database server and its database that's the source and receiver of the cloned schema.
When using one of the remote cloning functions remotecopyschema
or remotecopyschema_nb
, two of the required parameters include two foreign servers. The foreign server specified as the first parameter identifies the source database server and its database that's the provider of the cloned schema. The foreign server specified as the second parameter identifies the target database server and its database that's the receiver of the cloned schema.
For each foreign server, you must create a user mapping. When a selected database superuser invokes a cloning function, that superuser must be mapped to a database user name and password that has access to the foreign server that's specified as a parameter in the cloning function.
For general information about foreign data, foreign servers, and user mappings, see the PostgreSQL core documentation.
Foreign server and user mapping for local cloning functions
For the localcopyschema
and localcopyschema_nb
functions, the source and target schemas are both in the same database of the same database server. Thus, you must define and specify only one foreign server for these functions. This foreign server is also referred to as the local server because this server is the one to which you must be connected when invoking the localcopyschema
or localcopyschema_nb
function.
The user mapping defines the connection and authentication information for the foreign server. You must create this foreign server and user mapping in the database of the local server in which the cloning occurs.
The database user for whom the user mapping is defined must be a superuser and connected to the local server when invoking an EDB Clone Schema function.
This example creates the foreign server for the database containing the schema to clone and to receive the cloned schema:
For more information about using the CREATE SERVER
command, see the PostgreSQL core documentation.
The user mapping for this server is the following:
For more information about using the CREATE USER MAPPING
command, see the PostgreSQL core documentation.
The following psql
commands show the foreign server and user mapping:
When database superuser enterprisedb
invokes a cloning function, the database user enterprisedb
with its password is used to connect to local_server
on the localhost
with port 5444
to database edb
.
In this case, the mapped database user, enterprisedb
, and the database user, enterprisedb
, used to connect to the local edb
database are the same database user. However, that's not required.
For specific use of these foreign server and user mapping examples, see the example given in localcopyschema
.
Foreign server and user mapping for remote cloning functions
For the remotecopyschema
and remotecopyschema_nb
functions, the source and target schemas are in different databases of either the same or different database servers. You must define and specify two foreign servers for these functions.
The foreign server defining the originating database server and its database containing the source schema to clone is referred to as the source server or the remote server.
The foreign server defining the database server and its database to receive the schema to clone is referred to as the target server or the local server.
The target server is also referred to as the local server because this server is the one to which you must be connected when invoking the remotecopyschema
or remotecopyschema_nb
function.
The user mappings define the connection and authentication information for the foreign servers.
You must create all of these foreign servers and user mappings in the target database of the target/local server.
The database user for whom the user mappings are defined must be a superuser and the user connected to the local server when invoking an EDB Clone Schema function.
This example creates the foreign server for the local, target database that receives the cloned schema:
The following is the user mapping for this server:
This example creates the foreign server for the remote, source database that's the source for the cloned schema:
The following is the user mapping for this server:
The following psql
commands show the foreign servers and user mappings:
When database superuser enterprisedb
invokes a cloning function, the database user tgtuser
with password tgtpassword
is used to connect to tgt_server
on the localhost
with port 5444
to database tgtdb
.
In addition, database user srcuser
with password srcpassword
connects to src_server
on host 192.168.2.28
with port 5444
to database srcdb
.
Note
Be sure the pg_hba.conf
file of the database server running the source database srcdb
has an appropriate entry. This entry must permit connection from the target server location (address 192.168.2.27
in the following example) with the database user srcuser
that was included in the user mapping for the foreign server src_server
defining the source server and database.
For specific use of these foreign server and user mapping examples, see the example given in remotecopyschema
.
EDB Clone Schema functions
The EDB Clone Schema functions are created in the edb_util
schema when the parallel_clone
and edb_cloneschema
extensions are installed.
Verify the following conditions before using an EDB Clone Schema function:
- You're connected to the target or local database as the database superuser defined in the
CREATE USER MAPPING
command for the foreign server of the target or local database. - The
edb_util
schema is in the search path, or invoke the cloning function with theedb_util
prefix. - The target schema doesn't exist in the target database.
- When using the remote copy functions, if the
on_tblspace
parameter is set totrue
, then the target database cluster contains all tablespaces that are referenced by objects in the source schema. Otherwise, creating the DDL statements for those database objects fails in the target schema, which causes a failure of the cloning process. - When using the remote copy functions, if you set the
copy_acls
parameter totrue
, then all roles that haveGRANT
privileges on objects in the source schema exist in the target database cluster. Otherwise granting privileges to those roles fails in the target schema, which causes a failure of the cloning process. - pgAgent is running against the target database if you're using the non-blocking form of the function.
pgAgent is provided as a component with EDB Postgres Advanced Server. For information about pgAgent, see the pgAdmin documentation.
localcopyschema
The localcopyschema
function copies a schema and its database objects in a local database specified in the source_fdw
foreign server from the source schema to the specified target schema in the same database.
The function returns a Boolean value. If the function succeeds, then true
is returned. If the function fails, then false
is returned.
The source_fdw, source_schema, target_schema
, and log_filename
are required parameters while all other parameters are optional.
Parameters
source_fdw
Name of the foreign server managed by the postgres_fdw
foreign data wrapper from which to clone database objects.
source_schema
Name of the schema from which to clone database objects.
target_schema
Name of the schema into which to clone database objects from the source schema.
log_filename
Name of the log file in which information from the function is recorded. The log file is created under the directory specified by the log_directory
configuration parameter in the postgresql.conf
file.
on_tblspace
Boolean value to specify whether to create database objects in their tablespaces. If false
, then the TABLESPACE
clause isn't included in the applicable CREATE
DDL statement when added to the target schema. If true
, then the TABLESPACE
clause is included in the CREATE
DDL statement when added to the target schema. The default value is false
.
verbose_on
Boolean value to specify whether to print the DDLs in log_filename
when creating objects in the target schema. If false
, then DDLs aren't printed. If true
, then DDLs are printed. The default value is false
.
copy_acls
Boolean value to specify whether to include the access control list (ACL) while creating objects in the target schema. The access control list is the set of GRANT
privilege statements. If false
, then the access control list isn't included for the target schema. If true
, then the access control list is included for the target schema. The default value is false
.
worker_count
Number of background workers to perform the clone in parallel. The default value is 1
.
Example
This example shows the cloning of schema edb
containing a set of database objects to target schema edbcopy
. Both schemas are in database edb
as defined by local_server
.
The example is for the following environment:
- Host on which the database server is running:
localhost
- Port of the database server:
5444
- Database source/target of the clone:
edb
- Foreign server (
local_server
) and user mapping with the information of the preceding bullet points - Source schema:
edb
- Target schema:
edbcopy
- Database superuser to invoke
localcopyschema: enterprisedb
Before invoking the function, database user enterprisedb
connects to to database edb
:
The following displays the logging status using the process_status_from_log
function:
After the clone is complete, the following shows some of the database objects copied to the edbcopy
schema:
localcopyschema_nb
The localcopyschema_nb
function copies a schema and its database objects in a local database specified in the source_fdw
foreign server from the source schema to the specified target schema in the same database. The copy occurs in a non-blocking manner as a job submitted to pgAgent.
The function returns an INTEGER
value job ID for the job submitted to pgAgent. If the function fails, then null is returned.
The source_fdw
, source
, target
, and log_filename
parameters are required. All other parameters are optional.
After the pgAgent job completes, remove it with the remove_log_file_and_job
function.
Parameters
source_fdw
Name of the foreign server managed by the postgres_fdw
foreign data wrapper from which to clone database objects.
source
Name of the schema from which to clone database objects.
target
Name of the schema into which to clone database objects from the source schema.
log_filename
Name of the log file in which to record information from the function. The log file is created under the directory specified by the log_directory
configuration parameter in the postgresql.conf
file.
on_tblspace
Boolean value to specify whether to create database objects in their tablespaces. If false
, then the TABLESPACE
clause isn't included in the applicable CREATE
DDL statement when added to the target schema. If true
, then the TABLESPACE
clause is included in the CREATE
DDL statement when added to the target schema. The default value is false
.
verbose_on
Boolean value to specify whether to print the DDLs in log_filename
when creating objects in the target schema. If false
, then DDLs aren't printed. If true
, then DDLs are printed. The default value is false
.
copy_acls
Boolean value to specify whether to include the access control list (ACL) while creating objects in the target schema. The access control list is the set of GRANT
privilege statements. If false
, then the access control list isn't included for the target schema. If true
, then the access control list is included for the target schema. The default value is false
.
worker_count
Number of background workers to perform the clone in parallel. The default value is 1
.
Example
The same cloning operation is performed as the example in localcopyschema
but using the non-blocking function localcopyschema_nb
.
You can use this command to see whether pgAgent is running on the appropriate local database:
If pgAgent isn't running, you can start it as shown by the following. The pgagent
program file is located in the bin
subdirectory of the EDB Postgres Advanced Server installation directory.
Note
The pgagent -l 2
option starts pgAgent in DEBUG
mode, which logs continuous debugging information into the log file specified with the -s
option. Use a lower value for the -l
option, or omit it entirely to record less information.
The localcopyschema_nb
function returns the job ID shown as 4
in the example.
The following displays the job status:
The following removes the pgAgent job:
remotecopyschema
The remotecopyschema
function copies a schema and its database objects from a source schema in the remote source database specified in the source_fdw
foreign server to a target schema in the local target database specified in the target_fdw
foreign server:
The function returns a Boolean value. If the function succeeds, then true
is returned. If the function fails, then false
is returned.
The source_fdw
, target_fdw
, source_schema
, target_schema
, and log_filename
are required parameters. All other parameters are optional.
Parameters
source_fdw
Name of the foreign server managed by the postgres_fdw
foreign data wrapper from which to clone database objects.
target_fdw
Name of the foreign server managed by the postgres_fdw
foreign data wrapper to which to clone database objects.
source_schema
Name of the schema from which to clone database objects.
target_schema
Name of the schema into which to clone database objects from the source schema.
log_filename
Name of the log file in which information from the function is recorded. The log file is created under the directory specified by the log_directory
configuration parameter in the postgresql.conf
file.
on_tblspace
Boolean value to specify whether to create database objects in their tablespaces. If false
, then the TABLESPACE
clause isn't included in the applicable CREATE
DDL statement when added to the target schema. If true
, then the TABLESPACE
clause is included in the CREATE
DDL statement when added to the target schema. The default value is false
.
Note
If you specify true
and a database object has a TABLESPACE
clause, the tablespace must exist in the target database cluseter. Otherwise, the cloning function fails.
verbose_on
Boolean value to specify whether to print the DDLs in log_filename
when creating objects in the target schema. If false
, then DDLs aren't printed. If true
, then DDLs are printed. The default value is false
.
copy_acls
Boolean value to specify whether to include the access control list (ACL) while creating objects in the target schema. The access control list is the set of GRANT
privilege statements. If false
, then the access control list isn't included for the target schema. If true
, then the access control list is included for the target schema. The default value is false
.
Note
If you specify true
, a role with GRANT
privilege must exist in the target database cluster. Otherwise, the cloning function fails.
worker_count
Number of background workers to perform the clone in parallel. The default value is 1
.
Example
This example shows cloning schema srcschema
in database srcdb
(as defined by src_server
) to target schema tgtschema
in database tgtdb
(as defined by tgt_server
).
The source server environment:
- Host on which the source database server is running:
192.168.2.28
- Port of the source database server:
5444
- Database source of the clone:
srcdb
- Foreign server (
src_server
) and user mapping with the information of the preceding bullet points - Source schema:
srcschema
The target server environment:
- Host on which the target database server is running:
localhost
- Port of the target database server:
5444
- Database target of the clone:
tgtdb
- Foreign server (
tgt_server
) and user mapping with the information of the preceding bullet points - Target schema:
tgtschema
- Database superuser to invoke
remotecopyschema: enterprisedb
Before invoking the function, the connection database user enterprisedb
connects to database tgtdb
. A worker_count
of 4
is specified for this function.
The following displays the status from the log file during various points in the cloning process:
The following shows the cloned tables:
When the remotecopyschema
function was invoked, four background workers were specified.
The following portion of the log file clone_rmt_src_tgt
shows the status of the parallel data copying operation using four background workers:
The DATA-COPY
log message includes two square-bracket numbers, for example, [0][3]
. The first number is the job index. The second number is the worker index. The worker index values range from 0 to 3 for the four background workers.
In case two clone schema jobs are running in parallel, the first log file has 0
as the job index, and the second has 1
as the job index.
remotecopyschema_nb
The remotecopyschema_nb
function copies a schema and its database objects from a source schema in the remote source database specified in the source_fdw
foreign server to a target schema in the local target database specified in the target_fdw
foreign server. Copying occurs in a non-blocking manner as a job submitted to pgAgent.
The function returns an INTEGER
value job ID for the job submitted to pgAgent. If the function fails, then null is returned.
The source_fdw
, target_fdw
, source
, target
, and log_filename
parameters are required. All other parameters are optional.
After the pgAgent job is complete, remove it with the remove_log_file_and_job
function.
Parameters
source_fdw
Name of the foreign server managed by the postgres_fdw
foreign data wrapper from which to clone database objects.
target_fdw
Name of the foreign server managed by the postgres_fdw
foreign data wrapper to which to clone database objects.
source
Name of the schema from which to clone database objects.
target
Name of the schema into which to clone database objects from the source schema.
log_filename
Name of the log file in which to record information from the function. The log file is created under the directory specified by the log_directory
configuration parameter in the postgresql.conf
file.
on_tblspace
Boolean value to specify whether to create database objects in their tablespaces. If false
, then the TABLESPACE
clause isn't included in the applicable CREATE
DDL statement when added to the target schema. If true
, then the TABLESPACE
clause is included in the CREATE
DDL statement when added to the target schema. The default value is false
.
Note
If you specify true
is specified and a database object has a TABLESPACE
clause, that tablespace must exist in the target database cluster. Otherwise, the cloning function fails.
verbose_on
Boolean value to specify whether to print the DDLs in log_filename
when creating objects in the target schema. If false
, then DDLs aren't printed. If true
, then DDLs are printed. The default value is false
.
copy_acls
Boolean value to specify whether to include the access control list (ACL) while creating objects in the target schema. The access control list is the set of GRANT
privilege statements. If false
, then the access control list isn't included for the target schema. If true
, then the access control list is included for the target schema. The default value is false
.
Note
If you specify true
, a role with GRANT
privilege must exist in the target database cluster. Otherwise the cloning function fails.
worker_count
Number of background workers to perform the clone in parallel. The default value is 1
.
Example
The same cloning operation is performed as the example in remotecopyschema
but using the non-blocking function remotecopyschema_nb
.
The following command starts pgAgent on the target database tgtdb
. The pgagent
program file is located in the bin
subdirectory of the EDB Postgres Advanced Server installation directory.
The remotecopyschema_nb
function returns the job ID shown as 2
in the example:
The following shows the completed status of the job:
The following removes the log file and the pgAgent job:
process_status_from_log
The process_status_from_log
function provides the status of a cloning function from its log file:
The function returns the following fields from the log file:
Field name | Description |
---|---|
status | Displays either STARTING , RUNNING , FINISH , or FAILED . |
execution_time | When the command was executed. Displayed in timestamp format. |
pid | Session process ID in which clone schema is getting called. |
level | Displays either INFO , ERROR , or SUCCESSFUL . |
stage | Displays either STARTUP , INITIAL , DDL-COLLECTION , PRE-DATA , DATA-COPY , POST-DATA , or FINAL . |
message | Information respective to each command or failure. |
Parameters
log_file
Name of the log file recording the cloning of a schema as specified when the cloning function was invoked.
Example
The following shows the use of the process_status_from_log
function:
remove_log_file_and_job
The remove_log_file_and_job
function performs cleanup tasks by removing the log files created by the schema cloning functions and the jobs created by the non-blocking functions.
You can specify values for either or both of the two parameters when invoking the remove_log_file_and_job
function:
- If you specify only
log_file
, then the function removes only the log file. - If you specify only
job_id
, then the function removes only the job. - If you specify both, then the function removes only the log file and the job.
Parameters
log_file
Name of the log file to remove.
job_id
Job ID of the job to remove.
Example
This example removes only the log file, given the log file name:
This example removes only the job, given the job ID:
This example removes the log file and the job, given both values:
- On this page
- Setup process
- EDB Clone Schema functions