100% found this document useful (1 vote)
15 views

(Ebook) Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-Centric Applications with PL/pgSQL by Baji Shaik, Dinesh Kumar Chemuduru ISBN 9781484298398, 148429839X, B0CHLBQ323 - The ebook is ready for download, no waiting required

The document provides information about various eBooks available for instant download at ebooknice.com, including titles related to PostgreSQL programming and other subjects. It features details such as authors, ISBN numbers, and links for purchasing or downloading the eBooks. Additionally, it includes a dedication section and a table of contents for the book 'Procedural Programming with PostgreSQL' by Baji Shaik and Dinesh Kumar Chemuduru.

Uploaded by

neeruturve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
15 views

(Ebook) Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-Centric Applications with PL/pgSQL by Baji Shaik, Dinesh Kumar Chemuduru ISBN 9781484298398, 148429839X, B0CHLBQ323 - The ebook is ready for download, no waiting required

The document provides information about various eBooks available for instant download at ebooknice.com, including titles related to PostgreSQL programming and other subjects. It features details such as authors, ISBN numbers, and links for purchasing or downloading the eBooks. Additionally, it includes a dedication section and a table of contents for the book 'Procedural Programming with PostgreSQL' by Baji Shaik and Dinesh Kumar Chemuduru.

Uploaded by

neeruturve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

Instant Ebook Access, One Click Away – Begin at ebooknice.

com

(Ebook) Procedural Programming with PostgreSQL


PL/pgSQL: Design Complex Database-Centric
Applications with PL/pgSQL by Baji Shaik, Dinesh
Kumar Chemuduru ISBN 9781484298398, 148429839X,
B0CHLBQ323
https://ebooknice.com/product/procedural-programming-with-
postgresql-pl-pgsql-design-complex-database-centric-
applications-with-pl-pgsql-53467660

OR CLICK BUTTON

DOWLOAD EBOOK

Get Instant Ebook Downloads – Browse at https://ebooknice.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

(Ebook) Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-


Centric Applications with PL/pgSQL by Baji Shaik & Dinesh Kumar Chemuduru ISBN
9781484298404, 1484298403

https://ebooknice.com/product/procedural-programming-with-postgresql-pl-pgsql-
design-complex-database-centric-applications-with-pl-pgsql-55758230

ebooknice.com

(Ebook) Biota Grow 2C gather 2C cook by Loucas, Jason; Viles, James ISBN
9781459699816, 9781743365571, 9781925268492, 1459699815, 1743365578, 1925268497

https://ebooknice.com/product/biota-grow-2c-gather-2c-cook-6661374

ebooknice.com

(Ebook) Matematik 5000+ Kurs 2c Lärobok by Lena Alfredsson, Hans Heikne, Sanna
Bodemyr ISBN 9789127456600, 9127456609

https://ebooknice.com/product/matematik-5000-kurs-2c-larobok-23848312

ebooknice.com

(Ebook) PostgreSQL Development Essentials by Kaur, Manpreet, Shaik, Baji ISBN


9781783989003, 1783989009

https://ebooknice.com/product/postgresql-development-essentials-11861554

ebooknice.com
(Ebook) Beginning PostgreSQL on the Cloud: Simplifying Database as a Service on
Cloud Platforms by Baji Shaik, Avinash Vallarapu ISBN 9781484234464, 1484234464

https://ebooknice.com/product/beginning-postgresql-on-the-cloud-simplifying-
database-as-a-service-on-cloud-platforms-6988088

ebooknice.com

(Ebook) SAT II Success MATH 1C and 2C 2002 (Peterson's SAT II Success) by Peterson's
ISBN 9780768906677, 0768906679

https://ebooknice.com/product/sat-ii-success-math-1c-and-2c-2002-peterson-s-sat-
ii-success-1722018

ebooknice.com

(Ebook) PostgreSQL Configuration: Best Practices for Performance and Security by


Baji Shaik ISBN 9781484256633, 1484256638

https://ebooknice.com/product/postgresql-configuration-best-practices-for-
performance-and-security-46148980

ebooknice.com

(Ebook) PostgreSQL Configuration: Best Practices for Performance and Security by


Baji Shaik ISBN 9781484256626, 148425662X

https://ebooknice.com/product/postgresql-configuration-best-practices-for-
performance-and-security-46659076

ebooknice.com

(Ebook) Master SAT II Math 1c and 2c 4th ed (Arco Master the SAT Subject Test: Math
Levels 1 & 2) by Arco ISBN 9780768923049, 0768923042

https://ebooknice.com/product/master-sat-ii-math-1c-and-2c-4th-ed-arco-master-
the-sat-subject-test-math-levels-1-2-2326094

ebooknice.com
Q L
S
E
AG
NGU
Y LA
ER
D QU
RE
TU
RUC
ST

B
TA

Procedural Programming DA

with PostgreSQL DA
TAB
A SE

DA
T

PL/pgSQL
Design Complex Database-Centric
Applications with PL/pgSQL

Baji Shaik
Dinesh Kumar Chemuduru
Procedural Programming
with PostgreSQL
PL/pgSQL
Design Complex Database-Centric
Applications with PL/pgSQL

Baji Shaik
Dinesh Kumar Chemuduru
Procedural Programming with PostgreSQL PL/pgSQL: Design Complex
Database-­Centric Applications with PL/pgSQL
Baji Shaik Dinesh Kumar Chemuduru
Texas, TX, USA Andhra Pradesh, India

ISBN-13 (pbk): 978-1-4842-9839-8 ISBN-13 (electronic): 978-1-4842-9840-4


https://doi.org/10.1007/978-1-4842-9840-4

Copyright © 2023 by Baji Shaik and Dinesh Kumar Chemuduru


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Divya Modi
Development Editor: James Markham
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004,
U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit
www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer
Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint,
paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available
to readers on GitHub (https://github.com/Apress). For more detailed information, please visit
https://www.apress.com/gp/services/source-code.
Paper in this product is recyclable
I extend this dedication to Afrah Razzak, my exceptional wife.
Her enduring support and remarkable patience during the extended
writing sessions have been invaluable to me.
—Baji Shaik

I lovingly extend this dedication to my dear friend, Baji Shaik.


Your unwavering support and encouragement have been my guiding
light, especially in the most challenging moments. Your belief in me has
been a constant source of inspiration, and I am grateful for your
presence in my journey. This book is as much a tribute to our
friendship as it is a testament to the power of steadfast camaraderie.
Thank you for always being there.
—Dinesh Kumar Chemuduru
Table of Contents
About the Authors�������������������������������������������������������������������������������������������������� xiii

About the Technical Reviewer���������������������������������������������������������������������������������xv

Acknowledgments�������������������������������������������������������������������������������������������������xvii
Introduction������������������������������������������������������������������������������������������������������������xix

Chapter 1: Introduction to PL/pgSQL������������������������������������������������������������������������ 1


A Closer Look at PL/pgSQL����������������������������������������������������������������������������������������������������������� 1
PL/pgSQL Installation�������������������������������������������������������������������������������������������������������������������� 2
PL/pgSQL Execution Flow������������������������������������������������������������������������������������������������������������� 4
PL/pgSQL Blocks��������������������������������������������������������������������������������������������������������������������������� 6
Anonymous or Unnamed Blocks���������������������������������������������������������������������������������������������� 6
Named Blocks����������������������������������������������������������������������������������������������������������������������������� 10
Summary������������������������������������������������������������������������������������������������������������������������������������ 12
What’s Next��������������������������������������������������������������������������������������������������������������������������������� 12

Chapter 2: PL/pgSQL Variables������������������������������������������������������������������������������� 13


What Are Variables in PL/pgSQL?������������������������������������������������������������������������������������������������ 13
Declaring Variables��������������������������������������������������������������������������������������������������������������������� 13
Variable Scope���������������������������������������������������������������������������������������������������������������������������� 15
Constant Variables���������������������������������������������������������������������������������������������������������������������� 17
Variable Alias������������������������������������������������������������������������������������������������������������������������������ 18
Scalar Variables��������������������������������������������������������������������������������������������������������������������� 19
Array Variables���������������������������������������������������������������������������������������������������������������������� 21
Record Variables�������������������������������������������������������������������������������������������������������������������� 22
Cursor Variables�������������������������������������������������������������������������������������������������������������������� 23

v
Table of Contents

Summary������������������������������������������������������������������������������������������������������������������������������������ 25
What’s Next��������������������������������������������������������������������������������������������������������������������������������� 25

Chapter 3: PL/pgSQL Data Types���������������������������������������������������������������������������� 27


Data Types����������������������������������������������������������������������������������������������������������������������������������� 27
Declaring Variables with Data Types������������������������������������������������������������������������������������������� 28
Supported Types������������������������������������������������������������������������������������������������������������������������� 30
Base Type������������������������������������������������������������������������������������������������������������������������������������ 32
Composite Type��������������������������������������������������������������������������������������������������������������������������� 33
Domain Type�������������������������������������������������������������������������������������������������������������������������������� 35
Pseudo-Type������������������������������������������������������������������������������������������������������������������������������� 37
Range Type���������������������������������������������������������������������������������������������������������������������������������� 38
Multirange Types������������������������������������������������������������������������������������������������������������������������� 40
Summary������������������������������������������������������������������������������������������������������������������������������������ 41
What’s Next��������������������������������������������������������������������������������������������������������������������������������� 41

Chapter 4: Dealing with Strings, Numbers, and Arrays������������������������������������������ 43


Strings����������������������������������������������������������������������������������������������������������������������������������������� 43
Function Format�������������������������������������������������������������������������������������������������������������������� 45
Dealing with Null String��������������������������������������������������������������������������������������������������������� 47
Numbers������������������������������������������������������������������������������������������������������������������������������������� 50
Arrays������������������������������������������������������������������������������������������������������������������������������������������ 53
Example Use Cases��������������������������������������������������������������������������������������������������������������������� 54
Strings����������������������������������������������������������������������������������������������������������������������������������������� 54
Numbers�������������������������������������������������������������������������������������������������������������������������������� 55
Arrays������������������������������������������������������������������������������������������������������������������������������������ 57
Summary������������������������������������������������������������������������������������������������������������������������������������ 58
What’s Next��������������������������������������������������������������������������������������������������������������������������������� 59

Chapter 5: Control Statements������������������������������������������������������������������������������� 61


IF/ELSE Statement���������������������������������������������������������������������������������������������������������������������� 62
Cascading IF Statements������������������������������������������������������������������������������������������������������� 65
CASE Statement�������������������������������������������������������������������������������������������������������������������������� 66
vi
Table of Contents

Iterative Statement��������������������������������������������������������������������������������������������������������������������� 68
LOOP Statement�������������������������������������������������������������������������������������������������������������������� 69
WHILE Statement������������������������������������������������������������������������������������������������������������������������ 72
FOR Statement���������������������������������������������������������������������������������������������������������������������������� 74
Example Use Cases��������������������������������������������������������������������������������������������������������������������� 78
Example 1������������������������������������������������������������������������������������������������������������������������������ 78
Example 2������������������������������������������������������������������������������������������������������������������������������ 81
Best Practices of Using Control Statements in PL/pgSQL����������������������������������������������������� 84
Summary������������������������������������������������������������������������������������������������������������������������������������ 85
What’s Next��������������������������������������������������������������������������������������������������������������������������������� 85

Chapter 6: Handling Arrays������������������������������������������������������������������������������������ 87


Array Index���������������������������������������������������������������������������������������������������������������������������������� 88
Array Length������������������������������������������������������������������������������������������������������������������������������� 89
Iterate Array�������������������������������������������������������������������������������������������������������������������������������� 91
Find Duplicate Elements in Array������������������������������������������������������������������������������������������������ 92
Append Elements to Array����������������������������������������������������������������������������������������������������������� 93
Array Merge�������������������������������������������������������������������������������������������������������������������������������� 94
Multidimensional Arrays������������������������������������������������������������������������������������������������������������� 94
Summary������������������������������������������������������������������������������������������������������������������������������������ 96
What’s Next��������������������������������������������������������������������������������������������������������������������������������� 96

Chapter 7: Handling JSON�������������������������������������������������������������������������������������� 97


What Is JSON?���������������������������������������������������������������������������������������������������������������������������� 97
Use Cases��������������������������������������������������������������������������������������������������������������������������������� 100
Advantages and Disadvantages������������������������������������������������������������������������������������������������ 104
Build PL/pgSQL Functions for JSON������������������������������������������������������������������������������������������ 105
Indexing JSON Data������������������������������������������������������������������������������������������������������������������ 109
Other Useful JSON Functions���������������������������������������������������������������������������������������������������� 111
Summary���������������������������������������������������������������������������������������������������������������������������������� 111
What’s Next������������������������������������������������������������������������������������������������������������������������������� 112

vii
Table of Contents

Chapter 8: Cursors������������������������������������������������������������������������������������������������ 113


What Are Cursors?�������������������������������������������������������������������������������������������������������������������� 113
CURSOR Attributes��������������������������������������������������������������������������������������������������������������� 115
ISOPEN Attribute������������������������������������������������������������������������������������������������������������������ 115
FOUND Attribute������������������������������������������������������������������������������������������������������������������� 117
NOTFOUND Attribute������������������������������������������������������������������������������������������������������������ 119
ROWCOUNT Attribute����������������������������������������������������������������������������������������������������������� 120
Monitor Cursors������������������������������������������������������������������������������������������������������������������� 122
SCROLL Cursor�������������������������������������������������������������������������������������������������������������������� 123
NO SCROLL Cursor��������������������������������������������������������������������������������������������������������������� 125
WITH HOLD Cursors������������������������������������������������������������������������������������������������������������� 127
Refcursors��������������������������������������������������������������������������������������������������������������������������� 128
Summary���������������������������������������������������������������������������������������������������������������������������������� 130
What’s Next������������������������������������������������������������������������������������������������������������������������������� 130

Chapter 9: Custom Operators������������������������������������������������������������������������������� 131


Built-In Operators���������������������������������������������������������������������������������������������������������������������� 131
Creating a Custom Operator������������������������������������������������������������������������������������������������������ 135
Simple Example������������������������������������������������������������������������������������������������������������������� 136
SCENARIO 1: Case-Insensitive Comparison������������������������������������������������������������������������� 140
SCENARIO 2: Custom Data Type Math��������������������������������������������������������������������������������� 142
SCENARIO 3: Date Differentiate Operator���������������������������������������������������������������������������� 144
SCENARIO 4: Custom Operator for Data Classification�������������������������������������������������������� 146
Advantages������������������������������������������������������������������������������������������������������������������������������� 148
Disadvantages�������������������������������������������������������������������������������������������������������������������������� 149
Summary���������������������������������������������������������������������������������������������������������������������������������� 149
What’s Next������������������������������������������������������������������������������������������������������������������������������� 149

Chapter 10: Custom Casting��������������������������������������������������������������������������������� 151


Built-In Casts���������������������������������������������������������������������������������������������������������������������������� 151
Custom Casts���������������������������������������������������������������������������������������������������������������������������� 160

viii
Table of Contents

Creating a Custom Cast������������������������������������������������������������������������������������������������������� 160


Simple Example������������������������������������������������������������������������������������������������������������������� 161
SCENARIO 1: Converting Custom Data Types����������������������������������������������������������������������� 163
SCENARIO 2: Custom Data Type to JSONB��������������������������������������������������������������������������� 165
Summary���������������������������������������������������������������������������������������������������������������������������������� 168
What’s Next������������������������������������������������������������������������������������������������������������������������������� 168

Chapter 11: Dynamic SQL������������������������������������������������������������������������������������� 169


What Is Dynamic SQL?�������������������������������������������������������������������������������������������������������������� 169
Syntax of Dynamic SQL in PL/pgSQL����������������������������������������������������������������������������������� 169
Simple Example������������������������������������������������������������������������������������������������������������������� 170
Use Cases of Dynamic SQL�������������������������������������������������������������������������������������������������� 171
Best Practices and Considerations for Dynamic SQL���������������������������������������������������������������� 178
1. Preventing SQL Injection������������������������������������������������������������������������������������������������� 179
2. Sanitizing and Validating Inputs�������������������������������������������������������������������������������������� 179
3. Security Concerns����������������������������������������������������������������������������������������������������������� 180
4. Performance Optimization����������������������������������������������������������������������������������������������� 180
Summary���������������������������������������������������������������������������������������������������������������������������������� 181
What’s Next������������������������������������������������������������������������������������������������������������������������������� 181

Chapter 12: Building Functions and Procedures�������������������������������������������������� 183


Functions���������������������������������������������������������������������������������������������������������������������������������� 183
Defining Functions��������������������������������������������������������������������������������������������������������������� 184
Calling Functions����������������������������������������������������������������������������������������������������������������� 184
Categories��������������������������������������������������������������������������������������������������������������������������������� 185
Immutable Functions���������������������������������������������������������������������������������������������������������������� 186
STABLE Functions��������������������������������������������������������������������������������������������������������������������� 188
VOLATILE Functions������������������������������������������������������������������������������������������������������������������� 191
Procedures�������������������������������������������������������������������������������������������������������������������������������� 194
Temporary Functions/Procedures��������������������������������������������������������������������������������������������� 195
VARIADIC Functions/Procedures����������������������������������������������������������������������������������������������� 196
Best Practices���������������������������������������������������������������������������������������������������������������������� 198

ix
Table of Contents

Summary���������������������������������������������������������������������������������������������������������������������������������� 199
What’s Next������������������������������������������������������������������������������������������������������������������������������� 199

Chapter 13: Return Values and Parameters���������������������������������������������������������� 201


Return Values���������������������������������������������������������������������������������������������������������������������������� 201
Simple Example������������������������������������������������������������������������������������������������������������������� 202
Different Ways to Return Values������������������������������������������������������������������������������������������������ 203
RETURNS����������������������������������������������������������������������������������������������������������������������������� 203
RETURNS SETOF������������������������������������������������������������������������������������������������������������������ 204
RETURNS TABLE������������������������������������������������������������������������������������������������������������������ 204
OUT�������������������������������������������������������������������������������������������������������������������������������������� 205
Simple Difference Matrix����������������������������������������������������������������������������������������������������� 206
Different Examples for Each RETURN Type�������������������������������������������������������������������������� 206
Using SELECT Statements��������������������������������������������������������������������������������������������������� 207
Using RETURNS TABLE�������������������������������������������������������������������������������������������������������� 208
Using RETURN NEXT������������������������������������������������������������������������������������������������������������ 209
Using RETURNS SETOF TABLE��������������������������������������������������������������������������������������������� 210
Using RETURNS SETOF Data Type���������������������������������������������������������������������������������������� 210
Using RETURNS RECORD����������������������������������������������������������������������������������������������������� 211
Using RETURNS SETOF RECORD������������������������������������������������������������������������������������������ 212
Using OUT Parameters��������������������������������������������������������������������������������������������������������� 214
Using INOUT Parameter������������������������������������������������������������������������������������������������������� 216
Summary���������������������������������������������������������������������������������������������������������������������������������� 216
What’s Next������������������������������������������������������������������������������������������������������������������������������� 217

Chapter 14: Handling Exceptions�������������������������������������������������������������������������� 219


Exceptions��������������������������������������������������������������������������������������������������������������������������������� 219
GET DIAGNOSTICS���������������������������������������������������������������������������������������������������������������� 219
FOUND��������������������������������������������������������������������������������������������������������������������������������� 223
Exceptions in PL/pgSQL������������������������������������������������������������������������������������������������������������ 225
Different Ways to Handle Exceptions in PL/pgSQL�������������������������������������������������������������� 226
Using the BEGIN and END Statements��������������������������������������������������������������������������������� 226

x
Table of Contents

Using the RAISE Statement������������������������������������������������������������������������������������������������� 231


Custom Exceptions�������������������������������������������������������������������������������������������������������������� 232
Rethrow Exceptions������������������������������������������������������������������������������������������������������������� 233
ASSERT�������������������������������������������������������������������������������������������������������������������������������� 234
Get Call Stack���������������������������������������������������������������������������������������������������������������������� 235
Using the GET STACKED DIAGNOSTICS Statement�������������������������������������������������������������� 237
Advantages of Using Exceptions����������������������������������������������������������������������������������������� 238
Disadvantages of Using Exceptions������������������������������������������������������������������������������������� 239
Summary���������������������������������������������������������������������������������������������������������������������������������� 239
What’s Next������������������������������������������������������������������������������������������������������������������������������� 240

Chapter 15: Triggers��������������������������������������������������������������������������������������������� 241


What Are Triggers?�������������������������������������������������������������������������������������������������������������������� 241
Syntax���������������������������������������������������������������������������������������������������������������������������������� 242
Simple Example������������������������������������������������������������������������������������������������������������������� 243
Types of Triggers in PostgreSQL������������������������������������������������������������������������������������������������ 245
Row-Level Triggers�������������������������������������������������������������������������������������������������������������� 246
INSTEAD OF Triggers������������������������������������������������������������������������������������������������������������ 253
Statement-Level Triggers���������������������������������������������������������������������������������������������������� 254
Event Triggers���������������������������������������������������������������������������������������������������������������������� 258
Advantages of Triggers�������������������������������������������������������������������������������������������������������� 260
Disadvantages of Triggers��������������������������������������������������������������������������������������������������� 261
DROP Triggers��������������������������������������������������������������������������������������������������������������������������� 261
Summary���������������������������������������������������������������������������������������������������������������������������������� 262
What’s Next������������������������������������������������������������������������������������������������������������������������������� 262

Chapter 16: Transaction Management������������������������������������������������������������������ 263


Nested Transactions������������������������������������������������������������������������������������������������������������������ 263
Exception Handling������������������������������������������������������������������������������������������������������������������� 266
Summary���������������������������������������������������������������������������������������������������������������������������������� 270
What’s Next������������������������������������������������������������������������������������������������������������������������������� 270

xi
Table of Contents

Chapter 17: Aggregates���������������������������������������������������������������������������������������� 271


Custom Aggregate��������������������������������������������������������������������������������������������������������������������� 271
Simple Example������������������������������������������������������������������������������������������������������������������� 272
State Transition Function����������������������������������������������������������������������������������������������������� 273
Final Function���������������������������������������������������������������������������������������������������������������������� 275
Creating Custom Aggregate������������������������������������������������������������������������������������������������� 276
Create Type�������������������������������������������������������������������������������������������������������������������������� 277
Create State Transition Function����������������������������������������������������������������������������������������� 277
Create Aggregate����������������������������������������������������������������������������������������������������������������� 278
Final Function���������������������������������������������������������������������������������������������������������������������� 279
Summary���������������������������������������������������������������������������������������������������������������������������������� 281
What’s Next������������������������������������������������������������������������������������������������������������������������������� 281

Chapter 18: Listen and Notify������������������������������������������������������������������������������� 283


Simple Example������������������������������������������������������������������������������������������������������������������������ 283
Build Polling in psql������������������������������������������������������������������������������������������������������������������ 285
TCN Extension��������������������������������������������������������������������������������������������������������������������������� 290
Summary���������������������������������������������������������������������������������������������������������������������������������� 292
What’s Next������������������������������������������������������������������������������������������������������������������������������� 292

Chapter 19: PL/pgSQL Essential Extensions��������������������������������������������������������� 293


plprofiler Extension������������������������������������������������������������������������������������������������������������������� 293
Installation��������������������������������������������������������������������������������������������������������������������������� 295
Usage����������������������������������������������������������������������������������������������������������������������������������� 296
plpgsql_check Extension���������������������������������������������������������������������������������������������������������� 305
Installation��������������������������������������������������������������������������������������������������������������������������� 305
Usage����������������������������������������������������������������������������������������������������������������������������������� 306
Summary���������������������������������������������������������������������������������������������������������������������������������� 309

Index��������������������������������������������������������������������������������������������������������������������� 311

xii
About the Authors
Baji Shaik, currently serving as a Senior Database
Consultant at AWS Professional Services, embarked on
his journey into the world of databases in 2011. Since
then, his expertise has encompassed an array of database
technologies, including Oracle, PostgreSQL, EDB Postgres,
Amazon RDS, Amazon Aurora, Amazon Redshift, and
Greenplum. Baji’s extensive background spans both depth
and breadth, showcasing his mastery in SQL/NoSQL
database technologies.
Baji stands out as a Database Migration Expert, having
successfully developed numerous database solutions that
tackle complex business challenges, particularly in migrating databases from on-
premises environments to Amazon RDS and Aurora PostgreSQL/MySQL. His prowess
also extends to performance optimization, having fine-tuned RDS/Aurora PostgreSQL/
MySQL databases to achieve remarkable performance benchmarks.
With a passion for knowledge sharing, Baji has authored several notable books on
PostgreSQL, such as PostgreSQL Configuration, Beginning PostgreSQL on the Cloud, and
PostgreSQL Development Essentials. His commitment to education and information
dissemination is further evident through his contributions to conferences, workshops,
and a multitude of insightful blogs within the AWS blog community.

Dinesh Kumar Chemuduru, an accomplished Principal


Architect (OSS), brings a wealth of experience to the realm
of technology and open source solutions. With a notable
background at AWS as a proficient database consultant,
Dinesh excelled in orchestrating numerous successful
database migrations. His expertise extends to the open
source arena, where he has both crafted and augmented
solutions around PostgreSQL, showcasing his commitment
to collaborative innovation.

xiii
About the Authors

A coding enthusiast at heart, Dinesh finds joy in crafting applications using Flutter,
Golang, and C++, platforms where his creativity knows no bounds. His proficiency
extends to the deployment phase, as he deftly navigates Kubernetes to bring his coding
creations to life. In the literary domain, Dinesh stands as a coauthor of the esteemed
PostgreSQL High Performance Cookbook, a testament to his mastery of the subject
matter. Beyond his own works, he actively engages in the appraisal of fellow authors’
PostgreSQL books, cementing his status as a valued participant in the exchange of
knowledge.
Dinesh’s impact reverberates through his open source contributions, which include
the inception and enrichment of projects such as PTOR – an ingenious RPO/RTO/SLA
calculator tailored for PostgreSQL. Another tool, “hammerpost,” sets a benchmark for
synthetic parameter evaluation in PostgreSQL, seamlessly integrated with HammerDB.

xiv
About the Technical Reviewer
Deepak Ramnandan Mahto works as a PostgreSQL
Database Engineer at Google Cloud. He has been working
with PostgreSQL since 2018, and he also worked as a
database migration consultant at AWS. He is also a keen
blogger and loves to publish articles on migration, best
practices, and on cloud with PostgreSQL. He loves to
code and build database-related utilities using PL/pgSQL
and SQL.

xv
Acknowledgments
I would like to express my gratitude to several individuals who have played a crucial
role in making this book a reality. A heartfelt thank-you to Apress Media for providing
me with this valuable opportunity. I am especially grateful to my coauthor and mentor,
Dinesh Kumar Chemuduru, for his exceptional collaboration. I want to express my
gratitude to Divya Modi and Nirmal Selvaraj for being understanding of our hectic
schedules and providing us with continuous support throughout the entire process.
Special thanks to Deepak Mahto for his thorough review of the book. Lastly, I am
profoundly thankful to my parents, Lalu Saheb Shaik and Nasar Bee, whose unwavering
support has shaped me into the person I am today.

—Baji Shaik

I would like to extend my heartfelt gratitude to a remarkable group of individuals who


have been instrumental in making this endeavor a reality. My heartfelt thank-you
to Apress Media for providing me with this valuable opportunity. A special note of
appreciation to my esteemed coauthor, Baji Shaik. Sincere thanks to Deepak Mahto,
whose meticulous review and insightful feedback significantly enhanced the quality and
depth of this manuscript. To Divya Modi and Nirmal Selvaraj, our project coordinators,
your organizational skills and dedication ensured that every aspect of this project fell
seamlessly into place. To my parents Vanamma, Sreenivasulu and my dearest children,
Yashvi and Isha, and to the future luminaries, Hema Siri K and Rahul Sonu K – your
unwavering love and understanding throughout the demanding phases of this project
have served as my steadfast anchor. Your continuous support is my driving force.
Finally, a heartfelt thank-you to my exceptional team at Tessell. Your commitment
to excellence and innovation is awe-inspiring. Together, we are shaping the future of
DBaaS, and I am privileged to work alongside such talented individuals.

—Dinesh Kumar Chemuduru

xvii
Introduction
The PostgreSQL engine comes with its own dedicated procedural language, similar
to procedural languages found in other commercial database engines. This language,
known as PL/pgSQL, offers a range of powerful features that developers have long
desired. For instance, PL/pgSQL includes certain object-oriented programming
capabilities like the ability to define custom operators and types, as well as custom
aggregates.
In contrast to other programming languages supported by PostgreSQL, PL/pgSQL is
intricately linked with the PostgreSQL database engine interface. This tight integration
ensures optimal performance and a seamless fit for constructing business logic on
the database side. In this book, we not only introduce the fundamentals of PL/pgSQL,
but we also dive deep into specific use cases that we’ve implemented for particular
scenarios. Our aim is to comprehensively cover the various features, functionalities, and
application scenarios of PL/pgSQL, offering assistance in crafting effective server-side
objects with ease.
Through the content of this book, you will gain an understanding of PL/pgSQL’s
design and dive deep into its transaction model, including how commit and rollback
operations function. You’ll discover strategies for optimizing PL/pgSQL functions and
procedures and explore the mechanics of inline or anonymous server-side code, along
with its limitations. Furthermore, you’ll acquire insights into debugging and profiling
PL/pgSQL code and learn techniques for conducting statistical analyses on the PL/
pgSQL code you create.

xix
CHAPTER 1

Introduction to PL/pgSQL
In this chapter, we will start with an introduction of PL/pgSQL, on what is PL/pgSQL
and what are the key features of it. We will talk about some common use cases where
PL/pgSQL is used. PL/pgSQL comes by default when you install the PostgreSQL server.
However, we will provide the steps to install PL/pgSQL. We will explain how PL/pgSQL
works with a simple flow diagram. We will show some basic examples of PL/pgSQL code
blocks which are called anonymous and named code blocks.

A Closer Look at PL/pgSQL


PostgreSQL uses SQL (Structured Query Language) as a default query language.
SQL is a common domain-specific language for relational databases. PostgreSQL uses
some extensions and features to implement the standards of SQL. In addition to SQL,
PostgreSQL supports many procedural languages like PL/pgSQL, PL/Java, PLV8,
PL/Python, PL/Perl, etc. Using these languages, you can create functions, stored
procedures, and triggers which will improve the performance by reducing the multiple
iterations to the databases.
PL/pgSQL is the most commonly used procedural language in PostgreSQL. It is
an extension of SQL. It is similar to Oracle's PL/SQL and supports features like control
structures, exception handling, variables, loops, and conditional statements. These
features help us to develop complex database applications in an efficient way.
When working on designing a complex business logic inside the database, you would
need to develop multiple SQLs which are sometimes interdependent. Results of one SQL
will be used by other SQLs. In this case, running multiple SQLs increases the data flow
between the database and the client application and will cause performance bottlenecks
due to high data transfer through the network. To overcome this, you can use stored
procedures or functions.

1
© Baji Shaik and Dinesh Kumar Chemuduru 2023
B. Shaik and D. K. Chemuduru, Procedural Programming with PostgreSQL PL/pgSQL,
https://doi.org/10.1007/978-1-4842-9840-4_1
Chapter 1 Introduction to PL/pgSQL

PL/pgSQL supports stored procedures, functions, and triggers. A stored procedure


is a set of precompiled SQL statements which can be executed repeatedly. Stored
procedures can help to reduce network traffic and improve performance by reducing the
amount of data that needs to be sent between the database and the client application.
The common use cases to use stored procedures or functions using PL/pgSQL are
1. Improve data processing speed by using precompiled code
through stored procedures which will be faster than raw SQL
queries.
2. Write more complex code using features like control structures,
exception handling, variables, loops, conditional statements, etc.

3. Using stored procedures or functions, you can create a reusable


code to call from the applications to save time and effort.

4. PL/pgSQL is portable across different operating systems and


platforms. This makes it easier to migrate code between different
environments.
5. Prevent unauthorized access and data breaches by controlling the
user authentication on stored procedures or functions.

6. Use triggers to implement constraints of business processes that


cannot be expressed as foreign keys or check constraints.

PL/pgSQL Installation
PL/pgSQL is already included in PostgreSQL, so if you have PostgreSQL installed, you
should have PL/pgSQL as well. However, you may need to enable it if it is not already
enabled. Here are the steps to enable PL/pgSQL in PostgreSQL:
1. Install PostgreSQL psql client to connect to the database, or you
can use the pgAdmin client tool.

For Ubuntu, the following are the simple steps to install the client:

# Create the file repository configuration:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt


$(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

2
Chapter 1 Introduction to PL/pgSQL

# Import the repository signing key:

wget --quiet -O - https://www.postgresql.org/media/keys/


ACCC4CF8.asc | sudo apt-key add -

# Update the package lists:

sudo apt-get update

# Install the latest version of PostgreSQL. If you want a specific


version, use 'postgresql-12' or similar instead of 'postgresql':

sudo apt-get -y install postgresql-client-15

For Linux (RHEL), you can follow the steps here:


www.postgresql.org/download/linux/redhat/

2. Connect to the database and check if PL/pgSQL is already


installed:
postgres=# \dx
List of installed extensions
Name | Version | Schema | Description
-----+---------+--------+-------------
(0 rows)
postgres=# select * from pg_extension where extname='plpgsql';
oid | extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition
----+---------+----------+--------------+----------------+------------+-----------+--------------
(0 rows)

3. Execute the following command to enable PL/pgSQL:

postgres=# CREATE EXTENSION plpgsql;


CREATE EXTENSION

3
Chapter 1 Introduction to PL/pgSQL

4. Verify that PL/pgSQL is enabled by executing the following


command:
postgres=# \dx
List of installed extensions
Name    | Version |   Schema   |         Description
------ -+---------+------------+------------------------------
plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(1 row)
JavaScript
postgres=# select * from pg_extension where extname='plpgsql';
oid   | extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition
------+---------+----------+--------------+----------------+------------+-----------+-------------
16388 | plpgsql |       10 |        11 | f              | 1.0        |        |
(1 row)

PL/pgSQL Execution Flow


PL/pgSQL is like every other “loadable, procedural language.” PL/pgSQL gets loaded
through a function manager called fmgr. The fmgr loads the language handler when
a procedural language function or procedure is executed and calls it. The execution
flow of PL/pgSQL code is similar to that of other procedural programming languages,
with parsing, compilation, execution, and cleanup stages. However, PL/pgSQL code is
executed on the server side, which means that it has direct access to the database and
can perform database operations more efficiently than client-side code.
On the first call of a PL/pgSQL function or procedure in a session, the server first
parses the code to check for syntax errors. The call handler will “compile” a function
statement tree once the code is parsed. When the code is compiled, it turns into an
internal form that the server can execute more efficiently. SQL queries in the function
are just kept as a string at this point, and the expressions like the following are actually
SQL queries:

my_var := some_param * 10

4
Chapter 1 Introduction to PL/pgSQL

The SQL queries are actually parsed at this point, and parser hooks are used to replace
variables/parameters with PARAM nodes in the parse tree. The PL/pgSQL statement tree
is very similar to a PostgreSQL execution tree. After the parse and compile, the call handler
then executes that statement tree. On the first execution of a statement node that has an
SQL query in it, that query is prepared via the Server Programming Interface (SPI). The SPI
provides a simple and efficient way to execute SQL commands, retrieve query results, and
manipulate the database. The compiled code is then executed by the server. Based on any
variable and control structure declaration, the server creates a new execution environment
for the PL/pgSQL code. If the PL/pgSQL code is a function or stored procedure that returns
a result set, the server will send the result set back to the client. Once the execution of the
code is complete, the server will clean up any resources that were used by the PL/pgSQL
code, including variables and any temporary tables that were created.
Figure 1-1 represents the flow of execution.

Figure 1-1. PL/pgSQL execution flow

5
Chapter 1 Introduction to PL/pgSQL

This diagram illustrates the high-level steps of the PL/pgSQL execution flow.
However, it's important to note that PL/pgSQL code can be quite complex and may
include multiple control structures, error handling blocks, and nested and even
recursive PL/pgSQL function calls and trigger invocations and database operations. The
actual execution flow of a specific PL/pgSQL function or stored procedure will depend
on the specific code and logic used. This call hierarchy is not limited to PL/pgSQL. All
procedural languages share the common entry point of the fmgr, so they can be mixed
and matched in trigger and function call stacks.

PL/pgSQL Blocks
PL/pgSQL is a block-structured language. The basic unit in any PL/pgSQL code is a
block. All PL/pgSQL code is composed of a single block or blocks that occur either
sequentially or nested within another block. There are two kinds of blocks:
• Anonymous or unnamed blocks (DO)

• Named blocks (functions)

Anonymous or Unnamed Blocks


Anonymous or unnamed blocks are generally constructed dynamically and executed
only once by the user. It is sort of a complex SQL statement.
The following is the structure of an anonymous block, for example:

DO $$
[ <<label>> ]
[ DECLARE
-- Variable declaration here
]
BEGIN
-- Execute statements here
END [ label ];
$$;

6
Chapter 1 Introduction to PL/pgSQL

Now, let us start with a simple hello world code block, which does not have any
name associated with it:

postgres=# DO
$$
BEGIN
        RAISE NOTICE 'Hello World';
END;
$$;

NOTICE:  Hello World
DO

In the preceding example, the RAISE NOTICE command will help us to print the
given message on the client console. As you can see here, the block is declared without
a name, and if you want to print Hello World, then you have to repeat the same set of
instructions again.
Now, let us print the Hello World line by line rather than in a single line:

postgres=# DO
$o$
BEGIN
        RAISE NOTICE $i$
        Hello
        World
        $i$;
END;
$o$;

NOTICE:
        Hello
        World
DO

In the preceding example, we used different multiline specifiers. The whole block got
enclosed by $o$, and the inner Hello World got enclosed by $i$. From this example, we
can learn that in PL/pgSQL, we can have the nested multiliners, where each multiline
should follow its own enclosure.

7
Chapter 1 Introduction to PL/pgSQL

Now, let us write a nested BEGIN ... END inside a main BEGIN ... END block. Here
is an example:

postgres=# DO
$$
BEGIN

        BEGIN
                RAISE NOTICE 'Hello World';
        END;
END;
$$;
NOTICE:  Hello World
DO

In the preceding example, we print the Hello World message from the nested BEGIN
... END block. It is possible to have multiple nested statements inside a single BEGIN...
END block. We will see more of these in the coming chapters, where we discuss exception
handling.
Now, let us go a little deeper and print the Hello World message from the nested
unnamed code block:

postgres=# DO
$o$
BEGIN
        DO
        $i$
                BEGIN
                        RAISE NOTICE 'Hello World';
                END;
        $i$;
END;
$o$;
NOTICE:  Hello World
DO

As you can see in the preceding example, we are able to define an unnamed block
inside an unnamed block. By defining the nested code blocks, we can segregate a large
unnamed block into multiple stand-alone work units. We don’t need to write a nested
8
Chapter 1 Introduction to PL/pgSQL

unnamed block; in most of the cases, the nested BEGIN...END block would be sufficient.
But in general, we don’t keep large unnamed blocks in the database; rather, we store
them inside with a name (function/procedure), and we call that object name, whenever
it is required.
Here is another example where we can have a nested block inside an exception:

postgres=# DO $inline$
BEGIN
        PERFORM 1/0;
        RAISE NOTICE 'Hello World!';

EXCEPTION
        WHEN OTHERS THEN
        DO $$
        BEGIN
                RAISE NOTICE 'Got error';
        END;
        $$;
END;
$inline$;
NOTICE:  Got error
DO

PL/pgSQL does not restrict the anonymous blocks as stand-alone objects; we can
also embed these inline definitions inside the function or procedure object. We haven’t
discussed about procedures and functions yet, but showing you an example where
you can declare the inline anonymous block inside a function:

postgres=# CREATE OR REPLACE FUNCTION test_func()


RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
        DO $$ BEGIN RAISE NOTICE 'Hello World!'; END;$$;
END;
$function$;
CREATE FUNCTION

9
Chapter 1 Introduction to PL/pgSQL

postgres=# SELECT test_func();


NOTICE:  Hello World!
test_func
-----------

(1 row)

How it works is, unlike other procedural languages, PL/pgSQL gives an inline
statement handler plpgsql_inline_handler. By using this handler, PL/pgSQL executes
the unnamed or inline PL/pgSQL blocks. If there are any nested code blocks, then those
will be evaluated recursively by the plpgsql_inline_handler.

Note that it is not possible to return any value from the unnamed code blocks.
Always use anonymous code blocks to define the business logic, which involves
making a set of function or procedure calls. If you want to return any value from
anonymous blocks, then we might need to use any session-level variables, which
need to be set inside the anonymous block, and access them from the outside of
the blocks.

Named Blocks
Named blocks have a name associated with them, are stored in the database, can be
executed repeatedly, and can take in parameters.
A named block in PL/pgSQL is defined using the following syntax:

<<label>>
DECLARE
-- declare variables here
BEGIN
-- Named block's code here
END;

Here, label is the name of the block and is enclosed within << >> brackets.It is not
just cosmetic, but that nested code blocks can refer to outer variables by using that label
instead of finding the innermost match for the variable name.

10
Chapter 1 Introduction to PL/pgSQL

The DECLARE section is used to declare variables that are used within the block,
while the BEGIN and END sections contain the actual code for the block.
Once a named block has been defined, it can be called from within the same
function or procedure using the PERFORM statement:

PERFORM block_name;

This will execute the code within the named block. Named blocks can be called
multiple times within the same function or procedure, allowing for reusable and
modular code.
Here's an example of a PL/pgSQL function that uses named blocks to calculate the
factorial of a number:

CREATE OR REPLACE FUNCTION factorial(num INTEGER)


RETURNS INTEGER AS $$
DECLARE
   result INTEGER := 1;
BEGIN
   <<factorial_loop>>
   FOR i IN 1..num LOOP
      result := result * i;
   END LOOP factorial_loop;

   RETURN result;
END;
$$ LANGUAGE plpgsql;

In this example, the named block factorial_loop is used within a FOR loop to
calculate the factorial of the input number. The DECLARE section declares a variable
result to store the final result, while the BEGIN and END sections contain the code for
the named block.
The named block is called within the FOR loop using the LOOP statement. This
allows the loop to continue until it reaches the specified number of iterations.
Once the FOR loop is complete, the final result is returned by the function.
To call the function and calculate the factorial of a number, you would execute the
following SQL statement:

SELECT factorial(5);

This would return the value 120, which is the factorial of 5.


11
Chapter 1 Introduction to PL/pgSQL

Summary
In this chapter, we talked about PL/pgSQL use cases, installation, and how it works with
a flow diagram. We have also shown how simple PL/pgSQL code blocks look like and
how to execute them. These examples will help you to understand and start with PL/
pgSQL code. In the next chapter, we will talk about the variables that are used inside
PL/pgSQL code blocks. We will start with how to declare those variables and dive deep
into different types of methods to use based on the use cases. These will help you to
decide which type of variables you should use when building the PL/pgSQL code for the
functions or procedures.

What’s Next
In the next chapter, we will be covering some key features of PL/pgSQL variables like the
following:
• Variable Types: Explore the different types of variables and learn
how to choose the appropriate variable type for your needs.
• Variable Scoping Mastery: Gain a better grasp of variable scoping
rules and how to manage variables effectively within different blocks.
• Variable Naming Conventions: Learn about naming conventions
that can help you write more maintainable and readable code.
• Advanced Variable Usage: Extend your knowledge by using
variables in more complex scenarios.

12
CHAPTER 2

PL/pgSQL Variables
In the previous chapter, we talked about what PL/pgSQL is and some use cases where
you need it. We also explained the steps to install and the execution flow of PL/pgSQL
with a simple diagram. We have discussed some PL/pgSQL anonymous and named
code block examples which will help in understanding the basics. In this chapter, we will
introduce variables that are used in the PL/pgSQL code. We will provide different types
of variables and use cases on when to use them. We will show the declaration and scope
of variables and different types of variables with some examples.

What Are Variables in PL/pgSQL?


Similar to other programming languages, PL/pgSQL has variables which are used to
store values for later use in the program. These variables can hold different types of data
such as integers, floating-point numbers, strings, and boolean values.

Declaring Variables
PL/pgSQL offers to declare variables in its declaration section of the block. Here is an
example:

postgres=# DO
$$
DECLARE
        v_var1 INT;
        v_var2 INT:=10;
BEGIN
        RAISE NOTICE 'v_var1 %', v_var1;
        RAISE NOTICE 'v_var2 %', v_var2;

13
© Baji Shaik and Dinesh Kumar Chemuduru 2023
B. Shaik and D. K. Chemuduru, Procedural Programming with PostgreSQL PL/pgSQL,
https://doi.org/10.1007/978-1-4842-9840-4_2
Chapter 2 PL/pgSQL Variables

END;
$$;
NOTICE:  v_var1 <NULL>
NOTICE:  v_var2 10
DO

In the preceding example, we have declared two variables. The v_var1 variable is
declared but not defined with any value to it. The second variable v_var2 is declared but
has a value 10 to it. If we try to access a variable which is declared and not defined, then
expect that we would get NULL from it. In the preceding output, we can also see that the
v_var1 is set to NULL.
From the previous chapter, we learned that we can have nested blocks inside the
main PL/pgSQL block. By using that nested block feature, we have multiple declarations
and multiple variables in the single PL/pgSQL block. Here is an example:

postgres=# DO
$o$
DECLARE
        v_var1 INT:=10;
BEGIN

        RAISE NOTICE 'v_var1 %', v_var1;

        DO
        $i$
        DECLARE
                v_var1 INT:=100;
        BEGIN
                RAISE NOTICE 'v_var1 %', v_var1;
        END;
        $i$;

END;
$o$;
NOTICE:  v_var1 10
NOTICE:  v_var1 100
DO

14
Chapter 2 PL/pgSQL Variables

In the preceding example, we have nested unnamed blocks, where we have two
variable declarations. The variable v_var1 in the main block is declared with the value
10, and also v_var1 in the inside block is declared with the value 100. Also, the scope of
the variable is always local.

Note In PL/pgSQL, we can’t declare variables on the fly. We should always


declare them in the DECLARE section, and we have to use them in the block. But,
while defining the FOR LOOP, we can have the variables declared on the fly. We
will discuss more about this in the coming chapters.

Variable Scope
The scope of the declared variables in PL/pgSQL is always local to its current block. That
is, once we declare the variable in a block, we can’t access them outside of that block.
Here is an example:

postgres=# DO
$$
DECLARE
BEGIN
        DECLARE
                v_var1 INT:=10;
        BEGIN
                RAISE NOTICE 'v_var1 %', v_var1;
        END;

        RAISE NOTICE 'v_var1 %', v_var1;


END;
$$;
NOTICE:  v_var1 10
ERROR:  column "v_var1" does not exist
LINE 1: v_var1
        ^
QUERY:  v_var1
CONTEXT:  PL/pgSQL function inline_code_block line 10 at RAISE

15
Chapter 2 PL/pgSQL Variables

From the preceding example, as you can see we got an error, column "v_var1"
does not exist, when we try to access the variable v_var1 from the outer BEGIN...
END block. That is, the scope of variable v_var1 is local to that inner BEGIN...END block,
and we can’t access them from the outside. If we declare the variable v_var1 in the
parent BEGIN...END block, then we can access that variable inside the nested BEGIN...
END blocks too. Because the v_var1 is declared at the parent block level, its scope is
at the whole block level. Now, you might have questions like what if we declare the
same variable v_var1 in parent and nested BEGIN...END blocks and how to access the
parent block’s v_var1 along with the local declared variable. Here is an example for this
use case:

postgres=# DO
$$
DECLARE
        v_var1 INT:=1;
BEGIN
        DECLARE
                v_var1 INT:=10;
        BEGIN
                RAISE NOTICE 'v_var1 %', v_var1;
        END;
END;
$$;
NOTICE:  v_var1 10
DO

From the preceding example, we were only able to access the variable v_var1, which
was declared in the nested BEGIN...END block. To access the parent v_var1 variable,
then we should access that variable with the block’s label. That is, we have to give a label
name to the parent block, and then we should access the v_var1 along with its label
name. Here is an example:

postgres=# DO
$$
<<parent>>
DECLARE
        v_var1 INT := 1;

16
Chapter 2 PL/pgSQL Variables

BEGIN
        DECLARE
                v_var1 INT := 10;
        BEGIN
                RAISE NOTICE 'Parent v_var1 %', parent.v_var1;
                RAISE NOTICE 'Local  v_var1 %', v_var1;
        END;
END;
$$;
NOTICE:  Parent v_var1 1
NOTICE:  Local  v_var1 10
DO

Constant Variables
We can declare constant variables inside PL/pgSQL, which shouldn’t get updated by
further instructions. Here is an example:

postgres=# DO
$$
DECLARE
    v_c_pi CONSTANT REAL DEFAULT 3.14;
BEGIN
    v_c_pi = 3.15;
END;
$$;
ERROR:  variable "v_c_pi" is declared CONSTANT
LINE 6:     v_c_pi = 3.15;

In the preceding example, we declared the variable v_c_pi as CONSTANT and set its
DEFAULT value as 3.14. But, in further instructions, when we tried to update its value as
3.15, we got the exception as the variable is declared as CONSTANT, which should not get
updated by any of the instructions.

17
Chapter 2 PL/pgSQL Variables

Variable Alias
In PL/pgSQL, we can also create a reference variable which points to another variable or
system variables. For example, if we want to create a reference variable or a short-length
variable name to another variable, then we can create those short-length variables using
ALIAS. Here is an example:

DO
$$
DECLARE
    var_earth_sun_distance REAL DEFAULT 149.6;
    v_e_s_d ALIAS FOR var_earth_sun_distance;
BEGIN
    RAISE NOTICE 'Reference      #1 %', v_e_s_d;
    v_e_s_d = 149.5;
    RAISE NOTICE 'Actual Variable   %', var_earth_sun_distance;
END;
$$;
NOTICE:  Reference      #1 149.6
NOTICE:  Actual Variable   149.5
DO

In the preceding example, we created a reference variable v_e_s_d to the actual


variable var_earth_sun_distance. Also, if we perform any update on the reference
variable, then we can see those changes from the actual variable. ALIAS is not limited to
creating a reference variable to the actual variable, it will allow creating a reference to
another reference. Here is an example:

DO
$$
DECLARE
    var_earth_sun_distance REAL DEFAULT 149.6;
    v_e_s_d ALIAS FOR var_earth_sun_distance;
    vd ALIAS FOR v_e_s_d;
BEGIN
    RAISE NOTICE 'Reference       #1 %', v_e_s_d;
    RAISE NOTICE 'Reference       #2 %', vd;

18
Chapter 2 PL/pgSQL Variables

    RAISE NOTICE 'Update Ref #1';


    v_e_s_d = 149.5;
    RAISE NOTICE 'Actual Variable    %', var_earth_sun_distance;
    RAISE NOTICE 'Update Ref #2';
    vd = 149.4;
    RAISE NOTICE 'Actual Variable    %', var_earth_sun_distance;
END;
$$;
NOTICE:  Reference       #1 149.6
NOTICE:  Reference       #2 149.6
NOTICE:  Update Ref #1
NOTICE:  Actual Variable    149.5
NOTICE:  Update Ref #2
NOTICE:  Actual Variable    149.4
DO

In the preceding example, we created an ALIAS variable from another ALIAS variable.
That is, we created a new reference variable, which points to another reference variable.
Also, if we update the second reference variable, it will reflect those changes on the
actual variable via the first reference.
PL/pgSQL supports the following types of variables:
• Scalar Variables

• Array Variables

• Record Variables

• Cursor Variables

Scalar Variables
In all the previous examples, we demonstrated scalar variables. Scalar variables hold a
single value of a specific data type, such as an integer or a string. They can be declared
and initialized using the DECLARE keyword and can be assigned values using the :=
assignment operator.

19
Chapter 2 PL/pgSQL Variables

For example, the following code declares each type of scalar variable:

postgres=# DO $$
DECLARE
   my_int integer := 1;
   my_text text := 'Hello, world!';
   my_bool boolean := true;
BEGIN
   -- perform operations on the scalar variables
   my_int := my_int + 10;
   my_text := my_text || ' How are you?';
   my_bool := not my_bool;

   -- print the scalar variables


   RAISE NOTICE 'my_int = %', my_int;
   RAISE NOTICE 'my_text = %', my_text;
   RAISE NOTICE 'my_bool = %', my_bool;
END;
$$;
NOTICE:  my_int = 11
NOTICE:  my_text = Hello, world! How are you?
NOTICE:  my_bool = f
DO

In the preceding example, we declare three scalar variables: my_int with a value
of 1 and a data type of integer, my_text with a value of 'Hello, world!' and a data
type of text, and my_bool with a value of true and a data type of boolean. We then
perform some operations on these variables using arithmetic, concatenation, and logical
negation. Finally, we print the values of the variables using the RAISE NOTICE statement.
Scalar variables are useful for storing temporary values or performing calculations
within a stored procedure or function. They can be used in a variety of ways, such as
tracking state, performing conditional logic, or holding input or output parameters.

20
Chapter 2 PL/pgSQL Variables

Array Variables
Array variables are variables that can hold multiple values of the same data type. They
are declared using a data type followed by the [] syntax, such as integer[] or text[].
For example, the following code declares an array variable:

postgres=# DO $$
DECLARE
   my_array integer[] := '{1, 2, 3, 4, 5}';
BEGIN
   -- print the entire array
   RAISE NOTICE 'my_array = %', my_array;

   -- access individual elements of the array


   RAISE NOTICE 'my_array[2] = %', my_array[2];

   -- modify individual elements of the array


   my_array[3] := 10;

   -- print the modified array


   RAISE NOTICE 'my_array = %', my_array;
END;
$$;
NOTICE:  my_array = {1,2,3,4,5}
NOTICE:  my_array[2] = 2
NOTICE:  my_array = {1,2,10,4,5}
DO

In the preceding example, we declare an array variable my_array with a data type of
integer[] and initialize it with the values {1, 2, 3, 4, 5}. We then print the entire
array using the RAISE NOTICE statement, access and print the value of the second
element of the array, modify the value of the third element of the array, and print the
entire array again.
Array variables are useful for storing and manipulating sets of related data, such
as lists of numbers, strings, or boolean values. They can be used in a variety of ways,
such as for performing calculations on multiple values at once, storing input or output
parameters, or passing data between functions or procedures.

21
Chapter 2 PL/pgSQL Variables

Record Variables
Record variables are used to store a row of data from a table or a query result. They are
declared using the %ROWTYPE attribute and can be assigned values using the SELECT INTO
statement.
For example, the following code declares a record variable:

postgres=# CREATE TABLE emp (emp_id INT, emp_name VARCHAR, emp_salary


NUMERIC);
CREATE TABLE

postgres=# INSERT INTO emp VALUES (100, 'SCOTT', '10000.00');


INSERT 0 1

postgres=# DO $$
DECLARE
   my_record emp%ROWTYPE;
BEGIN
   -- select a row of data into the record variable
   SELECT * INTO my_record FROM emp WHERE emp_id = 100;

   -- print the values of the record variable


   RAISE NOTICE 'emp_id = %', my_record.emp_id;
   RAISE NOTICE 'emp_name = %', my_record.emp_name;
   RAISE NOTICE 'emp_salary = %', my_record.emp_salary;

   -- update the values of the record variable


   my_record.emp_name := 'Smith';
   my_record.emp_salary := 50000;

   -- update the row of data in the table


   UPDATE emp SET emp_name = my_record.emp_name, emp_salary = my_record.
emp_salary WHERE emp_id = 1001;
END;
$$;
NOTICE:  emp_id = 100
NOTICE:  emp_name = SCOTT
NOTICE:  emp_salary = 10000.00
DO

22
Chapter 2 PL/pgSQL Variables

In the preceding example, we declare a record variable my_record that holds a row
of data from the emp table. We then select a row of data into the record variable using
a SELECT statement, print the values of the record variable using the RAISE NOTICE
statement, update the values of the record variable, and update the row of data in the
table using an UPDATE statement.
Record variables are useful for storing and manipulating rows of data from tables
or query results within a stored procedure or function. They can be used in a variety of
ways, such as for passing data between functions or procedures, performing calculations
on data, or storing input or output parameters.

Cursor Variables
Cursor variables are variables that hold a reference to a cursor, which is a named SQL
statement that can be executed repeatedly. They are declared using the CURSOR
keyword.
For example, the following code declares a record variable named “my_row” that
corresponds to the columns of a table named “my_table”:

postgres=# CREATE TABLE emp (emp_id INT, emp_name VARCHAR, emp_salary


NUMERIC);
CREATE TABLE

postgres=# INSERT INTO emp VALUES (100, 'SCOTT', '10000.00');


INSERT 0 1

postgres=# INSERT INTO emp VALUES (100, 'ROBERT', '50000.00');


INSERT 0 1

postgres=# DO $$
DECLARE
   my_cursor refcursor;
   my_record emp%ROWTYPE;
BEGIN
   -- open the cursor and fetch the first row of data
   OPEN my_cursor FOR SELECT * FROM emp;
   FETCH my_cursor INTO my_record;

   -- print the values of the first row of data

23
Chapter 2 PL/pgSQL Variables

   RAISE NOTICE 'emp_id = %', my_record.emp_id;


   RAISE NOTICE 'emp_name = %', my_record.emp_name;
   RAISE NOTICE 'emp_salary = %', my_record.emp_salary;

   -- fetch the next row of data


   FETCH my_cursor INTO my_record;

   -- print the values of the second row of data


   RAISE NOTICE 'emp_id = %', my_record.emp_id;
   RAISE NOTICE 'emp_name = %', my_record.emp_name;
   RAISE NOTICE 'emp_salary = %', my_record.emp_salary;

   -- close the cursor


   CLOSE my_cursor;
END;
$$;
NOTICE:  emp_id = 100
NOTICE:  emp_name = SCOTT
NOTICE:  emp_salary = 10000.00
NOTICE:  emp_id = 100
NOTICE:  emp_name = ROBERT
NOTICE:  emp_salary = 50000.00
DO

In the preceding example, we declare a cursor variable my_cursor and a record


variable my_record that holds a row of data from the emp table. We then open the cursor
using a SELECT statement, fetch the first row of data into the record variable using
a FETCH statement, print the values of the first row of data using the RAISE NOTICE
statement, fetch the second row of data into the record variable, print the values of the
second row of data, and then close the cursor using the CLOSE statement.
Cursor variables are useful for manipulating and iterating through result sets
returned by queries. They can be used in a variety of ways, such as for processing large
datasets, performing calculations on data, or storing input or output parameters.
Variables can be very useful in PL/pgSQL programs as they allow you to store and
manipulate data in a flexible way.

24
Chapter 2 PL/pgSQL Variables

Summary
In this chapter, we learned about the types of variables with nice examples and
explanations of how they work. We went through the declaration of variables in the
DECLARE section of the code as well as in an independent code block on the fly. The
scope of variables is different for each block of PL/pgSQL code. We explained how the
scope of variables varies with some examples. Also, we looked at constant variables and
how to use aliases for the variables.

What’s Next
In the next chapter, we will be covering some key features of PL/pgSQL data types like
the following:
• Advanced Data Type Exploration: Delve into complex data types
like composite and range types to efficiently manage intricate data
structures.
• Custom Domain Types: Create user-defined domain types with
specific constraints to ensure data integrity and validation in your
applications.
• Optimal Type Selection: Understand how to choose the right
data type based on application needs, considering performance,
maintainability, and space efficiency.
• Dynamic Application Building: Harness PL/pgSQL data types to
create adaptable applications that handle diverse data inputs and
outputs.

25
CHAPTER 3

PL/pgSQL Data Types


In the previous chapter, we introduced variables that are used in the PL/pgSQL code.
We talked about different types of variables and use cases on when to use them. We have
shown the declaration and scope of the variables and different types of variables with
some examples. In this chapter, we will introduce the supported data types to use in PL/
pgSQL code. We will walk through each data type and use cases with some examples.

Data Types
Choosing the right data type for the data being used is a best practice that developers
should follow. Choosing the correct data type can help us avoid many data validation
checks and prevent exceptions when incorrect data is entered. The PostgreSQL database
engine supports multiple data types for each data kind. By using these data types, we
build tables and insert proper data into them. We can use most of the PostgreSQL data
types inside PL/pgSQL blocks, besides pseudo-types like any, anyenum, and record. It is
not just limited to using existing data types; we can also construct new composite types
by using these existing data types.
Understanding data types is crucial for developing efficient and effective database
applications. We will cover data types such as integers, floating-point numbers,
booleans, and strings, as well as more advanced data types like arrays and composite
types. By the end of this chapter, you will have a good understanding of the different data
types available in PL/pgSQL and how to use them effectively in your business logic. We
will also cover how to declare variables and constants using these data types and how to
convert between different data types using type casting. This knowledge is essential for
writing robust and error-free PL/pgSQL code that can handle a variety of data inputs and
outputs.

27
© Baji Shaik and Dinesh Kumar Chemuduru 2023
B. Shaik and D. K. Chemuduru, Procedural Programming with PostgreSQL PL/pgSQL,
https://doi.org/10.1007/978-1-4842-9840-4_3
Chapter 3 PL/pgSQL Data Types

Note You can find the list of supported data types here: www.postgresql.
org/docs/current/datatype.html.

Declaring Variables with Data Types


In the previous chapter, we saw several examples where we declared variables along
with their data types. Now, let us begin with a simple example where we demonstrate the
primitive data types like int, boolean, and text:

postgres=# DO
$$
declare
v_int int:=10;
v_text text:='text';
v_boolean boolean:=false;
begin
raise notice 'v_int      %',v_int;
raise notice 'v_text     %',v_text;
raise notice 'v_boolean  %',v_boolean;
end;
$$;

NOTICE:  v_int      10
NOTICE:  v_text     text
NOTICE:  v_boolean  f
DO

In the preceding example, we declared three variables along with their data types
and printed the results. Let us rewrite the previous example as follows and see the
results:

postgres=# DO
$$
declare
int int:=10;
text text:='text';

28
Chapter 3 PL/pgSQL Data Types

boolean boolean:=false;
begin
raise notice 'int  %',int;
raise notice 'text %',text;
raise notice 'boolean %', boolean;
end;
$$;
NOTICE:  int  10
NOTICE:  text text
NOTICE:  boolean f
DO

As you can see in the preceding example, variable names can be declared with the
corresponding data type names. As data type names are not reserved keywords, we can
also use them as variable names. Here, we are demonstrating the possibility of declaring
variable names as data types. However, this kind of variable declaration is not typically
used in production.
In the previous example, as well as in the previous chapter, we discussed only the
base scalar data types in PostgreSQL. These data types accept only a single value into
their declared variable type. To obtain a list of available base scalar types in the current
database, we can query the PostgreSQL catalog table pg_type. The following query
returns base data types in the database limited to three only:

postgres=# SELECT typname FROM pg_type WHERE typtype='b' LIMIT 3;


typname
---------
bool
bytea
char
(3 rows)

29
Discovering Diverse Content Through
Random Scribd Documents
—Qu'est-ce que c'est que Louisa Aumont?
—Louisa Aumont! c'est ma rivale.
Et je lui racontai toute la scène de la soirée de Versailles,
l'humiliation que j'avais subie, et l'abandon où m'avait laissée M.
Adolphe.
Marie paraissait aussi effrayée de mes plans de vengeance, que je
l'avais été quelques jours auparavant de ses pensées de suicide.
—Bah! au lieu de broyer du noir, comme tu le fais, tu ferais peut-être
mieux d'aller voir ton ami et de te réconcilier avec lui; on n'est pas
fier quand on aime!
—Chacun a son caractère... Je ne consentirai jamais à m'abaisser à
la prière.
—Je te donne huit jours!...
—Tu verras; il aime cette femme, mais il reviendra... Je ferai tant et
tant qu'il entendra parler de moi! C'est son luxe qui lui plaît; j'en
aurai plus qu'elle!...
—Viens au bal, ce soir, à la Chaumière, me dit Marie...
—Non, à Mabille!...
—Est-ce joli, me dit-elle?
—Je n'en sais rien.
—Je n'y suis jamais allée; j'aimerais mieux la Chaumière!
—Alors, je ne sortirai pas, car je ne veux pas le rencontrer... Il vient
à Paris; c'est là qu'il ira. Si je le rencontrais avec cette femme, je
souffrirais trop.
—Ah! tu es plus forte que moi, me dit-elle; moi, j'irais comme un
papillon me brûler au feu!...
—Non, ma pauvre amie, je ne suis pas plus forte que toi... je souffre
autant, peut-être plus, car je sens avec une ardeur qui me dévore!...
La chose la plus indifférente pour toute autre me frappe, m'exalte;
lorsque je veux quelque chose, pour l'avoir, pour rapprocher la
distance ou le temps qui m'en séparent, je donnerais dix ans, vingt
ans de ma vie!... Ainsi, je suis honteuse de mon ignorance, je brûle
du désir d'apprendre... Quand je prends un livre, je voudrais
comprendre, aller si vite, que le rouge me monte à la tête; mes yeux
s'embrouillent: je suis obligée de m'arrêter... Alors, je me mets dans
des colères ridicules contre moi, contre ma tête rebelle... je me
frappe le front. Quand j'essaye d'apprendre à écrire, et que ma main
n'obéit pas à ma volonté, je me pince le bras au point d'en porter les
marques... Si j'ai une espérance, une peine, je ne puis dormir, je
rêve, je suis agitée, je vis doublement. Eh bien! je veux dompter
tout cela!... Si mon cœur est en révolte contre ma volonté, je le
torturerai jusqu'à ce qu'il me cède. Je rirai, quand je le voudrai,
dussé-je m'étrangler avec mes larmes rentrées. Il y a un sentiment
que je ne solliciterai jamais, c'est la pitié. Est-ce que l'on plaint les
gens malheureux?... Est-ce que je suis intéressante? j'ai l'âme
navrée... Ah! si j'avais une coupure au doigt, une plaie, on me
plaindrait, peut-être chercherait-on à me soulager; mais la douleur
que j'éprouve, si je la laissais voir, on enfoncerait de nouveaux traits
dans la blessure. Je me tais, mais je n'en souffre pas moins. La
pensée que mon nom est inscrit sur ce livre infernal, cette pensée ne
me quitte pas. Je ne veux pas qu'il y reste; je veux qu'il soit effacé.
Comment l'obtiendrai-je? qui m'en donnera les moyens?... je
l'ignore, mais j'en viendrai à bout, et si, après avoir fait tous les
efforts, cela m'était impossible, je quitterais cette vie où je n'aurais
passé que pour faire une tache.
—Oh! tu vois bien que tu dis comme moi, que si tu étais misérable
tu te tuerais!
—Oui, mais après avoir essayé de vivre tranquille dans l'avenir, afin
d'oublier moi-même le passé, si je puis.
—Tu réussiras, me dit Marie pensive, moi, je suis sûre de finir d'une
mort violente!... Ça m'est égal, je n'ai pas été faite pour cette vie-
là... J'y suis, j'y resterai, à moins qu'un miracle ne m'en tire.
—Mais quelles absurdes idées allons-nous nous fourrer en tête; nous
allons avoir l'air de croque-morts au bal!...
—Oh! ne t'inquiète pas; je serai plus bruyante que tout le monde! Si
je rencontre de ses amis, je veux qu'on lui dise que je le pleure
gaiement.
X
LE BAL MABILLE.

Il était neuf heures quand nous arrivâmes allée des Veuves... Mabille
avait été un petit bal champêtre, éclairé avec des quinquets à
l'huile... On payait dix sous d'entrée... C'était le rendez-vous favori
des valets de chambre, des femmes de chambre, dans le temps où
ils étaient moins élégants que leurs maîtres.
A l'époque où je parle, Mabille s'était déjà beaucoup embelli. Ce
n'était pas encore le magnifique jardin que l'on voit aujourd'hui, avec
ses corbeilles de fleurs, ses guirlandes de feu, son jet d'eau, sa
grande salle tendue d'or, de velours et de glaces. C'était un jardin
modeste! Quelques becs de gaz avaient remplacé les quinquets; ils
étaient rares: était-ce par économie ou par discrétion?... Les calicots,
les grisettes, les modistes, pourraient nous renseigner à cet égard;
car les abonnés avaient changé. Le bal était en progrès... On payait
un franc d'entrée...
C'était au milieu de cette réunion que nous fîmes notre entrée...
L'orchestre était au milieu du jardin et me parut bon. Mon cœur se
mit à battre la mesure. J'adorais la musique. Tous ces jeunes gens,
toutes ces jeunes filles qui se livrent au travail toute la semaine
prennent le dimanche du plaisir pour huit jours; ils sont gais, en
nage, fatigués, mais si heureux que cela vous gagne. Je n'avais
jamais dansé; j'aurais voulu essayer, mais la crainte d'être ridicule
me retenait.
Pourtant, Adolphe m'avait dit que Louisa Aumont valsait bien. Je
voulais essayer. On vint m'inviter pour un quadrille... j'allais refuser
quand un jeune homme de Versailles vint me dire bonsoir. J'acceptai.
Je priai Marie de me faire vis-à-vis. J'espérais qu'il demanderait ce
que je faisais, et je me donnais un mal!... Mon danseur était galant!
je lui faisais mille coquetteries!... Il voulut me faire valser... j'acceptai
encore, et comme il avait beaucoup de patience... j'appris le même
soir la valse et la danse. Je demandai à mon danseur la permission
de me reposer.
Je fis le tour du bal m'arrêtant un peu à chaque cercle qui entourait
les bons danseurs. Un de ces cercles était plus garni de curieux que
les autres. Je cherchai à me faire une place; mais personne ne
bougea.
J'entendis rire, dire bravo! mais je ne vis rien. J'attendis la fin pour
voir ceux qui avaient eu tant de succès. Le rond s'ouvrit et tout le
monde se pressa sur les pas d'une femme, en riant, en parlant. Je
n'entendis qu'un bruit confus et des compliments ou des moqueries.
Cette femme regardait à droite, à gauche. Elle pouvait avoir cinq
pieds; sa taille était courte; sa poitrine bombée, ses épaules un peu
hautes... Elle portait fièrement la tête, ses cheveux étaient d'un beau
noir, ses raies blanches bien plantées. Elle se coiffait avec des
bandeaux plats, une natte ronde, derrière la tête; au-dessous de
cette natte, tombaient des cheveux frisés, qui lui cachaient le cou,
quoiqu'ils ne fussent pas très-longs. Son front était bas, ses sourcils
bien arqués se joignaient au milieu, ce qui lui donnait l'air dur;
ajoutez à cela de grands yeux noirs qui paraissaient regarder sans
voir, un nez un peu à la Roxelane, la lèvre dédaigneuse.
Elle était plutôt jolie que laide; pourtant on la trouvait généralement
peu agréable. Mon premier mouvement fut de la trouver laide. Je ne
comprenais pas pourquoi on l'entourait ainsi.
Elle allait du côté du café, je la suivis pour me trouver au premier
rang quand elle danserait. Elle paraissait haletante; elle toussa, mit
la main sur sa poitrine, puis avala deux verres d'eau glacée, comme
pour éteindre le feu qu'elle serrait sous ses doigts. Elle respira
bruyamment et se leva.
Un petit monsieur venait de lui faire signe. Il était gentil, mais très-
drôle; il avait une assez jolie figure, surtout des yeux intelligents.
Ses jambes étaient toutes petites, sa taille longue, son gilet aurait pu
lui servir de tablier. Il fit aller un bras comme une aile de moulin, mit
son chapeau de côté, leva son pied à la hauteur du nez de sa
danseuse, la salua jusqu'à terre, en faisant le gros dos. Après toutes
ces singeries, il la prit par la taille, et la première figure commença.
Il était léger comme un oiseau; toutes ces gambades, qui étaient
ridicules, faites par les autres, étaient gracieuses, faites par lui.
J'avais bien fait de les suivre: il y avait encore plus de monde que la
première fois.
A la seconde figure, sa danseuse regarda le chef d'orchestre, et en
même temps que le coup d'archet elle s'élança la tête baissée, les
bras en arrière, puis au bout du cercle elle se redressa, cambra ses
reins, fit presque toucher ses coudes, leva la tête et revint en avant.
Elle faisait toutes ces contorsions avec le plus grand sérieux du
monde...
—Bravo! bravo! disaient les spectateurs.
Elle avait une robe de laine noire qui sentait la misère; elle n'avait
peut-être pas mangé de la journée, car elle était bien pâle.
J'entendis à côté de moi deux jeunes gens dire:
—Emmenons-la souper!...
—Non, dit l'un, elle nous coûterait les yeux de la tête; je parie qu'elle
n'a pas mangé depuis huit jours.
—Bah! dit l'autre, nous la rationnerons; elle nous amusera.
Après la danse, ils s'approchèrent d'elle. Je l'entendis accepter. Je
m'en allai. Son danseur était près de moi; il s'essuyait le front et
disait:
—Je n'aime pas à danser avec elle; elle est raide comme un bâton.
On appela:
—Brididi!
Et le petit jeune homme qui dansait si bien répondit:
—Voilà! voilà! et s'en alla au café.
Le bal finissait... Nous rentrâmes.
J'espérais toujours recevoir des nouvelles de Versailles.
Je demandai s'il n'était venu personne...
On me répondit que non. Je me couchai en pleurant.
Le jeudi suivant, je retournai à Mabille avec Marie. Nous cherchâmes
Brididi et sa danseuse. Ce fut une des premières femmes que
j'aperçus. Elle avait une robe de barége lilas. Ses cheveux étaient
mieux peignés; elle me parut moins laide.
Un homme d'un certain âge, avec un chapeau gris, un pantalon
blanc, un petit paletot sac, s'arrêta devant elle...
—Ah! bah! dit-il, comme nous sommes requinquées! C'est égal, tu as
une figure ingrate! Elle a l'air sauvage; elle ressemble à la reine
Pomaré.
Tous ceux qui l'entouraient dirent ensemble:
—Chicard a raison, faut l'appeler Pomaré.
Quand elle se mit à danser, tout le monde l'entoura, et, pour
l'encourager, on criait à tue-tête:
—Bravo! Pomaré!
Cette soirée fit événement; plusieurs journaux en parlèrent le
lendemain.
Ce soir-là, M. Brididi n'avait pas l'air content; il dansait avec de jolies
filles, mais on n'était occupé que de Pomaré. Il pensa à lui donner
une rivale, et chercha un sujet nouveau. Je le regardais si souvent,
qu'il crut que j'avais envie de danser avec lui; ce qui était, du reste,
un grand honneur. Il vint à moi et m'invita. Je lui dis que ce serait
avec plaisir, mais que je ne savais pas danser.
—Eh bien, je vous apprendrai.
En effet, il m'apprit.—J'avais une jolie taille, de beaux bras... J'ôtai
mon petit châle, et je restai avec ma robe de barége à manches
courtes. On me regarda beaucoup, cela m'encouragea. Je sautais
comme une plume!... Après le quadrille, Marie, qui avait tenu mon
châle et mon chapeau, me dit:
—Sais-tu que tu danses très-bien!
—Certainement ajouta M. Brididi.
Je fus toute fière!... J'aurais bien voulu qu'on m'appelât aussi
Pomaré!...
Je pris un grand goût pour la danse!... A la fin de la soirée, M. Brididi
me dit sans façon:
—Voulez-vous venir souper avec nous?
J'acceptai de même, et nous partîmes une bande joyeuse, pour aller
souper chez Vachette.
—Ah! disais-je en regardant bien si l'on me voyait, si cela pouvait se
savoir à Versailles!
Marie me suivait partout. Son amant était en Bretagne... je l'avais
tout entière.
Nous quittâmes le restaurant à six heures du matin.
—Rien pour moi? dis-je au concierge.
—Non!
Je montai, toute triste; mais je m'endormis sans pleurer: j'étais trop
fatiguée.
A quatre heures, M. Brididi vint nous voir.
—Ah! vous ne savez pas, me dit-il sans me dire bonjour, il y a une
danse nouvelle: la polka! Venez passer la soirée chez moi, nous
l'apprendrons et nous la danserons ensemble, pour faire enrager
Pomaré.
Cette idée me plaisait assez, non par antipathie contre cette femme,
mais pour qu'on s'occupât de moi.
Nous apprîmes pendant cinq heures; enfin, je la savais à merveille.
On faisait une foule de figures qui vous donnaient l'air de chiens
savants: les bras, les jambes, le corps, la tête, tout remuait à la fois;
on eût dit un monde de télégraphes et de pantins. Mais c'était
nouveau, et on trouvait cela joli.
M. Brididi m'engagea à rester chez lui. Il était bien tard; je le
remerciai.
Il vint nous reconduire.—Comme c'était un charmant garçon, et que
je ne voulais pas qu'il me prît pour une bégueule, je lui racontai
l'état de mon cœur... mais, à chaque phrase, je faisais un saut de
polka, et j'en chantais l'air; ce qui fit rire Marie et dire à Brididi:
—Allons, allons, c'est bien! soignez-vous; j'espère que vous ne serez
pas longue à guérir.
Nous étions arrivés à la maison, je demandai:
—Il n'est venu personne?...
—Non!
J'étouffai un soupir au fond de mon cœur.
Arrivée dans la chambre, je me mis à polker.
—Je suis contente, me dit Marie; tu prends ton parti gaiement.
—C'est comme cela qu'il faut être. Si j'étais restée là, pleurant, il ne
serait pas venu davantage! On ne peut pas forcer les gens à vous
aimer; quand on court après eux, on les fatigue; ils s'y habituent et
vous traitent plus mal. Malheureusement, on n'efface pas l'amour de
son cœur comme un nom écrit sur une ardoise; mais, avec de la
patience, tout passe... Je désespère de me faire aimer de lui; mais je
veux qu'en me voyant passer, brillante et dédaigneuse, il me donne
un regret.
La marchande de meubles vint me dire, le lendemain, que mon
logement était prêt. Je pris mon paquet, et j'allai m'installer, 19, rue
de Buffaut, dans un petit entresol de deux pièces.
J'étais richement meublée, et je regardais mon luxe, fort inquiète de
la somme que j'avais à payer. J'avais dans ma chambre à coucher un
lit en acajou, une toilette-commode, un fauteuil Voltaire en laine
rouge, deux chaises, une petite table. Dans la première pièce, qui
servait d'antichambre ou de salle à manger, il y avait une table ronde
et quatre chaises cannelées. Je passai la journée à frotter mes
meubles... Je disais chez moi à tout propos.
Le lendemain, j'allai chez Marie, qui, je le savais bien, était trop
paresseuse pour venir.
Je me mis des nœuds de velours dans les cheveux; je fis des
reprises à ma robe et à mes bottines qui me quittaient, et je
retournai au bal.
Brididi vint à moi... Je n'étais pas fâchée de cette préférence... Je lus
à l'orchestre: Polka! Mon cœur battit; je devins très-pâle, et je dis à
Brididi:
—Je n'oserai jamais danser cela ici; personne ne sait cette danse. On
va nous regarder; je ferai quelque gaucherie, et on se moquera de
nous.
—Non, non, me dit-il, allons dans un coin.
J'allais résister, quand j'entendis derrière moi:
—Eh bien! personne ne la sait donc, cette danse?...
Je reconnus la voix de Louisa Aumont.
Ce fut moi qui pris Brididi dans mes bras, et le fis danser de force. Il
avait beau me dire:
—Je ne suis pas en mesure; j'allais toujours.
Enfin, je fis plus d'attention, et je dansai à merveille.—Je passai
plusieurs fois devant Louisa Aumont, et je me penchais si fort sur
mon danseur, qu'elle put croire que je l'embrassais. Il voulut se
reposer. Presque près d'elle, je lui criai: «Viens donc, viens donc!...»
Il n'y prit pas garde.—Je le tutoyais!...
On se mit à m'applaudir à outrance; on me suivait, on me désignait
du doigt!...
—Fi! l'horreur! disait Louisa Aumont au bras d'un vieux monsieur;
peut-on s'afficher ainsi!...
Je vis bien qu'elle était vexée!... C'est égal, le mot me piqua; je
voulus me venger. J'attendis qu'elle passât près de moi. Je l'arrêtai
par le bras, et je lui dis:
—Bonjour, ma chère Louisa; y a-t-il longtemps que vous n'êtes allée
à Versailles voir votre amant?
Elle devint pourpre, et voulut continuer son chemin; je l'arrêtai de
nouveau.
Elle me dit:—Je ne vous connais pas!
—Ah! je comprends; pardon, je prenais monsieur pour votre père. Si
j'avais su que c'était le vieux hibou qui vous ennuie tant et à qui
vous dites, pour vous débarrasser de lui trois fois la semaine, que
vous allez chez votre tante, à Versailles, je n'aurais pas parlé de
votre Henri. Mais, aussi, vous ne me prévenez pas! vous me parlez
d'un vieux monstre; je trouve monsieur très-bien, moi.
Je fis la révérence, et je partis en riant.
Je me mis à danser; tout le monde m'entoura. Je venais de faire une
méchanceté; j'étais radieuse. J'entendais dire:
—Elle est bien mieux que Pomaré!
On la quittait pour venir à moi.
—Ah! me dit Brididi, les œillades vous battent en brèche!
Tous les hommes vinrent m'inviter.
—Oh! mais, dit il, me tirant par le bras, j'aurais moins de peine à
défendre Mogador que ma danseuse!... Tiens, cria-t-il bien haut, je
vous appelle Mogador!
Ce qui se passa est ridicule, mais exact, et n'est pas assez éloigné
pour que l'on ne s'en souvienne pas!... Cent voix crièrent: vive
Mogador! On me jeta vingt bouquets dans le cercle où je dansais.
Il y eut deux camps... D'un côté, on criait: vive Pomaré! de l'autre:
vive Mogador! Les gens qui ne comprenaient rien et qui ne
saisissaient que le bruit, criaient: vive Pomador! La garde fut obligée
de s'en mêler!... Cela était si animé qu'on craignait une querelle de
partis! Je fus obligée de me sauver pour partir; on voulait me porter
en triomphe jusqu'à ma porte. Cela me fit grand peur, et, prévenue
par Brididi, je pris la fuite à temps. Pomaré fut mise dans un fiacre;
on détela les chevaux, et ce furent des jeunes gens qui la traînèrent
jusqu'à la Maison-d'Or.
Le lendemain, soit que Mabille eût payé pour faire une réclame, soit
que les maîtres de danse voulussent mettre la polka à la mode, tous
les journaux parlèrent de Pomaré et de moi.
Le Charivari nous reproduisit sous toutes les formes.
Il y avait deux autres femmes qui ne faisaient pas moins de bruit à
la Chaumière: c'étaient Maria-la-Polkeuse et Clara Fontaine.
On sait ce que c'est que Paris. Chaque excentricité a son heure de
vogue. Ces folies faisaient le sujet de toutes les conversations, et
notre triste renommée, par les cent voix de la presse, s'étendait
jusqu'en province.
Je n'avais pas vu Adolphe depuis trois semaines; il vint à Mabille par
curiosité, et demanda à voir les célébrités. Il trouva Pomaré laide!
Quand on me fit remarquer à lui, il recula d'un pas et dit:
—Vous vous trompez! ce n'est pas elle; c'est Céleste.
—Oui, dit son ami, Céleste Mogador!
Il me suivit. Je l'avais vu; j'avais senti mes jambes fléchir! Je le
montrai à Brididi.
—Diable! fit-il, il faut vous asseoir.
Adolphe vint à moi.
—Venez, j'ai à vous parler.
Je fis signe à mon danseur que j'allais revenir.
—Vous ne reviendrez pas, me dit Adolphe en me serrant le bras.
—Pourquoi donc? lui dis-je.
—Parce que je ne le veux pas.
—Ah! bah! vous avez été bien longtemps à m'apporter vos ordres; je
vous préviens que je suis changée, et je n'en veux plus recevoir! Je
veux aller danser; j'ai promis.
—N'y allez pas, me dit-il, pâle, ou je vais avoir une affaire avec ce
petit monsieur!
J'ai toujours tremblé à l'idée d'une querelle qui pouvait naître à
cause de moi. Je m'arrêtai.
—Est-ce que vous me feriez l'honneur d'être jaloux? Il y a quelque
temps, je vous avoue que ça m'aurait fait plaisir, mais aujourd'hui ça
me fait rire.
La marchande de fleurs m'apporta deux bouquets de roses qu'on me
pria d'accepter. Je les pris, mais M. Adolphe se jeta dessus et les mit
en pièces.
—Eh bien! lui dis-je, votre colère est-elle passée?
—Ne vous moquez pas de moi! me dit-il hors de lui.
Je vis qu'il fallait le prendre sur un autre ton, car il était
sérieusement en colère.
—Enfin, mon ami, que me voulez-vous? Vous m'avez quittée; vous
ne m'aimiez pas: je ne me suis pas imposée à vous; j'espère que
vous en ferez autant. Je n'ai pas troublé vos amours...
—Je n'ai jamais eu cette femme! Si elle était là, je le dirais devant
elle.
Au même instant, je la vis sortir d'un bosquet avec une autre
femme.
—Tenez, lui dis-je, la voilà! Si vous faites cela, je vous croirai.
Il hésita.
—Vous partirez avec moi?...
—Oui!
Il alla droit à Louisa, qui lui apprêtait son plus joli sourire.
—Voyons, mademoiselle, dites, je vous prie, à Céleste que je ne suis
pas votre amant, et que vous regrettez d'avoir été si dure pour elle.
Elle se mit à rire sans répondre. Il lui prit le bras et lui renouvela sa
demande.
Elle fit une affreuse grimace, devint rouge et dit:
—Oui!...
—Assez, dis-je à Adolphe, qui, je le voyais bien, lui serrait le poignet;
venez. Et nous partîmes en voiture. Il me fit mille amitiés! je restai
comme une pierre!...
—Je vois bien, me dit-il, que vous ne m'aimez plus!
—Je ne sais pas si je ne vous aime plus, mais ce qu'il y a de certain,
c'est que je vous aime moins. Vous n'avez pas de reproches à me
faire! Je vous aimais, je me serais tenue dans une boîte pour vous
plaire; vous m'avez quittée d'une façon pénible, humiliante. J'ai
cherché à me distraire et j'y parvenais bien, je vous assure. Cela
vous irrite! vous auriez mieux aimé que je m'enfermasse avec un
boisseau de charbon!... Ma foi, non!... vous ne m'aimez pas; d'autres
sont moins difficiles; je ne suis qu'embarrassée du choix!
—Vous me tourmentez à plaisir, me dit-il les larmes aux yeux.
J'allais m'attendrir, car je l'aimais encore, mais je repoussai toute
idée de faiblesse.
—Croyez-vous donc que je n'étais pas tourmentée, quand je fis la
route de Versailles à pied, la nuit, et que vous me laissâtes partir,
sans même vous inquiéter de ce que je pouvais faire dans mon
désespoir? Voyez-vous, nous ne serons pas fâchés, mais je
n'oublierai jamais cette nuit-là.
Il n'osa plus dire une parole. Il m'aimait, j'allais pouvoir me venger.
Je lui donnai un rendez-vous... j'arrivai deux heures plus tard, faisant
semblant de pas voir qu'il m'attendait avec impatience.
Il me défendit d'aller dans les endroits publics... j'y allais exprès! La
presse continuait à parler de nous... on venait en équipage nous
voir, le soir, comme des bêtes curieuses. Tous ces badauds se
disputaient une fleur de notre bouquet. Les femmes venaient nous
voir aussi; elles disaient aux gens qui les accompagnaient: «Tâchez
donc qu'elles viennent vous parler!...» On nous appelait, mais je me
dérangeais rarement.
Pomaré regardait d'un air impudent, faisait mille excentricités et
faisait fuir les curieux, rouges de honte. C'est à cause de cela, sans
doute, que beaucoup venaient autour de moi et me faisaient des
compliments.
Voilà pourtant les vilains services que vous rendent les oisifs. Ils ne
reculent devant rien, eux qui, trop vieux ou trop ennuyés d'eux-
mêmes pour savoir s'amuser, cherchent à tout prix des objets de
distraction.
J'avais ma cour, comme Pomaré avait la sienne.
—Elle est charmante! disait l'un en me lorgnant.
—Vous allez voir comme elle danse bien, disait l'autre.
—Quelle souplesse! comme elle est gracieuse!...
Voilà ce que me répétaient en masse ces gens qui me méprisaient,
qui peut-être me trouvaient affreuse, ridicule; mais ils m'excitaient,
je les amusais.—La vie est une comédie.
Quand je me reporte à ce temps et que je songe à toutes les
excentricités que nous faisions, il me semble qu'on devrait nous les
pardonner, car le succès qu'on nous a fait est plus coupable que
nous.
L'orateur qu'on approuve s'enflamme; l'acteur qu'on applaudit
redouble d'efforts; le soldat qu'on regarde, qu'on encourage devient
plus intrépide; il n'y a pas une créature, dans quelque position
qu'elle soit, qui ne soit sensible à la réclame!... Jugez si moi, sans
éducation, n'ayant rien à perdre, je ne pouvais pas me laisser éblouir
et entraîner.
Il n'est si petit théâtre qui n'ait ses rivalités. Mes succès à Mabille
m'avaient valu beaucoup d'envieuses. Les hommes me firent un
rempart d'amour et de fleurs.
Je détestais Pomaré, qui me le rendait bien.
Sans savoir pourquoi, on trouva qu'il serait charmant de nous faire
faire vis-à-vis, et nos partisans négocièrent l'affaire avec toute
l'importance d'un traité de paix.
Nous marchâmes l'une au-devant de l'autre, ne faisant pas un pas
de plus. Elle me regardait avec ses grands yeux noirs fixes!... Elle
me semblait plus désagréable que d'habitude!—J'eus envie de me
sauver; mais je réfléchis que cela serait ridicule, et je lui tendis la
main. Sa figure se dérida, et elle me dit d'une façon charmante:
—Je suis enchantée de faire connaissance avec vous; il y a
longtemps que je le désirais. Si vous voulez me permettre d'aller
vous faire une visite, je vous continuerai mon amitié.
Il y avait un air protecteur qui me déplut; mais je lui pris le bras et
nous fîmes le tour du bal ensemble.
On se pressait tellement autour de nous, que c'est à peine si nous
pouvions marcher.
Après quelques mots échangés, je vis qu'elle prenait son rôle au
sérieux et qu'elle se croyait reine. Le fait est qu'on ne l'abordait
qu'ainsi:
—Chère reine, allez-vous danser? Où vous placerez-vous, que vos
courtisans vous entourent?
Elle leur indiquait un endroit à voix basse. Ils s'en allaient tout fiers
et prenaient un air de protection avec leurs amis qu'ils plaçaient.
—Tenez! voilà la reine Pomaré! disaient les uns en se poussant.
—Où ça?... disaient les autres ébahis.
On la montrait.
—Ah! c'est vrai; elle a l'air bien sauvage, répondait un provincial qui
la prenait pour la reine de Taïti.
On avisa un abonné à lunettes, pion dans un collége, je crois: on
l'entoura, on l'applaudit en lui disant:
—Bravo, Pritchard!
Le pauvre homme perdit la tête et fit trente-six gambades... On le
porta en triomphe... Il enfonçait ses lunettes, relevait la tête, se
croyant un grand personnage; mais, comme on ne le payait pas pour
cet exercice, le pauvre diable fut renvoyé de sa place. Il conta ses
peines à Pomaré, qui lui dit devant moi, avec le plus grand sang-
froid du monde:
—Venez me voir, je vous protégerai.
Je ne pus m'empêcher de rire, en me disant:
—Ils sont aussi fous l'un que l'autre.
Elle était convaincue de sa puissance!... C'était de bonne foi qu'elle
promettait de lui faire du bien; pourtant elle paraissait avoir un esprit
supérieur. Je voulus connaître ce caractère, qui me parut étrange.
Depuis notre séparation, Marie m'avait tenu parole; elle ne rentrait
plus, je la voyais à peine.
Je demandai à Pomaré de passer la journée du lendemain avec moi.
Elle me dit qu'elle ne commandait sa voiture qu'à quatre heures; que
le matin elle recevait sa cour.
—Venez déjeuner chez moi, me dit-elle, nous causerons en fumant
une cigarette.
Elle me quitta, puis revint à moi:
—Voulez-vous souper avec moi et quelques-uns de mes amis?...
Je répondis oui, avant qu'elle eût fini sa phrase, car j'avais grande
envie de passer une soirée avec elle.
—Je vais vous prendre dans ma voiture.
Nous sortîmes. A la porte, elle fronça ses sourcils noirs...
—Jean! Jean! dit-elle deux fois, impatientée.
Un gamin d'une douzaine d'années s'avança en courant. Il était
habillé d'une façon burlesque: un pantalon de toile grise rentré dans
ses bottes à revers, une redingote dont la taille seule lui aurait fait
un paletot, un chapeau très grand, bordé d'un galon en clinquant
dont on se sert pour costume. Il portait dans ses bras un châle-tapis
en coton, tout passé, dont un coin traînait à terre. Pomaré le lui
arracha des mains, furieuse.
—Imbécile! tu ne peux pas faire attention!... tu essuies le pavé avec
mon cachemire... Si tu ne fais pas mieux ton service, je te chasserai.
Tout le monde riait autour d'elle. Je devins fort rouge. Le petit sortit
en haussant les épaules et fit avancer la voiture.
C'était une calèche à deux chevaux... elle l'avait au mois... C'était un
équipage presque grotesque, sortant des remises d'un mauvais
loueur.
—La voiture de la reine! crièrent à la fois dix gamins.
Pritchard s'avança pour lui baiser la main. Elle jeta en l'air quelques
pièces de monnaie; on se bouscula pour les ramasser en criant:
—Vive la reine Pomaré!
Le petit bonhomme était sur le siége, à côté du cocher, qui avait le
même galon de clinquant sur son chapeau et une redingote noisette.
Ajoutez à cela un cheval blanc et un autre bai. La calèche était
garnie à l'intérieur d'une vieille étoffe d'un rouge passé.
Je ne me connaissais pas en voiture, mais il me semblait que, pour
rien au monde, je n'aurais voulu sortir là-dedans en plein jour.
Nous arrivâmes au café Anglais.
Le maître du café vint au-devant de nous, la serviette sous le bras,
l'air affable:
—Quel cabinet désirez-vous? demanda-t-il à Pomaré.
—Le grand salon, dit-elle en regardant sa suite.
Il commençait à faire froid, elle ordonna qu'on fît bon feu. Quand la
porte s'ouvrait, nous apercevions plusieurs têtes qui cherchaient à
nous voir.
On alluma du petit bois... Elle s'approcha du feu en faisant un
mouvement de frisson.
—Les premiers froids font mal, me dit-elle.
Je la regardai; elle était pâle comme une morte.
—Gardez votre châle, lui dis-je.
Mais elle n'en fit rien. Je compris pourquoi; elle avait une robe de
taffetas bleu clair qui jurait passablement avec son châle.
Elle toussa une fois ou deux. Elle prit une coupe de champagne
frappé et l'avala d'un trait. Ses yeux brillèrent, ses couleurs lui
revinrent.
Alors on lui fit chanter une chanson, qu'elle dit avoir composée elle-
même, et qui passait en revue tous les dieux de la Mythologie. Je ne
compris pas bien, parce que je ne connaissais pas tous ces noms-là;
mais il paraît que c'était un chef-d'œuvre d'esprit.
Sa voix était faible; elle s'accompagnait au piano. Ses mains étaient
blanches, bien faites et paraissaient avoir l'habitude du clavier, car
elle ne regardait en chantant que ceux qui l'écoutaient.
Ce furent des compliments sans fin. On fit très-peu d'attention à
moi. Il me semble que j'en fus un peu jalouse et qu'elle me regardait
d'un air vainqueur.
Elle parla, fuma, tint tête à tout le monde: elle avait un esprit
intarissable et d'une originalité sans pareille.
La nuit se passa ainsi. Quand nous sortîmes, il faisait jour; ces
sorties sont curieuses et le paraîtraient surtout aux gens
raisonnables qui n'ont jamais vu ce spectacle, bien propre à
désillusionner des folles joies des viveurs de Paris.
Je ne sais au juste quelle figure j'avais; mais en regardant les autres,
je fus presque effrayée: les hommes sont débraillés, quelquefois
chancelants; les femmes sont jaunes; leurs toilettes chiffonnées,
enfumées, leur donnent l'air de paquets de chiffons; la plus jolie
serait laide.
Il y a aux portes de vieilles voitures qui passent la nuit; un pauvre
cheval maigre attend que son maître ait chargé pour avoir son
déjeuner. On ne veut pas de lui, et on l'appelle rosse. Pauvre bête! il
a été jeune et bon! Je regardai Pomaré, et je me dis:
—Voilà notre avenir!
Il n'y a, à cette heure-là, dans les rues, que les balayeurs et les
chiffonniers. Les premiers vous regardent, appuyés sur leurs balais,
et chacun se dit probablement:
—Ce que ces fous viennent de dépenser dans une nuit me ferait
vivre un an.
On leur donne quelquefois de l'argent, mais on passe le plus souvent
sans les regarder.
Nous rentrions à pied. Pomaré paraissait moins fatiguée que tout le
monde; elle était d'une pâleur extrême, mais ses lèvres étaient
rouges, ses yeux brillants.
Nous traversions le boulevard; une balayeuse comme il y en a peu,
elle travaillait activement, envoya toute sa poussière dans les jambes
de Pomaré, qui, peu endurante de sa nature, l'interpella en
l'appelant bête.
La femme au balai l'entreprit à son tour.
—Tiens! voyez-vous ça, madame l'embarras! Faut-il pas que je quitte
mon ouvrage pour faire place à cette demoiselle! Avec ça qu'elle est
belle! J'ai été un peu mieux que toi, ma petite, et un peu plus
huppée; mais j'étais pas fière avec le pauvre monde!
Nous étions déjà bien loin. Nous nous séparâmes quelques instants
après.
—A tantôt! dit Pomaré, rue Gaillon, 19. Si vous oubliez le numéro,
demandez dans la rue la reine Pomaré.
Je trouvai cette gasconnade prodigieuse, et il me sembla plus
prudent de me souvenir de la rue et du numéro.
Je venais de voir un échantillon de cette vie qui, de loin, me semblait
si belle et à laquelle j'avais si souvent rêvé de m'associer. Je puis me
rendre cette justice, que toute cette joie me parut triste, et que je
rentrai chez moi le cœur bien vide et l'âme bien découragée.
XI
LA REINE POMARÉ.

A onze heures, j'étais chez ma nouvelle amie. Je m'attendais à voir


un boudoir richement meublé; je fus toute surprise de me trouver
dans un chenil: c'est du moins l'effet que me fit le logement de
Pomaré, tant il y avait dans ce logement de désordre et de
malpropreté.
Elle habitait une grande chambre, à peine meublée; sa commode
était couverte d'une foule de petits objets, rappelant ses triomphes
au bal Mabille.
Il y avait sur chaque objet un pouce de poussière; on voyait sur une
table des papiers en désordre, une masse de numéros du Charivari;
sa robe bleue traînait à terre.
Je remarquai, appendue au mur, une bonne Vierge en plâtre avec un
petit collier et une couronne. La Vierge, avec ses bras ouverts,
semblait contempler ce désordre et le prendre en pitié. Sur la
cheminée, la reine avait mis son chapeau dans une assiette. Je
n'osais pas faire un pas. Elle était encore couchée, tête nue et les
cheveux tout ébouriffés.
—Pardonnez-moi, me dit-elle; mon ménage n'est pas encore fait. La
personne qui me loue se charge de tout faire et ne fait rien.
Asseyez-vous donc!
Et elle me montra du regard le bord de son lit.
Je m'approchai, mais j'étais fâchée d'être venue. Elle sauta en bas
du lit, passa dans une espèce d'antichambre dont la croisée donnait
sur une cour; elle appela son portier, qui était en même temps son
propriétaire; il monta.
—Faites-nous à déjeuner.
—Je veux bien; mais donnez-moi de l'argent.
—Je n'en ai pas.
—Bah! dit le vieux, vous avez bien vingt sous.
—Non, dit-elle, pas un liard.
—Alors, allez déjeuner où vous voudrez; je ne fais plus crédit.
—Voyons, ne soyez pas méchant! J'ai invité une amie; je ne peux
pas la renvoyer.
—Bon! dit le vieux, ce n'est pas assez de vous; il faut maintenant
que je nourrisse les autres.
Et il descendit en grognant.
J'avais tout entendu et j'étais fort embarrassée. Elle ne perdit pas
contenance, et me dit en rentrant que nous allions déjeuner dehors,
parce que son domestique n'était pas rentré; que le concierge venait
de l'en prévenir.
C'était trop fort; je me mordis les lèvres pour ne pas rire aux éclats.
Je l'avais vue, la veille, jeter au moins dix francs de monnaie.
Évidemment elle était folle.
—Ah! dit-elle, en passant devant la petite Vierge, je t'avais oubliée.
Elle prit cette petite Vierge, l'embrassa, lui adressa quelques paroles
qui ressemblaient à une prière. J'entendis ces mots: «Sainte Mère de
Dieu, aie bien soin de lui; il est plus heureux près de toi.»
Elle remit la Vierge à sa place et revint près de moi. Le mouvement
de ses bras fit ouvrir sa chemise; je vis sur sa poitrine un scapulaire,
des médailles, une petite croix. Cela me fit mal.
J'ai une grande croyance en Dieu; quand je souffre, je l'invoque;
quand je suis heureuse, je le remercie; mais ces insignes de la
religion chrétienne me semblaient un blasphème sur sa poitrine.
Je priai Pomaré de m'attendre; je descendis quelques instants, et je
revins avec tout ce qu'il nous fallait pour déjeuner.
—Je vais vous rendre tout-à-l'heure ce que vous avez dépensé, me
dit-elle avec un aplomb incroyable.
«Dieu! la vilaine femme! me disais-je, elle est menteuse comme
personne.»
Je lui fis deux ou trois questions sur sa vie passée; elle détourna la
conversation sans vouloir me répondre. J'insistai de nouveau
pendant le déjeuner; même silence.
J'avais pourtant bien envie de savoir ce qu'elle avait été, car tout le
monde en était intrigué.
—Allons, me dit-elle, après déjeuner, vous êtes une bonne fille,
promettez-moi de ne rien dire à personne, et je vais vous faire ma
confession.
Je le lui promis; elle commença:
...—Je suis venue au monde à Paris, en 1825; mon père était riche;
je fus son premier enfant. Il avait environ cent cinquante mille francs
de capital; ces cent cinquante mille francs étaient placés dans un
théâtre et lui rapportaient quinze, quelquefois vingt pour cent. On
n'épargna rien pour m'élever. Je fus mise dans un des premiers
pensionnats de Paris. J'avais les meilleurs maîtres.—Ma mère m'avait
donné deux frères et deux sœurs; cependant on ne retrancha rien
des dépenses qu'on faisait pour moi. J'avais dix-sept ans, on pensait
à me marier, mais on ne trouvait aucun parti digne de moi. J'aimais
mon père avec passion, tout en le craignant beaucoup; ma mère ne
le rendait pas très heureux.
Un jour, j'entendis raconter à la pension qu'un incendie affreux
venait de faire des ravages horribles, boulevard du Temple; on
assurait que personne n'avait péri, mais que l'on n'avait rien pu
sauver. Cela me rendit triste. Pourtant j'étais loin de me douter que
ce malheur me touchât de si près.
Deux jours après, mon père vint me voir. Mon père était grand et
fort; je fus effrayée du changement que je vis en lui: il était ployé
sur lui-même, ses yeux étaient rouges, il avait pleuré. Je lui sautai
au cou et le couvris de baisers.
—Mon père, mon bon père, que vous est-il donc arrivé? Ma mère!
mes frères! mes sœurs!
—Non, me dit-il; grâce à Dieu, ils se portent bien; mais je suis ruiné,
le feu a tout dévoré. Je n'étais pas assuré, mes enfants! ma pauvre
Lise! me dit-il en me serrant dans ses bras.
Je n'avais jamais vu pleurer mon père; cela me déchira le cœur.
J'étais très-pieuse; j'avais souvent parlé de mon désir d'entrer dans
un couvent; on se moquait de moi, et je n'insistais pas. L'idée m'en
vint ce jour-là, et je dis à mon père, en lui essuyant les yeux:
—Ne vous inquiétez pas pour moi, mon cher père; vous savez bien
que mon vœu le plus cher est d'être religieuse; vous n'aurez pas à
vous occuper de moi.
—Non, me dit-il en me serrant sur son cœur, non, mon enfant, tu ne
peux pas être à Dieu tout entière; j'ai besoin de toi pour élever tes
frères et tes sœurs; tu es instruite, tu feras leur éducation. Ta mère
est presque folle de chagrin, il faut la consoler, l'aider. Je viens te
chercher.
Je suivis mon père sans répondre. On ne répondait jamais à mon
père.
Rentrée chez nous, je trouvai tout le monde dans le désespoir.
Mon père sortait souvent pour des entreprises qu'il voulait essayer.
Ma mère avait un peu perdu la tête; j'avais soin des enfants.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebooknice.com

You might also like