---
title: "A way to build Postgres extensions in Rust"
publication: "Postgres Weekly"
issue: 364
published_at: "2020-07-15"
canonical_url: "https://postgresweekly.com/issues/364"
---

Plus a Postgres bug from 1997, a Postgres coin, and a fresh tip of the week.       

#364 — July 15, 2020 

[Web Version](https://postgresweekly.com/issues/364)   

Postgres Weekly 

[![\](https://res.cloudinary.com/cpress/image/upload/w_1280,e_sharpen:60/v1594744377/p6foxmu22977srw8jh18.png)](https://github.com/zombodb/pgx)  

[PGX: Build Postgres Extensions with Rust](https://github.com/zombodb/pgx) — A framework for building Postgres extensions with _Rust_ rather than the C or C++ you might be more used to. Why is this a good thing? Rust is becoming pretty popular in more cutting edge circles lately and the language’s safety features make it a compelling choice. 

ZomboDB   

[JSONB: A Container of Types](https://momjian.us/main/blogs/pgblog/2020.html#July_13_2020) — Bruce reminds us that JSONB columns don’t just have to hold collections like arrays or maps/hashes, you can directly store strings, numbers and booleans too. 

Bruce Momjian   [![\](https://copm.s3.amazonaws.com/52bf73b0.png)](https://resources.2ndquadrant.com/highly-available-postgresql-clusters-whitepaper?utm_campaign=Highly%20available%20PostgreSQL%20clusters&utm_source=CooperPress_HighlyAvailablePostgresClustersWhitepaper-M_July15) 

[Achieve High Availability with PostgreSQL [Whitepaper\]](https://resources.2ndquadrant.com/highly-available-postgresql-clusters-whitepaper?utm_campaign=Highly%20available%20PostgreSQL%20clusters&utm_source=CooperPress_HighlyAvailablePostgresClustersWhitepaper-M_July15) — Critical business applications require availability of their backend database cluster. Discover how to setup & deploy enterprise-grade High Availability of up to four 9s. Learn the features and benefits in the Highly Available Postgres Clusters Whitepaper. 

2ndQuadrant PostgreSQL Products sponsor   

[How to Find a Postgres Bug Born in 1997](https://www.highgo.ca/2020/07/11/a-simple-way-to-trace-a-bug/) — The tale of discovering a bug hidden away inside Postgres’s documentation or `circle` type (depending on your point of view) for the past 23 years. 

David Zhang     

**⚡️ Quick bytes:** 

- DigitalOcean's managed database service [now supports Postgres 12.](https://www.digitalocean.com/blog/managed-databases-now-supports-postgresql-12/)
- Luca Ferrari [received a Postgres 12 commemorative coin](https://fluca1978.github.io/2020/07/09/PostgreSQL12Coin.html) in the mail from PG-US.
- Luca also wants to [encourage you to test out Postgres 13 beta 2](https://fluca1978.github.io/2020/07/13/PostgreSQL13BetaPgenv.html).
- PGConf.EU 2020 [has been cancelled.](https://www.postgresql.eu/events/pgconfeu2020/news/pgconfeu-2020-conference-canceled-42/) Roll on 2021.     

[How to SCRAM with pgBouncer](https://info.crunchydata.com/blog/pgbouncer-scram-authentication-postgresql) — Not only can you see how [pgBouncer](https://www.pgbouncer.org/) (the connection pooler) uses SCRAM authentication but also _what_ SCRAM authentication is and how it works. 

Jonathan S. Katz   

[What Are Postgres Templates?](https://supabase.io/blog/2020/07/09/postgresql-templates/) — We’ve covered this in one of our ‘tips of the week’ before, but if you’re unaware of what template databases are and how they’re used, this is a quick primer. 

Angelico de los Reyes   

[Foreign Data Wrappers: Postgres's Secret Weapon?](https://www.splitgraph.com/blog/foreign-data-wrappers) — Foreign data wrappers (often called FDWs, for short) lets you query remote databases directly from your PostgreSQL instance and here’s how that works alongside Splitgraph, a toolkit for building and working with “data images”. 

Artjoms Iškovs (Splitgraph)   

[Distributed SQL Beats Polyglot Persistence for Building Microservices](https://bit.ly/304fwAa) — Polyglot persistence is the silent killer of release agility; distributed SQL is the answer for today’s multicloud era. 

YugabyteDB sponsor   

[PgTyped: Typesafe SQL in TypeScript](https://github.com/adelsz/pgtyped) — Use raw SQL in TypeScript with guaranteed type-safety. 

Adel Salakh   

[pgsodium: A Postgres Extension for Using `libsodium`](https://github.com/michelp/pgsodium) — [libsodium](https://github.com/jedisct1/libsodium) is a crypto library for encryption, decryption, signatures, password hashing and more. We first linked when it was in alpha but it’s now 1.0 :-) 

Michel Pelletier      supported by [![PopSQL\](https://res.cloudinary.com/cpress/image/upload/v1594299945/m0snjqfahpmeztbh7ilv.png)](https://popsql.com/?utm_source=pgweekly&utm_campaign=15july)  

💡**Tip of the Week** 

`RANK` and `DENSE_RANK` for ranking orders 

Let's say we've been taking votes of people's favorite database systems. Here's our schema:  

CREATE TABLE databases (name TEXT, votes INT);
 INSERT INTO databases VALUES 
 ('Postgres', 10000),
 ('MySQL', 4522),
 ('SQLite', 9500),
 ('MongoDB', 4522),
 ('Oracle', 4580),
 ('Redis', 9500);   

We want to get the results in order along with a rank of where each database stands:  

SELECT row_number() OVER (ORDER by votes DESC), * FROM databases; 

![](https://res.cloudinary.com/cpress/image/upload/w_1280,e_sharpen:60,g_north,h_410,c_crop/cxvt9iro8cn0hlby7ey8.png)   

It doesn't seem very fair to me that Redis gets ranked #3 despite having the same number of votes as SQLite! Ranks in the real world often take note of equivalent/tied scores and give the same rank to similarly scored items. 

Enter `RANK` and `DENSE_RANK`, two of [several window functions](https://www.postgresql.org/docs/12/functions-window.html):  

SELECT DENSE_RANK() OVER (ORDER by votes DESC), * FROM databases;
 SELECT RANK() OVER (ORDER by votes DESC), * FROM databases; 

![](https://res.cloudinary.com/cpress/image/upload/w_1280,e_sharpen:60/v1594652204/y9ccymacboucz3zeyc5m.png)   

The difference is subtle but `RANK` gives us a more traditional rank approach where if there are two second place entries, there's no longer a third place entry. `DENSE_RANK`, however, ensures there are continguous rankings even if there are duplicates. 

Try out this tip in [PopSQL](https://popsql.com/?utm_source=pgweekly&utm_campaign=15july). PopSQL is a SQL editor built for teams. Think “_Figma for SQL_”. A free Premium trial awaits Postgres Weekly readers (no credit card required).          

Curated by [Cooperpress](https://cooperpress.com/), [Peter Cooper](https://twitter.com/peterc), and [Craig Kerstiens](https://twitter.com/craigkerstiens). 

[Change your email address](https://postgresweekly.com/edit_subscription/*|UID|*) or[Stop getting this newsletter](https://postgresweekly.com/leave/*|UID|*) 

© Cooper Press Ltd · Fairfield Enterprise Centre, Louth, LN11 0LS, United Kingdom
