<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>MEPO Forum - PostgreSQL</title>
        <description></description>
        <link>http://mepopedia.com/forum/list.php?297</link>
        <lastBuildDate>Wed, 29 Apr 2026 16:01:54 +0800</lastBuildDate>
        <generator>Phorum 5.2.7</generator>
        <item>
            <guid>http://mepopedia.com/forum/read.php?297,48196,48196#msg-48196</guid>
            <title>Create a Table with Incremental Primary Key (no replies)</title>
            <link>http://mepopedia.com/forum/read.php?297,48196,48196#msg-48196</link>
            <description><![CDATA[1. Add primary key when creating a table<br />
<br />
<div style="background:#ddd;padding-left:1em">CREATE TABLE example<br />
(<br />
    id          bigserial primary key,<br />
    name        VARCHAR(255) not null,<br />
    value       bigint<br />
);</div>
<br />
2. Add primary key by updating a table<br />
<br />
<div style="background:#ddd;padding-left:1em">ALTER TABLE example add primary key (id);</div>]]></description>
            <dc:creator>mepoadm</dc:creator>
            <category>PostgreSQL</category>
            <pubDate>Wed, 25 Jun 2014 00:02:11 +0800</pubDate>
        </item>
        <item>
            <guid>http://mepopedia.com/forum/read.php?297,24699,24699#msg-24699</guid>
            <title>參數設定與效能校調(Configuration)：VACUUM / Automatic VACUUM (no replies)</title>
            <link>http://mepopedia.com/forum/read.php?297,24699,24699#msg-24699</link>
            <description><![CDATA[<div class='message-body' style='float:right; margin: 0; border: none; padding: 0 0 1em 1em; max-width: 216px'><div class='notice' style='padding: 10px 14px'><ol style='font-size: 0.8em; margin:0; padding: 0; list-style-type: none;'><li><a style='text-decoration:none' href=#.E4.BB.80.E9.BA.BC.E6.98.AFVACUUM>1. 什麼是VACUUM</a><li><a style='text-decoration:none' href=#Vacuum+.E8.A8.AD.E5.AE.9A>2. Vacuum 設定</a><li><a style='text-decoration:none' href=#Vacuum+.E8.87.AA.E5.8B.95.E5.9F.B7.E8.A1.8C.E8.A8.AD.E5.AE.9A>3. Vacuum 自動執行設定</a></ol></div></div><h2 id='.E4.BB.80.E9.BA.BC.E6.98.AFVACUUM'>1. 什麼是VACUUM</h2><br />
VACUUM 基本的功能就是 Garbage collection。主要原因是 PostgreSQL 資料庫在資料刪除的時候不會直接將資料刪除。要等執行 Vacuum 時才會將資料從硬碟等其他儲存設備上實體移除。如果要讓系統自動執行 Vacuum，有 autovacuum 等參數可以設定。系統預設 autovacuum 是啟用的。<br />
<br />
詳細說明可見 <a href=http://www.postgresql.org/docs/9.1/static/routine-vacuuming.html#VACUUM-BASICS>Routine Vacuuming</a>。<br />
<br />
<h2 id='Vacuum+.E8.A8.AD.E5.AE.9A'>2. Vacuum 設定</h2><br />
<br />
This feature is disabled by default for manually issued VACUUM commands. To enable it, set the vacuum_cost_delay variable to a nonzero value.<br />
<br />
vacuum_cost_delay (integer)<br />
The length of time, in milliseconds, that the process will sleep when the cost limit has been exceeded. The default value is zero, which disables the cost-based vacuum delay feature. Positive values enable cost-based vacuuming. Note that on many systems, the effective resolution of sleep delays is 10 milliseconds; setting vacuum_cost_delay to a value that is not a multiple of 10 might have the same results as setting it to the next higher multiple of 10.<br />
<br />
When using cost-based vacuuming, appropriate values for vacuum_cost_delay are usually quite small, perhaps 10 or 20 milliseconds. Adjusting vacuum's resource consumption is best done by changing the other vacuum cost parameters.<br />
<b>vacuum_cost_page_hit (integer)</b><br />
The estimated cost for vacuuming a buffer found in the shared buffer cache. It represents the cost to lock the buffer pool, lookup the shared hash table and scan the content of the page. The default value is one.<br />
<b>vacuum_cost_page_miss (integer)</b><br />
The estimated cost for vacuuming a buffer that has to be read from disk. This represents the effort to lock the buffer pool, lookup the shared hash table, read the desired block in from the disk and scan its content. The default value is 10.<br />
<b>vacuum_cost_page_dirty (integer)</b><br />
The estimated cost charged when vacuum modifies a block that was previously clean. It represents the extra I/O required to flush the dirty block out to disk again. The default value is 20.<br />
<br />
<b>vacuum_cost_limit (integer)</b><br />
The accumulated cost that will cause the vacuuming process to sleep. The default value is 200.<br />
<br />
<br />
<h2 id='Vacuum+.E8.87.AA.E5.8B.95.E5.9F.B7.E8.A1.8C.E8.A8.AD.E5.AE.9A'>3. Vacuum 自動執行設定</h2>]]></description>
            <dc:creator>HP</dc:creator>
            <category>PostgreSQL</category>
            <pubDate>Tue, 04 Sep 2012 22:32:27 +0800</pubDate>
        </item>
        <item>
            <guid>http://mepopedia.com/forum/read.php?297,15497,15497#msg-15497</guid>
            <title>Postgresql 和 MySQL 語法相異處整理：Create Table, KEY / INDEX, BIGINT / INT UNSIGNED (no replies)</title>
            <link>http://mepopedia.com/forum/read.php?297,15497,15497#msg-15497</link>
            <description><![CDATA[<b>1. PostgreSQL 沒有 CREATE TABLE IF NOT EXISTS 的語法</b><br />
<br />
乍看之下好像 PostgreSQL 的語法比較弱，但事實上是 PostgreSQL 會自動判斷 table 存在與否，所以直接寫 CREATE TABLE 就可以了。<br />
<br />
<b>2. PostgreSQL 在 CREATE TABLE 時不能建 non-unique key</b><br />
<br />
在 MySQL 中常會有這樣的語法：<br />
<pre>CREATE TABLE table (
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user_id INT UNSIGNED NOT NULL ,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;KEY (user_id)
);</pre>
<br />
最後的 KEY (user_id) 是建立一個非唯一限制的 index，但在 PostgreSQL 中 CREATE TABLE 時只能建立 UNIQUE INDEX，所在基本上要建 non-unique key 只能獨立地以 CREATE INDEX 建立。<br />
<br />
3. PostgreSQL 沒有 BIGINIT UNSIGNED / INT UNSIGNED 的 data type<br />
<br />
PostgreSQL 和 MySQL 的 Data type 對照表：<br />
<br />
http://en.wikibooks.org/wiki/Converting_MySQL_to_PostgreSQL#Data_Types<br />
<br />
<br />
參考資料：<br />
1. Postgresql: how to create table only if it does not already exist?<br />
http://stackoverflow.com/questions/435424/postgresql-how-to-create-table-only-if-it-does-not-already-exist<br />
2. Converting MySQL to PostgreSQL<br />
http://en.wikibooks.org/wiki/Converting_MySQL_to_PostgreSQL]]></description>
            <dc:creator>HP</dc:creator>
            <category>PostgreSQL</category>
            <pubDate>Sat, 03 Sep 2011 01:56:08 +0800</pubDate>
        </item>
        <item>
            <guid>http://mepopedia.com/forum/read.php?297,7766,7766#msg-7766</guid>
            <title>避免註冊會員「使用者名稱重複」(case-insensitive)：Functional indexes &amp; lower() search (no replies)</title>
            <link>http://mepopedia.com/forum/read.php?297,7766,7766#msg-7766</link>
            <description><![CDATA[第一步：對有關「會員」的 database table 中的 username 建立 UNIQUE KEY。SQL 如下：<br />
<br />
<b>CREATE UNIQUE INDEX users_unique_username ON the_user_table USING btree (lower(username));</b><br />
<br />
其中的 (lower(username)) 的用法稱作 Functional Indexes。USING btree 是指定建立 KEY 的方法，此處為以 btree 建立。不一定要寫上。<br />
<br />
第二步：在 Script 中確保使用者名稱不重複：Case-insensitive 搜尋<br />
<br />
<b>SELECT * FROM the_user_table WHERE lower(username) = '新註冊的使用者名稱';</b><br />
<br />
若 SELECT 結果有找到相同的使用者名稱，就表示該帳號已經不能申請。<br />
<br />
<br />
Reference:<br />
<a href=http://www.postgresql.org/docs/7.3/static/indexes-functional.html>Functional Indexes</a>]]></description>
            <dc:creator>mepoadm</dc:creator>
            <category>PostgreSQL</category>
            <pubDate>Sun, 26 Sep 2010 03:23:21 +0800</pubDate>
        </item>
        <item>
            <guid>http://mepopedia.com/forum/read.php?297,4146,4146#msg-4146</guid>
            <title>Performance Issue in PostgreSQL: Counting rows in a table (no replies)</title>
            <link>http://mepopedia.com/forum/read.php?297,4146,4146#msg-4146</link>
            <description><![CDATA[In summary, PostgreSQL sacrifices some performance to the overall reliability and data integrity. COUNT is slow for the whole table scan but with normal speed when COUNT with INDEXed tables.<br />
<br />
--<br />
The article below is from <a href=http://wiki.postgresql.org/wiki/Why_PostgreSQL_Instead_of_MySQL_2009#Counting_rows_in_a_table>Why PostgreSQL Instead of MySQL 2009</a>.<br />
--<br />
<br />
<b>Counting rows in a table</b><br />
<br />
One operation that PostgreSQL is known to be slow performing is doing a full count of rows in a table, typically using this SQL:<br />
<br />
SELECT COUNT(*) FROM table<br />
<br />
The reason why this is slow is related to the MVCC implementation in PostgreSQL. The fact that multiple transactions can see different states of the data means that there can be no straightforward way for "COUNT(*)" to summarize data across the whole table; PostgreSQL must walk through all rows, in some sense. This normally results in a sequential scan reading information about every row in the table.<br />
Some DBMSes provide the ability for "COUNT(*)" queries to work via consulting an index. Unfortunately, in PostgreSQL, this strategy does not work, as MVCC visibility information is not stored at the index level. It is necessary to actually examine the rows themselves to determine if they are visible to the transaction or not.<br />
<br />
In MySQL, MyISAM tables cache the row count information, making this type of count operation almost instant. That is the reason why there exists so much MySQL code that uses this construct assuming it's a trivial operation. But if you're using InnoDB instead, this is no longer the case. See COUNT(*) for Innodb Tables and COUNT(*) vs COUNT(col) for notes on the limitations of MySQL in this area. MySQL designs that may be deployed on InnoDB can't assume that a full row count will be fast, and therefore are hampered by similar limitations to those present in PostgreSQL. However, InnoDB's MVCC information is present in its indexes, so an index can be used to satisfy COUNT(*) queries, even when there is no WHERE clause; a full table scan is not necessary.<br />
<br />
It is worth observing that it is only this precise form of aggregate that must be so pessimistic; if augmented with a "WHERE" clause like<br />
<br />
<b>SELECT COUNT(*) FROM table WHERE status = 'something'</b><br />
<br />
PostgreSQL, MySQL, and most other database implementations will take advantage of available indexes against the restricted field(s) to limit how many records must be counted, which can greatly accelerate such queries. PostgreSQL will still need to read the resulting rows to verify that they exist; MySQL may or may not, depending on the storage engine and the transaction isolation level. InnoDB generally does not need to read the rows, and can satisfy the operation from the index alone.<br />
<br />
One popular approach for applications that need a row count but can tolerate it not including transactions that are in the middle of being committed is to use a trigger-based mechanism to count the rows in the table. In PostgreSQL, another alternative when only an approximate count is needed is to use the reltuples field from the pg_class catalog table.]]></description>
            <dc:creator>HP</dc:creator>
            <category>PostgreSQL</category>
            <pubDate>Fri, 12 Feb 2010 00:16:01 +0800</pubDate>
        </item>
        <item>
            <guid>http://mepopedia.com/forum/read.php?297,2028,2028#msg-2028</guid>
            <title>PostgreSQL的數值資料型態(Numeric Data Types)速查表 (no replies)</title>
            <link>http://mepopedia.com/forum/read.php?297,2028,2028#msg-2028</link>
            <description><![CDATA[在設定 PostgreSQL 資料庫時經常會用到，節錄此表方便大家查詢：<br />
<br />
<TABLE BORDER="1" ><COL><COL><COL><COL><THEAD><TR><TH>Name</TH><TH>Storage Size</TH><TH>Description</TH><TH>Range</TH></TR></THEAD><TBODY><TR><TD><TT CLASS="TYPE">smallint</TT></TD><TD>2 bytes</TD><TD>small-range integer</TD><TD>-32768 to +32767</TD></TR><TR><TD><TT CLASS="TYPE">integer</TT></TD><TD>4 bytes</TD><TD>usual choice for integer</TD><TD>-2,147,483,648 to +2,147,483,647</TD></TR><TR><TD><TT CLASS="TYPE">bigint</TT></TD><TD>8 bytes</TD><TD>large-range integer</TD><TD>-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807</TD></TR><TR><TD><TT CLASS="TYPE">decimal</TT></TD><TD>variable</TD><TD>user-specified precision, exact</TD><TD>no limit</TD></TR><TR><TD><TT CLASS="TYPE">numeric</TT></TD><TD>variable</TD><TD>user-specified precision, exact</TD><TD>no limit</TD></TR><TR><TD><TT CLASS="TYPE">real</TT></TD><TD>4 bytes</TD><TD>variable-precision, inexact</TD><TD>6 decimal digits precision</TD></TR><TR><TD><TT CLASS="TYPE">double precision</TT></TD><TD>8 bytes</TD><TD>variable-precision, inexact</TD><TD>15 decimal digits precision</TD></TR><TR><TD><TT CLASS="TYPE">serial</TT></TD><TD>4 bytes</TD><TD>autoincrementing integer</TD><TD>1 to 2147483647</TD></TR><TR><TD><TT CLASS="TYPE">bigserial</TT></TD><TD>8 bytes</TD><TD>large autoincrementing integer</TD><TD>1 to 9223372036854775807</TD></TR></TBODY></TABLE><br />
<br />
基本上常用的 integer 的大小是十億等級(2.1 billion)，bigint (big integer) 是 92 萬兆(京)。<br />
<br />
Reference:<br />
http://www.postgresql.org/docs/8.3/static/datatype-numeric.html]]></description>
            <dc:creator>HP</dc:creator>
            <category>PostgreSQL</category>
            <pubDate>Fri, 09 Oct 2009 16:56:21 +0800</pubDate>
        </item>
    </channel>
</rss>
