Comparison between PostgreSQL and MySQL

Source: Internet
Author: User
Tags ldap


Features MySQL PostgreSQL
Instance Run the MySQL Command (mysqld) to start the instance. One instance can manage one or more databases. One server can run multiple mysqld instances. An instance manager can monitor various instances of mysqld. Start the instance by executing the postmaster process (pg_ctl. One instance can manage one or more databases, which form a cluster. A cluster is an area on a disk. This area is initialized during installation and consists of a directory where all data is stored. Use initdb to create the first database. Multiple instances can be started on one machine.
Database A database is a set of named objects that are separated from other databases in the instance. All databases in a MySQL instance share the same system catalog. A database is a set of named objects. Each database is an entity separated from other databases. Each database has its own system catalog, but all databases share pg_databases.
Data Buffer Zone Use innodb_buffer_pool_size to configure the data buffer. This parameter is the number of bytes in the memory buffer. InnoDB uses this buffer to cache table data and indexes. On a dedicated database server, this parameter can be set to 80% of the physical memory volume of the machine. Shared_buffers cache. By default, 64 buffers are allocated. The default block size is 8 K. You can set the shared_buffers parameter in the PostgreSQL. conf file to update the buffer cache.
Database Connection The client uses the connect or use statement to connect to the database. You must specify the Database Name and user ID and password. Use roles to manage users and user groups in the database. The client uses the connect statement to connect to the database. You must specify the Database Name and user ID and password. Use roles to manage users and user groups in the database.
Authentication MySQL manages authentication at the database level. Only password authentication is supported. PostgreSQL supports a wide range of authentication methods: trust authentication, password authentication, Kerberos authentication, ident-based authentication, LDAP authentication, and PAM Authentication.
Encryption You can specify a password at the table level to encrypt data. You can also use the aes_encrypt and aes_decrypt functions to encrypt and decrypt column data. Network Encryption can be achieved through SSL connections. You can use functions in the pgcrypto library to encrypt/decrypt columns. Network Encryption can be achieved through SSL connections.
Audit You can execute grep on querylog. You can use the PL/pgsql trigger to audit a table.
Query description Run the explain command to view the explain Plan. Run the explain command to view the explain Plan.
Backup, recovery, and logs InnoDB uses the write-ahead log to record data. Supports both online and offline full backup and crash and transaction recovery. Third-party software is required to support hot backup. Maintain the pre-write log in a sub-directory of the data directory. Supports full online and offline backup, crash, time point, and transaction recovery. Supports hot backup.
JDBC driverProgram FromReferencesDownload the JDBC driver. FromReferencesDownload the JDBC driver.
Table type Depends on the storage engine. For example, the NDB storage engine supports partitioned tables and the memory engine supports memory tables. Supports partition tables of temporary tables, regular tables, and range and list types. Hash partition tables are not supported. Since table partitions in PostgreSQL are completed through table inheritance and rule systems, more complex partitioning methods can be implemented.
Index type Depends on the storage engine. MyISAM: btree, InnoDB: btree. Supports B-tree, hash, R-tree, and gist indexes.
Constraints The primary key, foreign key, unique, and non-null constraints are supported. Resolve the check constraints, but do not enforce them forcibly. Supports primary keys, foreign keys, unique, non-null, and check constraints.
Stored Procedures and user-defined functions Supports create procedure and create function statements. Stored procedures can be written in SQL and C ++. User-defined functions can be written in SQL, C, and C ++. No separate stored procedures are implemented through functions. User-defined functions can be written in PL/pgsql, PL/TCL, PL/perl, PL/python, SQL, and C.
Trigger Supports pre-row triggers, post-row triggers, and statement triggers. Trigger statements are written in a procedural language. Supports front-row triggers, post-row triggers, and statement triggers. The trigger process is written in C.
System Configuration File My. conf PostgreSQL. conf
Database Configuration My. conf PostgreSQL. conf
Client connection file My. conf Pg_mirror.conf
XML support Limited XML support. Limited XML support.
Data access and management server Optimize table -- reclaim unused space and remove data file fragments
Myisamchk-analyze -- Update the statistics used by the query optimizer (MyISAM storage engine)
Mysql-command line tool
MySQL Administrator -- Client GUI tool
Vacuum -- reclaim unused space
Analyze -- updates statistics used by the Query Optimizer
Psql-command line tool
Pgadmin -- Client GUI tool
Concurrency Control Table-level and row-level locks are supported. The InnoDB Storage engine supports read_committed, read_uncommitted, repeatable_read, and serializable. Use the SET transaction isolation level statement to set the isolation level at the transaction level. Table-level and row-level locks are supported. The supported ANSI isolation levels are read committed (default: You can see the snapshot of the database when the query is started) and serialization (similar to Repeatable read-you can only see the results submitted before the transaction starts ). Use the SET transaction statement to set the isolation level at the transaction level. Set sessions at the session level.

Disadvantages of MySQL over PostgreSQL:

MySQL PostgreSQL
The most important engine InnoDB has long been controlled by Oracle. Currently, the entire MySQL database is controlled by Oracle. The BSD Protocol is not monopolized by large companies.
Weak processing of complex queries and immature Query Optimizer A powerful query optimizer that supports complex query processing.
There is only one table connection type: nested loop join (Nested-loop), which does not support sorting-merge join (sort-merge join) and hash join ). All supported
Insufficient performance optimization tools and metrics

Some performance views are provided to conveniently view the select, delete, update, and insert statistics on a table and indexes, as well as the cache hit rate. There is an open-source pgstatspack tool on the Internet.

InnoDB tables and indexes are stored in the same way. That is to say, all tables are indexed and organized tables. This generally requires that the primary key should not be too long and the primary key at the time of insertion should increase sequentially, otherwise it will have a great impact on performance.

This problem does not exist.

Most queries can only use a single index on a table. In some cases, multiple indexes are used for queries, but the query optimizer usually underestimate the cost, they are often slower than table scans.

This problem does not exist.

It takes a long time to add columns to a table, which is basically to recreate the table and index.

Add columns to the table. Only table definitions are added to the data dictionary and the table is not rebuilt.

Stored Procedures and triggers have limited functions. Language functions that can be used to write stored procedures, triggers, scheduled events, and stored functions are weak.

In addition to PL/pgsql stored procedures, PL/perl, PL/python, and PL/TCL stored procedures are also supported.

It also supports writing stored procedures in C language.

Sequence is not supported.


Function indexes are not supported. You can only create an index based on a specific column.

Materialized views are not supported.

Function indexes and partial data indexes are supported. You can use the rule system to implement the materialized view function.

The execution plan is not shared globally. It is shared only within the connection.

Execution Plan sharing

A small part of the SQL syntax supported by MySQL (ansi SQL standard. Recursive queries, common table expressions (with statements in Oracle), and window functions (analysis functions) are not supported ).

All supported

User-Defined type or domain is not supported)


For time, date, interval, and other time types, there is no storage type of the second or lower level

It can be accurate to less than seconds.

The authentication function is fully built-in. It does not support operating system authentication, PAM Authentication, LDAP, and other similar external authentication functions.

Supports OS authentication, Kerberos authentication, ident authentication, LDAP authentication, and PAM Authentication.

Database link is not supported. There is a storage engine called federated that can pass query statements to a table on a remote server as a transit server. However, it is very functional and has many vulnerabilities.

Dblink and DBI-link can be connected to Oracle and MySQL.

MySQL cluster may be significantly different from what you think. There are few open-source cluster software.

Replication is asynchronous and has many limitations. for example, it is a single-thread (single-threaded), so the recovery speed of a server Load balancer with higher processing capability is hard to keep up with the master with a slower processing capability.

Supports a wide range of open-source cluster software.

Explain: the execution plan result is simple.

Explain returns a wealth of information.

Operations similar to alter table or create table are non-transactional. They commit uncommitted transactions and cannot be rolled back or perform disaster recovery.

DDL also has transactions.

Major advantages of postgresql:
1. postgreSQL is completely free, and it is a BSD Protocol. If you change PostgreSQL and then sell it for money, no one cares about you. This is very important, this indicates that the PostgreSQL database will not be controlled by other companies. Oracle databases are commercial databases and are not open. While MySQL databases are open-source, but now Sun has been acquired by Oracle and is now basically controlled by Oracle. Before sun was acquired, the most important InnoDB Engine in MySQL is also controlled by Oracle, and many important data in MySQL is stored in the InnoDB engine. Our company is like this. Therefore, if the market scope of MySQL conflicts with the market scope of Oracle databases, Oracle will sacrifice MySQL, which is undoubtedly true.
2. there are many open-source software that works with PostgreSQL, including many distributed cluster software, such as pgpool, pgcluster, slony, and plploxy. It is easy to implement solutions such as read/write splitting, load balancing, and horizontal data splitting, this is difficult in MySQL.
3. PostgreSQLSource codeIt is clearly written, and it is easier to understand than mysql. I suspect that the MySQL SourceCodeObfuscated. Therefore, many companies use PostgreSQL for secondary development.
4. PostgreSQL is better than MySql in many aspects, such as complex SQL Execution, stored procedures, triggers, and indexes. At the same time, PostgreSQL is multi-process, while MySQL is thread. Although the concurrency is not high, MySQL processing speed is fast, but when the concurrency is high, for a single machine with multiple cores, the overall processing performance of MySQL is not as good as that of postgresql because MySQL threads cannot fully utilize CPU capabilities.
Currently, I only think about this. I want to add it later. You are welcome to make a brick.

Differences between PostgreSQL and multiple versions of oracle or InnoDB

The major difference between PostgreSQL and multiple versions of oracle or InnoDB is whether the latest and historical versions are stored separately. PostgreSQL does not distinguish between the latest and historical versions, but Oracle and InnoDB, while InnoDB only separates data, the index itself is not separated.
PostgreSQL has the following advantages:
1. PostgreSQL does not have rollback segments, while Oracle and InnoDB both have rollback segments. For Oracle and InnoDB, the rollback segment is very important. If the rollback segment is damaged, it may cause data loss or even serious problems that the database cannot be started. Because PostgreSQL does not have a rollback segment, the old data is recorded in the original file, so when the database recovers after a crash exception occurs, it will not perform as complex recovery as Oracle and InnoDB databases, because redo and undo are required for synchronization between Oracle and InnoDB. Therefore, when a crash exception occurs in a PostgreSQL database, the chances of a database not getting started are lower than those of Oracle and MySQL.
2. Since the old data is directly recorded in the data file, rather than in the rollback segment, it does not often report ora-01555 errors as Oracle does.
3. rollback can be completed quickly, because rollback does not delete data, but Oracle and InnoDB are complicated during rollback, and the modifications made by this firm must be cleared during transaction rollback, the inserted record must be deleted, and the updated record must be updated (see the row_undo function). At the same time, the rollback process will generate a large number of redo logs again.
4. Wal logs are simpler than Oracle and InnoDB. For Oracle, you must not only record changes in data files, but also record changes in rollback segments.
The major disadvantage of postgresql multi-version is:
1. The latest and historical versions are not separated for storage. As a result, more scans are required to clear old versions at a high cost. However, normal databases have peak periods. If we reasonably arrange vacuum, this is not a big problem, and vacuum is further enhanced in postgresql9.0.
2. Because the index does not have version information, coverage index scan cannot be implemented. That is, the query only scans the index and returns the required attributes directly from the index. You also need to access the table. Oracle and InnoDB can;

Comparison between process mode and thread mode
PostgreSQL and Oracle adopt the process mode, while MySQL adopts the thread mode.
The process mode has high CPU utilization.
Shared Memory is required for data sharing in the process mode, while data in the thread mode is shared in the process space. Different threads only need to control synchronization between threads.
The thread mode consumes less resources.
Therefore, MySQL supports more connections than oracle.
For PostgreSQL, this problem also exists if the connection pool software is not used. However, PostgreSQL has excellent connection pool software, such as pgbouncer and pgpool, so many connections can be supported through the connection pool.

comparison between heap tables and index organization tables

Oracle supports heap tables, index organization table
PostgreSQL only supports heap tables, the table cannot be indexed.
InnoDB only supports index organization tables
advantages of index organization tables:
data in a table is organized by index and ordered, if the data is accessed by the primary key, the access data is faster. In a heap table, when accessing data by primary key, you must first locate the physical location of the data by primary key index.
disadvantages of Table Indexing:
when additional indexes are added to the index organization table, the Data Location of other index records is no longer a physical location, but a primary key value. Therefore, for an index organization table, the primary key value cannot be too large; otherwise, the occupied space is relatively large.
for an index organization table, if data is inserted in the middle each time, the index may be split, index splitting greatly reduces the insert performance. Therefore, for InnoDB, we generally recommend that the primary key be a meaningless sequence, so that each insertion occurs at the end to avoid this problem.
because the table is organized by an index tree, generally, data blocks must be accessed based on the relationship between data blocks, rather than accessing data by physical blocks. Therefore, full table scan is much slower than heap tables, this may not be obvious in OLTP, but it may be a problem in data warehouse applications.

Postgresql9.0 features:
Hot Standby function in PostgreSQL
That is, standby can also provide read-only services when synchronizing application logs, which is useful for read/write splitting. This function is only available in oracle11g.

Functions of postgresql asynchronous commit:
This feature is only available in Oracle at oracle11g R2. In many application scenarios, a small amount of data is allowed to be lost when the server goes down. This function is particularly suitable for such scenarios. Set synchronous_commit to false in postgresql9.0 to enable this function. It should be noted that, although it is set to asynchronous submission, when the host goes down, PostgreSQL will only lose a small amount of data, asynchronous submission will not cause data corruption and the database will not be able to start. I have never heard of this function in MySQL.

Special features of Indexing in PostgreSQL:
PostgreSQL can have partial indexes, that is, only some data in the table can be indexed. Create index can contain the where condition. At the same time, indexes in PostgreSQL can be scanned in reverse order, so you do not need to create a special descending index in PostgreSQL.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.