7 Parallel Seq Scan
PDF page 38
3.5.1 Overview
In the conventional PostgreSQL, all of the SQL statements ware executed only by the back-end process that accepts the connection. In PostgreSQL 9.6 now it is possible to perform parallel processing by multiple worker processes.

PDF page 38
Parallel processing can be executed only for Seq Scan, Join and Aggregate. The degree of parallelism depends on the size of the table. Processes executing parallel processing use the mechanism of the Background Worker. The maximum value of the degree of parallelism is determined by the parameter max_parallel_degree or max_worker_processes, whichever is smaller. Parameter max_parallel_degree can be changed by general users by per-session.
| Parameter Name | Description (context) | Default value |
|---|---|---|
| max_parallel_degree | The maximum value of the degree of parallelism (user) | 2 |
| parallel_setup_cost | Start cost of parallel processing (user) | 1000 |
| parallel_tuple_cost | Tuple cost of parallel processing (user) | 0.1 |
| max_worker_processes | The maximum value of the worker process (postmaster) | 8 |
| force_parallel_mode | Force parallel processing (user) | off |
PDF page 39
Parameter force_parallel_mode
Parallel processing is executed only when the cost is considered lower than the normal serial processing. By specifying the parameter force_parallel_mode to on, parallel processing is forced ( Also value ‘regress’ is for the regression test). However, the parallel processing is executed only when the parameter max_parallel_degree is 1 or more.
Related table option
Table option parallel_degree determines the degree of parallelism for each table. When the value is set to 0, parallel processing is prohibited. If not set, the parameters max_parallel_degree of the session will be the upper limit. If parallel_degree is set to greater than the max_parallel_degree, the upper limit of the actual degree of parallelism cannot exceed the max_parallel_degree.
3.5.2 Execution plan
The example below is the execution plan of the parallel processing SELECT statement. COUNT processing of large-scale table is processed in 3 parallelism.
PDF page 40
PDF page 41
| Plan component | Description | Explain Statement |
|---|---|---|
| Parallel Seq Scan | Parallel search processing | All |
| Partial Aggregate | Aggregation processing performed by the worker process | All |
| Gather | Processing to aggregate the worker process | All |
| Finalize Aggregate | The final aggregation processing | All |
| Workers Planned: | The number of planned worker processes | All |
| Workers Launched: | The number of workers that are actually run | ANALYZE |
| Worker N (N=0,1,…) | Processing time of each worker, etc | ANALYZE, VERBOSE |
| Single Copy | Processing to be executed in a single process | All |
3.5.3 Parallel processing and functions
There are usable functions and unusable functions in parallel processing. When functions which have ‘u’(PARALLEL UNSAFE) value for proparallel column in pg_proc catalog are user in SQL statement, parallel processing can not be performed. The following table shows major standard PARALLEL UNSAFE functions.
| Category | Function name example |
|---|---|
| JSON | json_populate_record, json_populate_recordset, jsonb_insert, jsonb_set |
| SEQUENCE object | nextval, currval, setval, lastval |
| Large Object | lo_*, loread, lowrite |
| Replication | pg_create_slot, pg_dropslot, pg_logical, pg_replication_ |
| Other | pg_advisory_, pg_try_advisory_, plpgsql_handler, pg_extension_config_dump, pg_backup, set_config, txid_current, query_to_xml* |
In the following example, two SQL statements that differ only conditional part of the WHERE clause are executed. SELECT statement with the literal in the WHERE clause will be performed parallel processing but, SELECT statement with the currval of sequence operation function is executed in serial.
PDF page 42
| Category | Function name example |
|---|---|
| Date and Age | age, now |
| Random number | random, setseed |
| Upgrade | binary_upgrade* |
| Convert to XML | cursor_to_xml*, database_to_xml*, schema_to_xml*, table_to_xml* |
| Other | pg_start_backup, inet_client*, current_query, pg_backend_pid, pg_conf*, pg_cursor, pg_get_viewdef, pg_prepared_statement, etc |
User-defined functions and PARALLEL SAFE
To indicate whether it is possible to perform parallel processing for user-defined functions, can be used PARALLEL SAFE clause or PARALLEL UNSAFE clause in the CREATE FUNCTION statement or ALTER FUNCTION statement. The default is PARALLEL UNSAFE.
PDF page 43
3.5.4 Calculation of the degree of parallelism
The degree of parallelism is calculated based on the size of the table, it is increased by one as the size of table is 3,000 blocks, 9,000 blocks and 27,000s block. After that, it is increased as the size of table becomes threefold within the range that does not exceed the parameter max_parallel_degree or parameter max_worker_processes. Actual calculation is executed by create_parallel_paths function in the source code src/backend/optimizer/path/allpaths.c.
PDF page 44