首页 > 数据库 >C++ insert into tables of pgsql via libpq-fe.h and compile by g++-13

C++ insert into tables of pgsql via libpq-fe.h and compile by g++-13

时间:2023-11-10 23:33:51浏览次数:52  
标签:std insert 13 via varchar null 40 time include

1.Install libpq-dev

sudo apt install libpq-dev
locate libpq-fe.h
/usr/include/postgresql/libpq-fe.h

 

2.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);    

 

3.main.cpp 

// g++-13 -std=c++23 -I. main.cpp -I/usr/include/postgresql -lpq -o h1;

#include <algorithm> #include <chrono> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <memory> #include <mutex> #include <pqxx/pqxx> #include <random> #include <set> #include <thread> #include <uuid/uuid.h> #include <vector> #include "libpq-fe.h" template <typename T1, typename T2> void print_mtx_map(const std::map<T1, T2> _mp, const int &interval); 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) { std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()); std::chrono::milliseconds mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); std::chrono::microseconds micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()); std::chrono::nanoseconds 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(); } std::string get_time_span(std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, std::chrono::time_point<std::chrono::high_resolution_clock> _end_time) { std::stringstream ss; ss << 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; return ss.str(); } char uuid_value[37]; uint32_t rand32() { return ((rand() & 0x3) << 30) | ((rand() & 0x7fff) << 15) | (rand() & 0x7fff); } char *gen_uuid4() { int n = snprintf(uuid_value, sizeof(uuid_value), "%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_psql_batch(const int &loops) { // 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); std::uint64_t num = 0; srand(time(NULL)); std::string conn_str = "host=localhost port=5432 user=fred password=Fred0001! dbname=db"; const char *conn_char_ptr = conn_str.c_str(); PGconn *conn = PQconnectdb(conn_char_ptr); PGresult *res; // std::cout << PQstatus(conn) << std::endl; if (PQstatus(conn) == CONNECTION_OK) { std::cout << "ConnStatusType:CONNECTION_OK" << std::endl; } else if (PQstatus(conn) == CONNECTION_BAD) { std::cout << "ConnStatusType:CONNECTION_BAD" << std::endl; } std::stringstream ss; std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, _end_time; for (int loop = 0; loop < loops; loop++) { _start_time = std::chrono::high_resolution_clock::now(); ss = std::stringstream(); ss << "insert into t1(author,comment,content,header,isbn,object,summary) values "; for (int i = 0; i < 1000000; i++) { ++num; ss << "('" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "'),"; } std::string insert_sql = ss.str(); int last_comma_idx = insert_sql.find_last_of(","); insert_sql = insert_sql.substr(0, last_comma_idx); res = PQexec(conn, insert_sql.c_str()); _end_time = std::chrono::high_resolution_clock::now(); if (PGRES_COMMAND_OK == PQresultStatus(res)) { std::cout << "PQresultStatus(res):PGRES_COMMAND_OK" << std::endl; PQclear(res); std::cout << "Loops:" << loop + 1 << ",num:" << num << ",time cost:" << get_time_span(_start_time, _end_time) << std::endl; } } PQfinish(conn); std::cout << get_time_now() << ",finished line:" << __LINE__ << " of " << __FUNCTION__ << std::endl; } int main(int args, char **argv) { // -I/usr/include/postgresq // g++-13 -std=c++23 -I. main.cpp -I/usr/include/postgresql -lpq -o h1; insert_into_psql_batch(atoi(argv[1])); std::cout << get_time_now() << ",finished in " << __LINE__ << " of " << __FUNCTION__ << std::endl; }

 

4.compile via g++-13

g++-13 -std=c++23 -I. main.cpp -I/usr/include/postgresql -lpq -o h1;

 

5.run ./h1

nohup ./h1 10000 >>insert_pgsql.txt |tail -f insert_pgsql.txt

 

 

6.validate insert result

select * from t1 order by id desc limit 1;

id    |                author                |               comment                |               content                |                header                |                 isbn                 |                object                |               summary                
----------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
 38000000 | fa1d4171-f4b3-4224-b96a-67c998b654b4 | 8dc0dcae-66cf-4563-837a-d654c45e36a2 | 35e1f2df-0fcc-470f-92d5-88dce1de628b | f3d0408a-e733-4377-87f6-b19247412150 | 315dc0e1-66b2-4830-ac6d-391900a1004c | 12a93754-a7bc-4679-bed9-8eb69c2b5ca6 | 629a4ed0-5a0f-41b9-befd-7c7961db96b2
(1 row)

(END)

 

 

select * from t1 where id in (10000000,20000000,30000000,40000000);


    id    |                author                |               comment                |               content                |                header                |                 isbn                 |                object                |               summary                
----------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
 10000000 | 56f91915-d9f0-4ab4-8a34-9c30f5d415cb | b55f22d9-5c5e-40b6-b97d-f4d40ec7367b | 2536cd06-08bb-4b2d-bd3a-0c6f9177e10f | c8d3f5e3-37de-44ef-aa31-38f035bd31d7 | 958cf24f-d19a-4620-9bb3-232581e50acf | 2ed27ac9-2aeb-4720-9579-bb40f2e28e42 | a6dbef4d-f27d-4cb8-a19e-007b96b72fd0
 20000000 | 6909f2ef-bed7-4112-82f2-13f2f76afe5a | a4f13397-bee9-4014-afef-685b66fe44e4 | 11de440b-86c9-48cd-a6ba-982e2d486a77 | 6fba810d-6926-4e96-84b4-13cfe9159e19 | 3bc85784-c694-4468-90ac-5fcb2ff72de7 | ca0254e6-35a8-45ba-b645-2636cb89229c | 62ea1b49-a5cb-4eb9-9729-ef07e419a665
 30000000 | ee91c2c7-eceb-4d79-83e0-0b6a23a61a56 | a5101ccf-ea3b-42ef-9042-f0ae2866cddd | 94667bd1-e292-41c5-b4a2-c7e5595b2e75 | a0a1a6a9-0cf8-4a41-9021-af24240ed06b | e40d8576-e6cb-4eae-b5e1-f6c8fb4a83bb | d36d61e1-490e-4279-9135-47cbef6ef259 | 81036b8e-8bb4-4cd7-a4bc-9072c0b4dfb9
 40000000 | 359dd0c2-9ea5-4b52-84a0-cc5c26d6fc49 | d9654733-fd9a-4fec-a520-f5bd6d881b58 | 886de209-e0d6-45c6-9a8a-3779f4d56960 | 5db19b83-1753-4da9-bade-89f7c6d93050 | c7bbf309-6041-428f-81b4-bf52e4cad4de | de26c999-d32c-4996-8e33-c5afd999eb6a | a14afd75-a8ec-45b8-a641-4a9973003da0
(4 rows)

(END)

 

 

标签:std,insert,13,via,varchar,null,40,time,include
From: https://www.cnblogs.com/Fred1987/p/17825340.html

相关文章

  • CF1316D Nash Matrix(构造/dfs)
    题目第一次做构造题,做了两节晚自习qwq一开始我完全是正着想,首先\(X\)是显然的,但其他的点就不好做了,然后我就想,可行的一般结论推不出,那就想反例,然后我想啊想......倒是想到了几个,比如说环与环之间不能有相交,环内外的点不能互相到达,跟本举不完,而且也不好实现,还是要想一般结论......
  • 10/13
    今日练习乒乓球正手击打,昨天乒乓球课上教了基础框架动作,但我不太理解,感觉自己动作不到位,今天早上约了同学进行乒乓球简单击打训练。打了一阵觉着自己的反手得到了加强,已经基本可以应对球型了,可是正手依旧没入门槛。  ......
  • 20211325 2023-2024-1 《信息安全系统设计与实现(上)》第九周学习笔记
     202113252023-2024-1《信息安全系统设计与实现(上)》第九周学习笔记一、任务要求自学教材第6章,提交学习笔记(10分),评分标准如下1.知识点归纳以及自己最有收获的内容,选择至少2个知识点利用chatgpt等工具进行苏格拉底挑战,并提交过程截图,提示过程参考下面内容(4分)“我在学***X知......
  • 13,zabbix web.page.regexp方法
    zabbix-agent#登录agent端,检查页面正常访问时的状态Copy]#curl-i10.117.x.x/path/login.jspHTTP/1.1200OK...#寻找正常页面返回中具有代表性的字符串zabbix-server#通过web.page.regexp匹配字符串检查状态Copy]#zabbix_get-s10.117.x.x-p10050-kweb.page.......
  • Sql Server根据表生成insert语句
    Createproc[dbo].[P_BackupsCompanyData](@tablenamevarchar(256),@wherevarchar(256))asbeginsetnocountondeclare@sqlstrvarchar(max)declare@sqlstr1varchar(max)declare@sqlstr2varchar(max)select@sqlstr='select''insertinto�......
  • 【洛谷 P1980】[NOIP2013 普及组] 计数问题 题解(取余)
    [NOIP2013普及组]计数问题题目描述试计算在区间到的所有整数中,数字()共出现了多少次?例如,在到中,即在中,数字出现了次。输入格式个整数,之间用一个空格隔开。输出格式个整数,表示出现的次数。样例#1样例输入#1111样例输出#14提示对于的数据,,。思路求每个数字的......
  • Leetcode133.克隆图
     需要注意图中存在环路。JAVA:publicfinalNodecloneGraph(Nodenode){returndeepCopy(node,newHashMap<Integer,Node>());}privateNodedeepCopy(Nodenode,HashMap<Integer,Node>hisMap){if(null==node)return......
  • DBMS_STATS ORA-20011 Approximate NDV failed ORA-29913 error in executing ODCIEXT
    DBMS_STATSORA-20011ApproximateNDVfailedORA-29913errorinexecutingODCIEXTTABLEOPENcallout目录DBMS_STATSORA-20011ApproximateNDVfailedORA-29913errorinexecutingODCIEXTTABLEOPENcallout1、现象2、分析3、解决1.查看表信息2.确认属于DataPump的表3.删......
  • CF1383C
    solution先做easyversion(A题)只需考虑小写字母点对。每个小写字母是图里一个节点。相当于给定一些\((x_i,y_i)\)的限制。然后在图中连边,每个连边表示一次操作,把部分起点的字母变成终点的字母。要求所有\(x_i\)可达\(y_i\),求最小连边数量。由于\(x_i<y_i\)的限制,一......
  • CMU-15213 笔记
    Recitation4讲了一些GDB常用操作,虽然不少已经在CS61C里面学过了,但是保险起见还是在这里再记录一下几个不熟悉的clearmain//removethebreakpointatfunctionmain(gdb)print(char*)[0x...]//printsastring(gdb)printargv[1](gdb)disassemblemain//show......