Page
1
(This page has no text content)
Page
2
Learn PostgreSQL Second Edition Use, manage, and build secure and scalable databases with PostgreSQL 16 Luca Ferrari Enrico Pirozzi BIRMINGHAM—MUMBAI
Page
3
Learn PostgreSQL Second Edition Copyright © 2023 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Senior Publishing Product Manager: Gebin George Acquisition Editor – Peer Reviews: Gaurav Gavas Project Editor: Meenakshi Vijay Content Development Editor: Elliot Dallow Copy Editor: Safis Editing Technical Editor: Kushal Sharma Proofreader: Safis Editing Indexer: Pratik Shirodkar Presentation Designer: Rajesh Shirsath Developer Relations Marketing Executive: Vignesh Raju First published: October 2020 Second edition: October 2023 Production reference: 1251023 Published by Packt Publishing Ltd. Grosvenor House 11 St Paul’s Square Birmingham B3 1RB, UK. ISBN 978-1-83763-564-1 www.packt.com
Page
4
To my beautiful wife, Emanuela; I love her like Santa loves his reindeer. To my great son, Diego, who changed our lives on 1283788200. To my parents, Miriam and Anselmo: my greatest fans since day one. – Luca Ferrari In loving memory of my father, Ilario. – Enrico Pirozzi
Page
5
Contributors About the authors Luca Ferrari has been passionate about computer science since the Commodore 64 era, and today holds a master’s degree (with honors) and a Ph.D. from the University of Modena and Reggio Emilia. He has written several research papers, technical articles, and book chapters. In 2011, he was named an adjunct professor by Nipissing University. An avid Unix user, he is a strong advo- cate of open-source, and in his free time, he collaborates on a few projects. He first encountered PostgreSQL back in the days of release 7.3; he was a founder and former president of the Italian PostgreSQL Users’ Group (ITPUG). He also talks regularly at technical conferences and events and delivers professional training. Enrico Pirozzi has been passionate about computer science since he was a 13-year-old. His first computer was a Commodore 64, and today he holds a master’s degree from the University of Bo- logna. He has participated as a speaker at national and international conferences on PostgreSQL. He first encountered PostgreSQL back in release 7.2, he was a co-founder of the first PostgreSQL Italian mailing list and the first Italian PostgreSQL website, and he talks regularly at technical conferences and events and delivers professional training. Right now, he is employed as a Post- greSQL database administrator at Zucchetti Hospitality (Zucchetti Group S.p.a).
Page
6
About the reviewers Chris Mair holds a master’s degree from the University of Trento, Italy, and has been freelance since 2003. His portfolio consists of contributions to over 25 companies, including consultancy work on database programming, performance optimization, and seamless migrations. Chris has expertise in system and network programming, data processing, ML, and more. He has a particular affinity for PostgreSQL. He has taught over 200 courses on various IT topics and is passionate about open-source software. Silvio Trancanella is a software engineer with around 12 years of experience in backend devel- opment, mainly using Java Enterprise and PostgreSQL. He has always been fascinated by database management and was immediately drawn to PostgreSQL from the very beginning of his career. He worked for about 10 years on tourism industry software, developing and maintaining critical services that relied on the PostgreSQL DBMS.
Page
7
Learn more on Discord To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below: https://discord.gg/jYWCjF6Tku
Page
8
(This page has no text content)
Page
9
(This page has no text content)
Page
10
(This page has no text content)
Page
11
(This page has no text content)
Page
12
(This page has no text content)
Page
13
(This page has no text content)
Page
14
Table of Contents xiii Chars with variable length with a limit data types • 190 Chars with a variable length without a limit data types • 191 Date/timestamp data types • 192 Date data types • 192 Timestamp data types • 195 The NoSQL data type 197 The hstore data type • 198 The JSON data type • 201 Exploring functions and languages 205 Functions • 205 SQL functions • 206 Basic functions • 206 SQL functions returning a set of elements • 207 SQL functions returning a table • 208 Polymorphic SQL functions • 210 PL/pgSQL functions • 211 First overview • 211 Dropping functions • 213 Declaring function parameters • 213 IN/OUT parameters • 214 Function volatility categories • 216 Control structure • 219 Conditional statements • 220 IF statements • 220 CASE statements • 222 Loop statements • 225 The record type • 226 Exception handling statements • 228 Security definer • 229 Summary 231 Verify your knowledge 231
Page
15
(This page has no text content)
Page
16
(This page has no text content)
Page
17
(This page has no text content)
Page
18
(This page has no text content)
Page
19
(This page has no text content)
Page
20
(This page has no text content)