バージョンごとのドキュメント一覧

35.4. サーバ側の関数 #

<title>Server-Side Functions</title>

Server-side functions tailored for manipulating large objects from SQL are listed in <xref linkend="lo-funcs-table"/>. SQLからラージオブジェクトを操作するのに適応したサーバ側の関数を表 35.1に列挙します。

表35.1 SQL向けラージオブジェクト関数

<title>SQL-Oriented Large Object Functions</title>

Function 関数

Description 説明

Example(s)

lo_from_bytea ( loid oid, data bytea ) → oid

Creates a large object and stores <parameter>data</parameter> in it. If <parameter>loid</parameter> is zero then the system will choose a free OID, otherwise that OID is used (with an error if some large object already has that OID). On success, the large object's OID is returned. ラージオブジェクトを作成してそこにdataを格納する。 loidが0であれば、システムが空いているOIDを選び、そうでなければそのOIDが使われる(すでにそのOIDを持つラージオブジェクトがあればエラーになる)。 成功すれば、そのラージオブジェクトのOIDが返される。

lo_from_bytea(0, '\xffffff00')24528

lo_put ( loid oid, offset bigint, data bytea ) → void

Writes <parameter>data</parameter> starting at the given offset within the large object; the large object is enlarged if necessary. ラージオブジェクト内の与えられたオフセットからdataを書き込む。必要であれば、ラージオブジェクトは拡張される。

lo_put(24528, 1, '\xaa')

lo_get ( loid oid [, offset bigint, length integer ] ) → bytea

Extracts the large object's contents, or a substring thereof. そこからラージオブジェクトの内容または部分文字列を取り出す。

lo_get(24528, 0, 3)\xffaaff


There are additional server-side functions corresponding to each of the client-side functions described earlier; indeed, for the most part the client-side functions are simply interfaces to the equivalent server-side functions. The ones just as convenient to call via SQL commands are <function>lo_creat</function><indexterm><primary>lo_creat</primary></indexterm>, <function>lo_create</function>, <function>lo_unlink</function><indexterm><primary>lo_unlink</primary></indexterm>, <function>lo_import</function><indexterm><primary>lo_import</primary></indexterm>, and <function>lo_export</function><indexterm><primary>lo_export</primary></indexterm>. Here are examples of their use: これまで説明したクライアント側の関数それぞれに対応する、追加のサーバ側の関数があります。 実際、ほとんどのクライアント側の関数は対応するサーバ側の関数に対する単なるインタフェースです。 SQLコマンドからの呼び出しが便利な関数は、lo_creatlo_createlo_unlinklo_importlo_exportです。 これらの使用例を示します。

CREATE TABLE image (
    name            text,
    raster          oid
);


SELECT lo_creat(-1);       &#45;- returns OID of new, empty large object

SELECT lo_creat(-1);       -- 新しい空のラージオブジェクトのOIDを返します


SELECT lo_create(43213);   &#45;- attempts to create large object with OID 43213

SELECT lo_create(43213);   -- OID 43213でラージオブジェクトの生成を試行します


SELECT lo_unlink(173454);  &#45;- deletes large object with OID 173454

SELECT lo_unlink(173454);  -- OID 173454のラージオブジェクトを削除します

INSERT INTO image (name, raster)
    VALUES ('beautiful image', lo_import('/etc/motd'));


INSERT INTO image (name, raster)  &#45;- same as above, but specify OID to use

INSERT INTO image (name, raster)  -- 上と同じですが使用するOIDを指定します
    VALUES ('beautiful image', lo_import('/etc/motd', 68583));

SELECT lo_export(image.raster, '/tmp/motd') FROM image
    WHERE name = 'beautiful image';

The server-side <function>lo_import</function> and <function>lo_export</function> functions behave considerably differently from their client-side analogs. These two functions read and write files in the server's file system, using the permissions of the database's owning user. Therefore, by default their use is restricted to superusers. In contrast, the client-side import and export functions read and write files in the client's file system, using the permissions of the client program. The client-side functions do not require any database privileges, except the privilege to read or write the large object in question. サーバ側のlo_importおよびlo_export関数の動作はクライアント側の関数とかなり異なります。 この2つの関数はサーバのファイルシステム上のファイルの読み書きを、データベースを所有するユーザの権限で行います。 したがって、デフォルトではこれらの使用はスーパーユーザに限定されています。 対照的に、クライアント側のインポート関数とエクスポート関数はクライアントのファイルシステム上のファイルをクライアントプログラムの権限で読み書きします。 このクライアント側の関数は、対象となるラージオブジェクトの読み出し、書き込み権限を除き、データベース権限を必要としません。

注意

It is possible to <xref linkend="sql-grant"/> use of the server-side <function>lo_import</function> and <function>lo_export</function> functions to non-superusers, but careful consideration of the security implications is required. A malicious user of such privileges could easily parlay them into becoming superuser (for example by rewriting server configuration files), or could attack the rest of the server's file system without bothering to obtain database superuser privileges as such. <emphasis>Access to roles having such privilege must therefore be guarded just as carefully as access to superuser roles.</emphasis> Nonetheless, if use of server-side <function>lo_import</function> or <function>lo_export</function> is needed for some routine task, it's safer to use a role with such privileges than one with full superuser privileges, as that helps to reduce the risk of damage from accidental errors. サーバサイドlo_importlo_export関数に対してGRANTを非スーパーユーザに適用することは可能ですが、その結果が意味することについて慎重な考慮が必要です。 そうした権限を持つ悪意のあるユーザは、(たとえば、サーバ設定ファイルを書き換えることによって)容易にその権限を拡張してスーパーユーザになることができるでしょう。 あるいは、そのようにしてデータベーススーパーユーザ権限を取得することなく、サーバのファイルシステムを攻撃することができるでしょう。 したがって、そうした権限を持つロールへのアクセスは、スーパーユーザロールへのアクセスとまったく同様に、注意深く防御されなければなりません。 にもかかわらず、サーバサイドのlo_importあるいはlo_exportを定形業務に使う必要があるなら、完全なスーパーユーザ権限よりは、そうした権限を持つロールを使う方が安全です。 偶発的な間違いから来る被害のリスクを減らすのに役立つからです。

The functionality of <function>lo_read</function> and <function>lo_write</function> is also available via server-side calls, but the names of the server-side functions differ from the client side interfaces in that they do not contain underscores. You must call these functions as <function>loread</function> and <function>lowrite</function>. またlo_readおよびlo_writeの機能はサーバサイドの呼び出しを介しても利用することができます。 しかしサーバサイドの関数名はクライアント側のインタフェースとは異なり、アンダースコアが含まれません。 loreadおよびlowriteとしてこれらの関数を呼び出さなければなりません。