YugabyteDB Engineering Update – June 27, 2019

Jimmy Guerrero

VP Developer Relations

We are pleased to announce that YugabyteDB 1.2.11 is live! You can read the release notes of this and previous versions here. This release is shipping with stored procedures support, plus 30 new features, enhancements and bug fixes. Here’s a few highlights from the 1.2.11 release.

What’s YSQL? It’s YugabyteDB’s PostgreSQL-compatible, distributed SQL API.

[#1155] Support for FUNCTION and PROCEDURE

Stored procedures allow developers to “bundle up” operations that would otherwise take several queries and round trips into a single function. Stored procedures also help minimize duplicate code, as developers can reuse existing stored procedures to perform the same actions. What’s the difference between a FUNCTION and a PROCEDURE? A function cannot execute transactions. In other words, inside a function you cannot open a new transaction, even commit or rollback the current transaction. In PostgreSQL 11, stored procedures with transactional support were introduced to remedy this problem.

Note: You’ll need to enable ysql_beta_features flag on your cluster to try out this feature.

[#870] Allow WITH clauses in CREATE TABLE

In PostgreSQL, the WITH clause is used in CREATE TABLE statements to specify storage parameters or primary keys in system tables, also known as object identifiers or OIDs. Although, PostgreSQL storage parameters aren’t relevant to tables stored in YugabyteDB because it handles data distribution differently, some standard utilities such as pgbench submit DDL statements with this clause.

Getting Started with Stored Procedures

Enable the FUNCTIONS Flag

If you haven’t already installed YugabyteDB, do so by following the first two steps in our quickstart.

Restart your cluster to enable the stored procedure feature:

./bin/yb-ctl restart --tserver_flags="ysql_beta_features=true"

Open up the YSQL Shell

Before executing any SQL, we’ll need to be in the YSQL shell. You can access it by executing:

./bin/ysqlsh --echo-queries

ysqlsh (11.2)
Type "help" for help.

postgres=#

Create an Accounts Table

CREATE TABLE accounts (
id INT GENERATED BY DEFAULT AS IDENTITY,
name VARCHAR(100) NOT NULL,
balance DEC(15,2) NOT NULL,
PRIMARY KEY(id)
);

Insert Data Into the Accounts Table

INSERT INTO accounts(name,balance) VALUES('Bob',10000);
INSERT INTO accounts(name,balance) VALUES('Alice',10000);

Verify the Data in the Accounts Table

SELECT * FROM accounts;

id | name | balance
----+-------+----------
1 | Bob | 10000.00
2 | Alice | 10000.00

(2 rows)

Create a Stored Procedure

The following example creates a stored procedure named transfer that transfers a specified amount of money from one account to another.

CREATE OR REPLACE PROCEDURE transfer(INT, INT, DEC)
LANGUAGE plpgsql
AS $
BEGIN
   -- subtracting the amount from the sender's account
   UPDATE accounts
   SET balance = balance - $3
   WHERE id = $1;

   -- adding the amount to the receiver's account
   UPDATE accounts
   SET balance = balance + $3
   WHERE id = $2;

   COMMIT;
END;
$;

Calling the Stored Procedure

Next, let’s call the transfer stored procedure and transfer $1000 from an account with id 1 (Bob) to the account with id 2 (Alice).

CALL transfer(1,2,1000);

Verify the Transfer of Money

select * from accounts;

id | name | balance
----+-------+----------
1 | Bob | 9000.00
2 | Alice | 11000.00

(2 rows)

New Documentation, Blogs and Videos

New Blogs

New Docs

Upcoming Meetups and Conferences

We will be at a number of conferences and meetups over the next few months, below are some select few with the highlights. Do stop by, say hello and ask us any questions you have.

Get Started

Ready to start exploring YugabyteDB? Getting up and running locally with a three node cluster on your laptop is fast. Just download, extract, create the cluster, check its status and your ready to go!

install

What’s Next?

  • Using YugabyteDB at your company? Tell us about it and we’ll send you a hoodie!
  • Compare YugabyteDB in depth to databases like CockroachDB, Google Cloud Spanner and MongoDB.
  • Get started with YugabyteDB on macOS, Linux, Docker, and Kubernetes.
  • Contact us to learn more about licensing, pricing or to schedule a technical overview.
Jimmy Guerrero

VP Developer Relations

Related Posts

Explore Distributed SQL and YugabyteDB in Depth

Discover the future of data management.
Learn at Yugabyte University
Get Started
Browse Yugabyte Docs
Explore docs
PostgreSQL For Cloud Native World
Read for Free