10. PostgreSQL 15.1 Documentation
37.51. sql_sizing ................................................................................ 1117
37.52. table_constraints .................................................................. 1117
37.53. table_privileges .................................................................... 1118
37.54. tables ........................................................................................ 1118
37.55. transforms ................................................................................ 1119
37.56. triggered_update_columns .................................................... 1120
37.57. triggers .................................................................................... 1120
37.58. udt_privileges ........................................................................ 1122
37.59. usage_privileges .................................................................... 1122
37.60. user_defined_types ................................................................ 1123
37.61. user_mapping_options ............................................................ 1125
37.62. user_mappings .......................................................................... 1125
37.63. view_column_usage .................................................................. 1125
37.64. view_routine_usage ................................................................ 1126
37.65. view_table_usage .................................................................... 1126
37.66. views .......................................................................................... 1127
V. Server Programming ............................................................................................. 1129
38. Extending SQL ........................................................................................... 1135
38.1. How Extensibility Works ................................................................... 1135
38.2. The PostgreSQL Type System ............................................................ 1135
38.3. User-Defined Functions ..................................................................... 1138
38.4. User-Defined Procedures ................................................................... 1139
38.5. Query Language (SQL) Functions ....................................................... 1139
38.6. Function Overloading ........................................................................ 1156
38.7. Function Volatility Categories ............................................................. 1157
38.8. Procedural Language Functions ........................................................... 1158
38.9. Internal Functions ............................................................................. 1158
38.10. C-Language Functions ..................................................................... 1159
38.11. Function Optimization Information .................................................... 1180
38.12. User-Defined Aggregates ................................................................. 1181
38.13. User-Defined Types ........................................................................ 1188
38.14. User-Defined Operators ................................................................... 1192
38.15. Operator Optimization Information .................................................... 1193
38.16. Interfacing Extensions to Indexes ....................................................... 1197
38.17. Packaging Related Objects into an Extension ....................................... 1210
38.18. Extension Building Infrastructure ....................................................... 1218
39. Triggers ..................................................................................................... 1223
39.1. Overview of Trigger Behavior ............................................................ 1223
39.2. Visibility of Data Changes ................................................................. 1226
39.3. Writing Trigger Functions in C ........................................................... 1226
39.4. A Complete Trigger Example ............................................................. 1229
40. Event Triggers ............................................................................................ 1233
40.1. Overview of Event Trigger Behavior .................................................... 1233
40.2. Event Trigger Firing Matrix ............................................................... 1234
40.3. Writing Event Trigger Functions in C .................................................. 1237
40.4. A Complete Event Trigger Example .................................................... 1238
40.5. A Table Rewrite Event Trigger Example .............................................. 1239
41. The Rule System ........................................................................................ 1241
41.1. The Query Tree ................................................................................ 1241
41.2. Views and the Rule System ................................................................ 1242
41.3. Materialized Views ........................................................................... 1249
41.4. Rules on INSERT, UPDATE, and DELETE ........................................... 1252
41.5. Rules and Privileges .......................................................................... 1263
41.6. Rules and Command Status ................................................................ 1265
41.7. Rules Versus Triggers ....................................................................... 1265
42. Procedural Languages .................................................................................. 1268
42.1. Installing Procedural Languages .......................................................... 1268
43. PL/pgSQL — SQL Procedural Language ........................................................ 1271
x
11. PostgreSQL 15.1 Documentation
43.1. Overview ........................................................................................ 1271
43.2. Structure of PL/pgSQL ...................................................................... 1272
43.3. Declarations ..................................................................................... 1274
43.4. Expressions ..................................................................................... 1280
43.5. Basic Statements .............................................................................. 1281
43.6. Control Structures ............................................................................. 1289
43.7. Cursors ........................................................................................... 1304
43.8. Transaction Management ................................................................... 1310
43.9. Errors and Messages ......................................................................... 1311
43.10. Trigger Functions ............................................................................ 1313
43.11. PL/pgSQL under the Hood ............................................................... 1322
43.12. Tips for Developing in PL/pgSQL ..................................................... 1325
43.13. Porting from Oracle PL/SQL ............................................................ 1329
44. PL/Tcl — Tcl Procedural Language ............................................................... 1339
44.1. Overview ........................................................................................ 1339
44.2. PL/Tcl Functions and Arguments ........................................................ 1339
44.3. Data Values in PL/Tcl ....................................................................... 1341
44.4. Global Data in PL/Tcl ....................................................................... 1341
44.5. Database Access from PL/Tcl ............................................................. 1342
44.6. Trigger Functions in PL/Tcl ............................................................... 1344
44.7. Event Trigger Functions in PL/Tcl ....................................................... 1346
44.8. Error Handling in PL/Tcl ................................................................... 1346
44.9. Explicit Subtransactions in PL/Tcl ....................................................... 1347
44.10. Transaction Management .................................................................. 1348
44.11. PL/Tcl Configuration ....................................................................... 1349
44.12. Tcl Procedure Names ...................................................................... 1349
45. PL/Perl — Perl Procedural Language ............................................................. 1350
45.1. PL/Perl Functions and Arguments ....................................................... 1350
45.2. Data Values in PL/Perl ...................................................................... 1355
45.3. Built-in Functions ............................................................................. 1355
45.4. Global Values in PL/Perl ................................................................... 1360
45.5. Trusted and Untrusted PL/Perl ............................................................ 1361
45.6. PL/Perl Triggers ............................................................................... 1362
45.7. PL/Perl Event Triggers ...................................................................... 1363
45.8. PL/Perl Under the Hood .................................................................... 1364
46. PL/Python — Python Procedural Language ..................................................... 1366
46.1. PL/Python Functions ......................................................................... 1366
46.2. Data Values ..................................................................................... 1367
46.3. Sharing Data .................................................................................... 1372
46.4. Anonymous Code Blocks ................................................................... 1373
46.5. Trigger Functions ............................................................................. 1373
46.6. Database Access ............................................................................... 1374
46.7. Explicit Subtransactions ..................................................................... 1377
46.8. Transaction Management ................................................................... 1378
46.9. Utility Functions .............................................................................. 1379
46.10. Python 2 vs. Python 3 ..................................................................... 1380
46.11. Environment Variables ..................................................................... 1380
47. Server Programming Interface ....................................................................... 1382
47.1. Interface Functions ........................................................................... 1382
47.2. Interface Support Functions ................................................................ 1424
47.3. Memory Management ....................................................................... 1433
47.4. Transaction Management ................................................................... 1443
47.5. Visibility of Data Changes ................................................................. 1446
47.6. Examples ........................................................................................ 1446
48. Background Worker Processes ...................................................................... 1450
49. Logical Decoding ........................................................................................ 1453
49.1. Logical Decoding Examples ............................................................... 1453
49.2. Logical Decoding Concepts ................................................................ 1457
xi
12. PostgreSQL 15.1 Documentation
49.3. Streaming Replication Protocol Interface .............................................. 1458
49.4. Logical Decoding SQL Interface ......................................................... 1458
49.5. System Catalogs Related to Logical Decoding ....................................... 1458
49.6. Logical Decoding Output Plugins ........................................................ 1459
49.7. Logical Decoding Output Writers ........................................................ 1467
49.8. Synchronous Replication Support for Logical Decoding ........................... 1467
49.9. Streaming of Large Transactions for Logical Decoding ............................ 1467
49.10. Two-phase Commit Support for Logical Decoding ................................ 1469
50. Replication Progress Tracking ....................................................................... 1470
51. Archive Modules ........................................................................................ 1471
51.1. Initialization Functions ...................................................................... 1471
51.2. Archive Module Callbacks ................................................................. 1471
VI. Reference .......................................................................................................... 1473
I. SQL Commands ............................................................................................ 1478
ABORT .................................................................................................. 1482
ALTER AGGREGATE ............................................................................. 1483
ALTER COLLATION .............................................................................. 1485
ALTER CONVERSION ............................................................................ 1488
ALTER DATABASE ................................................................................ 1490
ALTER DEFAULT PRIVILEGES .............................................................. 1493
ALTER DOMAIN .................................................................................... 1496
ALTER EVENT TRIGGER ....................................................................... 1500
ALTER EXTENSION ............................................................................... 1501
ALTER FOREIGN DATA WRAPPER ........................................................ 1505
ALTER FOREIGN TABLE ....................................................................... 1507
ALTER FUNCTION ................................................................................. 1512
ALTER GROUP ...................................................................................... 1516
ALTER INDEX ....................................................................................... 1518
ALTER LANGUAGE ............................................................................... 1521
ALTER LARGE OBJECT ......................................................................... 1522
ALTER MATERIALIZED VIEW ............................................................... 1523
ALTER OPERATOR ................................................................................ 1525
ALTER OPERATOR CLASS .................................................................... 1527
ALTER OPERATOR FAMILY .................................................................. 1528
ALTER POLICY ..................................................................................... 1532
ALTER PROCEDURE .............................................................................. 1534
ALTER PUBLICATION ........................................................................... 1537
ALTER ROLE ......................................................................................... 1540
ALTER ROUTINE ................................................................................... 1544
ALTER RULE ......................................................................................... 1546
ALTER SCHEMA ................................................................................... 1547
ALTER SEQUENCE ................................................................................ 1548
ALTER SERVER ..................................................................................... 1551
ALTER STATISTICS ............................................................................... 1553
ALTER SUBSCRIPTION .......................................................................... 1554
ALTER SYSTEM .................................................................................... 1557
ALTER TABLE ....................................................................................... 1559
ALTER TABLESPACE ............................................................................ 1577
ALTER TEXT SEARCH CONFIGURATION .............................................. 1579
ALTER TEXT SEARCH DICTIONARY ..................................................... 1581
ALTER TEXT SEARCH PARSER ............................................................. 1583
ALTER TEXT SEARCH TEMPLATE ........................................................ 1584
ALTER TRIGGER ................................................................................... 1585
ALTER TYPE ......................................................................................... 1587
ALTER USER ......................................................................................... 1592
ALTER USER MAPPING ......................................................................... 1593
ALTER VIEW ......................................................................................... 1594
ANALYZE .............................................................................................. 1596
xii
14. PostgreSQL 15.1 Documentation
DROP CAST ........................................................................................... 1806
DROP COLLATION ................................................................................ 1807
DROP CONVERSION .............................................................................. 1808
DROP DATABASE ................................................................................. 1809
DROP DOMAIN ...................................................................................... 1810
DROP EVENT TRIGGER ......................................................................... 1811
DROP EXTENSION ................................................................................. 1812
DROP FOREIGN DATA WRAPPER .......................................................... 1813
DROP FOREIGN TABLE ......................................................................... 1814
DROP FUNCTION .................................................................................. 1815
DROP GROUP ........................................................................................ 1817
DROP INDEX ......................................................................................... 1818
DROP LANGUAGE ................................................................................. 1820
DROP MATERIALIZED VIEW ................................................................. 1821
DROP OPERATOR .................................................................................. 1822
DROP OPERATOR CLASS ...................................................................... 1824
DROP OPERATOR FAMILY .................................................................... 1826
DROP OWNED ....................................................................................... 1828
DROP POLICY ....................................................................................... 1829
DROP PROCEDURE ............................................................................... 1830
DROP PUBLICATION ............................................................................. 1832
DROP ROLE .......................................................................................... 1833
DROP ROUTINE ..................................................................................... 1834
DROP RULE .......................................................................................... 1836
DROP SCHEMA ..................................................................................... 1837
DROP SEQUENCE .................................................................................. 1838
DROP SERVER ...................................................................................... 1839
DROP STATISTICS ................................................................................. 1840
DROP SUBSCRIPTION ............................................................................ 1841
DROP TABLE ........................................................................................ 1843
DROP TABLESPACE .............................................................................. 1844
DROP TEXT SEARCH CONFIGURATION ................................................ 1845
DROP TEXT SEARCH DICTIONARY ....................................................... 1846
DROP TEXT SEARCH PARSER ............................................................... 1847
DROP TEXT SEARCH TEMPLATE .......................................................... 1848
DROP TRANSFORM ............................................................................... 1849
DROP TRIGGER ..................................................................................... 1850
DROP TYPE ........................................................................................... 1851
DROP USER ........................................................................................... 1852
DROP USER MAPPING ........................................................................... 1853
DROP VIEW .......................................................................................... 1854
END ...................................................................................................... 1855
EXECUTE .............................................................................................. 1856
EXPLAIN ............................................................................................... 1857
FETCH ................................................................................................... 1862
GRANT .................................................................................................. 1866
IMPORT FOREIGN SCHEMA .................................................................. 1871
INSERT .................................................................................................. 1873
LISTEN .................................................................................................. 1881
LOAD .................................................................................................... 1883
LOCK .................................................................................................... 1884
MERGE .................................................................................................. 1887
MOVE ................................................................................................... 1893
NOTIFY ................................................................................................. 1895
PREPARE ............................................................................................... 1898
PREPARE TRANSACTION ...................................................................... 1901
REASSIGN OWNED ............................................................................... 1903
REFRESH MATERIALIZED VIEW ........................................................... 1904
xiv