Chapter 16. libpq

Table of Contents
データベース接続関数
問い合わせ実行関数
非同期問い合わせ処理
近道
非同期通知
COPY コマンドに関連した関数
libpq トレース関数
libpq 制御関数
ユーザ認証関数
環境変数
注意点
サンプルプログラム

1) libpq is the C application programmer's interface to Postgres. libpq is a set of library routines that allow client programs to pass queries to the Postgres backend server and to receive the results of these queries. libpq is also the underlying engine for several other Postgres application interfaces, including libpq++ (C++), libpgtcl (Tcl), perl5, and ecpg. So some aspects of libpq's behavior will be important to you if you use one of those packages.

C アプリケーションプログラマ向けの Postgres インターフェース,libpq は,クライアントプログラムから Postgres バックエンドへ問い合わせを渡し, その結果を受け取るライブラリルーチンの集合です. また libpq++(C++),libpgtcl(Tcl), perl5ecpg を含む,各種の Postgres アプリケーションインターフェースを支える エンジンが,この libpq です. もしこれらのパッケージを使うのであれば,libpq の動作に見られる一部の側面は大切なことになるはずです. 2) Three short programs are included at the end of this section to show how to write programs that use libpq. There are several complete examples of libpq applications in the following directories: このセクションの最後に,libpq を使ったプログラミング方法を 示すための短いプログラムを三つ載せておきました.また以下に示すディレクトリにも libpq アプリケーションの完成例があります.

    ../src/test/regress
    ../src/test/examples
    ../src/bin/psql

3) Frontend programs which use libpq must include the header file libpq-fe.h and must link with the libpq library.

なお libpq を使うフロントエンドはヘッダファイル libpq-fe.h をインクルードし,libpq ライブラリとリンクしなければなりません.

データベース接続関数

5) The following routines deal with making a connection to a Postgres backend server. The application program can have several backend connections open at one time. (One reason to do that is to access more than one database.) Each connection is represented by a PGconn object which is obtained from PQconnectdb() or PQsetdbLogin(). NOTE that these functions will always return a non-null object pointer, unless perhaps there is too little memory even to allocate the PGconn object. The PQstatus function should be called to check whether a connection was successfully made before queries are sent via the connection object.

Postgres バックエンドサーバ との接続は以下のルーチンが処理します.アプリケーションはバックエンドへの 接続を同時に複数持つことが可能です.(このようなことをする 事情のひとつとして,複数のデータベースへのアクセスが挙げられます) 個々の接続は PQconnectdb() または PQsetdblogin() を呼び出すと得られる, PGConn オブジェクトによって表されます.なおこれらの関数はわずかでも PGconn オブジェクトを割り当てるメモリの余裕があれば,NULL ではなく常に オブジェクトのポインタを返します. またこのコネクションオブジェクトを通じて問い合わせを送る前に,接続が 成功したかどうか PQstatus 関数で確認しておくべきでしょう.

28) libpq application programmers should be careful to maintain the PGconn abstraction. Use the accessor functions below to get at the contents of PGconn. Avoid directly referencing the fields of the PGconn structure because they are subject to change in the future. (Beginning in Postgres release 6.4, the definition of struct PGconn is not even provided in libpq-fe.h. If you have old code that accesses PGconn fields directly, you can keep using it by including libpq-int.h too, but you are encouraged to fix the code soon.)

libpq アプリケーションプログラマは PGconn による抽象化に注意を払うべきです. PGconn の内容は以下に挙げるアクセッサ関数を使って取り出してください. PGconn 構造体中のフィールドは将来予告なく変更されることがあります. ですから直接フィールドを参照することは避けてください. (Postgres リリース 6.4 の初期の段階から, PGconn 構造体の定義を libpq-fe.h の中から外しました) 以前作成したプログラムが PGconn のフィールドを直接操作している場合, libpq-int.h をインクルードすれば使い続けることができます. しかしそのプログラムは是非とも修正してください.