2017-04-20 8 views
1

こんにちは私はロードするためのコマンドのコピーを使用行のpostgresからのバイナリ文字列を圧縮取得する方法

def test_insert(): 
    str_test = '4 1 2\n 2 4 5\n'.encode('utf8') 
    cmpstr = zlib.compress(str_test) 
    str_test_to_write = '\\x' + cmpstr.encode('hex_codec') 

    with open('outfile.txt','w') as output_file: 
     output_file.write(str(1) + '|'+ str_test_to_write + '\n') 
     output_file.write(str(2) + '|'+ str_test_to_write + '\n') 

そして、Postgresデータベースに挿入される圧縮文字列をテキストファイルを生成し、このコードを持っています私のテーブルへの情報:

time cat outfile.txt |psql teste3 -c "\copy zstr(id,zstr) from stdout with delimiter '|'" 

これは私のテーブルです:

drop table if exists zstr; 
    create table zstr(
    id int, 
    zstr bytea, 
    primary key(id)); 

その後、私はしたいです私の文字列を選択するが、私はこのエラーが表示されます:

>>> import psycopg2 
>>> import zlib 
>>> con = psycopg2.connect(host = 'X', database = 'Y', user = 'Z') 
>>> con.autocommit = True 
>>> cur = con.cursor() 
>>> cur.execute('select * from zstr where id = 1') 
>>> row = cur.fetchone() 
>>> row 
(1, <read-only buffer for 0x7fe19b75f270, size 41, offset 0 at 0x7fe196976f30>) 
>>> a = str(row[1]) 
>>> q = zlib.decompress(a) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
zlib.error: Error -3 while decompressing data: incorrect header check 

どのように私の文字列を取得できますか?

私が欲しいの出力:

'4 1 2\n 2 4 5\n' 
+0

なぜzlibは文字列を圧縮していますか? –

答えて

2

これを行うにはほとんどない理由があるに。値がTOAST_TUPLE_THRESHOLDより大きい場合、PostgreSQLはLZを使用してテキストを自然に圧縮します。 docs on TOAST

The TOAST management code is triggered only when a row value to be stored in a table is wider than TOAST_TUPLE_THRESHOLD bytes (normally 2 kB). The TOAST code will compress and/or move field values out-of-line until the row value is shorter than TOAST_TUPLE_TARGET bytes (also normally 2 kB) or no more gains can be had. During an UPDATE operation, values of unchanged fields are normally preserved as-is; so an UPDATE of a row with out-of-line values incurs no TOAST costs if none of the out-of-line values change.

これはユーザーには透過的です。テキスト自体を保存してください。

+1

私はそれを知らなかった。そして、閾値を1kb未満に設定することはできますか? – 1pa

+1

しかし、私はそうすることはできません。 =)あなたが数百万(または何十億)の行を持っていない限り、それは本当に重要です。 –

+0

私は100万の行xDを持っていますので、しきい値の推奨は?なぜ? – 1pa

関連する問題