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

35.3. クライアントインタフェース #

<title>Client Interfaces</title>

This section describes the facilities that <productname>PostgreSQL</productname>'s <application>libpq</application> client interface library provides for accessing large objects. The <productname>PostgreSQL</productname> large object interface is modeled after the <acronym>Unix</acronym> file-system interface, with analogues of <function>open</function>, <function>read</function>, <function>write</function>, <function>lseek</function>, etc. 本節では、PostgreSQLlibpqクライアントインタフェースライブラリで提供されるラージオブジェクトへのアクセス手段について説明します。 PostgreSQLラージオブジェクトインタフェースは、Unixファイルシステムインタフェースに因んで設計されており、openreadwritelseekなど同様のインタフェースを有しています。

All large object manipulation using these functions <emphasis>must</emphasis> take place within an SQL transaction block, since large object file descriptors are only valid for the duration of a transaction. Write operations, including <function>lo_open</function> with the <symbol>INV_WRITE</symbol> mode, are not allowed in a read-only transaction. ラージオブジェクトファイル記述子はトランザクションの間でしか有効でありませんので、これらの関数を使用したラージオブジェクトの操作はすべてSQLトランザクションブロック内で行われなければなりませんINV_WRITEモードでのlo_openを含む書き込み操作は、読み取り専用トランザクションでは許可されません。

If an error occurs while executing any one of these functions, the function will return an otherwise-impossible value, typically 0 or -1. A message describing the error is stored in the connection object and can be retrieved with <xref linkend="libpq-PQerrorMessage"/>. これらの関数のいずれか1つの実行時にエラーが発生した場合、関数は他ではあり得ない値、通常は0または-1を返します。 エラーを説明するメッセージは接続オブジェクト内に格納され、PQerrorMessageを用いて取り出すことができます。

Client applications that use these functions should include the header file <filename>libpq/libpq-fs.h</filename> and link with the <application>libpq</application> library. これらの関数を使用するクライアントアプリケーションは、libpq/libpq-fs.hヘッダファイルをインクルードし、libpqライブラリとリンクしなければなりません。

Client applications cannot use these functions while a libpq connection is in pipeline mode. libpqの接続がパイプラインモードである間は、クライアントアプリケーションはこれらの関数を使用できません。

35.3.1. ラージオブジェクトの作成 #

<title>Creating a Large Object</title>

The function この関数は

Oid lo_create(PGconn *conn, Oid lobjId);

creates a new large object. The OID to be assigned can be specified by <replaceable class="parameter">lobjId</replaceable>; if so, failure occurs if that OID is already in use for some large object. If <replaceable class="parameter">lobjId</replaceable> is <symbol>InvalidOid</symbol> (zero) then <function>lo_create</function> assigns an unused OID. The return value is the OID that was assigned to the new large object, or <symbol>InvalidOid</symbol> (zero) on failure. ラージオブジェクトを新規に作成します。 割り当てられるOIDをlobjIdで指定することができます。 こうした場合、そのOIDが他のラージオブジェクトですでに使用されていた場合、失敗します。 lobjIdInvalidOid(ゼロ)の場合、lo_createは未使用のOIDを割り当てます。 戻り値は新規ラージオブジェクトに割り当てられたOIDで、失敗時にはInvalidOid(ゼロ)が返されます。

An example: 以下に例を示します。

inv_oid = lo_create(conn, desired_oid);

The older function 古い関数

Oid lo_creat(PGconn *conn, int mode);

also creates a new large object, always assigning an unused OID. The return value is the OID that was assigned to the new large object, or <symbol>InvalidOid</symbol> (zero) on failure. もまた新しいラージオブジェクトを作成し、常に未使用のOIDを割り当てます。 戻り値は新しいラージオブジェクトに割り当てられたOIDです。 失敗した場合はInvalidOid(ゼロ)が返されます。

In <productname>PostgreSQL</productname> releases 8.1 and later, the <replaceable class="parameter">mode</replaceable> is ignored, so that <function>lo_creat</function> is exactly equivalent to <function>lo_create</function> with a zero second argument. However, there is little reason to use <function>lo_creat</function> unless you need to work with servers older than 8.1. To work with such an old server, you must use <function>lo_creat</function> not <function>lo_create</function>, and you must set <replaceable class="parameter">mode</replaceable> to one of <symbol>INV_READ</symbol>, <symbol>INV_WRITE</symbol>, or <symbol>INV_READ</symbol> <literal>|</literal> <symbol>INV_WRITE</symbol>. (These symbolic constants are defined in the header file <filename>libpq/libpq-fs.h</filename>.) PostgreSQLリリース8.1以降では、modeは無視されます。 そのためlo_creatは2番目の引数が0であるlo_createとまったく同じです。 しかし、8.1より古いサーバで作業する必要がない限り、lo_creatを使用する理由はほとんどありません。 このような古いサーバで作業するには、lo_createではなくlo_creatを使用しなければなりません。 modeINV_READINV_WRITEINV_READ|INV_WRITEのいずれかに設定する必要があります。 (これらの定数シンボルは、libpq/libpq-fs.hヘッダファイルで定義されています。)

An example: 例を示します。

inv_oid = lo_creat(conn, INV_READ|INV_WRITE);

35.3.2. ラージオブジェクトのインポート #

<title>Importing a Large Object</title>

To import an operating system file as a large object, call オペレーティングシステム上のファイルをラージオブジェクトとしてインポートするには、以下の関数を呼び出します。

Oid lo_import(PGconn *conn, const char *filename);

<replaceable class="parameter">filename</replaceable> specifies the operating system name of the file to be imported as a large object. The return value is the OID that was assigned to the new large object, or <symbol>InvalidOid</symbol> (zero) on failure. Note that the file is read by the client interface library, not by the server; so it must exist in the client file system and be readable by the client application. filenameには、ラージオブジェクトとしてインポートするオペレーティングシステム上のファイルのパス名を指定します。 戻り値は、新規ラージオブジェクトに割り当てられたOIDです。 失敗時はInvalidOid(0)が返されます。 このファイルがサーバではなく、クライアントインタフェースライブラリから読み取られることに注意してください。 ですから、このファイルはクライアントのファイルシステム上に存在し、クライアントアプリケーションから読み取り可能でなければなりません。

The function この関数も

Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);

also imports a new large object. The OID to be assigned can be specified by <replaceable class="parameter">lobjId</replaceable>; if so, failure occurs if that OID is already in use for some large object. If <replaceable class="parameter">lobjId</replaceable> is <symbol>InvalidOid</symbol> (zero) then <function>lo_import_with_oid</function> assigns an unused OID (this is the same behavior as <function>lo_import</function>). The return value is the OID that was assigned to the new large object, or <symbol>InvalidOid</symbol> (zero) on failure. 新規のラージオブジェクトをインポートします。 割り当てられるOIDをlobjIdで指定することができます。 こうした場合、そのOIDが他のラージオブジェクトですでに使用されていた場合、失敗します。 lobjIdInvalidOid(0)の場合、lo_import_with_oidは未使用のOIDを割り当てます(これはlo_importと同じ動作です)。 戻り値は新規ラージオブジェクトに割り当てられたOIDで、失敗時にはInvalidOid(0)が返されます。

<function>lo_import_with_oid</function> is new as of <productname>PostgreSQL</productname> 8.4 and uses <function>lo_create</function> internally which is new in 8.1; if this function is run against 8.0 or before, it will fail and return <symbol>InvalidOid</symbol>. lo_import_with_oidPostgreSQL 8.4から導入され、8.1から導入されたlo_createを内部で使用しています。 この関数を8.0以前のバージョンで実行させると失敗し、InvalidOidが返されます。

35.3.3. ラージオブジェクトのエクスポート #

<title>Exporting a Large Object</title>

To export a large object into an operating system file, call ラージオブジェクトをオペレーティングシステム上のファイルにエクスポートするには、以下の関数を呼び出します。

int lo_export(PGconn *conn, Oid lobjId, const char *filename);

The <parameter>lobjId</parameter> argument specifies the OID of the large object to export and the <parameter>filename</parameter> argument specifies the operating system name of the file. Note that the file is written by the client interface library, not by the server. Returns 1 on success, -1 on failure. lobjId引数には、エクスポートさせるラージオブジェクトのOIDを指定し、filename引数には、オペレーティングシステム上のファイルのパス名を指定します。 このファイルはサーバではなく、クライアントインタフェースライブラリによって書き込まれることに注意してください。 成功時には1、失敗時には-1が返されます。

35.3.4. 既存のラージオブジェクトのオープン #

<title>Opening an Existing Large Object</title>

To open an existing large object for reading or writing, call 読み取りまたは書き込みのために既存のラージオブジェクトを開く場合は、以下の関数を呼び出します。

int lo_open(PGconn *conn, Oid lobjId, int mode);

The <parameter>lobjId</parameter> argument specifies the OID of the large object to open. The <parameter>mode</parameter> bits control whether the object is opened for reading (<symbol>INV_READ</symbol>), writing (<symbol>INV_WRITE</symbol>), or both. (These symbolic constants are defined in the header file <filename>libpq/libpq-fs.h</filename>.) <function>lo_open</function> returns a (non-negative) large object descriptor for later use in <function>lo_read</function>, <function>lo_write</function>, <function>lo_lseek</function>, <function>lo_lseek64</function>, <function>lo_tell</function>, <function>lo_tell64</function>, <function>lo_truncate</function>, <function>lo_truncate64</function>, and <function>lo_close</function>. The descriptor is only valid for the duration of the current transaction. On failure, -1 is returned. lobjId引数には開きたいラージオブジェクトのOIDを指定します。 modeの各ビットは、そのオブジェクトを読み取りのみ(INV_READ)、書き込みのみ(INV_WRITE)、またはその両方できるように開くのかを制御するものです。 (これらの定数シンボルはlibpq/libpq-fs.hヘッダファイルで定義されています。) lo_openは、lo_readlo_writelo_lseeklo_lseek64lo_telllo_tell64lo_truncatelo_truncate64lo_closeで使用する(非負の)ラージオブジェクト記述子を返します。 この記述子は現在のトランザクション期間のみで有効です。 失敗時には-1が返されます。

The server currently does not distinguish between modes <symbol>INV_WRITE</symbol> and <symbol>INV_READ</symbol> <literal>|</literal> <symbol>INV_WRITE</symbol>: you are allowed to read from the descriptor in either case. However there is a significant difference between these modes and <symbol>INV_READ</symbol> alone: with <symbol>INV_READ</symbol> you cannot write on the descriptor, and the data read from it will reflect the contents of the large object at the time of the transaction snapshot that was active when <function>lo_open</function> was executed, regardless of later writes by this or other transactions. Reading from a descriptor opened with <symbol>INV_WRITE</symbol> returns data that reflects all writes of other committed transactions as well as writes of the current transaction. This is similar to the behavior of <literal>REPEATABLE READ</literal> versus <literal>READ COMMITTED</literal> transaction modes for ordinary SQL <command>SELECT</command> commands. 現時点では、サーバはINV_WRITEモードとINV_READ | INV_WRITEモードとを区別しません。 どちらの場合でも記述子から読み取り可能です。 しかし、これらのモードとINV_READだけのモードとの間には大きな違いがあります。 INV_READモードでは記述子に書き込むことができません。 そして、読み込んだデータは、このトランザクションや他のトランザクションで後で書き込んだかどうかは関係なく、lo_openを実行した時に有効だったトランザクションスナップショットの時点のラージオブジェクトの内容を反映したものになります。 INV_WRITEを付けて開いた記述子から読み取ると、現在のトランザクションによる書き込みや他のトランザクションがコミットした書き込みすべてを反映したデータが返されます。 これは、通常のSELECT SQLコマンドにおけるREPEATABLE READトランザクションの動作とREAD COMMITTEDトランザクションの動作の違いに似ています。

<function>lo_open</function> will fail if <literal>SELECT</literal> privilege is not available for the large object, or if <symbol>INV_WRITE</symbol> is specified and <literal>UPDATE</literal> privilege is not available. (Prior to <productname>PostgreSQL</productname> 11, these privilege checks were instead performed at the first actual read or write call using the descriptor.) These privilege checks can be disabled with the <xref linkend="guc-lo-compat-privileges"/> run-time parameter. ラージオブジェクトにSELECT権限が与えられていなかったり、INV_WRITEが指定されていて、かつUPDATE権限が与えられていないと、lo_openは失敗します。 (PostgreSQL 11よりも前では、こうした権限チェックはディスクリプタを使って最初に読み出し、あるいは書き込みの呼び出しを実際に行う際に実施されていました。) この権限チェックは、lo_compat_privileges実行時パラメータで無効にすることができます。

An example: 以下に例を示します。

inv_fd = lo_open(conn, inv_oid, INV_READ|INV_WRITE);

35.3.5. ラージオブジェクトへのデータの書き込み #

<title>Writing Data to a Large Object</title>

The function この関数も

int lo_write(PGconn *conn, int fd, const char *buf, size_t len);

writes <parameter>len</parameter> bytes from <parameter>buf</parameter> (which must be of size <parameter>len</parameter>) to large object descriptor <parameter>fd</parameter>. The <parameter>fd</parameter> argument must have been returned by a previous <function>lo_open</function>. The number of bytes actually written is returned (in the current implementation, this will always equal <parameter>len</parameter> unless there is an error). In the event of an error, the return value is -1. lenバイトを、buflenサイズでなければなりません)からfdラージオブジェクト記述子に書き込みます。 fd引数は事前に実行したlo_openの戻り値でなければいけません。 実際に書き込まれたバイト数が返されます(現在の実装ではエラーが発生しない限りlenと常に等しくなります)。 エラーイベントが発生した場合は、-1を返します。

Although the <parameter>len</parameter> parameter is declared as <type>size_t</type>, this function will reject length values larger than <literal>INT_MAX</literal>. In practice, it's best to transfer data in chunks of at most a few megabytes anyway. lenパラメータはsize_tとして宣言されていますが、この関数はINT_MAXより大きな値を拒絶します。 実際には、多くても数メガバイトのチャンクでデータを転送することが最善です。

35.3.6. ラージオブジェクトからのデータの読み込み #

<title>Reading Data from a Large Object</title>

The function この関数は

int lo_read(PGconn *conn, int fd, char *buf, size_t len);

reads up to <parameter>len</parameter> bytes from large object descriptor <parameter>fd</parameter> into <parameter>buf</parameter> (which must be of size <parameter>len</parameter>). The <parameter>fd</parameter> argument must have been returned by a previous <function>lo_open</function>. The number of bytes actually read is returned; this will be less than <parameter>len</parameter> if the end of the large object is reached first. In the event of an error, the return value is -1. len長のバイトを、fdラージオブジェクト記述子からbuflenサイズでなければなりません)に読み込みます。 fd引数は事前に実行したlo_openの戻り値でなければいけません。 実際に読み込まれたバイト数が返されます。 ラージオブジェクトの最後に先に達した場合はlenより小さな値になります。 エラーイベントが発生した場合は、-1値を返します。

Although the <parameter>len</parameter> parameter is declared as <type>size_t</type>, this function will reject length values larger than <literal>INT_MAX</literal>. In practice, it's best to transfer data in chunks of at most a few megabytes anyway. lenパラメータはsize_tとして宣言されていますが、この関数はINT_MAXより大きな値を拒絶します。 実際には、多くても数メガバイトをチャンク内にデータを転送することが最善です。

35.3.7. ラージオブジェクトのシーク #

<title>Seeking in a Large Object</title>

To change the current read or write location associated with a large object descriptor, call ラージオブジェクト記述子に関連付けされている、現在の読み取りまたは書き込みを行う位置を変更するには、以下の関数を呼び出します。

int lo_lseek(PGconn *conn, int fd, int offset, int whence);

This function moves the current location pointer for the large object descriptor identified by <parameter>fd</parameter> to the new location specified by <parameter>offset</parameter>. The valid values for <parameter>whence</parameter> are <symbol>SEEK_SET</symbol> (seek from object start), <symbol>SEEK_CUR</symbol> (seek from current position), and <symbol>SEEK_END</symbol> (seek from object end). The return value is the new location pointer, or -1 on error. この関数はfdで識別されるラージオブジェクト識別子の現在の位置を指すポインタを、offsetで指定した新しい位置に変更します。 whenceに指定可能な値は、SEEK_SET(オブジェクトの先頭位置からシーク)、SEEK_CUR(現在位置からシーク)、SEEK_END(オブジェクトの末尾位置からシーク)のいずれかです。 戻り値は新しい位置ポインタで、エラー時に-1が返されます。

When dealing with large objects that might exceed 2GB in size, instead use 2GBを超えるサイズのラージオブジェクトを取り扱う場合は代わりに以下を使用してください。

pg_int64 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence);

This function has the same behavior as <function>lo_lseek</function>, but it can accept an <parameter>offset</parameter> larger than 2GB and/or deliver a result larger than 2GB. Note that <function>lo_lseek</function> will fail if the new location pointer would be greater than 2GB. この関数はlo_lseekと同じ動作をしますが、offsetとして2GBを超える値を受付け、2GBより大きな結果を出力します。 lo_lseekは2GBを超える新しい位置ポインタが指定された場合に失敗することに注意してください。

<function>lo_lseek64</function> is new as of <productname>PostgreSQL</productname> 9.3. If this function is run against an older server version, it will fail and return -1. lo_lseek64PostgreSQL 9.3にて追加されました。 この関数をより古いバージョンのサーバに対して実行した場合には失敗し、-1が返ります。

35.3.8. ラージオブジェクトのシーク位置の入手 #

<title>Obtaining the Seek Position of a Large Object</title>

To obtain the current read or write location of a large object descriptor, call ラージオブジェクト記述子の現在の読み取り、書き込み位置を入手するには、以下の関数を呼び出します。

int lo_tell(PGconn *conn, int fd);

If there is an error, the return value is -1. エラーが発生した場合は-1が返されます。

When dealing with large objects that might exceed 2GB in size, instead use サイズが2GBを超える可能性があるラージオブジェクトを取り扱う場合は代わりに以下を使用します。

pg_int64 lo_tell64(PGconn *conn, int fd);

This function has the same behavior as <function>lo_tell</function>, but it can deliver a result larger than 2GB. Note that <function>lo_tell</function> will fail if the current read/write location is greater than 2GB. この関数はlo_tellと同じ動作をしますが、2GBより大きな結果を出力します。 lo_tellは2GBを超える新しい位置での読み書きに失敗します。

<function>lo_tell64</function> is new as of <productname>PostgreSQL</productname> 9.3. If this function is run against an older server version, it will fail and return -1. lo_tell64PostgreSQL 9.3にて追加されました。 この関数をより古いバージョンのサーバに対して実行した場合には失敗し、-1が返ります。

35.3.9. ラージオブジェクトを切り詰める #

<title>Truncating a Large Object</title>

To truncate a large object to a given length, call ラージオブジェクトを指定した長さに切り詰めるには、以下を呼び出します。

int lo_truncate(PGconn *conn, int fd, size_t len);

This function truncates the large object descriptor <parameter>fd</parameter> to length <parameter>len</parameter>. The <parameter>fd</parameter> argument must have been returned by a previous <function>lo_open</function>. If <parameter>len</parameter> is greater than the large object's current length, the large object is extended to the specified length with null bytes ('\0'). On success, <function>lo_truncate</function> returns zero. On error, the return value is -1. この関数はラージオブジェクト記述子fdlen長に切り詰めます。 fd引数は前もってlo_openが返したものでなければなりません。 lenが現在のラージオブジェクト長より大きければ、ラージオブジェクトは指定された長さまでヌルバイト('\0')で拡張されます。 成功時lo_truncateはゼロを返します。 失敗時の戻り値は-1です。

The read/write location associated with the descriptor <parameter>fd</parameter> is not changed. fdディスクリプタの読み取り/書き出し位置は変わりません。

Although the <parameter>len</parameter> parameter is declared as <type>size_t</type>, <function>lo_truncate</function> will reject length values larger than <literal>INT_MAX</literal>. lenパラメータはsize_tとして宣言されていますが、lo_truncateINT_MAXより大きな値を拒絶します。

When dealing with large objects that might exceed 2GB in size, instead use 2GBを超える可能性があるラージオブジェクトを取り扱う場合は代わりに以下を使用します。

int lo_truncate64(PGconn *conn, int fd, pg_int64 len);

This function has the same behavior as <function>lo_truncate</function>, but it can accept a <parameter>len</parameter> value exceeding 2GB. この関数はlo_truncateと同じ動作をしますが、2GBを超えるlenを受け付けることができます。

<function>lo_truncate</function> is new as of <productname>PostgreSQL</productname> 8.3; if this function is run against an older server version, it will fail and return -1. lo_truncatePostgreSQL 8.3で新規に導入されました。 この関数を古いバージョンのサーバに対して実行した場合は失敗し、-1が返されます。

<function>lo_truncate64</function> is new as of <productname>PostgreSQL</productname> 9.3; if this function is run against an older server version, it will fail and return -1. lo_truncate64PostgreSQL 9.3にて追加されました。 この関数をより古いバージョンのサーバに対して実行した場合には失敗し、-1が返ります。

35.3.10. ラージオブジェクト記述子を閉じる #

<title>Closing a Large Object Descriptor</title>

A large object descriptor can be closed by calling 以下を呼び出すことでラージオブジェクト記述子を閉ざすことができます。

int lo_close(PGconn *conn, int fd);

where <parameter>fd</parameter> is a large object descriptor returned by <function>lo_open</function>. On success, <function>lo_close</function> returns zero. On error, the return value is -1. ここで、fdlo_openの戻り値であるラージオブジェクト記述子です。 成功すると、lo_closeは0を返します。 失敗すると、-1を返します。

Any large object descriptors that remain open at the end of a transaction will be closed automatically. 開いたままのラージオブジェクト記述子は全てトランザクションの終了時に自動的に閉ざされます。