database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
ㆍPostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
PostgreSQL Q&A 6828 게시물 읽기
No. 6828
trigger와 function 질문입니다. 답글 좀...
작성자
김일균
작성일
2006-09-15 11:22ⓒ
2006-09-15 13:03ⓜ
조회수
4,353

plpgsql은 처음쓰는겁니다 우선 데이터값들이 들어오는 상태에서 
CREATE TABLE event
(
  sid int4 NOT NULL,
  cid int8 NOT NULL,
  signature int4 NOT NULL,
  "timestamp" timestamptz NOT NULL,
  CONSTRAINT event_pkey PRIMARY KEY (sid, cid)

CREATE TABLE ips_count_week
(
  date timestamp NOT NULL,
  sig_id int4 NOT NULL,
  count_rule int4 NOT NULL default 0,
  CONSTRAINT ips_count_date_pkey PRIMARY KEY (date),
  CONSTRAINT meddle_rule_fkey FOREIGN KEY (sig_id) REFERENCES signature(sig_id)

이 둘 테이블간에 trigger를 걸었고 
create trigger count_week_trgger
after insert on event
for each row
execute procedure count_func();
실행 시켰을때 
WARNING:  changing return type of function count_func from "opaque" to "trigger"
이 부분은 나와서 좀 찜찜하고
이것에 대한 function은
create or replace function count_func() RETURNS opaque AS '
begin
   if TG_OP = ''INSERT'' then
     update ips_count_week SET count_rule = count_rule + 1 where sig_id = NEW.signature
     and date_trunc(hour, date) = date_trunc(hour, NEW.timestamp );
      return NEW;
   end if;
end;
'language 'plpgsql';
이것을 실행했을때의 결과는 로그상으로 

database: postgresql_error: ERROR:  column "hour" does not exist
CONTEXT:  PL/pgSQL function "count_func" line 4 at SQL statement

database: postgresql_error: ERROR:  column "hour" does not exist
CONTEXT:  PL/pgSQL function "count_func" line 4 at SQL statement

database: postgresql_error: ERROR:  column "hour" does not exist
CONTEXT:  PL/pgSQL function "count_func" line 4 at SQL statement

Sep 15 11:20:05 test postgres[4339]: [6-1] ERROR:  column "hour" does not exist
Sep 15 11:20:05 test postgres[4339]: [6-2] CONTEXT:  PL/pgSQL function "count_func" line 4 at SQL statement
Sep 15 11:20:05 test postgres[4339]: [7-1] ERROR:  column "hour" does not exist
Sep 15 11:20:05 test postgres[4339]: [7-2] CONTEXT:  PL/pgSQL function "count_func" line 4 at SQL statement
Sep 15 11:20:05 test postgres[4339]: [8-1] ERROR:  column "hour" does not exist
Sep 15 11:20:05 test postgres[4339]: [8-2] CONTEXT:  PL/pgSQL function "count_func" line 4 at SQL statement
database: postgresql_error: ERROR:  column "hour" does not exist
CONTEXT:  PL/pgSQL function "count_func" line 4 at SQL statement

Sep 15 11:20:05 test postgres[4339]: [9-1] ERROR:  column "hour" does not exist
Sep 15 11:20:05 test postgres[4339]: [9-2] CONTEXT:  PL/pgSQL function "count_func" line 4 at SQL statement

이렇게 나오고 있습니다 
plpgsql은 처음 사용하였고 new와 old개념또한 조금 애매모호한 상태입니다. 
책은 열심히 뒤지고 있으나 뽀족한 방법도 없고 
어떻게 해결에 실마리라도 제공해 주실 수 없는지요..... 부탁드리겠습니다.

이 글에 대한 댓글이 총 2건 있습니다.
create or replace function count_func() RETURNS opaque AS '
begin
   if TG_OP = ''INSERT'' then
     update ips_count_week SET count_rule = count_rule + 1 where sig_id = NEW.signature
     and date_trunc(hour, date) = date_trunc(hour, NEW.timestamp );
      return NEW;
   end if;
end;
'language 'plpgsql';

에서 and date_trunc(hour, date) = date_trunc(hour, NEW.timestamp);
를요 and date_trunc('hour', date) = date_trunc('hour', NEW.timestamp);
로 바꿔주세요. 그럼 일단 에러는 안날테구요.
리턴값이 opaque 보다는 trigger 로 리턴을 하시고 return null 를 하시면 에러가 안날겝니다~
신기배(소타)님이 2006-09-15 15:17에 작성한 댓글입니다.

대충은 해결이 되었습니다 감사합니다.

김일균님이 2006-09-15 16:59에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
6831[질문]그룹 D/W에서...
궁금2
2006-09-21
3585
6830성적에서 총점순위로 5위까지만 출력되게 하려면? [1]
궁금해요
2006-09-20
3379
6829일본어 관련 문제 입니다 [2]
2006-09-17
3486
6828trigger와 function 질문입니다. 답글 좀... [2]
김일균
2006-09-15
4353
6826a컴푸터에있는 postgresql의 테이블과내용을 [1]
초보
2006-09-11
3465
6825[질문]postgresql과 PDA연결.
이기자
2006-09-11
3309
6824PostgreSQL 접근 권한 설정 질문입니다. [2]
이정오
2006-09-10
5008
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.016초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다