pgsql create table,cpp fill psql table via the third party library pqxx

发布时间 2023-10-24 23:23:38作者: FredGrit
//create table t1;
create table t1(id bigserial not null primary key,author varchar(40) not null,comment varchar(40) not null,content varchar(40) not null,header varchar(40) not null,isbn varchar(40) not null,object varchar(40) not null,summary varchar(40) not null);

 

//main.cpp
#include <algorithm>
#include <barrier>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <map>
#include <queue>
#include <random>
#include <sstream>
#include <thread>
#include <time.h>
#include <uuid/uuid.h>
#include <vector>
#include <pqxx/pqxx>

std::string get_time_now(bool is_exact = true)
{
    std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);
    struct tm tm_info = *localtime(&raw_time);
    std::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S");
    if (is_exact)
    {
        auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
        auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
        auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
        auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
        ss << "_"
           << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000)
           << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000)
           << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000);
    }
    return ss.str();
}

char uuid_value[37];
uint32_t rand32()
{
    return ((rand() & 0x3) << 30) | ((rand() & 0x7fff) << 15) | (rand() & 0x7fff);
}

char *gen_uuid4(size_t len)
{
    int n = snprintf(uuid_value, len, "%08x-%04x-%04x-%04x-%04x%08x",
                     rand32(),                       // Generates a 32-bit Hex number
                     rand32() & 0xffff,              // Generates a 16-bit Hex number
                     ((rand32() & 0x0fff) | 0x4000), // Generates a 16-bit Hex number of the form 4xxx (4 indicates the UUID version)
                     (rand32() & 0x3fff) + 0x8000,   // Generates a 16-bit Hex number in the range [0x8000, 0xbfff]
                     rand32() & 0xffff, rand32());   // Generates a 48-bit Hex number
    // return n >= 0 && n < len;             // Success only when snprintf result is a positive number and the provided buffer was large enough.
    return uuid_value;
}

void insert_into_pqsql_t1(const int &loops)
{
    try
    {
        std::uint64_t num = 0;
        std::stringstream ss;
        std::string insert_sql;
        int last_comma_idx = -1;
        std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, _end_time;
        for (int loop = 0; loop < loops; ++loop)
        {
            pqxx::connection conn("dbname=db user=fred password=Fred0001!");
            pqxx::work trans(conn);
            ss = std::stringstream();
            srand(time(NULL));
            _start_time = std::chrono::high_resolution_clock::now();
            ss << "insert into t1(author,comment,content,header,isbn,object,summary) values ";
            for (int i = 0; i < 3000000; i++)
            {
                ++num;
                ss << "('" << gen_uuid4(37) << "','" << gen_uuid4(37) << "','" << gen_uuid4(37)
                   << "','" << gen_uuid4(37) << "','" << gen_uuid4(37) << "','" << gen_uuid4(37) << "','" << gen_uuid4(37) << "'),";
            }
            last_comma_idx = ss.str().find_last_of(",");
            if (last_comma_idx > 0)
            {
                std::cout << typeid(last_comma_idx).name() << " " << last_comma_idx << std::endl;
                insert_sql = ss.str();
                ss = std::stringstream();
                insert_sql = insert_sql.substr(0, last_comma_idx);
                // std::cout << insert_sql << std::endl;
                pqxx::result res = trans.exec(insert_sql);
                trans.commit();
                _end_time = std::chrono::high_resolution_clock::now();
                std::cout << get_time_now() << ",Num:" << num << ",loops:" << loop + 1 << ",time cost:"
                          << std::chrono::duration_cast<std::chrono::seconds>(_end_time - _start_time).count() << " seconds,"
                          << std::chrono::duration_cast<std::chrono::milliseconds>(_end_time - _start_time).count() << " mills,"
                          << std::chrono::duration_cast<std::chrono::microseconds>(_end_time - _start_time).count() << " micros,"
                          << std::chrono::duration_cast<std::chrono::nanoseconds>(_end_time - _start_time).count() << " nanos!!!" << std::endl;
            }
        }
        std::cout << get_time_now() << ",finished thread id:" << std::this_thread::get_id() << " line: " << __LINE__ << " of " << __FUNCTION__ << std::endl;
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }
}

void thread_insert_into_t1(const int &loops)
{
    std::thread t1(insert_into_pqsql_t1, std::cref(loops));
    t1.join();
}

int main(int args, char **argv)
{
    thread_insert_into_t1(atoi(argv[1]));
    std::cout << get_time_now() << ",finished in " << __LINE__ << " of " << __FUNCTION__ << std::endl;
}
//compile 
g++-13 -std=c++23 -I. main.cpp -luuid -lpthread -libpqxx -o h1;
//run 
nohup ./h1 10000 >>write.log |tail -f write.log

 

 

 

select * from t1 order by id desc limit 1;