multiple statement 'instead of' rule

Started by Merlin Moncurealmost 20 years ago5 messagesgeneral
Jump to latest
#1Merlin Moncure
mmoncure@gmail.com

I have a repeatable issue where on an update rule (attached to a view)
not all of the statements are firing under certain conditions. There
is no error but the rule does not seem to be completely going through
the statements. I'm not sure if this is correct behavior reading the
documentation but it smells funny. I confirmed the statements are not
updating by
inserting sql log statements in between the queries in the rule.
Also, by converting the rule to a rule + sql function it works 100%
correct every time. this is on postgresql 8.1.4 on windows xp.

create or replace rule insert_fsv as on insert to frequency_service_view
do instead
(
insert into frequency_operation
select new.table_name, new.frequency, old.code where new.set =
true and old.set = false;
delete from frequency_operation
where table_name = old.table_name and frequency_operation.code =
old.code and
frequency_operation.frequency = new.frequency and new.set = false;
update operation
set code = new.code where code = old.code and old.code != new.code;
);

merlin

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Merlin Moncure (#1)
Re: multiple statement 'instead of' rule

"Merlin Moncure" <mmoncure@gmail.com> writes:

create or replace rule insert_fsv as on insert to frequency_service_view
do instead
(
insert into frequency_operation
select new.table_name, new.frequency, old.code where new.set =
true and old.set = false;
delete from frequency_operation
where table_name = old.table_name and frequency_operation.code =
old.code and
frequency_operation.frequency = new.frequency and new.set = false;
update operation
set code = new.code where code = old.code and old.code != new.code;
);

What is frequency_service_view? Is it by any chance dependent on
frequency_operation? If so, your changes to frequency_operation will
affect the behavior of OLD references.

regards, tom lane

#3Kevin Jenkins
gameprogrammer@rakkar.org
In reply to: Merlin Moncure (#1)
Junk binary date?

I call PQexecParams with the last parameter as 1 to return binary
data. I then get this data with:

fileLengthPtr = PQgetvalue(result, rowIndex, fileLengthColumnIndex);
memcpy(&fileLength, fileLengthPtr, sizeof(fileLength));

The value being returned is of type integer.

It should have the value 7237 in binary.
It actually has the value:
fileLengthPtr[0] 0 char
fileLengthPtr[1] 0 char
fileLengthPtr[2] 28 '?' char
fileLengthPtr[3] 69 'E' char

Which is not 7237, it's:
fileLength 1159462912 int

Why?

#4Michael Fuhr
mike@fuhr.org
In reply to: Kevin Jenkins (#3)
Re: Junk binary date?

On Tue, Jun 20, 2006 at 09:12:50PM -0700, Kevin Jenkins wrote:

I call PQexecParams with the last parameter as 1 to return binary
data. I then get this data with:

fileLengthPtr = PQgetvalue(result, rowIndex, fileLengthColumnIndex);
memcpy(&fileLength, fileLengthPtr, sizeof(fileLength));

The value being returned is of type integer.

It should have the value 7237 in binary.
It actually has the value:
fileLengthPtr[0] 0 char
fileLengthPtr[1] 0 char
fileLengthPtr[2] 28 '?' char
fileLengthPtr[3] 69 'E' char

Which is not 7237, it's:
fileLength 1159462912 int

Why?

7237 decimal = 1c45 hex
28 decimal = 1c hex
69 decimal = 45 hex

The data looks correct once you recognize that it's in network byte
order (big endian). You'll need to convert it to host byte order.

--
Michael Fuhr

#5Merlin Moncure
mmoncure@gmail.com
In reply to: Tom Lane (#2)
Re: multiple statement 'instead of' rule

On 6/20/06, Tom Lane <tgl@sss.pgh.pa.us> wrote:

"Merlin Moncure" <mmoncure@gmail.com> writes:

create or replace rule insert_fsv as on insert to frequency_service_view
do instead
(
insert into frequency_operation
select new.table_name, new.frequency, old.code where new.set =
true and old.set = false;
delete from frequency_operation
where table_name = old.table_name and frequency_operation.code =
old.code and
frequency_operation.frequency = new.frequency and new.set = false;
update operation
set code = new.code where code = old.code and old.code != new.code;
);

What is frequency_service_view? Is it by any chance dependent on
frequency_operation? If so, your changes to frequency_operation will
affect the behavior of OLD references.

right, actually that was a typo, was supposed to be 'create or replace
rule insert_fov as on insert to frequency_operation_view'. I was
considering that old/new are invalid which is fine, but the problem is
in some cases the third (and sometimes second query) never fires at
all with any arguments. I confirmed this by inserting into a log
table in between the rule queries (they never fired either).

I can prepare a test case if you think it's worth it.

Merlin