在Greenplum中生成数据库对象的DDL

时间:2016-02-09 09:30:13

标签: greenplum

有什么方法可以生成GP函数的DDL

我的实际要求是我想在单独的SQL文件中创建/生成特定模式中可用的每个对象的DDL

我能够通过以下脚本

为表格和视图做到这一点
tbl_list=`psql -At -c "select tablename from pg_tables where schemaname ='${PGSCHEMA}' and tablename not like '%_prt_%' order by 1;"` # Fetch all table Name
for fname in $tbl_list
do
   ddl=`PGPASSWORD='passwd' pg_dump -h 10.128.19.297 -U gpadmin jiodata -s -t "${PGSCHEMA}.$fname" >${script_dir}/${output_dir}/$fname.sql` # Fetch ddl for all tables
   #pg_dump -h 10.128.19.297 -U gpadmin jiodata -s -t "${PGSCHEMA}.$fname" >${script_dir}/${output_dir}/$fname.sql
   echo "Table DDL generated    :   "${PGSCHEMA}.$fname | tee -a ${log_file}
done

是否有人尝试过功能

如果有人知道,请提供帮助

由于

1 个答案:

答案 0 :(得分:1)

创建一个名为" get_functions.sql"的sql文件。使用此代码:

    select sub.function_name || '(' || array_to_string(array_agg(typname), ',') || ')'
    from    (
            select n.nspname as schema_name, proname as function_name, unnest(proallargtypes) as parm_type, unnest(proargmodes) as parm_direction
            from pg_proc p
            join pg_namespace n on p.pronamespace = n.oid
            where n.nspname = :schema_name
            ) as sub
    join pg_type t on sub.parm_type = t.oid
    where sub.parm_direction = 'i'
    group by sub.function_name;

接下来,使用以下命令创建一个bash脚本:

    #!/bin/bash
    set -e

    schema_name="$1"

    if [ "$schema_name" == "" ]; then
            echo "ERROR: You must provide the schema name."
            echo "Example usage: ./runme.sh gp_toolkit"
            exit 1
    fi

    echo "pg_dump -Fc -s -n $schema_name > $schema_name.sql"
    pg_dump -Fc -s -n $schema_name > $schema_name.sql

    for i in $(psql -t -A -v schema_name="'$schema_name'" -f get_functions.sql); do
            filename=$(echo $i | tr \( _ | tr \) _)
            filename+=".sql"
            echo "cat $schema_name.sql | pg_restore -P '$i' > $filename"
            cat $schema_name.sql | pg_restore -P ''$i'' > $filename
    done

修复脚本的权限。

chmod 755 runme.sh

执行脚本。

./runme.sh gp_toolkit

这将为每个函数创建一个文件,它也允许重载函数。