[pgsql-jp: 32494] union を使ったselect結果のinsertができない 7.4.1,2

Mashiki mashiki @ yanah.com
2004年 3月 19日 (金) 12:57:02 JST


mashikiです。こんにちは。

 以下のようなテーブルにて、

  Create Table tab1 (
    c1 char(1) Not Null,
    c2 VarChar Not Null,
    c3 char(1) Not Null,
    c4 Int4 Not Null,
    c5 numeric(6,3),
    c6 numeric(6,3),
    c7 char(1),
    c8 char(1),
    c9 char(1),
    ca char(1),
    cb int4,
    cc int4,
    cd int4,
    Primary Key (c1,c2,c3,c4)
  );

unionを使ったSQL

  insert into tab1 (
    c1, c2, c3
  , c4, c5, c6
  , c7, c8, c9, ca
  , cb, cc, cd
  )
  select '1', '-1', '3'
    , 0, 3, null
    , '0', null, null, null
    , null, null, 99
  union
  select '1', '-1', '3'
    , 1, 3, 30.3
    , '0', '0', '0', '0'
    , null, null, 99

にてインサートを行おうとすると、

ERROR:  column "cb" is of type integer but expression is of type text
HINT:  You will need to rewrite or cast the expression.

というエラーが出ます。なにが悪いのでしょうか?

unionをばらして

  insert into tab1 (
    c1, c2, c3
  , c4, c5, c6
  , c7, c8, c9, ca
  , cb, cc, cd
  )
  select '1', '-1', '3'
    , 0, 3, null
    , '0', null, null, null
    , null, null, 99
 ;

  insert into tab1 (
    c1, c2, c3
  , c4, c5, c6
  , c7, c8, c9, ca
  , cb, cc, cd
  )
  select '1', '-1', '3'
    , 1, 3, 30.3
    , '0', '0', '0', '0'
    , null, null, 99
  ;

と実行すればうまくいきます。

Postgresql7.4.1のバグかと思い7.4.2にあげ、
「pg_statisticsテーブルに関連する定義に関する問題で、
 オプティマイザが クラッシュする可能性がある問題が
 修正されました。」に対応するSQL

 DELETE FROM pg_statistic;
 VACUUM pg_statistic;
 UPDATE pg_type SET typalign = 'd' WHERE oid = 2277;
 UPDATE pg_attribute SET attalign = 'd' WHERE atttypid = 2277;

 ANALYZE;

を流しましたが、私の問題は解決されませんでした。

上の例ではfromの無いselectを使ったっていますが、これは問題を
再現させつつ簡単なSQLに置き換えたもので、実際のアプリでは
少々複雑なSQLを使用しています。

環境は
OS: MiracleLinux2.1 (Linux 2.4.9)
gcc: 2.95.3
postgresql 7.4.1 and 7.4.2

cinfigure/make/initdbはpostgresqlユーザーで以下のように
行っています。

$ export CFLAGS='-O2 -march=i686' 
$ ./configure 
$ make 
$ make check 
$ make install 
$ initdb --encoding=EUC_JP --no-locale 



pgsql-jp メーリングリストの案内