PostgreSQL
 sql >> डेटाबेस >  >> RDS >> PostgreSQL

PostgreSQL के लिए जस्ट-इन-टाइम कंपाइलेशन (JIT) का अवलोकन

ऐतिहासिक रूप से PostgreSQL ने PL/pgSQL फ़ंक्शंस के लिए समय-समय पर संकलन के रूप में संकलन सुविधाएँ प्रदान की हैं और संस्करण 10 ने अभिव्यक्ति संकलन पेश किया है। हालांकि उनमें से कोई भी मशीन कोड उत्पन्न नहीं करता है।

SQL के लिए JIT पर कई साल पहले चर्चा की गई थी, और PostgreSQL के लिए यह सुविधा एक महत्वपूर्ण कोड परिवर्तन का परिणाम है।

यह जांचने के लिए कि क्या PostgreSQL बाइनरी को LLVM समर्थन के साथ बनाया गया था, pg_configure कमांड का उपयोग करके कंपाइल फ़्लैग प्रदर्शित करें और आउटपुट में -with-llvm देखें। PGDG RPM वितरण के लिए उदाहरण:

omiday ~ $ /usr/pgsql-11/bin/pg_config --configure
'--enable-rpath' '--prefix=/usr/pgsql-11' '--includedir=/usr/pgsql-11/include' '--mandir=/usr/pgsql-11/share/man' '--datadir=/usr/pgsql-11/share' '--enable-tap-tests' '--with-icu' '--with-llvm' '--with-perl' '--with-python' '--with-tcl' '--with-tclconfig=/usr/lib64' '--with-openssl' '--with-pam' '--with-gssapi' '--with-includes=/usr/include' '--with-libraries=/usr/lib64' '--enable-nls' '--enable-dtrace' '--with-uuid=e2fs' '--with-libxml' '--with-libxslt' '--with-ldap' '--with-selinux' '--with-systemd' '--with-system-tzdata=/usr/share/zoneinfo' '--sysconfdir=/etc/sysconfig/pgsql' '--docdir=/usr/pgsql-11/doc' '--htmldir=/usr/pgsql-11/doc/html' 'CFLAGS=-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' 'PKG_CONFIG_PATH=:/usr/lib64/pkgconfig:/usr/share/pkgconfig'

LLVM JIT क्यों?

यह सब लगभग दो साल पहले शुरू हुआ था, जैसा कि एड्रेस फ्रायंड की पोस्ट में बताया गया है, जब अभिव्यक्ति मूल्यांकन और टपल डिफॉर्मिंग बड़े प्रश्नों को गति देने में बाधा साबित हुए। जेआईटी कार्यान्वयन को जोड़ने के बाद एंड्रेस के शब्दों में "अभिव्यक्ति मूल्यांकन स्वयं पहले की तुलना में दस गुना तेज है"। इसके अलावा, उनके पद को समाप्त करने वाले प्रश्नोत्तर खंड में अन्य कार्यान्वयनों पर एलएलवीएम की पसंद की व्याख्या की गई है।

जबकि LLVM चुना गया प्रदाता था, GUC पैरामीटर jit_provider का उपयोग किसी अन्य JIT प्रदाता को इंगित करने के लिए किया जा सकता है। ध्यान दें कि इनलाइनिंग समर्थन केवल LLVM प्रदाता का उपयोग करते समय ही उपलब्ध होता है, क्योंकि निर्माण प्रक्रिया कैसे काम करती है।

JIT कब करें?

दस्तावेज़ीकरण स्पष्ट है:लंबे समय तक चलने वाले प्रश्न जो सीपीयू बाध्य हैं, जेआईटी संकलन से लाभान्वित होंगे। इसके अलावा इस पूरे ब्लॉग में संदर्भित मेलिंग सूची चर्चाएं इंगित करती हैं कि जेआईटी उन प्रश्नों के लिए बहुत महंगा है जो केवल एक बार निष्पादित होते हैं।

प्रोग्रामिंग भाषाओं की तुलना में, PostgreSQL को क्वेरी प्लानर पर भरोसा करके, JIT को कब "जानना" का फायदा है। उस प्रभाव के लिए कई जीयूसी पैरामीटर पेश किए गए थे। जेआईटी को सक्षम करते समय उपयोगकर्ताओं को नकारात्मक आश्चर्य से बचाने के लिए लागत संबंधी पैरामीटर जानबूझकर उचित रूप से उच्च मूल्यों पर सेट किए जाते हैं। ध्यान दें कि जेआईटी लागत पैरामीटर को '0' पर सेट करने से सभी प्रश्नों को जेआईटी-संकलित करने के लिए बाध्य किया जाएगा और परिणामस्वरूप आपके सभी प्रश्नों को धीमा कर दिया जाएगा।

जबकि JIT आम तौर पर फायदेमंद हो सकता है, ऐसे मामले हैं जब इसे सक्षम करना हानिकारक हो सकता है जैसा कि प्रतिबद्ध b9f2d4d3 में चर्चा की गई है।

JIT कैसे करें?

जैसा कि ऊपर बताया गया है कि आरपीएम बाइनरी पैकेज एलएलवीएम-सक्षम हैं। हालांकि, जेआईटी संकलन को काम करने के लिए कुछ अतिरिक्त चरणों की आवश्यकता है:

बुद्धि के लिए:

[email protected][local]:54311 test# show server_version;
server_version
----------------
11.1
(1 row)
[email protected][local]:54311 test# show port;
port
-------
54311
(1 row)
[email protected][local]:54311 test# create table t1 (id serial);
CREATE TABLE
[email protected][local]:54311 test# insert INTO t1 (id) select * from generate_series(1, 10000000);
INSERT 0 10000000
[email protected][local]:54311 test# set jit = 'on';
SET
[email protected][local]:54311 test# set jit_above_cost = 10; set jit_inline_above_cost = 10; set jit_optimize_above_cost = 10;
SET
SET
SET
[email protected][local]:54311 test# explain analyze select count(*) from t1;
                                                               QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate  (cost=97331.43..97331.44 rows=1 width=8) (actual time=647.585..647.585 rows=1 loops=1)
   ->  Gather  (cost=97331.21..97331.42 rows=2 width=8) (actual time=647.484..649.059 rows=3 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Partial Aggregate  (cost=96331.21..96331.22 rows=1 width=8) (actual time=640.995..640.995 rows=1 loops=3)
               ->  Parallel Seq Scan on t1  (cost=0.00..85914.57 rows=4166657 width=0) (actual time=0.060..397.121 rows=3333333 loops=3)
Planning Time: 0.182 ms
Execution Time: 649.170 ms
(8 rows)

ध्यान दें कि मैंने जेआईटी को सक्षम किया था (जो कि डिफ़ॉल्ट रूप से अक्षम है pgsql-hackers चर्चा के बाद प्रतिबद्ध b9f2d4d3 में संदर्भित)। मैंने दस्तावेज़ में सुझाए गए अनुसार JIT मापदंडों की लागत को भी समायोजित किया है।

पहला संकेत JIT दस्तावेज़ में संदर्भित src/backend/jit/README फ़ाइल में मिलता है:

Which shared library is loaded is determined by the jit_provider GUC, defaulting to "llvmjit".

चूंकि RPM पैकेज स्वचालित रूप से JIT निर्भरता में नहीं खींच रहा है - जैसा कि गहन विचार-विमर्श के बाद तय किया गया था (पूरा धागा देखें) - हमें इसे मैन्युअल रूप से स्थापित करने की आवश्यकता है:

[[email protected] ~]# dnf install postgresql11-llvmjit

एक बार स्थापना पूर्ण हो जाने पर हम तुरंत परीक्षण कर सकते हैं:

[email protected][local]:54311 test# explain analyze select count(*) from t1;
                                                               QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate  (cost=97331.43..97331.44 rows=1 width=8) (actual time=794.998..794.998 rows=1 loops=1)
   ->  Gather  (cost=97331.21..97331.42 rows=2 width=8) (actual time=794.870..803.680 rows=3 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Partial Aggregate  (cost=96331.21..96331.22 rows=1 width=8) (actual time=689.124..689.125 rows=1 loops=3)
               ->  Parallel Seq Scan on t1  (cost=0.00..85914.57 rows=4166657 width=0) (actual time=0.062..385.278 rows=3333333 loops=3)
Planning Time: 0.150 ms
JIT:
   Functions: 4
   Options: Inlining true, Optimization true, Expressions true, Deforming true
   Timing: Generation 2.146 ms, Inlining 117.725 ms, Optimization 47.928 ms, Emission 69.454 ms, Total 237.252 ms
Execution Time: 803.789 ms
(12 rows)

हम प्रति कार्यकर्ता JIT विवरण भी प्रदर्शित कर सकते हैं:

[email protected][local]:54311 test# explain (analyze, verbose, buffers) select count(*) from t1;
                                                                  QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate  (cost=97331.43..97331.44 rows=1 width=8) (actual time=974.352..974.352 rows=1 loops=1)
   Output: count(*)
   Buffers: shared hit=2592 read=41656
   ->  Gather  (cost=97331.21..97331.42 rows=2 width=8) (actual time=974.166..980.942 rows=3 loops=1)
         Output: (PARTIAL count(*))
         Workers Planned: 2
         Workers Launched: 2
         JIT for worker 0:
         Functions: 2
         Options: Inlining true, Optimization true, Expressions true, Deforming true
         Timing: Generation 0.378 ms, Inlining 74.033 ms, Optimization 11.979 ms, Emission 9.470 ms, Total 95.861 ms
         JIT for worker 1:
         Functions: 2
         Options: Inlining true, Optimization true, Expressions true, Deforming true
         Timing: Generation 0.319 ms, Inlining 68.198 ms, Optimization 8.827 ms, Emission 9.580 ms, Total 86.924 ms
         Buffers: shared hit=2592 read=41656
         ->  Partial Aggregate  (cost=96331.21..96331.22 rows=1 width=8) (actual time=924.936..924.936 rows=1 loops=3)
               Output: PARTIAL count(*)
               Buffers: shared hit=2592 read=41656
               Worker 0: actual time=900.612..900.613 rows=1 loops=1
               Buffers: shared hit=668 read=11419
               Worker 1: actual time=900.763..900.763 rows=1 loops=1
               Buffers: shared hit=679 read=11608
               ->  Parallel Seq Scan on public.t1  (cost=0.00..85914.57 rows=4166657 width=0) (actual time=0.311..558.192 rows=3333333 loops=3)
                     Output: id
                     Buffers: shared hit=2592 read=41656
                     Worker 0: actual time=0.389..539.796 rows=2731662 loops=1
                     Buffers: shared hit=668 read=11419
                     Worker 1: actual time=0.082..548.518 rows=2776862 loops=1
                     Buffers: shared hit=679 read=11608
Planning Time: 0.207 ms
JIT:
   Functions: 9
   Options: Inlining true, Optimization true, Expressions true, Deforming true
   Timing: Generation 8.818 ms, Inlining 153.087 ms, Optimization 77.999 ms, Emission 64.884 ms, Total 304.787 ms
Execution Time: 989.360 ms
(36 rows)

JIT कार्यान्वयन समानांतर क्वेरी निष्पादन सुविधा का भी लाभ उठा सकता है। उदाहरण के लिए, पहले समानांतरीकरण को अक्षम करें:

[email protected][local]:54311 test# set max_parallel_workers_per_gather = 0;
SET
[email protected][local]:54311 test# explain analyze select count(*) from t1;
                                                      QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
Aggregate  (cost=169247.71..169247.72 rows=1 width=8) (actual time=1447.315..1447.315 rows=1 loops=1)
   ->  Seq Scan on t1  (cost=0.00..144247.77 rows=9999977 width=0) (actual time=0.064..957.563 rows=10000000 loops=1)
Planning Time: 0.053 ms
JIT:
   Functions: 2
   Options: Inlining true, Optimization true, Expressions true, Deforming true
   Timing: Generation 0.388 ms, Inlining 1.359 ms, Optimization 7.626 ms, Emission 7.963 ms, Total 17.335 ms
Execution Time: 1447.783 ms
(8 rows)

समानांतर क्वेरी के साथ एक ही कमांड आधे समय में पूरी होती है:

[email protected][local]:54311 test# reset max_parallel_workers_per_gather ;
RESET
[email protected][local]:54311 test# explain analyze select count(*) from t1;
                                                               QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate  (cost=97331.43..97331.44 rows=1 width=8) (actual time=707.126..707.126 rows=1 loops=1)
   ->  Gather  (cost=97331.21..97331.42 rows=2 width=8) (actual time=706.971..712.199 rows=3 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Partial Aggregate  (cost=96331.21..96331.22 rows=1 width=8) (actual time=656.102..656.103 rows=1 loops=3)
               ->  Parallel Seq Scan on t1  (cost=0.00..85914.57 rows=4166657 width=0) (actual time=0.067..384.207 rows=3333333 loops=3)
Planning Time: 0.158 ms
JIT:
   Functions: 9
   Options: Inlining true, Optimization true, Expressions true, Deforming true
   Timing: Generation 3.709 ms, Inlining 142.150 ms, Optimization 50.983 ms, Emission 33.792 ms, Total 230.634 ms
Execution Time: 715.226 ms
(12 rows)

जेआईटी कार्यान्वयन बनाम अंतिम संस्करण के प्रारंभिक चरणों के दौरान, इस पोस्ट में चर्चा किए गए परीक्षणों के परिणामों की तुलना करना मुझे दिलचस्प लगा। पहले सुनिश्चित करें कि मूल परीक्षण में शर्तें पूरी होती हैं यानी डेटाबेस को मेमोरी में फिट होना चाहिए:

[email protected][local]:54311 test# \l+
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 8027 kB | pg_default | default administrative connection database
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7889 kB | pg_default | unmodifiable empty database
          |          |          |             |             | postgres=CTc/postgres |         |            |
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +| 7889 kB | pg_default | default template for new databases
          |          |          |             |             | postgres=CTc/postgres |         |            |
test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |                       | 2763 MB | pg_default |


[email protected][local]:54311 test# show shared_buffers ;
3GB

Time: 0.485 ms
आज श्वेतपत्र डाउनलोड करें क्लस्टरकंट्रोल के साथ पोस्टग्रेएसक्यूएल प्रबंधन और स्वचालन इस बारे में जानें कि पोस्टग्रेएसक्यूएल को तैनात करने, मॉनिटर करने, प्रबंधित करने और स्केल करने के लिए आपको क्या जानना चाहिए। श्वेतपत्र डाउनलोड करें

JIT अक्षम के साथ परीक्षण चलाएँ:

[email protected][local]:54311 test# set jit = off;
SET
Time: 0.483 ms

[email protected][local]:54311 test# select sum(c8) from t1;
   0

Time: 1036.231 ms (00:01.036)

[email protected][local]:54311 test# select sum(c2), sum(c3), sum(c4), sum(c5),
   sum(c6), sum(c7), sum(c8) from t1;
   0 |   0 |   0 |   0 |   0 |   0 |   0

Time: 1793.502 ms (00:01.794)

अगला परीक्षण JIT सक्षम के साथ चलाएँ:

[email protected][local]:54311 test# set jit = on; set jit_above_cost = 10; set
jit_inline_above_cost = 10; set jit_optimize_above_cost = 10;
SET
Time: 0.473 ms
SET
Time: 0.267 ms
SET
Time: 0.204 ms
SET
Time: 0.162 ms
[email protected][local]:54311 test# select sum(c8) from t1;
   0

Time: 795.746 ms

[email protected][local]:54311 test# select sum(c2), sum(c3), sum(c4), sum(c5),
   sum(c6), sum(c7), sum(c8) from t1;
   0 |   0 |   0 |   0 |   0 |   0 |   0

Time: 1080.446 ms (00:01.080)

यह पहले परीक्षण मामले के लिए लगभग 25% और दूसरे के लिए 40% की गति है!

अंत में, यह याद रखना महत्वपूर्ण है कि तैयार किए गए बयानों के लिए, जेआईटी संकलन तब किया जाता है जब फ़ंक्शन पहली बार निष्पादित होता है।

निष्कर्ष

डिफ़ॉल्ट रूप से, जेआईटी संकलन अक्षम है, और आरपीएम आधारित सिस्टम के लिए इंस्टॉलर डिफ़ॉल्ट प्रदाता एलएलवीएम के लिए बिटकोड प्रदान करने वाले जेआईटी पैकेज को स्थापित करने की आवश्यकता के बारे में संकेत नहीं देगा।

स्रोतों से निर्माण करते समय, प्रदर्शन समस्याओं से बचने के लिए संकलन फ़्लैग पर ध्यान दें, उदाहरण के लिए यदि LLVM अभिकथन सक्षम हैं।

जैसा कि pgsql-hackers सूची पर चर्चा की गई है, लागत पर JIT के प्रभाव को अभी तक पूरी तरह से समझा नहीं गया है, इसलिए फीचर क्लस्टर को व्यापक रूप से सक्षम करने से पहले सावधानीपूर्वक योजना की आवश्यकता है, क्योंकि ऐसे प्रश्न जो संकलन से अन्यथा लाभान्वित हो सकते हैं, वास्तव में धीमी गति से चल सकते हैं। हालाँकि, JIT को प्रति प्रश्न के आधार पर सक्षम किया जा सकता है।

जेआईटी संकलन के कार्यान्वयन के बारे में गहन जानकारी के लिए प्रोजेक्ट गिट लॉग्स, कमिटफेस्ट्स और पीजीएसक्यूएल-हैकर्स मेल थ्रेड की समीक्षा करें।

हैप्पी जिटिंग!


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. कैसे Atan2d () PostgreSQL में काम करता है

  2. docker pgadmin स्थानीय कनेक्शन पोस्टग्रेज करता है

  3. Postgresql में कैरिज रिटर्न और नई लाइनें कैसे निकालें?

  4. पोस्टग्रेस्क्ल में काम नहीं कर रहा है

  5. डॉकटर-कंपोज़ अप के बाद पोस्टग्रेज डंप लोड करें