PSQL函数无法转义引号

时间:2018-05-08 06:04:51

标签: postgresql escaping plpgsql psql greenplum

我有如下查询。分析和比较不同服务器上同一个表的行数。它按预期工作,但是如果有任何表格名称混合大小写,则会遇到问题:' stg.my_table_OTHER'。

如果我添加像stg。" my_table_OTHER"这样的引号,它就有效。但它似乎不起作用。已经试图逃避' \" stg.my_table_OTHER \"'但仍然没有运气。

请在下面查询我的问题。

的问候, RIAN

remove_tag => ["_grokparsefailure"]

错误抛出此部分

CREATE OR REPLACE FUNCTION dwh.sync_update_analyze(schemaname varchar, tablename varchar)
        RETURNS integer AS $body$
        DECLARE

            passedSrc boolean;
            passedTgt boolean;
            rowcount bigint;                                                                                                  
            tgtcount bigint;
            query text; 
        BEGIN
            SELECT EXISTS (                                                                                                   
                    SELECT 1                                                                                                       
                    FROM   pg_catalog.pg_class c                                                                                   
                    JOIN   pg_catalog.pg_namespace n ON n.oid = c.relnamespace                                                     
                    WHERE  n.nspname = $1                                                                                          
                    AND    c.relname = $2                                                                                          
                    AND    c.relkind = 'r'    -- only tables                                                                       
            ) into passedSrc;

            passedTgt = ops.runcmd('psql -h 666.66.6.666 -Atc "SELECT EXISTS ( SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = ''' || schemaname || ''' AND c.relname = ''' || tablename || ''' AND c.relkind = ''r'');"');



            IF passedSrc = 't' and passedTgt = 't' THEN

            query = ops.runcmd('psql -h 666.66.6.666 -Atc "ANALYZE ' || schemaname || '.' || tablename || ';"'); 

                tgtcount = ops.runcmd('psql -h 666.66.6.666 -Atc "select count(*) from ' || schemaname || '.' || tablename || ';"'); 
                EXECUTE '(select count(1) from ' || quote_ident($1) || '.' || quote_ident($2) || ')' INTO rowcount;               
                         query := 'UPDATE dwh.table_list_2018 SET                                                                  
                         move_data_to_dca = ''D'',                                                                                 
                         row_count = '|| rowcount || ',
                         target_cnt = '|| tgtcount ||'
                         WHERE schema = ''' || $1 || ''' and table_name = ''' || $2 || '''';          
                         EXECUTE query;

            ELSE                                                                                                           
                         query := 'UPDATE dwh.table_list_2018 SET                                                                  
                         move_data_to_dca = ''D'',                                                                                 
                         purge_table = ''Y'',
                         backup_location = ''table maybe purge on source or relation doesnt exists''                                                                                       
                         WHERE schema = ''' || $1 || ''' and table_name = ''' || $2 || '''';          
                         EXECUTE query;                                                                                            

           END IF; 

                 return 0;

                    EXCEPTION WHEN OTHERS THEN
                 query := 'UPDATE dwh.table_list_2018 SET                                                                  
             move_data_to_dca = ''D'',                                                                                 
             purge_table = ''Y'',
             backup_location = ''table maybe purge on source or relation doesnt exists''                                                                                
             WHERE schema = ''' || $1 || ''' and table_name = ''' || $2 || '''';          
             EXECUTE query; 
             RETURN 1;


        END
        $body$ LANGUAGE plpgsql;

当我进入

 tgtcount = ops.runcmd('psql -h 666.66.6.666 -Atc "select count(*) from ' || schemaname || '.' || tablename || ';"'); 

它抛出

 tgtcount = ops.runcmd('psql -h 666.66.6.666 -Atc "select count(*) from ' || schemaname || '.\"' || tablename || '\";"'); 

仅供参考。我正在使用

 HINT:  Use the escape string syntax for escapes, e.g., E'\r\n'
 INT:  Use the escape string syntax for escapes, e.g., E'\r\n'.
 QUERY:  SELECT  ops.runcmd('psql -h 666.66.6.666 -Atc "select count(*) from ' ||  $1  || '.\"' ||  $2  || '\";"')
 CONTEXT:  SQL statement in PL/PgSQL function "sync_update_analyze1" near line 26
 WARNING:  nonstandard use of escape in a string literal

1 个答案:

答案 0 :(得分:0)

我对greenplum一无所知,但如果它的行为与PostgreSQL类似,我会尝试这样:

tgtcount := ops.runcmd(
               'psql -h 666.66.6.666 -Atc ''select count(*) from '
               || quote_ident(schemaname) || '.'
               || quote_ident(tablename) || ';'''
            );

这样,您就可以对SQL语句使用单引号,这样就不会与quote_ident可能添加的双引号冲突。