This is the multi-page printable view of this section.
Click here to print.
Return to the regular view of this page.
Using EXPORT functions
Vertica provides several EXPORT_ functions that let you recreate a database, or specific schemas and tables, in a target database.
Vertica provides several EXPORT_ functions that let you recreate a database, or specific schemas and tables, in a target database. For example, you can use the EXPORT_ functions to transfer some or all of the designs and objects you create in a development or test environment to a production database.
The EXPORT_ functions create SQL scripts that you can run to generate the exported database designs or objects. These functions serve different purposes to the export statements, COPY FROM VERTICA (pull data) and EXPORT TO VERTICA (push data). These statements transfer data directly from source to target database across a network connection between both. They are dynamic actions and do not generate SQL scripts.
The EXPORT_ functions appear in the following table. Depending on what you need to export, you can use one or more of the functions. EXPORT_CATALOG creates the most comprehensive SQL script, while EXPORT_TABLES and EXPORT_OBJECTS are subsets of that function to narrow the export scope.
Use this function... |
To recreate... |
EXPORT_CATALOG |
These catalog items:
-
An existing schema design, tables, projections, constraints, views, and stored procedures.
-
The Database Designer-created schema design, tables, projections, constraints, and views
-
A design on a different cluster.
|
EXPORT_TABLES |
Non-virtual objects up to, and including, the schema of one or more tables. |
EXPORT_OBJECTS |
Catalog objects in order dependency for replication. |
The designs and object definitions that the script creates depend on the EXPORT_ function scope you specify. The following sections give examples of the commands and output for each function and the scopes it supports.
Saving scripts for export functions
All of the examples in this section were generated using the standard Vertica VMART database, with some additional test objects and tables. One output directory was created for all SQL scripts that the functions created:
/home/dbadmin/xtest
If you specify the destination argument as an empty string (''
), the function writes the export results to STDOUT.
Note
A superuser can export all available database output to a file with the EXPORT_ functions. For a non-superuser, the EXPORT_ functions generate a script containing only the objects to which the user has access.
1 - Exporting the catalog
EXPORT_CATALOG generates a SQL script for copying a database design to another cluster.
The EXPORT_CATALOG function generates a SQL script that copies a database design to another cluster. This script replicates the physical schema design of the source database. For example:
=> SELECT EXPORT_CATALOG (
'/home/dbadmin/xtest/sql_cat_tables.sql',
'TABLES');
EXPORT_CATALOG
-------------------------------------
Catalog data exported successfully
(1 row)
The last argument specifies what to export:
-
TABLES: Tables, schemas, and table-dependent objects: constraints, and access policies.
-
DESIGN (default): All catalog objects.
-
DIRECTED_QUERIES: All directed queries that are stored in the catalog. For details, see Managing directed queries.
Use the DESIGN scope to export all design elements of a source database in order of their dependencies. This scope exports all catalog objects, including schemas, tables, constraints, projections, views, access policies, stored procedures, and data loaders:
=> SELECT EXPORT_CATALOG(
'/home/dbadmin/xtest/sql_cat_design.sql',
'DESIGN' );
EXPORT_CATALOG
-------------------------------------
Catalog data exported successfully
(1 row)
This function can generate the following types of statements:
The SQL script:
-
Only includes objects to which the user has access.
-
Orders CREATE statements based on object dependencies so they can be recreated in the correct sequence. For example, if a table is in a non-public schema, the required CREATE SCHEMA statement precedes the CREATE TABLE statement. Similarly, a table's CREATE ACCESS POLICY statement follows the table's CREATE TABLE statement.
-
If possible, creates projections with a KSAFE clause, if any, otherwise with an OFFSET clause.
Projection considerations
If a projection to export was created with no ORDER BY clause, the SQL script reflects the default behavior for projections. Vertica implicitly creates projections using a sort order based on the SELECT columns in the projection definition.
The EXPORT_CATALOG script is portable if all projections are created using UNSEGMENTED ALL NODES or SEGMENTED ALL NODES.
See also
2 - Exporting tables
Vertica function EXPORT_TABLES exports DDL for tables and related objects in the current database.
Vertica function EXPORT_TABLES exports DDL for tables and related objects in the current database. The generated SQL includes all non-virtual table objects to which you have access. You can use this SQL to recreate tables and related non-virtual objects on a different cluster.
You execute EXPORT_TABLES as follows:
EXPORT_TABLES( ['[destination]' [, '[scope]']] )
The SQL script conforms to the following requirements:
-
Only includes objects to which the user has access.
-
Orders CREATE statements according to object dependencies so they can be recreated in the correct sequence. For example, if a table references a named sequence, a CREATE SEQUENCE statement precedes the CREATE TABLE statement. Similarly, a table's CREATE ACCESS POLICY statement follows the table's CREATE TABLE statement.
Setting export scope
The EXPORT_TABLES scope
argument specifies the scope of the export operation:
To export... |
Set scope to... |
All tables to which you have access, including constraints |
Empty string (' ') |
One or more named tables |
Comma-delimited list of table objects. For example:
myschema.newtable, yourschema.oldtable
You can optionally qualify the schema with the name of the current database:
mydb.myschema.newtable
|
A named table object in the current search path: a schema, table, or sequence. If you specify a schema, EXPORT_TABLES exports all table objects in that schema to which you have access. |
Table object's name and, optionally, its path:
VMart.myschema
|
Note
EXPORT_TABLES does not export views. If you specify a view name, Vertica silently ignores it and the view is omitted from the generated script. To export views, use
EXPORT_OBJECTS.
Exporting all table objects
If you set the scope parameter to an empty string (''
), EXPORT_TABLES exports all tables and their related objects. For example, the following call to EXPORT_TABLES exports all table objects in the VMart database to the specified output file.
=> SELECT EXPORT_TABLES(
'/home/dbadmin/xtest/sql_tables_empty.sql', '');
EXPORT_TABLES
-------------------------------------
Catalog data exported successfully
(1 row)
The exported SQL includes the following types of statements, depending on what is required to recreate the tables and related objects, such as schemas, sequences and access policies:
Exporting individual table objects
EXPORT_TABLES can specify a comma-separated list of tables and table-related objects such as sequences or schemas to export. The generated SQL script includes the CREATE statements for the specified objects and their dependent objects:
For example, the following call to EXPORT_TABLES exports two VMart tables: store.store_sales_fact
and store.store_dimension
:
=> SELECT export_tables('','store.store_sales_fact, store.store_dimension');
export_tables
-------------------------------------------------------------------------
CREATE TABLE store.store_dimension
(
store_key int NOT NULL,
store_name varchar(64),
store_number int,
store_address varchar(256),
store_city varchar(64),
store_state char(2),
store_region varchar(64),
floor_plan_type varchar(32),
photo_processing_type varchar(32),
financial_service_type varchar(32),
selling_square_footage int,
total_square_footage int,
first_open_date date,
last_remodel_date date,
number_of_employees int,
annual_shrinkage int,
foot_traffic int,
monthly_rent_cost int,
CONSTRAINT C_PRIMARY PRIMARY KEY (store_key) DISABLED
);
CREATE TABLE store.store_sales_fact
(
date_key int NOT NULL,
product_key int NOT NULL,
product_version int NOT NULL,
store_key int NOT NULL,
promotion_key int NOT NULL,
customer_key int NOT NULL,
employee_key int NOT NULL,
pos_transaction_number int NOT NULL,
sales_quantity int,
sales_dollar_amount int,
cost_dollar_amount int,
gross_profit_dollar_amount int,
transaction_type varchar(16),
transaction_time time,
tender_type varchar(8)
);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_date FOREIGN KEY (date_key) references public.date_dimension (date_key);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_product FOREIGN KEY (product_key, product_version) references public.product_dimension (product_key, product_version);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_store FOREIGN KEY (store_key) references store.store_dimension (store_key);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_promotion FOREIGN KEY (promotion_key) references public.promotion_dimension (promotion_key);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_customer FOREIGN KEY (customer_key) references public.customer_dimension (customer_key);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_employee FOREIGN KEY (employee_key) references public.employee_dimension (employee_key);
The following call to EXPORT_TABLES specifies to export all tables in the VMart schema store
:
=> select export_tables('','store');
export_tables
-----------------------------------------------------------------
CREATE SCHEMA store;
CREATE TABLE store.store_dimension
(
...
);
CREATE TABLE store.store_sales_fact
(
...
);
CREATE TABLE store.store_orders_fact
(
...
);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_date FOREIGN KEY (date_key) references public.date_dimension (date_key);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_product FOREIGN KEY (product_key, product_version) references public.product_dimension (product_key, product_version);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_store FOREIGN KEY (store_key) references store.store_dimension (store_key);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_promotion FOREIGN KEY (promotion_key) references public.promotion_dimension (promotion_key);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_customer FOREIGN KEY (customer_key) references public.customer_dimension (customer_key);
ALTER TABLE store.store_sales_fact ADD CONSTRAINT fk_store_sales_employee FOREIGN KEY (employee_key) references public.employee_dimension (employee_key);
ALTER TABLE store.store_orders_fact ADD CONSTRAINT fk_store_orders_product FOREIGN KEY (product_key, product_version) references public.product_dimension (product_key, product_version);
ALTER TABLE store.store_orders_fact ADD CONSTRAINT fk_store_orders_store FOREIGN KEY (store_key) references store.store_dimension (store_key);
ALTER TABLE store.store_orders_fact ADD CONSTRAINT fk_store_orders_vendor FOREIGN KEY (vendor_key) references public.vendor_dimension (vendor_key);
ALTER TABLE store.store_orders_fact ADD CONSTRAINT fk_store_orders_employee FOREIGN KEY (employee_key) references public.employee_dimension (employee_key);
(1 row)
See also
3 - Exporting objects
EXPORT_OBJECTS generates a SQL script that recreates non-virtual catalog objects on a different cluster.
The EXPORT_OBJECTS function generates a SQL script that recreates non-virtual catalog objects on a different cluster. It takes arguments for the output file, scope, and K-safety enforcement. For example:
=> SELECT EXPORT_OBJECTS(
'/home/dbadmin/xtest/sql_objects_all.sql',
'', --- what to export (all, in this case)
'true'); --- enforce K-safety
EXPORT_OBJECTS
-------------------------------------
Catalog data exported successfully
(1 row)
If you set the scope to an empty string (''
) as in this example, Vertica exports all non-virtual objects to which you have access in order of dependencies. To specify the objects to export, use a comma-delimited list. The function automatically exports objects on which the target objects depend. For example, if you export myschema.mytable
, you do not also need to specify myschema
.
If you specify a schema, EXPORT_OBJECTS exports all objects in that schema to which you have access. If you name a table that references a sequence, the generated script exports the sequence, then the table that references the sequence, and finally any projections of that table.
Running the generated SQL script on another cluster creates all referenced objects and their dependent objects.
The SQL script:
-
Only includes objects to which the user has access.
-
Orders CREATE statements based on object dependencies so they can be recreated in the correct sequence. For example, if a table is in a non-public schema, the required CREATE SCHEMA statement precedes the CREATE TABLE statement. Similarly, a table's CREATE ACCESS POLICY statement follows the table's CREATE TABLE statement.
-
If possible, creates projections with a KSAFE clause, if any, otherwise with an OFFSET clause.
This function can generate the following types of statements:
Note
EXPORT_OBJECTS does not export grants. Preserving grants on libraries can be especially important when you upgrade your database: if the prototypes of UDx libraries change, Vertica drops the grants on them. In order to preserve grants on UDx libraries, back up the grants before upgrading, and then restore them in the upgraded database. For details, see
Backing up and restoring grants.
K-safety
By default, the function's KSAFE argument is set to true. In this case, the generated script calls MARK_DESIGN_KSAFE, which replicates K-safety of the original database.
The following example output includes the start and end of an output SQL file:
CREATE SCHEMA store;
CREATE SCHEMA online_sales;
CREATE SEQUENCE public.my_seq ;
CREATE TABLE public.customer_dimension
(
customer_key int NOT NULL,
customer_type varchar(16),
customer_name varchar(256),
customer_gender varchar(8),
title varchar(8),
household_id int,
...
);
...
SELECT MARK_DESIGN_KSAFE(1);
Exporting individual objects
You can specify one or more objects to export in a comma-delimited list. The names of objects in any non-public schema must include their respective schemas.
If listed objects have overlapping scopes—for example, the list includes a table and one of its projections—EXPORT_OBJECTS exports the projection only once:
=> SELECT EXPORT_OBJECTS ('','customer_dimension, customer_dimension_super');
export_objects
--------------------------------------------------------------------------
CREATE TABLE public.customer_dimension
(
customer_key int NOT NULL,
customer_type varchar(16),
customer_name varchar(256),
...
CONSTRAINT C_PRIMARY PRIMARY KEY (customer_key) DISABLED
);
CREATE ACCESS POLICY ON public.customer_dimension FOR COLUMN customer_address CASE WHEN enabled_role('administrator') THEN customer_address ELSE '**************' END ENABLE;
CREATE PROJECTION public.customer_dimension_super /*+basename(customer_dimension),createtype(L)*/
(
customer_key,
customer_type,
customer_name,
...
)
AS
SELECT customer_dimension.customer_key,
customer_dimension.customer_type,
customer_dimension.customer_name,
...
FROM public.customer_dimension
ORDER BY customer_dimension.customer_key
SEGMENTED BY hash(customer_dimension.customer_key) ALL NODES OFFSET 0;
SELECT MARK_DESIGN_KSAFE(0);
You can export stored procedures by specifying their names and the types of their formal parameters. For stored procedures with the same name but different formal parameters, you can export all implementations by exporting the parent schema.
To export a particular implementation of a stored procedure, specify either the types or both the names and types of the procedure's formal parameters. The following example specifies the types:
=> SELECT EXPORT_OBJECTS('','raiseXY(int, int)');
EXPORT_OBJECTS
----------------------
CREATE PROCEDURE public.raiseXY(x int, y int)
LANGUAGE 'PL/vSQL'
SECURITY INVOKER
AS '
BEGIN
RAISE NOTICE ''x = %'', x;
RAISE NOTICE ''y = %'', y;
-- some processing statements
END
';
SELECT MARK_DESIGN_KSAFE(0);
(1 row)
To export all implementations of an overloaded stored procedure, export its parent schema:
=> SELECT EXPORT_OBJECTS('','public');
EXPORT_OBJECTS
----------------------
...
CREATE PROCEDURE public.raiseXY(x int, y varchar)
LANGUAGE 'PL/vSQL'
SECURITY INVOKER
AS '
BEGIN
RAISE NOTICE ''x = %'', x;
RAISE NOTICE ''y = %'', y;
-- some processing statements
END
';
CREATE PROCEDURE public.raiseXY(x int, y int)
LANGUAGE 'PL/vSQL'
SECURITY INVOKER
AS '
BEGIN
RAISE NOTICE ''x = %'', x;
RAISE NOTICE ''y = %'', y;
-- some processing statements
END
';
SELECT MARK_DESIGN_KSAFE(0);
(1 row)
See also
Exporting tables