PostgreSQL/Storage/Async.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« PostgreSQL/JSONB/Op.pm ↑ Summary PostgreSQL/JSONB.pm »
1 package DBIO::PostgreSQL::Storage::Async;
2 # ABSTRACT: future_io async PostgreSQL transport over DBD::Pg's pg_async binding
3  
4 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 our $VERSION = '0.900004';
8  
9 31 ▶use base 'DBIO::Async::Storage';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 31 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶use DBD::Pg qw(:async);
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use Future::IO;
16 use DBIO::Util ();
17 use DBIO::Exception ();
18 use DBIO::PostgreSQL::SQLMaker;
19 use DBIO::PostgreSQL::DDL ();
20 use DBIO::PostgreSQL::Util ();
21 use namespace::clean;
22  
23 # The private attribute of a database handle that holds the values bound to the
24 # statement in flight (_submit_query ... _collect_result), for _pg_raise.
25 use constant BINDS_SLOT => 'private_dbio_binds';
26  
27  
28 # --- Transport capabilities -------------------------------------------------
29  
30  
31 sub transport_capabilities {
32 my $self = shift;
33 return ($self->next::method(@_), qw(listen notify broker_connect_options));
34 }
35  
36 # --- SQL shaping seams ------------------------------------------------------
37  
38  
39 sub sql_maker_class { 'DBIO::PostgreSQL::SQLMaker' }
40  
41  
42 sub _sql_maker_args {
43 my ($self) = @_;
44 my $owner = ref $self ? $self->_owner_storage : undef;
45  
46 my @quoting = (quote_char => '"', name_sep => '.');
47 if ($owner) {
48 my $sync = $owner->sql_maker;
49 @quoting = (
50 (defined $sync->{quote_char} ? (quote_char => $sync->{quote_char}) : ()),
51 name_sep => $sync->{name_sep},
52 );
53 }
54  
55 return (
56 @quoting,
57 limit_dialect => 'LimitOffset',
58 );
59 }
60  
61 # The shared DBIO::PostgreSQL::SQLMaker also feeds the sync DBI driver, which
62 # needs '?', so the maker keeps emitting '?'. The numbering therefore happens
63 # here, in the transport, once, on what reaches the query seams (core k70 /
64 # ADR 0032). DBD::Pg would take '?' as well as '$N'; the statement is numbered
65 # all the same, so that _submit_query can set pg_placeholder_dollaronly and
66 # DBD::Pg leaves every '?' that is no placeholder -- the jsonpath operator,
67 # which the maker writes '@\?' and the numbering hands on as '@?', first of
68 # all -- as it is.
69 #
70 # Which '?' is a placeholder is not decided here: that is one rule about
71 # PostgreSQL's scanner, kept in DBIO::PostgreSQL::Util::number_placeholders and
72 # shared with the native ev transport.
73  
74  
75 sub _transform_sql {
76 my ($self, $sql) = @_;
77 return DBIO::PostgreSQL::Storage::Async::_Refusal::of_util(
78 sub { DBIO::PostgreSQL::Util::number_placeholders($sql) },
79 );
80 }
81  
82  
83 sub _post_insert_sql { ' RETURNING *' }
84  
85 # --- Connect-info seam ------------------------------------------------------
86  
87  
88 sub _normalize_conninfo {
89 my ($self, $info) = @_;
90  
91 # DBI-form: [ 'dbi:Pg:...', $user, $pass, \%attrs, \%extra ] -> our conninfo
92 # shape. Both attribute hashes, the later winning, as
93 # DBIO::Storage::DBI::_normalize_connect_info merges them (karr k91).
94 if (ref $info eq 'ARRAY'
95 && defined $info->[0] && !ref $info->[0] && $info->[0] =~ /^dbi:/i) {
96 my ($dsn, $user, $pass) = @$info;
97 my %conninfo = (
98 dsn => $dsn,
99 user => $user,
100 password => $pass,
101 attrs => { map { %$_ } grep { ref $_ eq 'HASH' } @{$info}[3, 4] },
102 );
103 $conninfo{pool_size} = delete $conninfo{attrs}{pool_size}
104 if defined $conninfo{attrs}{pool_size};
105 return [ \%conninfo, {} ];
106 }
107  
108 # Broker arrayref [ $broker ], or an already-normalised [ \%conninfo, \%opts ]
109 # pair -- leave for the inherited broker detection / normalisation.
110 return $info;
111 }
112  
113  
114 sub _async_broker_conninfo {
115 my ($self, $mode) = @_;
116  
117 # Without a broker _current_async_connect_info answers what is stored, and
118 # that is no broker's answer (karr k97; as dbio-async refuses, its k43).
119 _pg_refuse(sprintf '%s has no AccessBroker attached to ask for connect info',
120 ref($self) || $self)
121 unless $self->access_broker;
122  
123 my ($conninfo) = $self->_normalize_async_connect_info(
124 $self->_pg_broker_connect_info($self->_current_async_connect_info($mode))
125 );
126 $conninfo->{attrs} = $self->_merge_access_broker_settings(
127 $conninfo->{attrs}, $self->_access_broker_call_attributes,
128 );
129 return $conninfo;
130 }
131  
132 # What the broker answered, in any shape _async_broker_conninfo lists, as the
133 # [ \%conninfo, \%opts ] pair core's _normalize_async_connect_info splits.
134 sub _pg_broker_connect_info {
135 my ($self, $info) = @_;
136  
137 $info = [ $info, {} ] if ref $info eq 'HASH';
138  
139 if (ref $info eq 'ARRAY') {
140 my $first = $info->[0];
141 if (defined $first && !ref $first && $first =~ /^dbi:/i) {
142 # Core's strip also records the storage options it pulls out; that
143 # record belongs to connect_info, not to one broker answer.
144 local $self->{_storage_options};
145 return $self->_normalize_conninfo($self->_strip_private_connect_attrs($info));
146 }
147 return [ $self->_pg_broker_conninfo_hash($first), $info->[1] ]
148 if ref $first eq 'HASH';
149 }
150  
151 _pg_refuse(sprintf(
152 'PostgreSQL future_io cannot connect from what the AccessBroker returned: %s. '
153 . "It takes a DBI-form arrayref [ 'dbi:Pg:...', \$user, \$password, \\%%attrs ], "
154 . 'a hashref of libpq connection parameters (dbname, host, port, user, password, ...), '
155 . 'a hashref with a dsn (dsn, user, password, attrs), '
156 . 'or either hashref paired with an options hashref',
157 $self->_pg_describe_shape($info)),
158 _pg_answer_connect_info($info));
159 }
160  
161 # One conninfo hash from a broker: taken as it is when it has a dsn, otherwise
162 # its libpq connection parameters become the dsn.
163 sub _pg_broker_conninfo_hash {
164 my ($self, $hash) = @_;
165 my %conninfo = %$hash;
166 return \%conninfo if defined $conninfo{dsn};
167  
168 my %transport_key = map { $_ => 1 } qw( user password attrs pool_size );
169 my $known = _pg_answer_connect_info($hash);
170 # An empty dbname names no database: refused, as the sync storage refuses
171 # it (core karr k340), not dropped into a DSN without one.
172 _pg_refuse('PostgreSQL future_io cannot connect from what the AccessBroker '
173 . 'returned: its dbname is an empty dbname, which names no database. Give '
174 . 'the database name, or leave dbname out to connect with host and port alone',
175 $known)
176 if defined $conninfo{dbname} && !ref $conninfo{dbname} && !length $conninfo{dbname};
177  
178 my %param;
179 for my $key (grep { !$transport_key{$_} } keys %conninfo) {
180 my $value = delete $conninfo{$key};
181 $param{$key} = $value if defined $value && length $value;
182 }
183  
184 # A dbname that is a whole DSN is the DSN, as core and dbio-async read it
185 # (core ADR 0057) -- not a database of that name. Nothing set beside it can
186 # be put into it: where it would go is the DSN's business. Core says so for
187 # host and port; here every other parameter would become a part of the DSN
188 # as well, so each one is refused in the same words rather than dropped.
189 if (defined $param{dbname} && !ref $param{dbname} && $param{dbname} =~ /^dbi:/i) {
190 $conninfo{dsn} = delete $param{dbname};
191 _pg_refuse('Connection parameter(s) ' . join(', ', map { "'$_'" } sort keys %param)
192 . ' given beside a dbname that is a whole DSN: they cannot be put into '
193 . 'it. Give them inside the DSN, or give dbname as a database name.',
194 $known)
195 if %param;
196 return \%conninfo;
197 }
198  
199 # "dbi:Pg:" alone leaves the target to libpq's environment (PGHOST,
200 # PGDATABASE, a service file); the sync storage refuses it too.
201 _pg_refuse('PostgreSQL future_io cannot connect from what the AccessBroker '
202 . 'returned: it names no dbname, host or port, so libpq would take the '
203 . 'target from its environment (PGHOST, PGDATABASE, ...). Give at least '
204 . 'one of dbname, host or port (or service, hostaddr)',
205 $known)
206 unless grep { exists $param{$_} } qw(dbname host port hostaddr service);
207  
208 my @params;
209 for my $key (sort keys %param) {
210 # A key is a libpq parameter name: it goes into the DSN as it is, so only
211 # the plain form is a key.
212 _pg_refuse("PostgreSQL future_io cannot connect from what the AccessBroker returned: "
213 . "its connection parameter '$key' is not a libpq parameter name "
214 . "(lower case letters, digits and underscores, beginning with a letter "
215 . "or an underscore)", $known)
216 unless $key =~ /\A[a-z_][a-z0-9_]*\z/;
217 _pg_refuse("PostgreSQL future_io cannot connect from what the AccessBroker returned: "
218 . "its libpq connection parameter '$key' is a reference, not a value", $known)
219 if ref $param{$key};
220 # The DSN is a C string from DBD::Pg on: it would end at the NUL, and
221 # every parameter sorted behind this one would be gone with it.
222 _pg_refuse("PostgreSQL future_io cannot connect from what the AccessBroker returned: "
223 . "its libpq connection parameter '$key' contains a NUL character, which "
224 . 'no connection string can carry', $known)
225 if $param{$key} =~ /\0/;
226 push @params, "$key=" . $self->_pg_dsn_value($param{$key});
227 }
228 $conninfo{dsn} = 'dbi:Pg:' . join ';', @params;
229 return \%conninfo;
230 }
231  
232 # A libpq parameter value as it goes into a DBD::Pg DSN, so that libpq reads
233 # back the value it was given (karr k80, k96). The rule -- what libpq and
234 # DBD::Pg each do to the text, and what is written against it -- is kept in
235 # DBIO::PostgreSQL::Util::quote_dsn_value, which the sync storage writes
236 # through as well (DBIO::PostgreSQL::Storage::_connect_params_dsn, karr k102):
237 # the two connections of one schema get the same DSN for the same parameters.
238 sub _pg_dsn_value {
239 my ($self, $value) = @_;
240 return DBIO::PostgreSQL::Util::quote_dsn_value($value);
241 }
242  
243 # The shape of a broker's answer, for an error message: types and hash keys,
244 # never values -- they carry the password.
245 sub _pg_describe_shape {
246 my ($self, $value) = @_;
247 return 'nothing (undef)' unless defined $value;
248 return 'a plain string' unless ref $value;
249 return sprintf 'a hashref with keys (%s)', join ', ', sort keys %$value
250 if ref $value eq 'HASH';
251 return sprintf 'an arrayref of (%s)', join ', ',
252 map { !defined $_ ? 'undef' : ref $_ ? ref($_) . ' ref' : 'string' } @$value
253 if ref $value eq 'ARRAY';
254 return 'a ' . ref($value) . ' ref';
255 }
256  
257 # --- Pool connection lifecycle ----------------------------------------------
258  
259  
260 sub _create_pool_connection {
261 my ($self, $conninfo) = @_;
262 return $self->_pg_connect($conninfo);
263 }
264  
265 # The one DBI->connect of this transport: a pool connection, and the dedicated
266 # listen connection (_listen_open). The forced attributes win over the
267 # caller's; AutoInactiveDestroy among them, so no connection of this transport
268 # can be ended by a forked child's copy of its handle (karr k79). HandleError
269 # is forced too, around the caller's (_pg_handle_error): this is where every
270 # error DBI reports for the handle, and for the connect itself, is raised.
271 sub _pg_connect {
272 my ($self, $conninfo) = @_;
273  
274 _pg_refuse('PostgreSQL async conninfo must be a hashref with a dsn',
275 _pg_answer_connect_info($conninfo))
276 unless ref $conninfo eq 'HASH' && defined $conninfo->{dsn};
277  
278 # A whole DSN that names no target leaves it to libpq's environment, as the
279 # connection parameters a broker answers do when they name none (karr k121).
280 _pg_refuse('PostgreSQL future_io cannot connect with a DSN that names no '
281 . 'dbname, host or port, so libpq would take the target from its '
282 . 'environment (PGHOST, PGDATABASE, ...). Give at least one of dbname, '
283 . 'host or port (or service, hostaddr) in the DSN',
284 _pg_answer_connect_info($conninfo))
285 if !ref $conninfo->{dsn} && $conninfo->{dsn} =~ /\Adbi:Pg\b/i
286 && !DBIO::PostgreSQL::Util::dsn_names_target($conninfo->{dsn});
287  
288 my %attrs = %{ $conninfo->{attrs} || {} };
289 my @info = (
290 $conninfo->{dsn},
291 $conninfo->{user},
292 $conninfo->{password},
293 {
294 %attrs,
295 AutoCommit => 1,
296 RaiseError => 1,
297 PrintError => 0,
298 AutoInactiveDestroy => 1,
299 HandleError => _pg_handle_error($attrs{HandleError}),
300 },
301 );
302  
303 # Never the password in what a failed connect says (karr k111, core k297).
304 # DBI's message quotes the DSN it was given; libpq's reason quotes what it
305 # could not read -- a URI whole, and the user and password DBD::Pg puts
306 # behind it. Both ways a connect fails are raised in here -- DBI's message
307 # by the HandleError above, and the connect that answered false because the
308 # caller's HandleError took the error -- so one mask covers them, and the
309 # stack trace Carp's verbose mode adds to either.
310 my ($dbh, $ok, $err);
311 {
312 local $@;
313 $ok = eval {
314 $dbh = DBI->connect(@info)
315 or _pg_raise("DBD::Pg connect failed: $DBI::errstr");
316 1;
317 };
318 $err = $@;
319 }
320 die DBIO::Util::mask_connect_secrets($err, \@info) unless $ok;
321  
322 return { dbh => $dbh };
323 }
324  
325 # The HandleError of every handle this transport opens: _pg_dbi_error. When
326 # the connect attributes carry one of the caller's, that one is asked first
327 # and keeps what DBI gives a HandleError -- answering true it has taken the
328 # error, dying it has replaced it. Anything that is no code is left for DBI to
329 # refuse.
330 sub _pg_handle_error {
331 my ($theirs) = @_;
332 return \&_pg_dbi_error unless defined $theirs;
333 return $theirs unless (Scalar::Util::reftype($theirs) // '') eq 'CODE';
334 return sub { $theirs->(@_) ? 1 : _pg_dbi_error(@_) };
335 }
336  
337 # DBI calls its HandleError with the message it would have died with -- the
338 # driver and method, "failed:" and the driver's text -- the handle, and what
339 # the method returned.
340 sub _pg_dbi_error {
341 my ($message, $h) = @_;
342 my $dbh = eval { ref $h && $h->{Type} && $h->{Type} eq 'st' ? $h->{Database} : $h };
343 return _pg_raise($message, $dbh);
344 }
345  
346  
347 sub _pg_raise {
348 my ($message, $dbh) = @_;
349 $message =~ s/\n\z//;
350 $message = _pg_mask_bound($message, $dbh);
351 $message = DBIO::PostgreSQL::Util::mask_error_details($message);
352 die $Carp::Verbose ? DBIO::Util::masked_longmess($message) : "$message\n";
353 }
354  
355 # The values of the statement in flight stand at its handle (BINDS_SLOT), so
356 # that the HandleError, which knows the handle and nothing else, can take them
357 # out of the text.
358 sub _pg_mask_bound {
359 my ($message, $dbh) = @_;
360 my $binds = eval { ref $dbh ? $dbh->{+BINDS_SLOT} : undef };
361 return $message unless ref $binds eq 'ARRAY' && @$binds;
362 my $masked = eval {
363 DBIO::Util::mask_bound_values($message, $binds, sub { $dbh->quote($_[0]) });
364 };
365 return defined $masked ? $masked : $message;
366 }
367  
368 # The statement is over: the values leave the handle, and the statement handle
369 # that holds them as its parameters is let go. A statement without values
370 # leaves its handle where it is (_txn_commit_rolled_back reads a COMMIT's).
371 sub _pg_forget_binds {
372 my ($conn) = @_;
373 return unless ref $conn eq 'HASH';
374 my $dbh = $conn->{dbh};
375 my $held = eval { $dbh ? $dbh->{+BINDS_SLOT} : undef };
376 return unless $held;
377 eval { $dbh->{+BINDS_SLOT} = undef };
378 delete $conn->{sth};
379 return;
380 }
381  
382  
383 sub _pg_refuse { goto &DBIO::PostgreSQL::Storage::Async::_Refusal::raise }
384  
385 # The connect info a refusal is about, in the DBI form the trace masks
386 # (DBIO::Util::masked_longmess): from a conninfo hash, an answer in the DBI
387 # form, or the hash of a [ \%conninfo, \%opts ] pair. Nothing for anything
388 # else.
389 sub _pg_answer_connect_info {
390 my ($info) = @_;
391 $info = $info->[0] if ref $info eq 'ARRAY' && ref $info->[0] eq 'HASH';
392 return [ @{$info}{qw( dsn user password attrs )} ] if ref $info eq 'HASH';
393 return [ @{$info}[0 .. 3] ] if ref $info eq 'ARRAY';
394 return undef;
395 }
396  
397 # Where a refusal is thrown from. DBIO::Exception->throw names the first frame
398 # that is not this package's, nor of a package the patterns of
399 # _skip_namespace_frames name (DBIO::Carp): DBIO's own, and here Future's and
400 # IO::Async's as well -- a refusal reached in a callback of the loop would
401 # otherwise name Future/PP.pm. %Carp::Internal keeps these frames out of the
402 # origin the exception names (the transport's method that refused) and out of
403 # the start of a stack trace.
404 {
405 package # not a class of its own: the place a refusal is thrown from
406 DBIO::PostgreSQL::Storage::Async::_Refusal;
407  
408 $Carp::Internal{ (__PACKAGE__) }++;
409  
410 sub _skip_namespace_frames { '^Future\b|^IO::Async\b' }
411  
412 sub raise {
413 my ($message, $connect_info) = @_;
414 DBIO::Exception->throw($message, $Carp::Verbose, $connect_info);
415 }
416  
417 # A refusal of a DBIO::PostgreSQL::Util rule, raised again as this
418 # transport's. The rule croaks, naming its caller -- the code ref, in this
419 # file; Carp's short form is asked for, so that is what comes back, and the
420 # refusal is raised again in the rule's words without that place.
421 sub of_util {
422 my ($code) = @_;
423 my ($answer, $ok, $err);
424 {
425 local $@;
426 $ok = eval { local $Carp::Verbose = 0; $answer = $code->(); 1 };
427 $err = $@;
428 }
429 return $answer if $ok;
430 die $err unless !ref $err && $err =~ s/ at \Q${\ __FILE__}\E line \d+[^\n]*\n\z//;
431 raise($err);
432 }
433 }
434  
435  
436 sub _reset_pool_connection {
437 my ($self, $conn) = @_;
438 return $self->_query_async_pinned($conn, 'DISCARD ALL', [])->then(sub {
439 $self->_setup_pool_connection($conn);
440 return $self->future_class->done;
441 });
442 }
443  
444  
445 sub _shutdown_pool_connection {
446 my ($self, $conn) = @_;
447 my $dbh = $conn->{dbh} or return;
448 $dbh->disconnect if $dbh->{Active};
449 }
450  
451 # --- Readiness / socket seams -----------------------------------------------
452  
453  
454 sub _conn_ready {
455 my ($self, $conn) = @_;
456 return $conn->{dbh} && $conn->{dbh}{Active} ? 1 : 0;
457 }
458  
459  
460 sub _conn_fileno {
461 my ($self, $conn) = @_;
462 return $conn->{dbh}{pg_socket};
463 }
464  
465 # --- Query submit / collect -------------------------------------------------
466  
467  
468 sub _submit_query {
469 my ($self, $conn, $sql, $bind) = @_;
470 $bind //= [];
471  
472 my $dbh = $conn->{dbh};
473  
474 # Release the previous (already-collected) async statement handle BEFORE
475 # starting a new async query. DBD::Pg tracks async state on the DATABASE
476 # handle; destroying a spent async sth *after* the new execute clobbers the
477 # new query's async status back to 0, so the next _collect_result sees
478 # "pg_ready: No asynchronous query is running" on the second query over a
479 # reused pooled connection. Clearing it here means the new execute owns the
480 # dbh async slot uncontested.
481 delete $conn->{sth};
482 $dbh->{+BINDS_SLOT} = @$bind ? [ @$bind ] : undef;
483  
484 my $ok = eval {
485 my $sth = $dbh->prepare($sql, {
486 pg_async => PG_ASYNC,
487 pg_placeholder_dollaronly => 1,
488 });
489 $sth->execute(@$bind);
490 $conn->{sth} = $sth;
491 1;
492 };
493 my $error = $@;
494 unless ($ok) {
495 _pg_forget_binds($conn);
496 die $error;
497 }
498  
499 return;
500 }
501  
502  
503 sub _collect_result {
504 my ($self, $conn) = @_;
505  
506 my $future;
507 my $ok = eval { $future = $self->_pg_collect($conn); 1 };
508 my $error = $@;
509  
510 # Waiting for more of the result (the re-poll recurses here) keeps the values
511 # for the HandleError; every other end of the statement takes them off.
512 _pg_forget_binds($conn) unless $ok && !$future->is_ready;
513 die $error unless $ok;
514 return $future;
515 }
516  
517 sub _pg_collect {
518 my ($self, $conn) = @_;
519  
520 my $dbh = $conn->{dbh};
521  
522 # The result may not have fully arrived on the first readable event. pg_ready
523 # consumes what is on the wire and reports whether the result is complete;
524 # if not, wait for more and re-check rather than blocking in pg_result.
525 unless ($dbh->pg_ready) {
526 return $self->_await_readable($conn)->then(sub {
527 return $self->_collect_result($conn);
528 });
529 }
530  
531 # Finalise; dies (RaiseError) on a query error and on a lost connection.
532 my $sth = $conn->{sth};
533 my $rv = $dbh->pg_result;
534  
535 # A cancelled statement is the one failure pg_result does not die on: it
536 # returns 0E0 and leaves SQLSTATE 57014 as the only trace (see POD).
537 _pg_raise("DBD::Pg::db pg_result failed: PostgreSQL cancelled the statement "
538 . "(SQLSTATE 57014 query_canceled: statement_timeout or a cancel request; "
539 . "DBD::Pg does not pass on the server's message)", $dbh)
540 if ($dbh->state // '') eq '57014';
541  
542 if (($sth->{NUM_OF_FIELDS} || 0) > 0) {
543 my $rows = $sth->fetchall_arrayref;
544 return $self->future_class->done(@$rows);
545 }
546  
547 return $self->future_class->done($rv);
548 }
549  
550  
551 sub _txn_commit_rolled_back {
552 my ($self, $conn) = @_;
553 my $sth = ref $conn eq 'HASH' ? $conn->{sth} : undef;
554 return 0 unless $sth;
555 return (($sth->{pg_cmd_status} // '') eq 'ROLLBACK') ? 1 : 0;
556 }
557  
558  
559 sub _conn_error_is_fatal {
560 my ($self, $conn) = @_;
561 return $self->_pg_socket_gone($conn);
562 }
563  
564 # The one verdict on whether a connection is dead, shared by
565 # _conn_error_is_fatal and _idle_connection_alive: libpq has no server
566 # connection open (pg_socket -1, or unknown), or there is no handle at all.
567 sub _pg_socket_gone {
568 my ($self, $conn) = @_;
569 my $dbh = ref $conn eq 'HASH' ? $conn->{dbh} : undef;
570 return $dbh && ($dbh->{pg_socket} // -1) >= 0 ? 0 : 1;
571 }
572  
573  
574 sub _idle_connection_alive {
575 my ($self, $conn) = @_;
576 return 0 if $self->_pg_socket_gone($conn);
577  
578 my $dbh = $conn->{dbh};
579 for (1 .. 4) {
580 return 1 unless $self->_pg_readable_now($conn);
581  
582 my ($ok, $err);
583 {
584 local $@;
585 $ok = eval { $self->_pg_nonblocking($conn, sub { $dbh->pg_notifies }); 1 };
586 $err = $@;
587 }
588 return 0 if $self->_pg_socket_gone($conn);
589 die $err unless $ok;
590 }
591 return 1;
592 }
593  
594 # Is the connection's socket readable right now? A select with a zero timeout,
595 # on the cached dup of the socket (_conn_poll_fh). A select error counts as
596 # readable: the read that follows is what finds out.
597 sub _pg_readable_now {
598 my ($self, $conn) = @_;
599 my $rin = '';
600 vec($rin, fileno($self->_conn_poll_fh($conn)), 1) = 1;
601 return select($rin, undef, undef, 0) != 0;
602 }
603  
604 # --- LISTEN / NOTIFY seams --------------------------------------------------
605 #
606 # Core DBIO::Storage::Async owns the contract and the machinery (ADR 0056);
607 # DBIO::Async::Storage owns the Future::IO watch loop, the backoff timer and the
608 # pooled _notify_send (dbio-async ADR 0005). What is left is PostgreSQL's:
609 # the dedicated DBD::Pg handle, quoted LISTEN / UNLISTEN, the pg_notifies drain,
610 # the probe and the pg_notify statement. Private helpers are named _pg_*, not
611 # _listen_*: they share an inheritance chain with core's private _listen_*
612 # helpers, and a same-named method appearing in core later would be shadowed.
613  
614  
615 sub _listen_open {
616 my ($self) = @_;
617 my $provider = $self->_conninfo_provider;
618 my $conninfo = $provider ? $provider->() : $self->{_conninfo};
619 return $self->future_class->done($self->_pg_connect($conninfo));
620 }
621  
622  
623 sub _listen_close {
624 my ($self, $conn) = @_;
625 my $dbh = delete $conn->{dbh} or return;
626 $dbh->disconnect if $dbh->{Active};
627 return;
628 }
629  
630  
631 sub _listen_subscribe {
632 my ($self, $conn, $channel) = @_;
633 return $self->_pg_listen_command($conn, LISTEN => $channel);
634 }
635  
636  
637 sub _listen_unsubscribe {
638 my ($self, $conn, $channel) = @_;
639 return $self->_pg_listen_command($conn, UNLISTEN => $channel);
640 }
641  
642 sub _pg_listen_command {
643 my ($self, $conn, $verb, $channel) = @_;
644 my $sql = "$verb " . $self->_pg_quote_channel($channel);
645 my $dbh = $conn->{dbh} or _pg_refuse("$verb: the listen connection is closed");
646 $self->_pg_nonblocking($conn, sub { $dbh->do($sql) });
647 $self->_pg_dispatch_buffered($conn);
648 return $self->future_class->done;
649 }
650  
651  
652 sub _pg_quote_channel {
653 my ($self, $channel) = @_;
654 return DBIO::PostgreSQL::Storage::Async::_Refusal::of_util(
655 sub { DBIO::PostgreSQL::Util::quote_channel($channel) },
656 );
657 }
658  
659  
660 sub _listen_drain {
661 my ($self, $conn) = @_;
662 my $dbh = $conn->{dbh} or _pg_refuse('the listen connection is closed');
663  
664 my @drained;
665 $self->_pg_nonblocking($conn, sub {
666 while (my $n = $dbh->pg_notifies) {
667 my ($channel, $sender_pid, $payload) = @$n;
668 push @drained, [ $channel, $payload, $sender_pid ];
669 }
670 _pg_raise('draining the listen connection failed: ' . ($dbh->errstr // 'unknown error'))
671 if $dbh->err;
672 });
673 return \@drained;
674 }
675  
676  
677 sub _listen_probe {
678 my ($self, $conn) = @_;
679 my $dbh = $conn->{dbh} or return 0;
680 return 0 unless $self->_pg_nonblocking($conn, sub { $dbh->ping });
681 $self->_pg_dispatch_buffered($conn);
682 return 1;
683 }
684  
685  
686 sub _pg_dispatch_buffered {
687 my ($self, $conn) = @_;
688 for my $n (@{ $self->_listen_drain($conn) }) {
689 last unless $conn->{dbh};
690 $self->_listen_dispatch(@$n);
691 }
692 return;
693 }
694  
695  
696 sub _pg_nonblocking {
697 my ($self, $conn, $code) = @_;
698 my $fh = do { local $@; eval { $self->_conn_poll_fh($conn) } };
699 my $was_blocking = $fh ? $fh->blocking(0) : 0;
700  
701 my ($r, $ok, $err);
702 {
703 local $@;
704 $ok = eval { $r = $code->(); 1 };
705 $err = $@;
706 }
707 $fh->blocking(1) if $was_blocking;
708 die $err unless $ok;
709 return $r;
710 }
711  
712  
713 sub _notify_sql {
714 my ($self, $channel, $payload) = @_;
715 return ('SELECT pg_notify(?, ?)', [ $channel, $payload ]);
716 }
717  
718 # --- Deploy seam ------------------------------------------------------------
719  
720  
721 sub _ddl_class { 'DBIO::PostgreSQL::DDL' }
722  
723  
724 sub deploy_async {
725 my ($self, $schema, @rest) = @_;
726  
727 my @declared = DBIO::PostgreSQL::DDL::_schema_decl($schema, 'pg_extensions');
728 return $self->SUPER::deploy_async($schema, @rest) unless @declared;
729  
730 return $self->_seam_future(_query_async =>
731 DBIO::PostgreSQL::DDL::control_schemas_sql(@declared), [],
732 )->then(sub {
733 my %fixed = map { $_->[0] => $_->[1] } @_;
734 DBIO::PostgreSQL::DDL::check_control_schemas($schema, \%fixed);
735 return $self->_deploy_async_with_control_schemas($schema, \%fixed, @rest);
736 });
737 }
738  
739 # The schemas a control file fixes are handed to _install_ddl for the length
740 # of the synchronous call core's deploy_async makes to it, and no longer: a
741 # second deploy on the same storage reads its own.
742 sub _deploy_async_with_control_schemas {
743 my ($self, $schema, $fixed, @rest) = @_;
744 local $self->{_extension_control_schemas} = $fixed;
745 return $self->SUPER::deploy_async($schema, @rest);
746 }
747  
748  
749 sub _install_ddl {
750 my ($self, $schema) = @_;
751 return $self->_ddl_class->install_ddl($schema,
752 extension_control_schemas => $self->{_extension_control_schemas} // {});
753 }
754  
755 1;
756  
757 __END__
« PostgreSQL/JSONB/Op.pm ↑ Summary PostgreSQL/JSONB.pm »