@@ -1167,32 +1167,40 @@ middle_pgsql_t::~middle_pgsql_t() {
11671167
11681168}
11691169
1170- std::shared_ptr<const middle_query_t > middle_pgsql_t::get_instance () const {
1171- middle_pgsql_t * mid = new middle_pgsql_t ();
1172- mid->out_options = out_options;
1173- mid->append = out_options->append ;
1174- mid->mark_pending = mark_pending;
1175-
1176- // NOTE: this is thread safe for use in pending async processing only because
1177- // during that process they are only read from
1178- mid->cache = cache;
1179- mid->persistent_cache = persistent_cache;
1180-
1181- // We use a connection per table to enable the use of COPY */
1182- for (int i=0 ; i<num_tables; i++) {
1170+ std::shared_ptr<middle_query_t >
1171+ middle_pgsql_t ::get_query_instance(std::shared_ptr<middle_t > const &from) const
1172+ {
1173+ auto *src = dynamic_cast <middle_pgsql_t *>(from.get ());
1174+ assert (src);
1175+
1176+ // Return a copy of the original, as we need separate database connections.
1177+ std::unique_ptr<middle_pgsql_t > mid (new middle_pgsql_t ());
1178+ mid->out_options = src->out_options ;
1179+ mid->append = src->out_options ->append ;
1180+ mid->mark_pending = src->mark_pending ;
1181+
1182+ // NOTE: this is thread safe for use in pending async processing only because
1183+ // during that process they are only read from
1184+ mid->cache = src->cache ;
1185+ mid->persistent_cache = src->persistent_cache ;
1186+
1187+ // We use a connection per table to enable the use of COPY
1188+ for (int i = 0 ; i < num_tables; i++) {
11831189 mid->connect (mid->tables [i]);
11841190 PGconn* sql_conn = mid->tables [i].sql_conn ;
11851191
1186- if (tables[i].prepare ) {
1187- pgsql_exec (sql_conn, PGRES_COMMAND_OK, " %s" , tables[i].prepare );
1192+ if (mid->tables [i].prepare ) {
1193+ pgsql_exec (sql_conn, PGRES_COMMAND_OK, " %s" ,
1194+ mid->tables [i].prepare );
11881195 }
11891196
1190- if (append && tables[i].prepare_intarray ) {
1191- pgsql_exec (sql_conn, PGRES_COMMAND_OK, " %s" , tables[i].prepare_intarray );
1197+ if (mid->append && mid->tables [i].prepare_intarray ) {
1198+ pgsql_exec (sql_conn, PGRES_COMMAND_OK, " %s" ,
1199+ mid->tables [i].prepare_intarray );
11921200 }
11931201 }
11941202
1195- return std::shared_ptr<const middle_query_t >(mid);
1203+ return std::shared_ptr<middle_query_t >(mid. release () );
11961204}
11971205
11981206size_t middle_pgsql_t::pending_count () const {
0 commit comments