<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Hi,<br>
<br>
On 09/06/2018 9:07 CH, Trần Đức Thông wrote:<br>
<blockquote type="cite"
cite="mid:CAAriO7Ebvwt41ofQ8_aznaHTR6zfqGsDXcwuaH50yTjEg-wrKg@mail.gmail.com">
<div dir="ltr">Xin bạn,
<div><br>
</div>
<div>Mình hiện tại đang sử dụng PostgreSQL vào trong công việc.
Tuy nhiên hiện giờ mình gặp một bug mà chưa giải quyết được.
Nhờ các bạn tư vấn giúp. Chi tiết như sau</div>
<div><br>
</div>
<div>
<p style="margin:6px
0px;font-family:Helvetica,Arial,sans-serif;color:rgb(29,33,41);font-size:14px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">Phiên
bản PostgreSQL 10</p>
<p style="margin:6px
0px;display:block;font-family:Helvetica,Arial,sans-serif;color:rgb(29,33,41);font-size:14px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">Mình
có thực hiện một test như sau.<span> </span><span
class="gmail-text_exposed_show"
style="display:inline;font-family:inherit"><br>
1. Tạo một Table có 4 cột, và insert vào đó 1120 bản ghi.</span></p>
<div class="gmail-text_exposed_show"
style="display:inline;font-family:Helvetica,Arial,sans-serif;color:rgb(29,33,41);font-size:14px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">
<p style="margin:0px 0px 6px;font-family:inherit">2. Viêt
một web api sử dụng thư viện<span> </span><a
class="gmail-_58cn"
href="https://www.facebook.com/hashtag/npgsql?source=feed_text"
style="color:rgb(54,88,153);text-decoration:none;font-family:inherit"
moz-do-not-send="true"><span class="gmail-_5afx"
style="direction:ltr;unicode-bidi:isolate;font-family:inherit"><span
class="gmail-_58cl gmail-_5afz"
style="unicode-bidi:isolate;color:rgb(54,88,153);font-family:inherit">#</span><span
class="gmail-_58cm" style="font-family:inherit">Npgsql</span></span></a><span> </span>tạo
kết nối đến db vào execute một function. Function này thực
hiện một nhiệm vụ đơn giản là trả ra hết các bản ghi của
table trên.</p>
<p style="margin:6px 0px;font-family:inherit">3. Sử dụng
jemeter load test vào function trên. Với tham số Number Of
Theard = 200, Ramp-Up = 0, Loop Count = 5.</p>
<p style="margin:6px 0px;font-family:inherit">4. Kết quả xảy
ra lỗi "sorry, too many clients already" -> check trong
db thấy số connection đang mở là 105. Trong khi theo
config của PostgreSql max-connection = 100.</p>
</div>
</div>
</div>
</blockquote>
Số connections hiện tại trên PostgreSQL có thể xem từ view
pg_stat_activity của PostgreSQL.<br>
<br>
max_connections không tính số lượng worker process (như background
worker, autovacuum launcher, ...).<br>
Nên có trường hợp số lượng rows trong pg_stat_activity trả về giá
trị lớn hơn max_connections.<br>
<br>
Ở phiên bản 10, có một số connections không được tính cho
max_connections cũng hiện thị trong pg_stat_activity như:<br>
background writer, checkpointer, walwriter.<br>
Nên giá trị connection đang mở có thể có giá trị 105 như bạn xác
nhận.<br>
<br>
<blockquote type="cite"
cite="mid:CAAriO7Ebvwt41ofQ8_aznaHTR6zfqGsDXcwuaH50yTjEg-wrKg@mail.gmail.com">
<div dir="ltr">
<div>
<div class="gmail-text_exposed_show"
style="display:inline;font-family:Helvetica,Arial,sans-serif;color:rgb(29,33,41);font-size:14px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">
<p style="margin:6px 0px;font-family:inherit">Mình không có
nhiều kinh nghiệm với sql nên nhờ mọi người tư vấn giúp.
Với 2 câu hỏi như sau.</p>
<p style="margin:6px 0px;font-family:inherit">1. Tạo sao lại
xảy ra hiện tượng mở nhiều connection (sao connection pool
k được dùng đến) trong chuỗi Connection string đã có tham
số Pooling = true</p>
</div>
</div>
</div>
</blockquote>
<br>
Chức năng connection pooling bạn nói là ở phía npgsql, không phải
bên PostgreSQL.<br>
Nên mình chỉ có thể trả lời khái quát như bên dưới.<br>
Để tìm hiểu cụ thể bạn tham khảo documents (hoặc source) phía
npgsql.<br>
<br>
Thông thường vì PostgreSQL sử dụng mỗi process cho một kết nối, <br>
nên ở một số Driver có chức năng pool connection (hoặc tạo kết nối
trước (prefork)) <br>
để giảm overhead khi có kết nối mới.<br>
<br>
Bạn có thể chỉnh số lượng connections pool ở phía driver.<br>
<br>
<blockquote type="cite"
cite="mid:CAAriO7Ebvwt41ofQ8_aznaHTR6zfqGsDXcwuaH50yTjEg-wrKg@mail.gmail.com">
<div dir="ltr">
<div>
<div class="gmail-text_exposed_show"
style="display:inline;font-family:Helvetica,Arial,sans-serif;color:rgb(29,33,41);font-size:14px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">
<p style="margin:6px 0px;font-family:inherit">Câu 2. Tại sao
khi postgresql đã max connection -> các request sau
không được được cho vào queue đợi khi thực khi xong các
request hiện tại mới tiếp tự thưc thi các request mới. Mà
theo hiên tại postgresql vẫn cố mở thêm conneciton mới dẫn
đến xảy ra lỗi trên</p>
</div>
</div>
</div>
</blockquote>
<br>
Lỗi "sorry, too many clients already" là phía PostgreSQL trả về, khi
số lượng kết nối yêu cầu lớn hơn max_connections.<br>
but PostgreSQL không tự mở thêm kết nối. Mở kết nối là do phía
npgsql.<br>
PostgreSQL cũng không có cơ chế queue để chứa các kết nối. <br>
<br>
Bạn nên xem lại hệ thống bạn có mở kết nối khác ngoài npgsql không
(Ví dụ như cho mornitoring?) và <br>
điều chỉnh lại max_connections (postgresql.conf) hoặc Max pool size
(npgsql) cho đúng.<br>
<br>
<blockquote type="cite"
cite="mid:CAAriO7Ebvwt41ofQ8_aznaHTR6zfqGsDXcwuaH50yTjEg-wrKg@mail.gmail.com">
<div dir="ltr">
<div>
<div><img src="cid:part2.AEE1E14E.DE1647DD@postgresql.vn"
class="" height="267" width="412"><br>
</div>
<div><img src="cid:part3.F6E813A4.A16503BC@postgresql.vn"
class="" height="179" width="412"><br>
</div>
<div><img src="cid:part4.46C38613.5F091644@postgresql.vn"
class="" height="239" width="412"><br>
</div>
<div><img src="cid:part5.6DDCCD07.E2C46ED9@postgresql.vn"
class="" height="151" width="412"><br>
</div>
<div><img src="cid:part6.81578AF0.43196DC4@postgresql.vn"
class="" height="155" width="412"><br>
</div>
</div>
<div>
<div>Nhờ các bạn tư vấn giúp.</div>
<div><br>
</div>
<div>Mình xin cảm ơn.</div>
<div class="gmail_signature" data-smartmail="gmail_signature">
<div>-------------------------------------------------------------------------------------------------
</div>
<b><font color="#cc0000">Tran Duc Thong</font></b>
<div>Software engineering</div>
<div>Mobile: 0989 452 004</div>
<div>Email: <a href="mailto:humg.thongit@gmail.com"
target="_blank" moz-do-not-send="true">humg.thongit@gmail.com</a></div>
</div>
</div>
</div>
</blockquote>
<br>
<div>Xin cảm ơn,</div>
<div>— </div>
<div>Together we work better </div>
<div>Cộng đồng PostgreSQL Việt Nam</div>
<br>
<br>
</body>
</html>