Greenplum acquisition table structure

Recently I toss greenplum, experiencing a pain egg problem, and that is to get a table structure, which is the construction of the table statement. We all know in which MySQL is very easy in, show create table table_name to get, not so easy in gpdb inside, after a lot of information query has finally found a way. That is their definition of a function to acquire, functions can be nested python code, very convenient. But there is a lot of information in the code error, after repeated after debugging can finally use.

Without this function in fact you can also get table structure that can only export the table structure of the view. Export table structure of the command is:

pg_dump -s --table=tb1_partition_range_yyyymmdd testdb > tb1_partition_range_yyyymmdd.sql

View table structure:

Copy the code
[gpadmin@mdw ~]$ cat tb1_partition_range_yyyymmdd.sql 
--
-- Greenplum Database database dump
--

SET statement_timeout = 0;
SET client_encoding
= UTF8;
SET standard_conforming_strings
= off;
SET check_function_bodies
= false;
SET client_min_messages
= warning;
SET escape_string_warning
= off;

SET search_path = public, pg_catalog;

SET default_tablespace = ‘’;

SET default_with_oids = false;


Name: tb1_partition_range_yyyymmdd; Type: TABLE; Schema: public; Owner: gpadmin; Tablespace:

CREATE TABLE tb1_partition_range_yyyymmdd (
id numeric,
yyyymmdd
date
)
WITH (appendonly
=true, compresslevel=5) DISTRIBUTED BY (id) PARTITION BY RANGE(yyyymmdd)
(
PARTITION p20120811 START (
2012-08-11::date) END (2012-08-12::date) WITH (tablename=tb1_partition_range_yyyymmdd_1_prt_p20120811, orientation=row , appendonly=true, compresslevel=5 ),
PARTITION p20120812 START (
2012-08-12::date) END (2012-08-13::date) WITH (tablename=tb1_partition_range_yyyymmdd_1_prt_p20120812, orientation=row , appendonly=true, compresslevel=5 )
);

ALTER TABLE public.tb1_partition_range_yyyymmdd OWNER TO gpadmin;


Name: idx_yyyymmdd; Type: INDEX; Schema: public; Owner: gpadmin; Tablespace:

CREATE INDEX idx_yyyymmdd ON tb1_partition_range_yyyymmdd USING btree (yyyymmdd);


Greenplum Database database dump complete

Copy the code

Another way is to \ d table_name view. This field can only view information about her.

Copy the code
testdb=# \d+ tb1_partition_range_yyyymmdd
Append-Only Table "public.tb1_partition_range_yyyymmdd"
  Column  |  Type   | Modifiers | Storage | Description 
----------+---------+-----------+---------+-------------
 id       | numeric |           | main    | 
 yyyymmdd | date    |           | plain   | 
Compression Type: zlib
Compression Level: 5
Block Size: 32768
Checksum: t
Indexes:
    "idx_yyyymmdd" btree (yyyymmdd)
Child tables: tb1_partition_range_yyyymmdd_1_prt_p20120811,
              tb1_partition_range_yyyymmdd_1_prt_p20120812
Has OIDs: no
Options: appendonly=true, compresslevel=5
Distributed by: (id)
Partition by: (yyyymmdd)

testdb=#

Copy the code

The whole is not very convenient, create a custom function to view the following:

1. Create language

CREATE PROCEDURAL LANGUAGE plpythonu;

2. Create function (code below):

vim get_table_structure.sql
Copy the code
create or replace function get_table_structure(tablename text)
    returns text
as $$
    try:
        table_name = tablename.lower().split('.')[1]
        talbe_schema=tablename.lower().split('.')[0]
    except (IndexError):
        return 'Please in put "tableschema.table_name"'
    get_table_oid="select oid,reloptions,relkind from pg_class where oid='%s'::regclass"%(tablename)
    try:
        rv_oid=plpy.execute(get_table_oid,5)
        if not rv_oid:
            return 'Did not find any relation named"'+tablename +'".'
    except (Error):
        return 'Did not find any relation named"'+tablename +'".'
    table_oid=rv_oid[0]['oid']
    rv_reloptions=rv_oid[0]['reloptions']
    rv_relkind=rv_oid[0]['relkind']
    create_sql="";
    table_kind='table';
    if rv_relkind !='r' and rv_relkind !='v':
        plpy.error('%s is not table or view'%(tablename));
    elif rv_relkind=='v':
        get_view_def="select pg_get_viewdef(%s,'t') as viewdef;" % (table_oid)
        rv_viewdef=plpy.execute(get_view_def);
        create_sql='create view %s as \n' % (tablename)
        create_sql += rv_viewdef[0]['viewdef']+'\n';
        table_kind='view'
    else:
        get_columns="select a.attname,pg_catalog.format_type(a.atttypid,a.atttypmod),\
       (select substring(pg_catalog.pg_get_expr(d.adbin,d.adrelid) for 128) \
        from pg_catalog.pg_attrdef d where d.adrelid=a.attrelid and d.adnum=a.attnum and a.atthasdef) \
        as default,a.attnotnull as isnull from pg_catalog.pg_attribute \
        a where a.attrelid= %s and a.attnum >0 and not a.attisdropped order by a.attnum;" % (table_oid);
        rv_columns=plpy.execute(get_columns)
    get_table_distribution1</span><span style="color: #808080;">=</span>"<span style="color: #0000ff;">select</span> attrnums <span style="color: #0000ff;">from</span> pg_catalog.gp_distribution_policy t <span style="color: #0000ff;">where</span> localoid <span style="color: #808080;">=</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">" + table_oid + "</span><span style="color: #ff0000;">'</span><span style="color: #000000;"> "
    rv_distribution1</span><span style="color: #808080;">=</span>plpy.<span style="color: #0000ff;">execute</span>(get_table_distribution1,<span style="color: #800000; font-weight: bold;">500</span><span style="color: #000000;">)
    rv_distribution2</span><span style="color: #808080;">=</span><span style="color: #ff0000;">''</span>
    <span style="color: #0000ff;">if</span> rv_distribution1 <span style="color: #808080;">and</span> rv_distribution1<span style="color: #ff0000;">[</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">][</span><span style="color: #ff0000;">'attrnums'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">:
        get_table_distribution2</span><span style="color: #808080;">=</span>"<span style="color: #0000ff;">select</span> attname <span style="color: #0000ff;">from</span> pg_attribute <span style="color: #0000ff;">where</span> attrelid<span style="color: #808080;">=</span><span style="color: #ff0000;">'</span><span style="color: #ff0000;">"+table_oid+"</span><span style="color: #ff0000;">'</span> <span style="color: #808080;">and</span> attnum <span style="color: #808080;">in</span> (" <span style="color: #808080;">+</span> <span style="color: #ff00ff;">str</span>(rv_distribution1<span style="color: #ff0000;">[</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">][</span><span style="color: #ff0000;">'attrnums'</span><span style="color: #ff0000;">]</span>).strip(<span style="color: #ff0000;">'</span><span style="color: #ff0000;">{</span><span style="color: #ff0000;">'</span>).strip(<span style="color: #ff0000;">'</span><span style="color: #ff0000;">}</span><span style="color: #ff0000;">'</span>).strip(<span style="color: #ff0000;">'</span><span style="color: #ff0000;">[</span><span style="color: #ff0000;">'</span>).strip(<span style="color: #ff0000;">'</span><span style="color: #ff0000;">]</span><span style="color: #ff0000;">'</span>)<span style="color: #808080;">+</span><span style="color: #000000;">")"
        rv_distribution2</span><span style="color: #808080;">=</span>plpy.<span style="color: #0000ff;">execute</span>(get_table_distribution2,<span style="color: #800000; font-weight: bold;">500</span><span style="color: #000000;">)

    create_sql</span><span style="color: #808080;">=</span><span style="color: #ff0000;">'</span><span style="color: #ff0000;">create table %s (\n</span><span style="color: #ff0000;">'</span> <span style="color: #808080;">%</span><span style="color: #000000;"> (tablename)
    get_index</span><span style="color: #808080;">=</span>"<span style="color: #0000ff;">select</span> pg_get_indexdef(indexrelid) <span style="color: #0000ff;">as</span> indexdef <span style="color: #0000ff;">from</span> pg_index <span style="color: #0000ff;">where</span> indrelid<span style="color: #808080;">=%</span>s" <span style="color: #808080;">%</span><span style="color: #000000;"> (table_oid);
    rv_index</span><span style="color: #808080;">=</span>plpy.<span style="color: #0000ff;">execute</span><span style="color: #000000;">(get_index);

    
    get_parinfo1</span><span style="color: #808080;">=</span>"<span style="color: #0000ff;">select</span> attname <span style="color: #0000ff;">as</span> columnname <span style="color: #0000ff;">from</span> pg_attribute <span style="color: #0000ff;">where</span> attnum <span style="color: #808080;">=</span>(<span style="color: #0000ff;">select</span> paratts<span style="color: #ff0000;">[</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">]</span> <span style="color: #0000ff;">from</span> pg_partition <span style="color: #0000ff;">where</span> parrelid<span style="color: #808080;">=%</span>s) <span style="color: #808080;">and</span> attrelid<span style="color: #808080;">=%</span>s;"<span style="color: #808080;">%</span><span style="color: #000000;">(table_oid,table_oid);
    get_parinfo2</span><span style="color: #808080;">=</span>""" <span style="color: #0000ff;">select</span> pp.parrelid,prl.parchildrelid,<span style="color: #ff00ff;">case</span> <span style="color: #0000ff;">when</span> pp.parkind<span style="color: #808080;">=</span><span style="color: #ff0000;">'</span><span style="color: #ff0000;">h</span><span style="color: #ff0000;">'</span>::"<span style="color: #0000ff;">char</span>" <span style="color: #0000ff;">then</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">hash</span><span style="color: #ff0000;">'</span>::<span style="color: #0000ff;">text</span> <span style="color: #0000ff;">when</span> pp.parkind<span style="color: #808080;">=</span><span style="color: #ff0000;">'</span><span style="color: #ff0000;">r</span><span style="color: #ff0000;">'</span>::"<span style="color: #0000ff;">char</span>" <span style="color: #0000ff;">then</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">range</span><span style="color: #ff0000;">'</span>::<span style="color: #0000ff;">text</span> <span style="color: #0000ff;">when</span> pp.parkind<span style="color: #808080;">=</span><span style="color: #ff0000;">'</span><span style="color: #ff0000;">l</span><span style="color: #ff0000;">'</span>::"<span style="color: #0000ff;">char</span>" <span style="color: #0000ff;">then</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">list</span><span style="color: #ff0000;">'</span>::<span style="color: #0000ff;">text</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">null</span>::<span style="color: #0000ff;">text</span> <span style="color: #0000ff;">end</span> <span style="color: #0000ff;">as</span> partitiontype,pg_get_partition_rule_def(prl.oid,true) <span style="color: #0000ff;">as</span> partitionboundary <span style="color: #0000ff;">from</span> pg_partition pp,pg_partition_rule prl <span style="color: #0000ff;">where</span> pp.paristemplate<span style="color: #808080;">=</span>false <span style="color: #808080;">and</span> pp.parrelid <span style="color: #808080;">=</span> <span style="color: #808080;">%</span>s <span style="color: #808080;">and</span> prl.paroid <span style="color: #808080;">=</span> pp.oid <span style="color: #0000ff;">order</span> <span style="color: #0000ff;">by</span> prl.parname; """ <span style="color: #808080;">%</span><span style="color: #000000;"> (table_oid)
    v_par_parent</span><span style="color: #808080;">=</span>plpy.<span style="color: #0000ff;">execute</span><span style="color: #000000;">(get_parinfo1);
    v_par_info</span><span style="color: #808080;">=</span>plpy.<span style="color: #0000ff;">execute</span><span style="color: #000000;">(get_parinfo2);
    max_column_len</span><span style="color: #808080;">=</span><span style="color: #800000; font-weight: bold;">10</span><span style="color: #000000;">
    max_type_len</span><span style="color: #808080;">=</span><span style="color: #800000; font-weight: bold;">4</span><span style="color: #000000;">
    max_modifiers_len</span><span style="color: #808080;">=</span><span style="color: #800000; font-weight: bold;">4</span><span style="color: #000000;">
    max_default_len</span><span style="color: #808080;">=</span><span style="color: #800000; font-weight: bold;">4</span>
    <span style="color: #0000ff;">for</span> i <span style="color: #808080;">in</span><span style="color: #000000;"> rv_columns:
        </span><span style="color: #0000ff;">if</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'attname'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">:
            </span><span style="color: #0000ff;">if</span> max_column_len <span style="color: #808080;">&lt;</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'attname'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">.__len__():
                max_column_len</span><span style="color: #808080;">=</span>i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'attname'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">.__len__()
        </span><span style="color: #0000ff;">if</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'format_type'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">:
            </span><span style="color: #0000ff;">if</span> max_type_len <span style="color: #808080;">&lt;</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'format_type'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">.__len__():
                max_type_len</span><span style="color: #808080;">=</span>i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'format_type'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">.__len__()
        </span><span style="color: #0000ff;">if</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">:
            </span><span style="color: #0000ff;">if</span> max_type_len <span style="color: #808080;">&lt;</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">.__len__():
                max_default_len</span><span style="color: #808080;">=</span>i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">.__len__()
    first</span><span style="color: #808080;">=</span><span style="color: #000000;">True
    </span><span style="color: #0000ff;">for</span> i <span style="color: #808080;">in</span><span style="color: #000000;"> rv_columns:
        </span><span style="color: #0000ff;">if</span> first<span style="color: #808080;">==</span><span style="color: #000000;">True:
            split_char</span><span style="color: #808080;">=</span><span style="color: #ff0000;">'</span> <span style="color: #ff0000;">'</span><span style="color: #000000;">;
            first</span><span style="color: #808080;">=</span><span style="color: #000000;">False
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">:
            split_char</span><span style="color: #808080;">=</span><span style="color: #ff0000;">'</span><span style="color: #ff0000;">,</span><span style="color: #ff0000;">'</span><span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'attname'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">:
            create_sql </span><span style="color: #808080;">+=</span> " " <span style="color: #808080;">+</span> split_char <span style="color: #808080;">+</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'attname'</span><span style="color: #ff0000;">]</span>.ljust(max_column_len<span style="color: #808080;">+</span><span style="color: #800000; font-weight: bold;">6</span>)<span style="color: #808080;">+</span><span style="color: #ff0000;">''</span>
        <span style="color: #0000ff;">else</span><span style="color: #000000;">:
            create_sql </span><span style="color: #808080;">+=</span> "" <span style="color: #808080;">+</span> split_char <span style="color: #808080;">+</span> <span style="color: #ff0000;">'</span> <span style="color: #ff0000;">'</span>.ljust(max_column_len<span style="color: #808080;">+</span><span style="color: #800000; font-weight: bold;">6</span><span style="color: #000000;">)
        </span><span style="color: #0000ff;">if</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'format_type'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">:
            create_sql </span><span style="color: #808080;">+=</span> <span style="color: #ff0000;">'</span> <span style="color: #ff0000;">'</span> <span style="color: #808080;">+</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'format_type'</span><span style="color: #ff0000;">]</span>.ljust(max_type_len <span style="color: #808080;">+</span><span style="color: #800000; font-weight: bold;">2</span><span style="color: #000000;">)
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">:
            create_sql </span><span style="color: #808080;">+=</span> <span style="color: #ff0000;">'</span> <span style="color: #ff0000;">'</span> <span style="color: #808080;">+</span> <span style="color: #ff0000;">'</span> <span style="color: #ff0000;">'</span>.ljust(max_type_len<span style="color: #808080;">+</span><span style="color: #800000; font-weight: bold;">2</span><span style="color: #000000;">)
        </span><span style="color: #0000ff;">if</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'isnull'</span><span style="color: #ff0000;">]</span> <span style="color: #808080;">and</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'isnull'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">:
            create_sql </span><span style="color: #808080;">+=</span> <span style="color: #ff0000;">'</span> <span style="color: #ff0000;">'</span> <span style="color: #808080;">+</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;"> not null </span><span style="color: #ff0000;">'</span>.ljust(<span style="color: #800000; font-weight: bold;">8</span><span style="color: #000000;">)
        </span><span style="color: #0000ff;">if</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #ff0000;">]</span><span style="color: #000000;">:
            create_sql </span><span style="color: #808080;">+=</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;"> default </span><span style="color: #ff0000;">'</span> <span style="color: #808080;">+</span> i<span style="color: #ff0000;">[</span><span style="color: #ff0000;">'default'</span><span style="color: #ff0000;">]</span>.ljust(max_default_len<span style="color: #808080;">+</span><span style="color: #800000; font-weight: bold;">6</span><span style="color: #000000;">)
        create_sql </span><span style="color: #808080;">+=</span><span style="color: #000000;"> "\n"
    create_sql </span><span style="color: #808080;">+=</span><span style="color: #000000;"> ")"

    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> rv_reloptions:
        create_sql </span><span style="color: #808080;">+=</span>" <span style="color: #0000ff;">with</span> ("<span style="color: #808080;">+</span><span style="color: #ff00ff;">str</span>(rv_reloptions).strip(<span style="color: #ff0000;">'</span><span style="color: #ff0000;">{</span><span style="color: #ff0000;">'</span>).strip(<span style="color: #ff0000;">'</span><span style="color: #ff0000;">}</span><span style="color: #ff0000;">'</span>).strip(<span style="color: #ff0000;">'</span><span style="color: #ff0000;">[</span><span style="color: #ff0000;">'</span>).strip(<span style="color: #ff0000;">'</span><span style="color: #ff0000;">]</span><span style="color: #ff0000;">'</span>) <span style="color: #808080;">+</span><span style="color: #000000;">")\n"
        create_sql </span><span style="color: #808080;">=</span> create_sql.<span style="color: #ff00ff;">replace</span>("<span style="color: #ff0000;">'</span><span style="color: #ff0000;">",</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">)
    if rv_distribution2:
        create_sql += </span><span style="color: #ff0000;">'</span><span style="color: #0000ff;">Distributed</span> <span style="color: #0000ff;">by</span> (<span style="color: #ff0000;">'</span><span style="color: #ff0000;">
        for i in rv_distribution2:
            create_sql += i[</span><span style="color: #ff0000;">'</span>attname<span style="color: #ff0000;">'</span><span style="color: #ff0000;">] + </span><span style="color: #ff0000;">'</span>,<span style="color: #ff0000;">'</span><span style="color: #ff0000;">
        create_sql =create_sql.strip(</span><span style="color: #ff0000;">'</span>,<span style="color: #ff0000;">'</span><span style="color: #ff0000;">)+</span><span style="color: #ff0000;">'</span>)<span style="color: #ff0000;">'</span><span style="color: #ff0000;">
    elif rv_distribution1:
        create_sql += </span><span style="color: #ff0000;">'</span><span style="color: #0000ff;">Distributed</span> randomly\n<span style="color: #ff0000;">'</span><span style="color: #ff0000;">
    if v_par_parent:
        partitiontype=v_par_info[0][</span><span style="color: #ff0000;">'</span>partitiontype<span style="color: #ff0000;">'</span><span style="color: #ff0000;">];
        create_sql +=</span><span style="color: #ff0000;">'</span>\nPARTITION <span style="color: #0000ff;">BY</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">+ partitiontype + "("+v_par_parent[0][</span><span style="color: #ff0000;">'</span>columnname<span style="color: #ff0000;">'</span><span style="color: #ff0000;">]+")\n(\n";
        for i in v_par_info:
            create_sql +=" " +i[</span><span style="color: #ff0000;">'</span>partitionboundary<span style="color: #ff0000;">'</span><span style="color: #ff0000;">]+</span><span style="color: #ff0000;">'</span>,\n<span style="color: #ff0000;">'</span><span style="color: #ff0000;">;
        create_sql=create_sql.strip(</span><span style="color: #ff0000;">'</span>,\n<span style="color: #ff0000;">'</span><span style="color: #ff0000;">);
        create_sql+="\n)"
    create_sql+=";\n\n"
    for i in rv_index:
        create_sql += i[</span><span style="color: #ff0000;">'</span>indexdef<span style="color: #ff0000;">'</span><span style="color: #ff0000;">]+</span><span style="color: #ff0000;">'</span>;\n<span style="color: #ff0000;">'</span><span style="color: #ff0000;">

    
    get_table_comment="select </span><span style="color: #ff0000;">'</span>comment <span style="color: #0000ff;">on</span> <span style="color: #808080;">%</span>s <span style="color: #808080;">%</span>s <span style="color: #0000ff;">is</span> <span style="color: #ff0000;">'''</span><span style="color: #ff0000;">|| COALESCE (description,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">)|| </span><span style="color: #ff0000;">''''</span><span style="color: #ff0000;"> as comment from pg_description where objoid=%s and objsubid=0;" % (table_kind,tablename,table_oid)
    get_column_comment="select </span><span style="color: #ff0000;">'</span>comment <span style="color: #0000ff;">on</span> <span style="color: #0000ff;">column</span> <span style="color: #808080;">%</span>s.<span style="color: #ff0000;">'</span><span style="color: #ff0000;">||b.attname ||</span><span style="color: #ff0000;">'</span> <span style="color: #0000ff;">is</span> <span style="color: #ff0000;">'''</span><span style="color: #ff0000;"> || COALESCE(a.description,</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">)|| </span><span style="color: #ff0000;">'''</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;"> as comment from pg_catalog.pg_description a,pg_catalog.pg_attribute b where objoid=%s and a.objoid=b.attrelid and a.objsubid=b.attnum;" % (tablename,table_oid)
    rv_table_comment=plpy.execute(get_table_comment);
    rv_column_comment=plpy.execute(get_column_comment);


    for i in rv_table_comment:
        create_sql += i[</span><span style="color: #ff0000;">'</span>comment<span style="color: #ff0000;">'</span><span style="color: #ff0000;">]+</span><span style="color: #ff0000;">'</span>;\n<span style="color: #ff0000;">'</span><span style="color: #ff0000;">
    for i in rv_column_comment:
        create_sql +=i[</span><span style="color: #ff0000;">'</span>comment<span style="color: #ff0000;">'</span><span style="color: #ff0000;">]+</span><span style="color: #ff0000;">'</span>;\n<span style="color: #ff0000;">'</span><span style="color: #ff0000;">
    return create_sql;

$$ LANGUAGE plpythonu;

Copy the code
View Code

3. Test

Copy the code
testdb=# SELECT get_table_structure('public.tb1_partition_range_yyyymmdd');
                                                get_table_structure                                                
-------------------------------------------------------------------------------------------------------------------
 create table public.tb1_partition_range_yyyymmdd (                                                                
   id               numeric                                                                                        
  ,yyyymmdd         date                                                                                           
 ) with (appendonly=true, compresslevel=5)                                                                         
 Distributed by (id)                                                                                               
 PARTITION BY range(yyyymmdd)                                                                                      
 (                                                                                                                 
  PARTITION p20120811 START ('2012-08-11'::date) END ('2012-08-12'::date) WITH (appendonly=true, compresslevel=5), 
  PARTITION p20120812 START ('2012-08-12'::date) END ('2012-08-13'::date) WITH (appendonly=true, compresslevel=5)  
 );                                                                                                                

CREATE INDEX idx_yyyymmdd ON tb1_partition_range_yyyymmdd USING btree (yyyymmdd);

(1 row)

Copy the code

 

References:

"Greenplum real enterprise applications."

 

Author: Atlas

Source: Atlas of blog http://www.cnblogs.com/gomysql

Your support is the greatest encouragement blogger, thank you for your read. This article belongs to the author of all, welcome to reprint, but please keep that statement. If you need technical support, it also offers paid services.

Category: Greenplum
Tags: Greenplum
<div id="blog_post_info">
0
0
<div class="clear"></div>
<div id="post_next_prev">

<a href="https://www.cnblogs.com/gomysql/p/5993499.html" class="p_n_p_prefix">« </a> 上一篇:    <a href="https://www.cnblogs.com/gomysql/p/5993499.html" title="发布于 2016-10-24 16:28">Greenplum 生成加分区语句</a>
<br>
<a href="https://www.cnblogs.com/gomysql/p/6130405.html" class="p_n_p_prefix">» </a> 下一篇:    <a href="https://www.cnblogs.com/gomysql/p/6130405.html" title="发布于 2016-12-04 11:22">MySQL启动错误排查</a>
Original articles published 0 · won praise 27 · views 80000 +

Guess you like

Origin blog.csdn.net/yimenglin/article/details/102910438