agora inbox for pgsql-admin@postgresql.org
help / color / mirror / Atom feedProblem with "on conflict"
3+ messages / 3 participants
[nested] [flat]
* Problem with "on conflict"
@ 2025-03-26 16:15 Udo Polder <udo.polder@gmail.com>
0 siblings, 2 replies; 3+ messages in thread
From: Udo Polder @ 2025-03-26 16:15 UTC (permalink / raw)
To: pgsql-admin@lists.postgresql.org
Hi all. I have some really strange behavior with postgres and 'on conflict'.
I had some error today at a customer and i figured out, the problem is
triggered by postgres doing an insert on a table with wrong data (not
even provided), when it should insert the data.
After some playing around with the query, the error just went away,
magically ….
Here you can see, that the insert is updating stuff:
There also is a trigger on the db:
CREATE OR REPLACE FUNCTION fn_hut_bundle_create_id() returns TRIGGER AS $$
begin
if new.bundle_id is null or new.bundle_id='' then
NEW.bundle_id = concat('HB-', nextval('seq_hut_bundle_id'));
end if;
return NEW;
END;
$$ LANGUAGE plpgsql;
create trigger tr_hut_bundle_id before insert on hut_bundle for each row
EXECUTE FUNCTION fn_hut_bundle_create_id();
now working:
Can someone give me some hint, what the problem could be? After my
„playing“ the error can not be reproduced any longer, and the statement
is inserting stuff like it should.
i played with null and '' as a primary key(bundle_id)
Postgres is:
PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu,
compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
Any help would be very welcome ....
Thanks
Attachments:
[image/png] ti600gsoOaUj04Ng.png (119.4K, ../../939e57d2-9294-4557-b436-eb5e9b268f6f@gmail.com/3-ti600gsoOaUj04Ng.png)
download | view image
[image/png] U4iCI1t7CX28e3oM.png (111.0K, ../../939e57d2-9294-4557-b436-eb5e9b268f6f@gmail.com/4-U4iCI1t7CX28e3oM.png)
download | view image
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Problem with "on conflict"
@ 2025-03-26 16:31 Rui DeSousa <rui.desousa@icloud.com>
parent: Udo Polder <udo.polder@gmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: Rui DeSousa @ 2025-03-26 16:31 UTC (permalink / raw)
To: Udo Polder <udo.polder@gmail.com>; +Cc: pgsql-admin@lists.postgresql.org
> On Mar 26, 2025, at 12:15 PM, Udo Polder <udo.polder@gmail.com> wrote:
>
> Hi all. I have some really strange behavior with postgres and 'on conflict'.
> I had some error today at a customer and i figured out, the problem is triggered by postgres doing an insert on a table with wrong data (not even provided), when it should insert the data.
>
> After some playing around with the query, the error just went away, magically ….
> Here you can see, that the insert is updating stuff:
>
> There also is a trigger on the db:
>
> CREATE OR REPLACE FUNCTION fn_hut_bundle_create_id() returns TRIGGER AS $$
> begin
> if new.bundle_id is null or new.bundle_id='' then
> NEW.bundle_id = concat('HB-', nextval('seq_hut_bundle_id'));
> end if;
>
> return NEW;
> END;
> $$ LANGUAGE plpgsql;
> create trigger tr_hut_bundle_id before insert on hut_bundle for each row EXECUTE FUNCTION fn_hut_bundle_create_id();
>
> <ti600gsoOaUj04Ng.png>
>
>
>
> now working:
> <U4iCI1t7CX28e3oM.png>
>
> Can someone give me some hint, what the problem could be? After my „playing“ the error can not be reproduced any longer, and the statement is inserting stuff like it should.
> i played with null and '' as a primary key(bundle_id)
>
> Postgres is:
> PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
>
> Any help would be very welcome ....
>
> Thanks
>
To me it looks like the record already existed and the on conflict update occurred. Given that hut_id is not updated on conflict; the old value remained. Was the sequence recycled or was 'HB-32’ created manually beforehand?
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Problem with "on conflict"
@ 2025-03-26 16:32 Tom Lane <tgl@sss.pgh.pa.us>
parent: Udo Polder <udo.polder@gmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: Tom Lane @ 2025-03-26 16:32 UTC (permalink / raw)
To: Udo Polder <udo.polder@gmail.com>; +Cc: pgsql-admin@lists.postgresql.org
Udo Polder <udo.polder@gmail.com> writes:
> I had some error today at a customer and i figured out, the problem is
> triggered by postgres doing an insert on a table with wrong data (not
> even provided), when it should insert the data.
> After some playing around with the query, the error just went away,
> magically ….
Hmmm...
> Postgres is:
> PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu,
> compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
If it's really 14.1, that's quite ancient and full of known bugs.
The first thing you ought to do is update to latest minor release,
14.17. I did not look at the release notes to see if any fixes
overlap this area, but it'd be silly to spend a lot of time
tracking down a problem only to find it's already fixed.
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2025-03-26 16:32 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-03-26 16:15 Problem with "on conflict" Udo Polder <udo.polder@gmail.com>
2025-03-26 16:31 ` Rui DeSousa <rui.desousa@icloud.com>
2025-03-26 16:32 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox