Tag: sql help
If you have SQL questions, we have answers – get step-by-step instructions for how to fix what ails you right here in CYBERTEC’s SQL help blog posts.
Row locks in PostgreSQL
© Laurenz Albe 2023 The PostgreSQL documentation has some information about row locks. But typically, you cannot see them in pg_locks, and not everybody knows how they work and how to track and debug row locks. This article intends to give some insight into how PostgreSQL row locks work “under the hood”. Why are there […]
PostgreSQL: DELETE vs. TRUNCATE
Data isn’t only about storage and accumulation – sometimes it’s also about deletion, cleanup and archiving. In SQL there’s more than one way to empty a table. Two essential methods are available: DELETE TRUNCATE DELETE vs. TRUNCATE Both commands serve totally different purposes, which are sometimes not fully understood. The key difference is that DELETE […]
Breaking your PostgreSQL database with bad CHECK constraints
© Laurenz Albe 2023 I am on a spree! After writing about breaking your database and transaction ID wraparound, here I am writing about damage caused by CHECK constraints! Trust me, I don’t want to destroy PostgreSQL databases. It’s just that this Twitter message was brought to my attention and triggered memories of other reports […]
Stored procedures in PostgreSQL: getting started
Stored procedures are a core concept which can be found in most relational database systems. They have proven to be useful in many areas and have been widely adopted by developers and DBA’s alike. Stored procedures basics In PostgreSQL stored procedures have been around for a number of years. The syntax of this important command […]
PostgreSQL: Finding the current timestamp
PostgreSQL provides all kinds of time-related functions. But time is not as easy as it seems. One question many people keep asking: How can I find the current timestamp? What time is it? While this seems like an easy question the answer isn’t as clear-cut as you might expect. Basically, there are three functions we […]
usql: universal psql?
usql? But why? usql is a universal command-line interface for many database. But why are we still using CLI (command line utilities) in the 21st century? And what is wrong with psql? Despite the widespread availability and ease of use of GUI-based tools, many people still prefer to use command line utilities for a number […]
Improving GROUP BY with CREATE STATISTICS
Analyzing time series often comes with challenges. This is especially true if you want to do a bit more than just run simple counts on values in a column. More often than not you’ll want to create a month’s report, a weekly overview and so on. This requires you to group on expressions rather than […]
hex, oct, bin integers in PostgreSQL 16
SQL and integer notations NEW in PostgreSQL 16 – support for non-decimal notation of integer constants! PostgreSQL already has powerful support for string constants, with E’\t’, E’\011′, E’\u0009′ and U&’\0009′ all meaning the same thing (a “horizontal tab” character). However, PostgreSQL’s support for numerical constants has – up until now – been rather limited; it […]
Faceting large result sets in PostgreSQL
While the term faceting may sound foreign to you, you almost certainly have run into it in your adventures online. It is those helpful little boxes that turn up when browsing in web shops or searching in catalogs, telling how to further narrow down the search results, (for example, by color) and how many items […]
GRANT VACUUM, ANALYZE in PostgreSQL 16
What is GRANT VACUUM, ANALYZE? PostgreSQL uses table VACUUM and ANALYZE commands to optimize the database. The VACUUM command reclaims storage space and makes it available for re-use. It also updates the visibility map, which helps the query planner to quickly identify which parts of the table have live rows. The ANALYZE command collects statistics […]
LISTEN / NOTIFY: Automatic client notification in PostgreSQL
What is LISTEN and NOTIFY? LISTEN / NOTIFY is a feature that enables users to listen to what goes on in the database. It is one of the oldest functionalities in PostgreSQL and is still widely used. The main question is: What is the purpose of the asynchronous query interface (LISTEN / NOTIFY), and what […]
PostgreSQL: Sequences vs. Invoice numbers
Sequences are a core feature of SQL. However, some users are tempted to implement sequences to generate invoices. That’s dangerous and should be avoided. The core question is: Why? What’s the problem with using database-side sequences to put unique invoice numbers to send to clients? Let’s dive in and find out. Getting started with CREATE […]