@@ -492,7 +492,7 @@ void ConnectionImpl::Start(SteadyClock::time_point start_time) {
492492void ConnectionImpl::Finish () { stats_.trx_end_time = SteadyClock::now (); }
493493
494494Connection::StatementId ConnectionImpl::PortalBind (
495- USERVER_NAMESPACE::utils::zstring_view statement ,
495+ const Query& query ,
496496 const std::string& portal_name,
497497 const QueryParameters& params,
498498 OptionalCommandControl statement_cmd_ctl
@@ -504,24 +504,33 @@ Connection::StatementId ConnectionImpl::PortalBind(
504504 } // TODO Prepare unnamed query instead
505505
506506 CheckBusy ();
507+ auto statement = query.GetStatementView ();
507508 const TimeoutDuration network_timeout = NetworkTimeout (statement_cmd_ctl);
508509 auto deadline = testsuite_pg_ctl_.MakeExecuteDeadline (network_timeout);
509510 SetStatementTimeout (std::move (statement_cmd_ctl));
510511 tracing::Span span{FindQueryShortInfo (scopes::kBind , statement)};
511512 conn_wrapper_.FillSpanTags (span, {network_timeout, GetStatementTimeout ()});
512513 if (settings_.statement_log_mode == ConnectionSettings::kLog ) {
513- span.AddTag (tracing::kDatabaseStatement , std::string{statement});
514+ switch (query.GetLogMode ()) {
515+ case Query::LogMode::kFull :
516+ span.AddTag (tracing::kDatabaseStatement , std::string{query.GetStatementView ()});
517+ [[fallthrough]];
518+ case Query::LogMode::kNameOnly :
519+ if (const auto name = query.GetOptionalNameView (); name) {
520+ span.AddTag (tracing::kDatabaseStatementName , std::string{*name});
521+ }
522+ }
514523 }
515524 CheckDeadlineReached (deadline);
516525 auto scope = span.CreateScopeTime ();
517526 CountPortalBind count_bind (stats_);
518527
519- const auto & prepared_info = DoPrepareStatement (statement , params, deadline, span, scope);
528+ const auto & prepared_info = DoPrepareStatement (query , params, deadline, span, scope);
520529
521530 scope.Reset (scopes::kBind );
522- conn_wrapper_.SendPortalBind (prepared_info.statement_name , portal_name, params, scope);
531+ conn_wrapper_.SendPortalBind (prepared_info.meta_statement_name , portal_name, params, scope);
523532
524- WaitResult (statement, deadline, network_timeout, count_bind, span, scope, nullptr );
533+ WaitResult (deadline, network_timeout, count_bind, span, scope, nullptr );
525534 return prepared_info.id ;
526535}
527536
@@ -543,10 +552,21 @@ ResultSet ConnectionImpl::PortalExecute(
543552 " Portal execute uses statement id that is absent in prepared "
544553 " statements"
545554 );
546- tracing::Span span{FindQueryShortInfo (scopes::kExec , prepared_info->statement )};
555+
556+ const auto & query = prepared_info->query ;
557+
558+ tracing::Span span{FindQueryShortInfo (scopes::kExec , query.GetStatementView ())};
547559 conn_wrapper_.FillSpanTags (span, {network_timeout, GetStatementTimeout ()});
548560 if (settings_.statement_log_mode == ConnectionSettings::kLog ) {
549- span.AddTag (tracing::kDatabaseStatement , prepared_info->statement );
561+ switch (query.GetLogMode ()) {
562+ case Query::LogMode::kFull :
563+ span.AddTag (tracing::kDatabaseStatement , std::string{query.GetStatementView ()});
564+ [[fallthrough]];
565+ case Query::LogMode::kNameOnly :
566+ if (const auto name = query.GetOptionalNameView (); name) {
567+ span.AddTag (tracing::kDatabaseStatementName , std::string{*name});
568+ }
569+ }
550570 }
551571 if (deadline.IsReached ()) {
552572 ++stats_.execute_timeout ;
@@ -558,15 +578,7 @@ ResultSet ConnectionImpl::PortalExecute(
558578 CountExecute count_execute (stats_);
559579 conn_wrapper_.SendPortalExecute (portal_name, n_rows, scope);
560580
561- return WaitResult (
562- prepared_info->statement ,
563- deadline,
564- network_timeout,
565- count_execute,
566- span,
567- scope,
568- &prepared_info->description
569- );
581+ return WaitResult (deadline, network_timeout, count_execute, span, scope, &prepared_info->description );
570582}
571583
572584void ConnectionImpl::Listen (std::string_view channel, OptionalCommandControl cmd_ctl) {
@@ -809,12 +821,13 @@ void ConnectionImpl::SetStatementTimeout(OptionalCommandControl cmd_ctl) {
809821}
810822
811823const ConnectionImpl::PreparedStatementInfo& ConnectionImpl::DoPrepareStatement (
812- USERVER_NAMESPACE::utils::zstring_view statement ,
824+ const Query& query ,
813825 const QueryParameters& params,
814826 engine::Deadline deadline,
815827 tracing::Span& span,
816828 tracing::ScopeTime& scope
817829) {
830+ auto statement = query.GetStatementView ();
818831 auto query_hash = QueryHash (statement, params);
819832 const Connection::StatementId query_id{query_hash};
820833
@@ -824,7 +837,7 @@ const ConnectionImpl::PreparedStatementInfo& ConnectionImpl::DoPrepareStatement(
824837 auto * statement_info = prepared_.Get (query_id);
825838 if (statement_info) {
826839 if (statement_info->description .pimpl_ ) {
827- LOG_TRACE () << " Query " << statement << " is already prepared. " ;
840+ LOG_TRACE () << " Query is already prepared" ;
828841 return *statement_info;
829842 } else {
830843 LOG_DEBUG () << " Found prepared but not described statement" ;
@@ -841,12 +854,12 @@ const ConnectionImpl::PreparedStatementInfo& ConnectionImpl::DoPrepareStatement(
841854 }
842855
843856 scope.Reset (scopes::kPrepare );
844- LOG_TRACE () << " Query " << statement << " is not yet prepared" ;
857+ LOG_TRACE () << " Query is not yet prepared" ;
845858
846- const std::string statement_name = " q" + std::to_string (query_hash) + " _" + uuid_;
859+ const std::string meta_statement_name = " q" + std::to_string (query_hash) + " _" + uuid_;
847860 const bool should_prepare = !statement_info;
848861 if (should_prepare) {
849- conn_wrapper_.SendPrepare (statement_name , statement, params, scope);
862+ conn_wrapper_.SendPrepare (meta_statement_name , statement, params, scope);
850863
851864 try {
852865 conn_wrapper_.WaitResult (deadline, scope, nullptr );
@@ -856,13 +869,12 @@ const ConnectionImpl::PreparedStatementInfo& ConnectionImpl::DoPrepareStatement(
856869 // it. This situation might happen when `SendPrepare` times out and we
857870 // erase the statement from `prepared_` map.
858871 LOG_DEBUG ()
859- << " Statement `" << statement
860- << " ` was already prepared, there was possibly a timeout "
861- " while preparing, see log above." ;
872+ << " Statement was already prepared, there was possibly a timeout while preparing, see log "
873+ " above." ;
862874 ++stats_.duplicate_prepared_statements ;
863875
864876 // Mark query as already sent
865- prepared_.Put (query_id, {query_id, std::string{statement}, statement_name , ResultSet{nullptr }});
877+ prepared_.Put (query_id, {query_id, query, meta_statement_name , ResultSet{nullptr }});
866878
867879 if (IsInTransaction ()) {
868880 // Transaction failed, need to throw
@@ -876,7 +888,7 @@ const ConnectionImpl::PreparedStatementInfo& ConnectionImpl::DoPrepareStatement(
876888 LOG_DEBUG () << " Don't send prepare, already sent" ;
877889 }
878890
879- conn_wrapper_.SendDescribePrepared (statement_name , scope);
891+ conn_wrapper_.SendDescribePrepared (meta_statement_name , scope);
880892 auto res = conn_wrapper_.WaitResult (deadline, scope, nullptr );
881893 if (!res.pimpl_ ) {
882894 throw CommandError (" WaitResult() returned nullptr" );
@@ -886,7 +898,7 @@ const ConnectionImpl::PreparedStatementInfo& ConnectionImpl::DoPrepareStatement(
886898 res.GetRowDescription ().CheckBinaryFormat (db_types_);
887899
888900 if (!statement_info) {
889- prepared_.Put (query_id, {query_id, std::string{statement}, statement_name , std::move (res)});
901+ prepared_.Put (query_id, {query_id, query, meta_statement_name , std::move (res)});
890902 statement_info = prepared_.Get (query_id);
891903 } else {
892904 statement_info->description = std::move (res);
@@ -908,8 +920,8 @@ void ConnectionImpl::DiscardOldPreparedStatements(engine::Deadline deadline) {
908920}
909921
910922void ConnectionImpl::DiscardPreparedStatement (const PreparedStatementInfo& info, engine::Deadline deadline) {
911- LOG_DEBUG () << " Discarding prepared statement " << info.statement_name ;
912- ExecuteCommandNoPrepare (" DEALLOCATE " + info.statement_name , deadline);
923+ LOG_DEBUG () << " Discarding prepared statement " << info.meta_statement_name ;
924+ ExecuteCommandNoPrepare (" DEALLOCATE " + info.meta_statement_name , deadline);
913925}
914926
915927ResultSet ConnectionImpl::ExecuteCommand (const Query& query, engine::Deadline deadline, logging::Level span_log_level) {
@@ -930,9 +942,8 @@ ResultSet ConnectionImpl::ExecuteCommand(
930942 return ExecuteCommandNoPrepare (query, params, deadline);
931943 }
932944
933- const auto & statement = query.GetStatementView ();
934945 if (settings_.ignore_unused_query_params == ConnectionSettings::kCheckUnused ) {
935- CheckQueryParameters (statement , params);
946+ CheckQueryParameters (query. GetStatementView () , params);
936947 }
937948
938949 DiscardOldPreparedStatements (deadline);
@@ -948,7 +959,7 @@ ResultSet ConnectionImpl::ExecuteCommand(
948959 auto scope = span.CreateScopeTime ();
949960 CountExecute count_execute (stats_);
950961
951- const auto & prepared_info = DoPrepareStatement (statement , params, deadline, span, scope);
962+ const auto & prepared_info = DoPrepareStatement (query , params, deadline, span, scope);
952963
953964 const ResultSet* description_ptr_to_read = nullptr ;
954965 PGresult* description_ptr_to_send = nullptr ;
@@ -958,8 +969,8 @@ ResultSet ConnectionImpl::ExecuteCommand(
958969 }
959970
960971 scope.Reset (scopes::kExec );
961- conn_wrapper_.SendPreparedQuery (prepared_info.statement_name , params, scope, description_ptr_to_send);
962- return WaitResult (statement, deadline, network_timeout, count_execute, span, scope, description_ptr_to_read);
972+ conn_wrapper_.SendPreparedQuery (prepared_info.meta_statement_name , params, scope, description_ptr_to_send);
973+ return WaitResult (deadline, network_timeout, count_execute, span, scope, description_ptr_to_read);
963974}
964975
965976const ConnectionImpl::PreparedStatementInfo& ConnectionImpl::PrepareStatement (
@@ -970,21 +981,27 @@ const ConnectionImpl::PreparedStatementInfo& ConnectionImpl::PrepareStatement(
970981 const auto deadline = testsuite_pg_ctl_.MakeExecuteDeadline (timeout);
971982 CheckDeadlineReached (deadline);
972983
973- const auto & statement = query.GetStatementView ();
974-
975984 tracing::Span span{FindQueryShortInfo (scopes::kPrepare , query.GetStatementView ())};
976985 conn_wrapper_.FillSpanTags (span, {timeout, GetStatementTimeout ()});
977986 if (settings_.statement_log_mode == ConnectionSettings::kLog ) {
978- span.AddTag (tracing::kDatabaseStatement , std::string{statement});
987+ switch (query.GetLogMode ()) {
988+ case Query::LogMode::kFull :
989+ span.AddTag (tracing::kDatabaseStatement , std::string{query.GetStatementView ()});
990+ [[fallthrough]];
991+ case Query::LogMode::kNameOnly :
992+ if (const auto name = query.GetOptionalNameView (); name) {
993+ span.AddTag (tracing::kDatabaseStatementName , std::string{*name});
994+ }
995+ }
979996 }
980997
981998 auto scope = span.CreateScopeTime ();
982- return DoPrepareStatement (statement , params, deadline, span, scope);
999+ return DoPrepareStatement (query , params, deadline, span, scope);
9831000}
9841001
9851002void ConnectionImpl::AddIntoPipeline (
9861003 CommandControl cc,
987- const std::string& prepared_statement_name ,
1004+ const std::string& meta_statement_name ,
9881005 const detail::QueryParameters& params,
9891006 const ResultSet& description,
9901007 tracing::ScopeTime& scope
@@ -999,7 +1016,7 @@ void ConnectionImpl::AddIntoPipeline(
9991016 SetStatementTimeout (cc);
10001017
10011018 PGresult* description_to_send = IsOmitDescribeInExecuteEnabled () ? description.pimpl_ ->handle .get () : nullptr ;
1002- conn_wrapper_.SendPreparedQuery (prepared_statement_name , params, scope, description_to_send);
1019+ conn_wrapper_.SendPreparedQuery (meta_statement_name , params, scope, description_to_send);
10031020
10041021 conn_wrapper_.PutPipelineSync ();
10051022}
@@ -1037,15 +1054,14 @@ ResultSet ConnectionImpl::ExecuteCommandNoPrepare(
10371054 const QueryParameters& params,
10381055 engine::Deadline deadline
10391056) {
1040- const auto & statement = query.GetStatementView ();
10411057 auto network_timeout = std::chrono::duration_cast<std::chrono::milliseconds>(deadline.TimeLeft ());
10421058 CheckBusy ();
10431059 CheckDeadlineReached (deadline);
10441060 auto span = MakeQuerySpan (query, {network_timeout, GetStatementTimeout ()});
10451061 auto scope = span.CreateScopeTime ();
10461062 CountExecute count_execute (stats_);
1047- conn_wrapper_.SendQuery (statement , params, scope);
1048- return WaitResult (statement, deadline, network_timeout, count_execute, span, scope, nullptr );
1063+ conn_wrapper_.SendQuery (query. GetStatementView () , params, scope);
1064+ return WaitResult (deadline, network_timeout, count_execute, span, scope, nullptr );
10491065}
10501066
10511067void ConnectionImpl::SendCommandNoPrepare (const Query& query, engine::Deadline deadline) {
@@ -1149,7 +1165,6 @@ void ConnectionImpl::FillBufferCategories(ResultSet& res) {
11491165
11501166template <typename Counter>
11511167ResultSet ConnectionImpl::WaitResult (
1152- USERVER_NAMESPACE::utils::zstring_view statement,
11531168 engine::Deadline deadline,
11541169 TimeoutDuration network_timeout,
11551170 Counter& counter,
@@ -1181,8 +1196,7 @@ ResultSet ConnectionImpl::WaitResult(
11811196 } catch (const ConnectionTimeoutError& e) {
11821197 ++stats_.execute_timeout ;
11831198 LOG_LIMITED_WARNING ()
1184- << " Statement `" << statement << " ` network timeout error: " << e << " . "
1185- << " Network timeout was " << network_timeout.count () << " ms" ;
1199+ << " Network timeout error: " << e << " . Network timeout was " << network_timeout.count () << " ms" ;
11861200 span.AddTag (tracing::kErrorFlag , true );
11871201 throw ;
11881202 } catch (const QueryCancelled& e) {
@@ -1192,16 +1206,14 @@ ResultSet ConnectionImpl::WaitResult(
11921206 server::request::MarkTaskInheritedDeadlineExpired ();
11931207 }
11941208 LOG_LIMITED_WARNING ()
1195- << " Statement ` " << statement << " ` was cancelled" << (cancelled_by_dp ? " by deadline propagation" : " " )
1196- << " : " << e << " . Statement timeout was " << current_statement_timeout_.count () << " ms" ;
1209+ << " Statement was cancelled" << (cancelled_by_dp ? " by deadline propagation" : " " ) << " : " << e
1210+ << " . Statement timeout was " << current_statement_timeout_.count () << " ms" ;
11971211 span.AddTag (tracing::kErrorFlag , true );
11981212 throw ;
11991213 } catch (const FeatureNotSupported& e) {
12001214 // yes, this is the only way to discern this error
12011215 if (e.GetServerMessage ().GetPrimary () == kBadCachedPlanErrorMessage ) {
1202- LOG_LIMITED_WARNING ()
1203- << " Scheduling prepared statements invalidation due to "
1204- " cached plan change" ;
1216+ LOG_LIMITED_WARNING () << " Scheduling prepared statements invalidation due to cached plan change" ;
12051217 is_discard_prepared_pending_ = true ;
12061218 }
12071219 span.AddTag (tracing::kErrorFlag , true );
0 commit comments