<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yaya Jhang on Medium]]></title>
        <description><![CDATA[Stories by Yaya Jhang on Medium]]></description>
        <link>https://medium.com/@hiein2012?source=rss-6756fd44998f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*S5iIs2ptiW32zux-.</url>
            <title>Stories by Yaya Jhang on Medium</title>
            <link>https://medium.com/@hiein2012?source=rss-6756fd44998f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 10:54:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hiein2012/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[PostgreSQL Parallel Query]]></title>
            <link>https://medium.com/@hiein2012/postgresql-parallel-query-34adf4769dcb?source=rss-6756fd44998f------2</link>
            <guid isPermaLink="false">https://medium.com/p/34adf4769dcb</guid>
            <category><![CDATA[postgresql]]></category>
            <dc:creator><![CDATA[Yaya Jhang]]></dc:creator>
            <pubDate>Wed, 27 Dec 2017 06:02:29 GMT</pubDate>
            <atom:updated>2017-12-27T06:02:29.150Z</atom:updated>
            <content:encoded><![CDATA[<p>做個小筆記/翻譯部分文件(以下是以安裝版本postgreSQL 10為出發點)</p><p>Postgresql 可以利用多個CPU來設計query plans，加快運行速度。這樣的功能稱作parallel query(平行查詢)。</p><p>許多Query 無法從平行查詢中受益，是因為目前能實行的技術的限制，或者是目前沒有想出比serial query plan還要更快的query plan(查詢規劃)。</p><p>但是對於受用平行查詢的Query來說，加快的速度通常非常地顯著。使用平行查詢時，許多query的查詢速度會快兩倍，甚至是快四倍或比這更快的都有。<strong>觸及大量資訊但只回傳少量資料(a few rows)的query通常會受益的更多。</strong></p><blockquote>How Parallel Query Works? 如何運作</blockquote><p>當優化器(optimizer)覺得某個特定query的最佳執行策略是用parallel query的時候，它會創造一個包含<em>Gather</em>或<em>Gather merge </em>node的query plan。</p><p>在任何情況下，Gather和Gather Merge node都肯定會有一個子規劃(child plan)，而這子規劃是能夠做平行運算的規劃(plan that will be executed in parallel)的一部分。如果Gather和Gather Merge node在計算樹(plan tree)非常上方的地方，那麼整個query都會進行平行運算。</p><blockquote>When can parallel query be used? 啟用時機?</blockquote><p>有一些設定會導致query planner在任何情況下都無法產生parallel query plan。為了要讓parallel query plan在任何情況下能夠生成，需要做以下的設定。</p><ul><li>max_parallel_workers_per_gather 必須要設一個大於零的數字。使用者數量不能大於這個數字。</li><li>dynamic_shared_memory_type 必須要設一個數字，而不能什麼都不設。平行查詢需要動態分享記憶體來在過程(cooperating process)中傳送資料。</li></ul><p>另外系統必須是非單一使用者模式(single-user mode)，不然背景工作者(background workers)將無法使用資料庫。</p><p>就算平行查詢規劃經常能夠被生成。但在以下的情況不會生成平行規劃:</p><ul><li>這個query會寫入任何資料或者是lock住任何資料庫的資料。如果一個query包含資料修正的程序，不管是在最高等級還是在一個CTE，這個query將不會被生成任何平行規劃。這是目前實現的限制，未來有可能會解除這樣的限制。</li><li>這個query可能會在執行期間暫停，在系統認為部分或增量執行(incremental execution)的任何情況下，不會生成平行規劃。例如:使用DECLARE CURSOR所創造的資料指標(cursor)不會使用平行規劃。同樣地，一個FOR x IN query LOOP … END形式的PL/pgSQL loop也不會使用平行規劃，因為平行查詢系統無法驗證循環中的程式碼是否能安全地執行著平行查詢。</li><li>這個query使用任何的function是被標示PARALLEL UNSAFE。大部分系統定義的function是PARALLEL SAFE，但是使用者定義的function都是被預設標註PARALLEL UNSAFE。</li><li>這個query在另一個已經平行運算的query中執行。例如:如果有個由parallel query所呼叫的function發出一個SQL query，這個query將不會使用平行規劃。這是一個當前實現的限制，但是可能不希望這限制給移除掉，因為它可能會導致單個查詢會使用到非常大數量的processes。</li><li>交易隔離等級(Transaction isolation levels)是可序列化的，這是目前實現的限制。</li></ul><p>即使在特定query下生成的平行查詢規劃，在執行的時候也會有幾種情況之下無法平行執行。如果發生這情況，leader會將Gather node 下面的規畫部分完全執行，就好像Gather node不存在一樣，如果滿足以下任何一個條件，將會發生這種情況:</p><ul><li>因為後台工作人員總人數不能超過max_worker_processess數量的限制，所以無法獲得後台工作人員。</li><li>因為執行平行查詢的後台工作人員總人數不能超過max_parallel_workers，所以無法獲得後台工作人員。</li><li>客戶端發送一個非零的fetch count執行訊息。因為libpq目前無法發送這樣的訊息，所以只有在使用<em>不依賴libpq的客戶端</em>時才會發生這種情況，如果這是經常發生的事情，那麼將max_parallel_workers_per_gather 設成0會是比較好的主意，以避免生成的查詢規劃在連續運行(run serially)時，可能是不理想的狀況。</li><li>使用CREATE TABLE .. AS EXECUTE的語句來執行。該構造將read-only operation轉換成 read-write operation ，使其不適合平行查詢。</li><li>交易隔離等級(Transaction isolation levels)是可序列化的。這種情況通常不會出現，因為當交易隔離等級是可序列化的時候，並不會生成平行查詢規劃。但是如果在規劃生成之後或者是執行之前，交易隔離等級更改成可序列的話，仍有可能會發生這種情況。</li></ul><blockquote>Parallel plans 平行規劃</blockquote><p>因為每個worker執行計畫的平行部分來完成，因此不可能簡單地使用一個普通的query plan 並使用多個worker來跑。每個worker會產出output result set的完整副本，因此query的查詢速度不會比一般狀況下還要快，反而會產生出不正確的result。相反地，規劃的平行部分必須是query optimizer內部已知的部分規畫，也就是說，它必須被建構，來讓每一個執行規劃的process可以生成只有一種output rows的子集，使得每個output rows被保證是由恰好一個cooperating process來產生。通常這意味著在查詢的driving table上的掃瞄必須是平行感知掃描。</p><blockquote>實作</blockquote><p>將 max_parallel_workers_per_gather 的值修改成大於零的數字。</p><pre>sudo vim /mnt/vdb1/pgsql/10/data/postgresql.conf</pre><p>可以直接去 postgresql.conf 的檔案做修改(如上)，做完之後記得要重啟postgresql的服務。</p><pre>systemctl restart postgresql-10 </pre><p>也可以直接用 psql 模式直接做修改(如下)</p><pre>postgres=# SET max_parallel_workers_per_gather=2;</pre><pre>SET</pre><p>然後再以這樣的設定去下sql式跑就大功告成囉。</p><ul><li>以上資料參考<a href="https://www.postgresql.org/docs/10/static/parallel-query.html">PostgreSQL官方資料</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34adf4769dcb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PostgreSQL 10 installation on CentOS 7]]></title>
            <link>https://medium.com/@hiein2012/postgresql-10-installation-on-centos-7-ab8814454271?source=rss-6756fd44998f------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab8814454271</guid>
            <category><![CDATA[centos7]]></category>
            <category><![CDATA[postgresql]]></category>
            <dc:creator><![CDATA[Yaya Jhang]]></dc:creator>
            <pubDate>Wed, 20 Dec 2017 03:32:25 GMT</pubDate>
            <atom:updated>2017-12-20T03:32:25.404Z</atom:updated>
            <content:encoded><![CDATA[<p>PostgreSQL 10 installation on CentOS 7</p><p>唉呀實在是需要紀錄一下，讓未來的我如果忘記的話可以有東西參考參考。</p><p>內容打得非常非常零碎~~~</p><blockquote>Step 1. Add Postgres Yum Repository</blockquote><p>先安裝PostgreSQL repository到系統裡面，可以根據自己使用的作業系統來選擇相對應的語法來進行安裝。</p><pre>## CentOS/RHEL — 7</pre><pre># rpm -Uvh <a href="https://yum.postgresql.org/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm">https://yum.postgresql.org/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm</a></pre><pre>## CentOS/RHEL — 6</pre><pre># rpm -Uvh <a href="https://yum.postgresql.org/10/redhat/rhel-6-x86_64/pgdg-redhat10-10-2.noarch.rpm">https://yum.postgresql.org/10/redhat/rhel-6-x86_64/pgdg-redhat10-10-2.noarch.rpm</a></pre><pre>## Fedora — 26</pre><pre># rpm -Uvh <a href="https://yum.postgresql.org/10/fedora/fedora-26-x86_64/pgdg-fedora10-10-3.noarch.rpm">https://yum.postgresql.org/10/fedora/fedora-26-x86_64/pgdg-fedora10-10-3.noarch.rpm</a></pre><pre>## Fedora — 25</pre><pre># rpm -Uvh <a href="https://yum.postgresql.org/10/fedora/fedora-25-x86_64/pgdg-fedora10-10-3.noarch.rpm">https://yum.postgresql.org/10/fedora/fedora-25-x86_64/pgdg-fedora10-10-3.noarch.rpm</a></pre><blockquote>Step 2. 安裝 PostgreSQL 10</blockquote><p>安裝完第一步驟的repositoy之後，再用 yum將PostgreSQL10 安裝進來。</p><pre># yum install postgresql10-server postgresql10</pre><blockquote>Step 2.5 改變 PGDATA path</blockquote><p>若是需要改變PGDATA的路徑的話，要在Step3的初始化環境之前做唷!</p><pre># vi /usr/lib/systemd/system/postgresql-10.service</pre><p>以上的指令會讓你進入有一長串文字的文本，</p><p>其中找到像是下面內容的這段，將它最後面的等號內容做一個適當的修改 。</p><pre>Environment=PGDATA=/mnt/vdb1/pqsql/10/data</pre><p>然後再修改data_directory的位置。</p><pre># vi /mnt/vdb1/wiprognosis/pqsql/10/data/postgresql.conf</pre><p>其中找到像是下面內容的這段，將它最後面的等號內容做一個適當的修改 。</p><pre>data_directory = ‘/mnt/vdb1/pqsql/10/data ’</pre><p>以上的步驟要注意一下是否有註解符號在前面搗亂，有的話要記得刪除。</p><blockquote>Step 3. 初始化環境</blockquote><pre># /usr/pgsql-10/bin/postgresql10-setup initdb </pre><pre>Initializing database … OK</pre><blockquote>Step 4. 啟動PostgreSQL服務，並設定為開機啟動。</blockquote><p>啟動postgreSQL：</p><pre># systemctl start postgresql-10.service</pre><p>讓postgreSQL可以在開機時自動啟動：</p><pre># systemctl enable postgresql-10.service</pre><blockquote>Step 5. 設定postgreSQL</blockquote><ul><li>設定密碼</li></ul><p>首先我們要來修改postgreSQL預設的最高權限者：postgres 的密碼。</p><p>首先先轉換到 postgres ，然後進入 psql (互動式終端)</p><pre>[root@localhost helen ~]# sudo -i -u postgres<br>-bash-4.2 psql<br>psql (10.1)<br>Type &quot;help&quot; for help.</pre><p>成功進去之後， 用以下的指令來修改密碼</p><pre>postgres=# \password postgres</pre><pre>Enter new password:</pre><pre>Enter it agin:</pre><p>打完之後再退出設定介面。</p><pre>postgres=# \q</pre><pre>-bash-4.2$ exit</pre><pre>logout</pre><ul><li>設定postgresSQL登入權限</li></ul><p>使用 vim 指令，來編輯pg_hba.conf 這個文件修改登入權限設定。</p><p>碎碎念一下:vim 是 vi的進階版，也有程式編輯的能力，但特別的是 vim可將字體標上顏色，用來辨別語法，方便我們編輯文件和作程式設計。</p><pre># vim /mnt/vdb1/pgsql/10/data/pg_hba.conf</pre><p>將method都改成md5 ，然後最後增加一行 host /all/ all /all /md5</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*ggJbRPZV1gMEelsNFcf_RA.png" /></figure><ul><li>開啟外部ID存取資料庫</li></ul><p>PostgreSQL 連線原本只預設本機 IP 才能存取，如需要讓外部 IP 存取，須打開預設的連線設定檔案 postgresql.conf，並修改 listen_addresses = ‘*’ (記得要把註解去掉)</p><pre># vim /mnt/vdb1/pgsql/10/data/postgresql.conf</pre><ul><li>加入自行指定的連接埠</li></ul><p>以下是開啟port 5432和port 80，出現success的字樣代表有開成功~</p><pre># firewall-cmd — permanent — add-port=5432/tcp<br>success</pre><pre># firewall-cmd --permanent --add-port=80/tcp<br>success</pre><pre># firewall-cmd — reload</pre><pre># firewall-cmd — list-all</pre><p>最後記得要讓postgreSQL服務重啟</p><pre># systemctl restart postgresql-10</pre><p>檢查 PostgreSQL 服務是否已被開啟 (port:5432)</p><pre># netstat –an | grep 5432</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab8814454271" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>