译者简介
晏杰宏,任职于上海新炬网络信息技术股份有限公司,Oracle DBA。负责数据库、中间件、大数据等基础软件建设、优化和业务保障工作。具有10年以上电信与银行企业一线/二线数据库运维管理经验。目前专注研究云计算、开源软件和国产化数据库等领域技术研究。
校对者简介
崔鹏,任职于海能达通信股份有限公司,数据库开发高级工程师,致力于postgresql数据库在专网通信领域、公共安全领域的应用与推广。
使用PostgreSQL BLOB接口
test=# CREATE TABLE t_image (id int, name text, image bytea);
CREATE TABLE
test=# \d t_image
Table "public.t_image"
Column | Type | Collation | Nullable | Default
-------+---------+-----------+----------+---------
id | integer | | |
name | text | | |
image | bytea | | |
复制
test=# SHOW bytea_output;
bytea_output
--------------
hex
(1 row)
复制
test=# SELECT lo_import('/etc/hosts');
lo_import
-----------
80343
(1 row)
复制
test=# CREATE TABLE t_file (
id int,
name text,
object_id oid
);
CREATE TABLE
test=# INSERT INTO t_file
VALUES (1, 'some_name', lo_import('/etc/hosts'))
RETURNING *;
id | name | object_id
----+---------------+-----------
1 | some_name | 80350
(1 row)
1个 INSERT 0 1
复制
test=# DELETE FROM t_file WHERE id = 1;
DELETE 1
复制
test=# \x
Expanded display is on.
test=# SELECT * FROM pg_largeobject WHERE loid = 80350;
-[ RECORD 1 ]------------------------------------------
loid | 80350
pageno | 0
data | ##\012# Host Database\012#\012# localhost ...
复制
test=# \x
Expanded display is off.
test=#
test=# SELECT lo_unlink(80350);
lo_unlink
-----------
1
(1 row)
test=# SELECT * FROM pg_largeobject WHERE loid = 80350;
loid | pageno | data
------+--------+------
(0 rows)
复制
vacuumlo:清理死的大对象
test=# SELECT lo_import('/etc/hosts');
lo_import
-----------
80351
(1 row)
复制
iMac:~ hs$ vacuumlo -h localhost -v test
Connected to database "test"
Checking object_id in public.t_file
Successfully removed 2 large objects from database "test".
复制
附加功能
test=# \df lo_*
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+---------------+------------------+---------------------------+------
pg_catalog | lo_close | integer | integer | func
pg_catalog | lo_creat | oid | integer | func
pg_catalog | lo_create | oid | oid | func
pg_catalog | lo_export | integer | oid, text | func
pg_catalog | lo_from_bytea | oid | oid, bytea | func
pg_catalog | lo_get | bytea | oid | func
pg_catalog | lo_get | bytea | oid, bigint, integer | func
pg_catalog | lo_import | oid | text | func
pg_catalog | lo_import | oid | text, oid | func
pg_catalog | lo_lseek | integer | integer, integer, integer | func
pg_catalog | lo_lseek64 | bigint | integer, bigint, integer | func
pg_catalog | lo_open | integer | oid, integer | func
pg_catalog | lo_put | void | oid, bigint, bytea | func
pg_catalog | lo_tell | integer | integer | func
pg_catalog | lo_tell64 | bigint | integer | func
pg_catalog | lo_truncate | integer | integer, integer | func
pg_catalog | lo_truncate64 | integer | integer, bigint | func
pg_catalog | lo_unlink | integer | oid | func
(18 rows)
复制
1个
2 pg_catalog | loread | bytea | integer, integer | func
pg_catalog | lowrite | integer | integer, bytea | func
复制
最后
PostgreSQL中文社区欢迎广大技术人员投稿
投稿邮箱:press@postgres.cn
文章转载自PostgreSQL中文社区,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。