Re: Slow query to get last created row using CURRVAL - Mailing list pgsql-performance

From Віталій Тимчишин
Subject Re: Slow query to get last created row using CURRVAL
Date
Msg-id AANLkTinwFRuBMshSntDUiY_fwSiW7npTkHzt7owB=6Ta@mail.gmail.com
Whole thread Raw
In response to Slow query to get last created row using CURRVAL  (Mathieu De Zutter <mathieu@dezutter.org>)
List pgsql-performance


2010/12/4 Mathieu De Zutter <mathieu@dezutter.org>

For each page load I first create an entry in that table, e.g.:

INSERT INTO log_event (user_id, ip, action_id, object1_id, object2_id,
event_timestamp, comments) VALUES (1, '127.0.0.1', 96, null, null,
NOW(), 'TEST');

After that, I want to retrieve the data stored in log_event from a
trigger, e.g.:

SELECT user_id FROM log_event WHERE id = CURRVAL('log_event_id_seq');

This way my insert-trigger knows who is creating the new row, while
using only one pg-user to query the database.

Please note that you can use next query to perform both insert and select:
 
INSERT INTO log_event (user_id, ip, action_id, object1_id, object2_id,
event_timestamp, comments) VALUES (1, '127.0.0.1', 96, null, null,
NOW(), 'TEST') returning user_id;
--
Best regards,
 Vitalii Tymchyshyn

pgsql-performance by date:

Previous
From: "Kevin Grittner"
Date:
Subject: Re: problem with from_collapse_limit and joined views
Next
From: Tom Lane
Date:
Subject: Re: Query-plan for partitioned UPDATE/DELETE slow and swaps vmem compared to SELECT