2ndQuadrant is now part of EDB

Bringing together some of the world's top PostgreSQL experts.

2ndQuadrant | PostgreSQL
Mission Critical Databases
  • Contact us
  • Support & Services
    • Support
      • 24/7 PostgreSQL Support
      • Developer Support
    • DBA Services
      • Remote DBA
      • Database Monitoring
    • Consulting Services
      • Health Check
      • Performance Tuning
      • Database Security Audit
      • PostgreSQL Upgrade
    • Migration Services
      • Migrate to PostgreSQL
      • Migration Assessment
  • Products
    • Postgres-BDR ®
    • PostgreSQL High Availability
    • Kubernetes Operators for BDR & PostgreSQL
    • Managed PostgreSQL in the Cloud
    • Installers
      • Postgres Installer
      • 2UDA
    • 2ndQPostgres
    • pglogical
    • Barman
    • repmgr
    • OmniDB
    • SQL Firewall
    • Postgres-XL
  • Downloads
    • Installers
      • Postgres Installer
      • 2UDA – Unified Data Analytics
    • Whitepapers
      • Business Case for PostgreSQL Support
      • AlwaysOn Postgres
      • PostgreSQL with High Availability
      • Security Best Practices
      • BDR
    • Case Studies
      • Performance Tuning
        • BenchPrep
        • tastyworks
      • Distributed Clusters
        • ClickUp
        • European Space Agency (ESA)
        • Telefónica del Sur
        • Animal Logic
      • Database Administration
        • Agilis Systems
      • Professional Training
        • Met Office
        • London & Partners
      • Database Upgrades
        • Alfred Wegener Institute (AWI)
      • Database Migration
        • International Game Technology (IGT)
        • Healthcare Software Solutions (HSS)
        • Navionics
  • Postgres Learning Center
    • Webinars
      • Upcoming Webinars
      • Webinar Library
    • Whitepapers
      • Business Case for PostgreSQL Support
      • AlwaysOn Postgres
      • PostgreSQL with High Availability
      • Security Best Practices
      • BDR
    • Blog
    • Training
      • Course Catalogue
    • Case Studies
      • Performance Tuning
        • BenchPrep
        • tastyworks
      • Distributed Clusters
        • ClickUp
        • European Space Agency (ESA)
        • Telefónica del Sur
        • Animal Logic
      • Database Administration
        • Agilis Systems
      • Professional Training
        • Met Office
        • London & Partners
      • Database Upgrades
        • Alfred Wegener Institute (AWI)
      • Database Migration
        • International Game Technology (IGT)
        • Healthcare Software Solutions (HSS)
        • Navionics
    • Books
      • PostgreSQL 11 Administration Cookbook
      • PostgreSQL 10 Administration Cookbook
      • PostgreSQL High Availability Cookbook – 2nd Edition
      • PostgreSQL 9 Administration Cookbook – 3rd Edition
      • PostgreSQL Server Programming Cookbook – 2nd Edition
      • PostgreSQL 9 Cookbook – Chinese Edition
    • Videos
    • Events
    • PostgreSQL
      • PostgreSQL – History
      • Who uses PostgreSQL?
      • PostgreSQL FAQ
      • PostgreSQL vs MySQL
      • The Business Case for PostgreSQL
      • Security Information
      • Documentation
  • About Us
    • About 2ndQuadrant
    • What Does “2ndQuadrant” Mean?
    • 2ndQuadrant’s Passion for PostgreSQL
    • News
    • Careers
    • Team Profile
  • Blog
  • Menu Menu
You are here: Home1 / Blog2 / Umair's PlanetPostgreSQL3 / PostgreSQL 11 – Server-side Procedures (Part 1)
Umair Shahid

PostgreSQL 11 – Server-side Procedures (Part 1)

December 7, 2017/12 Comments/in Umair's PlanetPostgreSQL /by Umair Shahid

Last week marked a rather big step in the PostgreSQL world that went largely unnoticed. Thanks to the work done by 2ndQuadrant contributors, we now have the ability to write Stored Procedures in PostgreSQL!

(Ok, well not exactly now but we will have the ability once PostgreSQL 11 comes out)

A procedure is essentially a set of commands to be executed in a particular order. As opposed to functions, procedures are not required to return a value. With this addition, you can now invoke a procedure by simply using the new CALL statement rather than using SELECT. The implementation is fully compliant with the SQL standard and will allow users to write procedures that are somewhat compatible with DB2, MySQL, and to a lesser extent, Oracle.

The commit from last week adds new commands CALL, CREATE/ALTER/DROP PROCEDURE, as well as ALTER/DROP ROUTINE that can refer to either a function or a procedure (or an aggregate function, as an extension to SQL). It also includes support for procedures in various utility commands such as COMMENT and GRANT, as well as support in pg_dump and psql. Support for defining procedures is available in all the languages supplied by the core distribution.

While this commit simply adds structure that is built upon existing functionality, it lays down the foundation on which we will be building the real meat to be made available in PostgreSQL 11. Next steps include the implementation of:

  • Transaction control – allowing us to COMMIT and ROLLBACK inside procedures
  • Returning multiple result sets

Part 2 here: https://www.2ndquadrant.com/postgresql-11-server-side-procedures-part-2/


Tags: PostgreSQL, PostgreSQL 11, PostgreSQL 11 New Features, PostgreSQL11
Share this entry
  • Share on Facebook
  • Share on Twitter
  • Share on WhatsApp
  • Share on LinkedIn
12 replies
  1. Thor
    Thor says:
    December 7, 2017 at 1:36 pm

    Ok, I feel a little dumbfounded here.

    A procedure is basically a function with no return value – a function where the return value simply is ignored.

    function blah1(var x): aType
    return aType

    procedure blah2(var x): aType
    return

    blah1(“x”)
    blah2(“x”)

    no difference.

    Also – next step: return multiple result sets? So… then you’ve taken the procedure that you made from a retvar-less function and made it a function again? Uh…

    And… we can commit and rollback inside functions, right?

    I really fail to see how it can be so complicated to substitute “create procedure blah( varlist )” with “create function blah( varlist ): void” – simply allow a return *nothing* inside a function with undefined return value and call it a day?

    Reply
    • Simon Riggs
      Simon Riggs says:
      December 7, 2017 at 5:59 pm

      No, we can’t commit and rollback inside functions – they operate within a transaction. I’m guessing you’re not a Postgres user?

      Procedures will allow us to operate across transactions, one of the things that is new and cool.

      Reply
  2. Dong Jiang
    Dong Jiang says:
    December 7, 2017 at 6:20 pm

    autonomous transaction possible?
    https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/autonotransaction_pragma.htm

    Reply
    • Umair Shahid
      Umair Shahid says:
      December 7, 2017 at 6:40 pm

      This might help: https://wiki.postgresql.org/wiki/Autonomous_subtransactions

      Reply
  3. Merlin Moncure
    Merlin Moncure says:
    December 7, 2017 at 9:43 pm

    Functions:
    *) can be called inside a query (select func() from foo)
    *) generally return a result
    *) must return a single set
    *) are scoped to a transaction

    Procedures:
    *) can not be called inside a query
    *) typically don’t return results except for maybe error code
    *) can return multiple result sets (although FWICT the postgres implementation does not allow for this yet)
    *) can flush the transaction (essentially a COMMIT; followed by a BEGIN;) within the proceure

    Being able to flush the transaction is by far the most important part; it allows for various kinds of things that are dangerous or impossible with functions (for example, a routine that never terminates).

    Reply
  4. Ravi Krishna
    Ravi Krishna says:
    December 8, 2017 at 12:48 am

    Does this mean that this will unlike pl/pgsql where are basically extensions. Will the new one be part of core engine. What syntax would it follow? Like pl/pgsql or something new.

    thanks.

    Reply
    • Umair Shahid
      Umair Shahid says:
      December 8, 2017 at 6:37 am

      If you are asking about the procedure syntax, yes it will be part of the core engine. Please see the blog to understand the new keywords and syntax inserted as part of this commit.

      Reply
  5. sam
    sam says:
    December 8, 2017 at 11:48 am

    astounding feature!

    Reply
  6. Artur
    Artur says:
    December 9, 2017 at 12:30 pm

    What about CTE?

    Reply
    • Umair Shahid
      Umair Shahid says:
      December 11, 2017 at 7:29 am

      Are you referring to Common Table Expressions? If yes, then that’s a different topic unrelated to Procedures.

      Reply
  7. Nathan
    Nathan says:
    January 16, 2018 at 7:02 pm

    Looks good, will you have to extend the protocol to handle multiple return sets? IIRC, it currently can’t handle multiple return sets.

    Reply
    • Umair Shahid
      Umair Shahid says:
      January 17, 2018 at 10:44 am

      Yes, that’s part of the roadmap.

      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Get in touch with us!

Recent Posts

  • Random Data December 3, 2020
  • Webinar: COMMIT Without Fear – The Beauty of CAMO [Follow Up] November 13, 2020
  • Full-text search since PostgreSQL 8.3 November 5, 2020
  • Random numbers November 3, 2020
  • Webinar: Best Practices for Bulk Data Loading in PostgreSQL [Follow Up] November 2, 2020

Featured External Blogs

Tomas Vondra's Blog

Our Bloggers

  • Simon Riggs
  • Alvaro Herrera
  • Andrew Dunstan
  • Craig Ringer
  • Francesco Canovai
  • Gabriele Bartolini
  • Giulio Calacoci
  • Ian Barwick
  • Marco Nenciarini
  • Mark Wong
  • Pavan Deolasee
  • Petr Jelinek
  • Shaun Thomas
  • Tomas Vondra
  • Umair Shahid

PostgreSQL Cloud

2QLovesPG 2UDA 9.6 backup Barman BDR Business Continuity community conference database DBA development devops disaster recovery greenplum Hot Standby JSON JSONB logical replication monitoring OmniDB open source Orange performance PG12 pgbarman pglogical PG Phriday postgres Postgres-BDR postgres-xl PostgreSQL PostgreSQL 9.6 PostgreSQL10 PostgreSQL11 PostgreSQL 11 PostgreSQL 11 New Features postgresql repmgr Recovery replication security sql wal webinar webinars

Support & Services

24/7 Production Support

Developer Support

Remote DBA for PostgreSQL

PostgreSQL Database Monitoring

PostgreSQL Health Check

PostgreSQL Performance Tuning

Database Security Audit

Upgrade PostgreSQL

PostgreSQL Migration Assessment

Migrate from Oracle to PostgreSQL

Products

HA Postgres Clusters

Postgres-BDR®

2ndQPostgres

pglogical

repmgr

Barman

Postgres Cloud Manager

SQL Firewall

Postgres-XL

OmniDB

Postgres Installer

2UDA

Postgres Learning Center

Introducing Postgres

Blog

Webinars

Books

Videos

Training

Case Studies

Events

About Us

About 2ndQuadrant

What does 2ndQuadrant Mean?

News

Careers 

Team Profile

© 2ndQuadrant Ltd. All rights reserved. | Privacy Policy
  • Twitter
  • LinkedIn
  • Facebook
  • Youtube
  • Mail
pgxc_ctl: Teaching Postgres-XL in New York City PG Phriday: Getting RAD with Docker [Part 4]
Scroll to top
×