Does Rails use one database session indefinitely?

I have a use case for custom prepared PostgreSQL reports in a Rails application (other than doing ActiveRecord automatically ).

My use case is to insert a bunch of lines in bulk, for example:

PREPARE insert_2_events (text, text, timestamp, int, json, timestamp, timestamp)
AS
INSERT INTO events (uuid, kind, generated_at, team_id, data, created_at, updated_at)
VALUES
($1, $2, $3, $4, $5, $6, $7), ($8, $9, $10, $11, $12, $13, $14)
ON CONFLICT(uuid) DO NOTHING;

EXECUTE insert_2_events(
  'ed8bd52a-4ea9-4548-ab02-87adfca90789', 'call', '2017-04-06T16:14:20-04:00', 1, '{"some":"data"}', '2017-04-06T20:14:20+00:00', '2017-04-06T20:14:20+00:00',
  '27c3251e-aff0-4005-918a-d7011f984515', 'text', '2017-04-06T16:14:20-04:00', 1, '{awesome:true}', '2017-04-06T20:14:20+00:00', '2017-04-06T20:14:20+00:00'
);

I cannot know if for any given execution there will be 2 lines, as shown, 10 lines or something else. So I would like to be able to do this:

  • If my method is called with 3 lines, see if I have already PREPAREedited the three-string version of this statement.
  • If not, PREPAREit
  • EXECUTE it

I could track: "Have I already prepared this?" in the application code, but I'm not sure if it is reliable.

PostgreSQL :

. , , . , ; . DEALLOCATE.

, " " Rails-.

  • Rails ?
  • ( Unicorn, Puma - ) ( ), , ?

, , DEALLOCATE , .

, , , , .

+4
1

PostgreSQL Rails : PostgresSQL Rails.

Rails ?

: .

: Rails - pool /. .

, . Rails ( , )

, ( PgBouncer, ), .

( Unicorn, Puma - )

: .

: . , , , , .

+3

Source: https://habr.com/ru/post/1674245/


All Articles