How to access array elements via PL/pgSQL trigger?

From: Roland Roberts <roland(at)astrofoto(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: How to access array elements via PL/pgSQL trigger?
Date: 2001-12-28 04:21:51
Message-ID: m21yhgug80.fsf@kuiper.rlent.pnet
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have a (partial) schema as shown below. When I attempt to insert a
row from a Tcl script, I get the following error from the script:

NOTICE: plpgsql: ERROR during compile of exam_statistics_fixup near line 4
ERROR: parse error at or near "["
insert into exam (type, school_id, year, population, level)
values ('ELA', 4, 1999, 'GE', '{ 7,87,208,73 }')

This is running with PostgreSQL 7.1.3 on both client and server.

It looks like I don't understand something about PL/pgSQL functions
and arrays, but I can't find any examples in the documentation or
anything in the TODO list that would indicate there should be a
problem. So what *am* I missing?

NB: remove the "references" constraints, and you should be able to run
this example in isolation from the rest of my database.

create table exam (
type varchar(4) references exam_type_lookup (type),
school_id int references school(id),
year numeric(4) not null,
population varchar(3) not null,
total numeric(4),
level numeric(4)[],
levelpct numeric(5,2)[]
);

create unique index uk_exam on exam (school_id, year, type, population);
create index idx_exam_1 on exam (school_id);

comment on column exam.type is 'Type of exam';
comment on column exam.year is 'Year in which the exam was performed';
comment on column exam.level is 'Number of students performing at the particular level';
comment on column exam.population is 'SE - Special Education, GE - General';

create function exam_statistics_fixup () returns opaque AS '
begin
-- Force derived columns to be consistent with new data.
new.total := new.level[1] + new.level[2] + new.level[3] + new.level[4];
new.levelpct[1] := 100 * new.level[1] / new.total;
new.levelpct[2] := 100 * new.level[2] / new.total;
new.levelpct[3] := 100 * new.level[3] / new.total;
new.levelpct[4] := 100 * new.level[4] / new.total;
return new;
end;
' language 'plpgsql';

create trigger exam_biur
before insert or update
on exam
for each row execute procedure exam_statistics_fixup();

roland
--
PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD RL Enterprises
roland(at)rlenter(dot)com 76-15 113th Street, Apt 3B
roland(at)astrofoto(dot)org Forest Hills, NY 11375

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2001-12-28 04:43:29 Re: How to access array elements via PL/pgSQL trigger?
Previous Message jlr 2001-12-27 19:57:08 Re: Shipping database help desperately needed