Postgresql Programmer's Guide

Total Page:16

File Type:pdf, Size:1020Kb

Postgresql Programmer's Guide PostgreSQL Programmer’s Guide The PostgreSQL Development Team Edited by Thomas Lockhart PostgreSQL Programmer’s Guide by The PostgreSQL Development Team Edited by Thomas Lockhart PostgreSQL is Copyright © 1996-9 by the Postgres Global Development Group. Table of Contents Summary......................................................................................................................................i 1. Introduction ............................................................................................................................1 Resources............................................................................................................................1 Terminology........................................................................................................................2 Notation ..............................................................................................................................3 Y2K Statement....................................................................................................................3 Copyrights and Trademarks................................................................................................4 2. Architecture ............................................................................................................................5 Postgres Architectural Concepts .........................................................................................5 3. Extending SQL: An Overview...............................................................................................7 How Extensibility Works....................................................................................................7 The Postgres Type System..................................................................................................7 About the Postgres System Catalogs ..................................................................................8 4. Extending SQL: Functions ..................................................................................................11 Query Language (SQL) Functions....................................................................................11 SQL Functions on Base Types ................................................................................11 SQL Functions on Composite Types.......................................................................12 Programming Language Functions ...................................................................................13 Programming Language Functions on Base Types .................................................13 Programming Language Functions on Composite Types........................................15 Caveats ....................................................................................................................16 5. Extending SQL: Types.........................................................................................................18 User-Defined Types..........................................................................................................18 Functions Needed for a User-Defined Type............................................................18 Large Objects ..........................................................................................................19 6. Extending SQL: Operators..................................................................................................20 Operator Optimization Information ..................................................................................21 COMMUTATOR ....................................................................................................21 NEGATOR..............................................................................................................22 RESTRICT..............................................................................................................22 JOIN .......................................................................................................................23 HASHES .................................................................................................................23 SORT1 and SORT2.................................................................................................24 7. Extending SQL: Aggregates ................................................................................................26 8. The Postgres Rule System....................................................................................................28 What is a Querytree?.........................................................................................................28 The Parts of a Querytree..........................................................................................28 Views and the Rule System ..............................................................................................30 Implementation of Views in Postgres......................................................................30 How SELECT Rules Work .....................................................................................30 View Rules in Non-SELECT Statements................................................................35 The Power of Views in Postgres .............................................................................36 i Benefits..........................................................................................................36 Concerns ........................................................................................................36 Implementation Side Effects ...................................................................................37 Rules on INSERT, UPDATE and DELETE.....................................................................38 Differences to View Rules ......................................................................................38 How These Rules Work ..........................................................................................38 A First Rule Step by Step ..............................................................................39 Cooperation with Views..........................................................................................42 Rules and Permissions ......................................................................................................48 Rules versus Triggers........................................................................................................49 9. Interfacing Extensions To Indices.......................................................................................52 10. GiST Indices........................................................................................................................59 11. Procedural Languages........................................................................................................61 Installing Procedural Languages.......................................................................................61 PL/pgSQL .........................................................................................................................62 Overview .................................................................................................................62 Description ..............................................................................................................63 Structure of PL/pgSQL ..................................................................................63 Comments......................................................................................................63 Declarations ...................................................................................................63 Data Types.....................................................................................................64 Expressions....................................................................................................65 Statements......................................................................................................66 Trigger Procedures.........................................................................................68 Exceptions......................................................................................................69 Examples.................................................................................................................69 Some Simple PL/pgSQL Functions ...............................................................70 PL/pgSQL Function on Composite Type.......................................................70 PL/pgSQL Trigger Procedure ........................................................................70 PL/Tcl ...............................................................................................................................71 Overview .................................................................................................................71 Description ..............................................................................................................71 Postgres Functions and Tcl Procedure Names...............................................71 Defining Functions in PL/Tcl ........................................................................71 Global Data in PL/Tcl....................................................................................72 Trigger Procedures in PL/Tcl.........................................................................72 Database Access from PL/Tcl........................................................................74
Recommended publications
  • Delivering Oracle Compatibility
    DELIVERING ORACLE COMPATIBILITY A White Paper by EnterpriseDB www.EnterpriseDB.com TABLE OF CONTENTS Executive Summary 4 Introducing EnterpriseDB Advanced Server 6 SQL Compatibility 6 PL/SQL Compatibility 9 Data Dictionary Views 11 Programming Flexibility and Drivers 12 Transfer Tools 12 Database Replication 13 Enterprise-Class Reliability and Scalability 13 Oracle-Like Tools 14 Conclusion 15 Downloading EnterpriseDB 15 EnterpriseDB EXECUTIVE SUMMARY Enterprises running Oracle® are generally interested in alternative databases for at least three reasons. First, these enterprises are experiencing budget constraints and need to lower their database Total Cost of Ownership (TCO). Second, they are trying to gain greater licensing flexibility to become more agile within the company and in the larger market. Finally, they are actively pursuing vendors who will provide superior technical support and a richer customer experience. And, subsequently, enterprises are looking for a solution that will complement their existing infrastructure and skills. The traditional database vendors have been unable to provide the combination of all three benefits. While Microsoft SQL ServerTM and IBM DB2TM may provide the flexibility and rich customer experience, they cannot significantly reduce TCO. Open source databases, on the other hand, can provide the TCO benefits and the flexibility. However, these open source databases either lack the enterprise-class features that today’s mission critical applications require or they are not equipped to provide the enterprise-class support required by these organizations. Finally, none of the databases mentioned above provide the database compatibility and interoperability that complements their existing applications and staff. The fear of the costs of changing databases, including costs related to application re-coding and personnel re-training, outweigh the expected savings and, therefore, these enterprises remain paralyzed and locked into Oracle.
    [Show full text]
  • Serveraid-M Software User Guide
    ® ServeRAID-M Software User Guide June 2014 Part Number: 00AH215 ServeRAID M Software User Guide June 2014 Eleventh Edition (June 2014) © Copyright IBM Corporation 2007, 2013. US Government Users Restricted Rights -- Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. ServeRAID M Software User Guide Table of Contents June 2014 Table of Contents Chapter 1: Overview . 14 1.1 SAS Technology. .14 1.2 Serial-attached SCSI Device Interface . .14 1.3 Serial ATA Features. .15 1.4 Solid State Drive Features . .15 1.5 Solid State Drive Guard. .16 1.6 Integrated MegaRAID Mode and MegaRAID Mode . .16 1.7 Feature on Demand (FoD) Upgrades. .17 1.8 Feature on Demand: iMR RAID 5 + SafeStore Disk Encryption . .17 1.8.1 Feature on Demand: ServeRAID RAID 6/60 Upgrade . 18 1.9 Feature on Demand: FastPath Upgrade . .19 1.10 Feature on Demand: CacheCade 2 Upgrade. .19 1.11 UEFI 2.0 Support . .20 1.12 Configuration Scenarios. .20 1.12.1 Valid Drive Mix Configurations . 21 Chapter 2: Introduction to RAID. 22 2.1 RAID Description . .22 2.2 RAID Benefits . .22 2.3 RAID Functions. .22 2.4 Components and Features . .22 2.5 Physical Array . .23 2.6 Virtual Drive . .23 2.7 RAID Drive Group . .23 2.8 Fault Tolerance . .23 2.8.1 Multipathing . 24 2.8.2 Consistency Check. 24 2.8.3 Copyback . 24 2.8.4 Background Initialization . 25 2.8.5 Patrol Read . 25 2.8.6 Disk Striping. 25 2.8.7 Disk Mirroring . 26 2.8.8 Parity.
    [Show full text]
  • Sql Server to Aurora Postgresql Migration Playbook
    Microsoft SQL Server To Amazon Aurora with Post- greSQL Compatibility Migration Playbook 1.0 Preliminary September 2018 © 2018 Amazon Web Services, Inc. or its affiliates. All rights reserved. Notices This document is provided for informational purposes only. It represents AWS’s current product offer- ings and practices as of the date of issue of this document, which are subject to change without notice. Customers are responsible for making their own independent assessment of the information in this document and any use of AWS’s products or services, each of which is provided “as is” without war- ranty of any kind, whether express or implied. This document does not create any warranties, rep- resentations, contractual commitments, conditions or assurances from AWS, its affiliates, suppliers or licensors. The responsibilities and liabilities of AWS to its customers are controlled by AWS agree- ments, and this document is not part of, nor does it modify, any agreement between AWS and its cus- tomers. - 2 - Table of Contents Introduction 9 Tables of Feature Compatibility 12 AWS Schema and Data Migration Tools 20 AWS Schema Conversion Tool (SCT) 21 Overview 21 Migrating a Database 21 SCT Action Code Index 31 Creating Tables 32 Data Types 32 Collations 33 PIVOT and UNPIVOT 33 TOP and FETCH 34 Cursors 34 Flow Control 35 Transaction Isolation 35 Stored Procedures 36 Triggers 36 MERGE 37 Query hints and plan guides 37 Full Text Search 38 Indexes 38 Partitioning 39 Backup 40 SQL Server Mail 40 SQL Server Agent 41 Service Broker 41 XML 42 Constraints
    [Show full text]
  • Modèle Bull 2009 Blanc Fr
    CNAF PostgreSQL project Philippe BEAUDOIN, Project leader [email protected] 2010, Dec 7 CNAF - Caisse Nationale des Allocations Familiales - Key organization of the French social security system - Distributes benefits to help - Families - Poor people - 123 CAF (local organizations) all over France - 11 million families and 30 million people - 69 billion € in benefits distributed (2008) 2 ©Bull, 2010 CNAF PostgreSQL project The project … in a few clicks GCOS 8 GCOS 8 CRISTAL SDP CRISTAL SDP (Cobol) (Cobol) (Cobol) (Cobol) DBSP RFM-II RHEL-LINUX PostgreSQL Bull - NovaScale 9000 servers (mainframes) 3 ©Bull, 2010 CNAF PostgreSQL project DBSP GCOS 8 CRISTAL SDP (Cobol) (Cobol) InfiniBand link LINUX S.C. S.C. (c+SQL) (c+SQL) PostgreSQL S.C. = Surrogate Client 4 ©Bull, 2010 CNAF PostgreSQL project CNAF I.S. - CRISTAL + SDP : heart of the Information System - Same application running on Bull (GCOS 8) and IBM (z/OS+DB2) mainframes - Also J2E servers, under AIX, … and a lot of peripheral applications - 6 to 8 CRISTAL versions per year ! 5 ©Bull, 2010 CNAF PostgreSQL project Migration to PostgreSQL project plan - A lot of teams all over France (developers, testers, production,...) - 3 domains - CRISTAL application, SDP application - Infrastructure and production - National project leading CRISTAL Developmt Tests 1 CAF C. Deployment C. PRODUCTION Preparation 1 Depl S SDP Develpmt Tests 9/08 1/09 5/09 9/09 12/09 3/10 6 ©Bull, 2010 CNAF PostgreSQL project How BULL participated to the project - Assistance to project leading - Technical expertise
    [Show full text]
  • On Snakes and Elephants Using Python Inside Postgresql
    On snakes and elephants Using Python inside PostgreSQL Jan Urba´nski [email protected] New Relic PyWaw Summit 2015, Warsaw, May 26 Jan Urba´nski (New Relic) On snakes and elephants PyWaw Summit 1 / 32 For those following at home Getting the slides $ wget http://wulczer.org/pywaw-summit.pdf Jan Urba´nski (New Relic) On snakes and elephants PyWaw Summit 2 / 32 1 Introduction Stored procedures PostgreSQL's specifics 2 The PL/Python language Implementation Examples 3 Using PL/Python Real-life applications Best practices Jan Urba´nski (New Relic) On snakes and elephants PyWaw Summit 3 / 32 Outline 1 Introduction Stored procedures PostgreSQL's specifics 2 The PL/Python language 3 Using PL/Python Jan Urba´nski (New Relic) On snakes and elephants PyWaw Summit 4 / 32 What are stored procedures I procedural code callable from SQL I used to implement operations that are not easily expressed in SQL I encapsulate business logic Jan Urba´nski (New Relic) On snakes and elephants PyWaw Summit 5 / 32 Stored procedure examples Calling stored procedures SELECT purge_user_records(142); SELECT lower(username) FROM users; CREATE TRIGGER notify_user_trig AFTER UPDATEON users EXECUTE PROCEDURE notify_user(); Jan Urba´nski (New Relic) On snakes and elephants PyWaw Summit 6 / 32 Stored procedure languages I most RDBMS have one blessed language in which stored procedures can we written I Oracle has PL/SQL I MS SQL Server has T-SQL I but Postgres is better Jan Urba´nski (New Relic) On snakes and elephants PyWaw Summit 7 / 32 Stored procedures in Postgres I a stored
    [Show full text]
  • Ubuntu Server Guide Basic Installation Preparing to Install
    Ubuntu Server Guide Welcome to the Ubuntu Server Guide! This site includes information on using Ubuntu Server for the latest LTS release, Ubuntu 20.04 LTS (Focal Fossa). For an offline version as well as versions for previous releases see below. Improving the Documentation If you find any errors or have suggestions for improvements to pages, please use the link at thebottomof each topic titled: “Help improve this document in the forum.” This link will take you to the Server Discourse forum for the specific page you are viewing. There you can share your comments or let us know aboutbugs with any page. PDFs and Previous Releases Below are links to the previous Ubuntu Server release server guides as well as an offline copy of the current version of this site: Ubuntu 20.04 LTS (Focal Fossa): PDF Ubuntu 18.04 LTS (Bionic Beaver): Web and PDF Ubuntu 16.04 LTS (Xenial Xerus): Web and PDF Support There are a couple of different ways that the Ubuntu Server edition is supported: commercial support and community support. The main commercial support (and development funding) is available from Canonical, Ltd. They supply reasonably- priced support contracts on a per desktop or per-server basis. For more information see the Ubuntu Advantage page. Community support is also provided by dedicated individuals and companies that wish to make Ubuntu the best distribution possible. Support is provided through multiple mailing lists, IRC channels, forums, blogs, wikis, etc. The large amount of information available can be overwhelming, but a good search engine query can usually provide an answer to your questions.
    [Show full text]
  • Mysql to Postgres Migration
    MySQL to Postgres Migration Deepak Murthy Who am I Database Administrator working at Pictage Inc The company provides online image hosting, printing and sales solutions, and services to professional photographers. The company is a leader in the industry. Pictage services more than 10,000 professional photographers all over the United States. Pictage is the photographer's one-stop and complete partner enabling online viewing, selling, and printing professional images. www.pictage.com www.shootq.com mysql2psql It can create postgresql dump file from mysql database or directly load data from mysql to postgresql (at about 100 000 records per minute). Translates most data types and indexes. Authors Max Lapshin et. al contents Installation requirements Things to take care before migration Migration of only schema Migration of only data Migration of both schema and data Excluding tables during migration Migrating only the selected tables Things to take care after migration Problems during migration Installation requirements Postgres 8.4 and above, Mysql 5.1 and above Ruby 1.9.1 – latest version Ruby gem 1.8.14 - Package manager for Ruby postgresql-server-dev-9.1 - The postgresql-devel package contains the header files and libraries ruby1.9.1-dev: This package contains the header files and library, necessary to make extension library for Ruby 1.9.1 Other libraries required: libmysqlclient-dev, libmysql- ruby1.9 Install Ruby postgresql interface (gem install pg) and Ruby mysql interface (gem install mysql) Things to take care before migration Generate a mysql2pgsql.yml file Create a separate user to access the mysql database or existing user which has capability to dump the database.
    [Show full text]
  • Drupal 8 Development Retrospective
    Drupal 8 Development Retrospective A timeline and retrospective from a core contributor 2011 2016 Drupal 7, Gates & Initiatives • DrupalCon Chicago 2011, Drupal 8 development starts immediately. • Dries discusses “gates” and initiatives to lead Drupal 8 development to improve quality and release management. • Initiatives include: Web Services, HTML5, UUID, and CMI • Followed by Multilingual, Scotch and Spark. 2011 2016 • PostgreSQL had critical bugs in 2010 during Drupal 7 RC1. Promised myself to be involved earlier in Drupal 8 development. • Woo! Drupal 7 is released, let’s go build some sites! • Entity API • Organic Groups • Views 2011 2012 2016 Symfony2 Adopted and Code Freeze Announced • DrupalCon Denver 2012: • Symfony2 adopted. • DrupalCon Munich 2012: • Code Slush and Code Freeze announced. • Moshe presents Migrate as an alternative to Upgrade Path at core conversation. 2011 2012 2016 • Began learning about Drupal 8. • Worked with Web Services initiative to bring Serializer and serialization module into Drupal 8. • Skeptical of Moshe’s Migrate proposal. • Took code freeze seriously: • Began porting contributed modules. • Began PostgreSQL bug fixes. 2011 2013 2016 Migrate adopted, Backdrop announced • Migrate is adopted as a new initiative. • Plugin API is rewritten again. • Some grow frustrated at the new OOP architecture. • Backdrop announced as alternative to compete in the smaller markets. Start dropping features from Drupal 8. 2011 2013 2016 • Became a mentor at DrupalCon Portland 2013. • PostgreSQL driver development • Blocked on a single issue for 9 months. 2011 2014 2016 Plugin, Performance, and Testing • Plugin API refactored again. • Configuration returns to database, but CMI still utilizes YAML for import/export. • DrupalCI initiative kicks into gear to improve drupal.org testing infrastructure.
    [Show full text]
  • SPI Annual Report 2015
    Software in the Public Interest, Inc. 2015 Annual Report July 12, 2016 To the membership, board and friends of Software in the Public Interest, Inc: As mandated by Article 8 of the SPI Bylaws, I respectfully submit this annual report on the activities of Software in the Public Interest, Inc. and extend my thanks to all of those who contributed to the mission of SPI in the past year. { Martin Michlmayr, SPI Secretary 1 Contents 1 President's Welcome3 2 Committee Reports4 2.1 Membership Committee.......................4 2.1.1 Statistics...........................4 3 Board Report5 3.1 Board Members............................5 3.2 Board Changes............................6 3.3 Elections................................6 4 Treasurer's Report7 4.1 Income Statement..........................7 4.2 Balance Sheet............................. 13 5 Member Project Reports 16 5.1 New Associated Projects....................... 16 5.2 Updates from Associated Projects................. 16 5.2.1 0 A.D.............................. 16 5.2.2 Chakra............................ 16 5.2.3 Debian............................. 17 5.2.4 Drizzle............................. 17 5.2.5 FFmpeg............................ 18 5.2.6 GNU TeXmacs........................ 18 5.2.7 Jenkins............................ 18 5.2.8 LibreOffice.......................... 18 5.2.9 OFTC............................. 19 5.2.10 PostgreSQL.......................... 19 5.2.11 Privoxy............................ 19 5.2.12 The Mana World....................... 19 A About SPI 21 2 Chapter 1 President's Welcome SPI continues to focus on our core services, quietly and competently supporting the activities of our associated projects. A huge thank-you to everyone, particularly our board and other key volun- teers, whose various contributions of time and attention over the last year made continued SPI operations possible! { Bdale Garbee, SPI President 3 Chapter 2 Committee Reports 2.1 Membership Committee 2.1.1 Statistics On January 1, 2015 we had 512 contributing and 501 non-contributing mem- bers.
    [Show full text]
  • Install Perl Modules
    How to install OTRS (Open Source Trouble Ticket System) on Ubuntu 16.04 Prerequisites Ubuntu 16.04. Min 2GB of Memory. Root privileges. Step 1 - Install Apache and PostgreSQL In this first step, we will install the Apache web server and PostgreSQL. We will use the latest versions from the Ubuntu repository. Login to your Ubuntu server with SSH: ssh [email protected] Update Ubuntu repository. sudo apt-get update Install Apache2 and a PostgreSQL with the apt: sudo apt-get install -y apache2 libapache2-mod-perl2 postgresql Then make sure that Apache and PostgreSQL are running by checking the server port. netstat -plntu You will see port 80 is used by apache, and port 5432 used by PostgreSQL database. Step 2 - Install Perl Modules OTRS is based on Perl, so we need to install some Perl modules that are required by OTRS. Install perl modules for OTRS with this apt command: sudo apt-get install -y libapache2-mod-perl2 libdbd-pg-perl libnet-dns-perl libnet-ldap-perl libio-socket-ssl-perl libpdf-api2-perl libsoap-lite-perl libgd-text-perl libgd-graph-perl libapache- dbi-perl libarchive-zip-perl libcrypt-eksblowfish-perl libcrypt-ssleay-perl libencode-hanextra- perl libjson-xs-perl libmail-imapclient-perl libtemplate-perl libtemplate-perl libtext-csv-xs-perl libxml-libxml-perl libxml-libxslt-perl libpdf-api2-simple-perl libyaml-libyaml-perl When the installation is finished, we need to activate the Perl module for apache, then restart the apache service. a2enmod perl systemctl restart apache2 Next, check the apache module is loaded with the command below: apachectl -M | sort And you will see perl_module under 'Loaded Modules' section.
    [Show full text]
  • Migration to Postgresql - Preparation and Methodology
    Overview Oracle to PostgreSQL Informix to PostgreSQL MySQL to PostgreSQL MSSQL to PostgreSQL Replication and/or High Availability Discussion Migration to PostgreSQL - preparation and methodology Joe Conway, Michael Meskes credativ Group September 14, 2011 Joe Conway, Michael Meskes Postgres Open 2011 Overview Oracle to PostgreSQL Presenters Informix to PostgreSQL Intro MySQL to PostgreSQL Preparation MSSQL to PostgreSQL Conversion Replication and/or High Availability Discussion Joe Conway - Open Source PostgreSQL (and Linux) user since 1999 Community member since 2000 Contributor since 2001 Commiter since 2003 PostgreSQL Features PL/R Set-returning (a.k.a. table) functions feature Improved bytea and array datatypes, index support Polymorphic argument types Multi-row VALUES list capability Original privilege introspection functions pg settings VIEW and related functions dblink, connectby(), crosstab()), generate series() Joe Conway, Michael Meskes Postgres Open 2011 Overview Oracle to PostgreSQL Presenters Informix to PostgreSQL Intro MySQL to PostgreSQL Preparation MSSQL to PostgreSQL Conversion Replication and/or High Availability Discussion Joe Conway - Business Currently President/CEO of credativ USA Previously IT Director of large company Wide variety of experience, closed and open source Full profile: http://www.linkedin.com/in/josepheconway Joe Conway, Michael Meskes Postgres Open 2011 Overview Oracle to PostgreSQL Presenters Informix to PostgreSQL Intro MySQL to PostgreSQL Preparation MSSQL to PostgreSQL Conversion Replication and/or
    [Show full text]
  • Pivotal™ Greenplum Database® Version 4.3
    PRODUCT DOCUMENTATION Pivotal™ Greenplum Database® Version 4.3 Reference Guide Rev: A10 © 2015 Pivotal Software, Inc. Copyright Reference Guide Notice Copyright Copyright © 2015 Pivotal Software, Inc. All rights reserved. Pivotal Software, Inc. believes the information in this publication is accurate as of its publication date. The information is subject to change without notice. THE INFORMATION IN THIS PUBLICATION IS PROVIDED "AS IS." PIVOTAL SOFTWARE, INC. ("Pivotal") MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WITH RESPECT TO THE INFORMATION IN THIS PUBLICATION, AND SPECIFICALLY DISCLAIMS IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Use, copying, and distribution of any Pivotal software described in this publication requires an applicable software license. All trademarks used herein are the property of Pivotal or their respective owners. Revised July 2015 (4.3.5.3) 2 Contents Reference Guide Contents Chapter 1: Preface.....................................................................................10 About This Guide.............................................................................................................................. 11 About the Greenplum Database Documentation Set........................................................................12 Document Conventions..................................................................................................................... 13 Command Syntax Conventions.............................................................................................
    [Show full text]