-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
v_generate_unload_copy_cmd.sql
40 lines (40 loc) · 1.68 KB
/
v_generate_unload_copy_cmd.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
--DROP VIEW admin.v_generate_unload_copy_cmd;
/**********************************************************************************************
Purpose: View to get that will generate unload and copy commands for an object. After running
the view the user will need to fill in what filter to use in the UNLOAD query if any
(--WHERE audit_id > ___auditid___), the bucket location (__bucketname__) and the AWS
credentials (__creds_here__). The where clause is commented out currently and can
be left so if the UNLOAD needs to get all data of the table.
History:
2014-02-12 jjschmit Created
2022-08-15 saeedma8 excluded system tables
**********************************************************************************************/
CREATE OR REPLACE VIEW admin.v_generate_unload_copy_cmd
AS
SELECT
schemaname
,tablename
,cmd_type
,dml
FROM
(
SELECT
schemaname
,tablename
,'unload' AS cmd_type
,'UNLOAD (''SELECT * FROM ' + schemaname + '.' + tablename + ' --WHERE audit_id > ___auditid___'') TO ''s3://__bucketname__/' + TO_CHAR(GETDATE(), 'YYYYMMDD_HH24MISSMS') + '/' + schemaname + '.' + tablename + '-'' CREDENTIALS ''__creds_here__'' GZIP DELIMITER ''\\t'';' AS dml
FROM
pg_tables
WHERE schemaname !~ '^information_schema|catalog_history|pg_'
UNION ALL
SELECT
schemaname
,tablename
,'copy' AS cmd_type
,'COPY ' + schemaname + '.' + tablename + ' FROM ''s3://__bucketname__/' + TO_CHAR(GETDATE(), 'YYYYMMDD_HH24MISSMS') + '/' + schemaname + '.' + tablename + '-'' CREDENTIALS ''__creds_here__'' GZIP DELIMITER ''\\t'';' AS copy_dml
FROM
pg_tables
)
WHERE schemaname !~ '^information_schema|catalog_history|pg_'
ORDER BY 3 DESC,1,2
;