0% found this document useful (0 votes)
12 views

Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-Centric Applications with PL/pgSQL 1st Edition Baji Shaik download

The document is a promotional overview of the book 'Procedural Programming with PostgreSQL PL/pgSQL' by Baji Shaik and Dinesh Kumar Chemuduru, which focuses on designing complex database-centric applications using PL/pgSQL. It includes links to download the book and other related titles, along with information about the authors and the book's content structure. The document also emphasizes the importance of the book for those interested in PostgreSQL programming.

Uploaded by

noguemmimran
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
0% found this document useful (0 votes)
12 views

Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-Centric Applications with PL/pgSQL 1st Edition Baji Shaik download

The document is a promotional overview of the book 'Procedural Programming with PostgreSQL PL/pgSQL' by Baji Shaik and Dinesh Kumar Chemuduru, which focuses on designing complex database-centric applications using PL/pgSQL. It includes links to download the book and other related titles, along with information about the authors and the book's content structure. The document also emphasizes the importance of the book for those interested in PostgreSQL programming.

Uploaded by

noguemmimran
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

Procedural Programming with PostgreSQL PL/pgSQL:

Design Complex Database-Centric Applications


with PL/pgSQL 1st Edition Baji Shaik install
download
https://ebookmeta.com/product/procedural-programming-with-
postgresql-pl-pgsql-design-complex-database-centric-applications-
with-pl-pgsql-1st-edition-baji-shaik/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

PostgreSQL Configuration: Best Practices for


Performance and Security 1st Edition Baji Shaik

https://ebookmeta.com/product/postgresql-configuration-best-
practices-for-performance-and-security-1st-edition-baji-shaik-2/

PostgreSQL Configuration: Best Practices for


Performance and Security 1st Edition Baji Shaik

https://ebookmeta.com/product/postgresql-configuration-best-
practices-for-performance-and-security-1st-edition-baji-shaik/

Resilient Oracle PL/SQL: Building Resilient Database


Solutions for Continuous Operation 1st Edition Stephen
Morris

https://ebookmeta.com/product/resilient-oracle-pl-sql-building-
resilient-database-solutions-for-continuous-operation-1st-
edition-stephen-morris/

Home to Roost Bower Colorado 1st Edition Reina Torres


Torres Reina

https://ebookmeta.com/product/home-to-roost-bower-colorado-1st-
edition-reina-torres-torres-reina/
Pull Up a Chair Recipes from My Family to Yours Rachel
Holtzman

https://ebookmeta.com/product/pull-up-a-chair-recipes-from-my-
family-to-yours-rachel-holtzman/

Moving Ideas Multimodality and Embodied Learning in


Communities and Schools New Literacies and Digital
Epistemologies Mira-Lisa Katz (Editor)

https://ebookmeta.com/product/moving-ideas-multimodality-and-
embodied-learning-in-communities-and-schools-new-literacies-and-
digital-epistemologies-mira-lisa-katz-editor/

St Dominic The Story of a Preaching Friar Donald J


Goergen

https://ebookmeta.com/product/st-dominic-the-story-of-a-
preaching-friar-donald-j-goergen/

Bred by the Mafia Monster Breeding Season 7 1st Edition


Sam Crescent Stacey Espino

https://ebookmeta.com/product/bred-by-the-mafia-monster-breeding-
season-7-1st-edition-sam-crescent-stacey-espino/

The Other Alcott 1st Edition Elise Hooper

https://ebookmeta.com/product/the-other-alcott-1st-edition-elise-
hooper/
Routledge Handbook of Identity Studies 2nd Edition
Anthony Elliott

https://ebookmeta.com/product/routledge-handbook-of-identity-
studies-2nd-edition-anthony-elliott/
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 orders-ny@springer-sbm.com, 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 booktranslations@springernature.com; for reprint,
paperback, or audio rights, please e-mail bookpermissions@springernature.com.
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
Random documents with unrelated
content Scribd suggests to you:
"Then at regular hours I ceased working; and when I prayed with
my arms extended, I felt as though a fountain of mercy were
pouring from the height of heaven into my heart. That fountain is
now dried up. Why?"
(He walks up and down slowly, within the circuit of the rocks.)
"All blamed me when I left the house. My mother sank to the
ground, dying; my sister from afar off made signs to me to return;
and the other—wept, Ammonaria, the child whom I used to meet
every evening at the cistern, when she took the oxen to drink. She
ran after me. Her foot rings glittered in the dust; and her tunic, open
at the hips, fluttered loosely in the wind. The aged anchorite who
was leading me away called her vile names. Our two camels
galloped forward without respite; and I have seen none of my
people since that day.
"At first, I selected for my dwelling place, the tomb of a Pharaoh.
But an enchantment circulates through all those subterranean
palaces, where the darkness seems to have been thickened by the
ancient smoke of the aromatics. From the depths of Sarcophagi, I
heard doleful voices arise, and call my name; or else, I suddenly
beheld the abominable things painted upon the walls live and move;
and I fled away to the shore of the Red Sea, and took refuge in a
ruined citadel. There my only companions were the scorpions
dragging themselves among the stones, and the eagles continually
wheeling above my head, in the blue of heaven. At night I was torn
by claws, bitten by beaks; soft wings brushed against me; and
frightful demons, shrieking in my ears, flung me upon the ground.
Once I was even rescued by the people of a caravan going to
Alexandria; and they took me away with them.
"Then I sought to obtain instruction from the good old man
Didymus. Although blind, none equalled him in the knowledge of the
Scriptures. When the lesson was finished, he used to ask me to give
him my arm to lean upon, that we might walk together. Then I
would conduct him to the Paneum, whence may be seen the Pharos
and the open sea. Then we would return by way of the post,
elbowing men of all nations, even Cimmerians clad in the skins of
bears and Gymnosophists of the Ganges anointed with cow-dung.
But there was always some fighting in the streets—either on account
of the Jews refusing to pay taxes, or of seditious people who wished
to drive the Romans from the city. Moreover, the city is full of
heretics—followers of Manes; Valentinus, Basilides, Arius—all seeking
to engross my attention in order to argue with me and to convince
me.
"Their discourses often come back to my memory. Vainly do I seek
to banish them from my mind. They trouble me!
"I took refuge at Colzin, and there lived a life of such penance that I
ceased to fear God. A few men, desirous of becoming anchorites,
gathered about me. I imposed a practical rule of life upon them,
hating, as I did, the extravagance of Gnosus and the assertions of
the philosophers. Messages were sent to me from all parts, and men
came from afar off to visit me.
"Meanwhile the people were torturing the confessors; and the thirst
of martyrdom drew me to Alexandria. The persecution had ceased
three days before I arrived there!
"While returning thence, I was stopped by a great crowd assembled
before the temple of Serapis. They told me it was a last example
which the Governor had resolved to make. In the centre of the
portico, under the sunlight, a naked woman was fettered to a
column, and two soldiers were flogging her with thongs; at every
blow her whole body writhed. She turned round, her mouth open;
and over the heads of the crowd, through the long hair half hiding
her face, I thought that I could recognize Ammonaria....
... through the long hair half hiding her face, I thought that I could recognize
Ammonaria ...
"Nevertheless ... this one was taller ... and beautiful ... prodigiously
beautiful!"
(He passes his hands over his forehead.)
"No! no! I must not think of it!
"Another time Athanasius summoned me to assist him against the
Arians. The contest was limited to invectives and laughter. But since
that time he has been calumniated, dispossessed of his see, obliged
to fly for safety elsewhere. Where is he now? I do not know! The
people give themselves very little trouble to bring me news. All my
disciples have abandoned me—Hilarion like the rest.
"He was perhaps fifteen years of age when he first came to me and
his intelligence was so remarkable that he asked me questions
incessantly. Then he used to listen to me with a pensive air, and
whatever I needed he brought it to me without a murmur—nimbler
than a kid, merry enough to make even the patriarchs laugh. He was
a son to me."
(The sky is red; the earth completely black. Long drifts of sand
follow the course of the gusts of wind, rising like great shrouds and
falling again. Suddenly against a bright space in the sky a flock of
birds pass, forming a triangular battalion, gleaming like one sheet of
metal, of which the edges alone seem to quiver.
Anthony watches them.)
"Ah, how I should like to follow them.
"How often also have I enviously gazed upon those long vessels,
whose sails resemble wings—and above all when they were bearing
far away those I had received at my hermitage! What pleasant hours
we passed!—what out-pourings of feeling! No one ever interested
me more than Ammon: he told me of his voyage to Rome, of the
Catacombs, the Coliseum, the piety of illustrious women, and a
thousand other things!—and it grieved me to part with him!
Wherefore my obstinacy in continuing to live such a life as this? I
would have done well to remain with the monks of Nitria, inasmuch
as they supplicated me to do so. They have cells apart, and
nevertheless communicate with each other. On Sundays a trumpet
summons them to assemble at the church, where one may see three
scourges hanging up, which serve to punish delinquents, robbers,
and intruders; for their discipline is severe.
"Nevertheless they are not without some enjoyments. The faithful
bring them eggs, fruits, and even instruments with which they can
extract thorns from their feet. There are vineyards about Prisperi;
those dwelling at Pabena have a raft on which they may journey
when they go to seek provisions.
"But I might have served my brethren better as a simple priest. As a
priest one may aid the poor, administer the sacraments, and exercise
authority over families.
"Furthermore, all laics are not necessarily damned, and it only
depended upon my own choice to become—for example—a
grammarian, a philosopher. I would then have had in my chamber a
sphere of reeds, and tablets always ready at hand, young men
around me, and a wreath of laurel suspended above my door, as a
sign.
"But there is too much pride in triumphs such as those. A soldier's
life would have been preferable. I was robust and bold: bold enough
to fasten the cables of the military machines—to traverse dark
forests, or to enter, armed and helmeted, into smoking cities....
Neither was there anything to have prevented me from purchasing
with my money the position of publican at the toll-office of some
bridge; and travellers would have taught me many strange things,
and told me strange stories, the while showing me many curious
objects packed up among their baggage....
"The merchants of Alexandria sail upon the river Canopus on
holidays, and drink wine in the chalices of lotus-flowers, to a music
of tambourines which makes the taverns along the shore tremble!
Beyond, trees, made cone-shaped by pruning, protect the quiet
farms against the wind of the south. The roof of the lofty house
leans upon thin colonettes placed as closely together as the laths of
a lattice; and through their interspaces the master, reclining upon his
long couch, beholds his plains stretching about him—the hunter
among the wheat-fields—the winepress where the vintage is being
converted into wine, the oxen treading out the wheat. His children
play upon the floor around him; his wife bends down to kiss him."
(Against the grey dimness of the twilight, here and there appear
pointed muzzles, with straight, pointed ears and bright eyes.
Anthony advances toward them. There is a sound of gravel
crumbling down; the animals take flight. It was a troop of jackals.
One still remains, rising upon his hinder legs, with his body half
arched and head raised in an attitude full of defiance.)
"How pretty he is! I would like to stroke his back gently!"
(Anthony whistles to coax him to approach. The jackal disappears.)
"Ah! he is off to join the others. What solitude! what weariness!"
(Laughing bitterly.)
"A happy life this indeed!—bending palm-branches in the fire to
make shepherds' crooks, fashioning baskets, stitching mats together
—and then exchanging these things with the Nomads for bread
which breaks one's teeth! Ah! woe, woe is me! will this never end?
Surely death were preferable! I can endure it no more! Enough!
enough!"
(He stamps his foot upon the ground, and rushes frantically to and
fro among the rocks; then pauses, out of breath, bursts into tears,
and lies down upon the ground, on his side.
The night is calm; multitudes of stars are palpitating; only the
crackling noise made by the tarantulas is audible.
The two arms of the cross make a shadow upon the sand; Anthony,
who is weeping, observes it.)
"Am I, then, so weak, O my God! Courage, let me rise from here!"
(He enters his hut, turns over a pile of cinders, finds a live ember,
lights his torch and fixes it upon the wooden desk, so as to throw a
light upon the great book.)
"Suppose I take the Acts of the Apostles?—yes!—no matter where!"
'And he saw the heaven opened, and a certain vessel descending, as
it were a great linen sheet let down by the four corners from heaven
to the earth—wherein were all manner of four-footed beasts, and
creeping things of the earth and fowls of the air. And there came a
voice to him: Arise, Peter! Kill and eat!'[1]
"Then the Lord desired that his apostle should eat of all things?...
while I...."
(Anthony remains thoughtful, his chin resting against his breast. The
rustling of the pages, agitated by the wind, causes him to lift his
head again; and he reads:)
'So the Jews made a great slaughter of their enemies with the
sword, and killed them, repaying according to what they had
prepared to do to them....[2]
"Then, comes the number of people slain by them—seventy-five
thousand. They had suffered so much! Moreover, their enemies were
the enemies of the true God. And how they must have delighted in
avenging themselves thus by the massacre of idolaters! Doubtless
the city must have been crammed with the dead! There must have
been corpses at the thresholds of the garden gates, upon the
stairways, in all the chambers, and piled up so high that the doors
could no longer move upon their hinges!... But lo! here I am
permitting my mind to dwell upon ideas of murder and of blood!..."
(He opens the book at another place.)
'Then King Nabuchodonosor fell on his face, and worshipped
Daniel....'[3]
"Ah! that was just! The Most High exalts his prophets above Kings;
yet that monarch spent his life in banqueting, perpetually drunk with
pleasure and pride. But God, to punish him, changed him into a
beast! He walked upon four feet!"
(Anthony begins to laugh; and in extending his arms, involuntarily
disarranges the leaves of the book with the tips of his fingers. His
eyes fell upon this phrase:—)
'And Ezechias rejoiced at their coming, and he showed them the
house of his aromatical spices, and the gold and the silver, and
divers precious odours and ointments, and the house of his vessels,
and all that he had in his treasures....'[4]
"I can imagine that spectacle; they must have beheld precious
stones, diamonds and darics heaped up to the very roof. One who
possesses so vast an accumulation of wealth is no longer like other
men. While handling his riches he knows that he controls the total
result of innumerable human efforts—as it were the life of nations
drained by him and stored up, which he can pour forth at will. It is a
commendable precaution on the part of Kings. Even the Wisest of all
did not neglect it. His navy brought him elephants' teeth and apes....
Where is that passage?"
(He turns the leaves over rapidly.)
"Ah! here it is:"
'And the Queen of Saba, having heard of the fame of Solomon in the
name of the Lord, came to try him with hard questions.'[5]
"How did she hope to tempt him? The Devil indeed sought to tempt
Jesus! But Jesus triumphed because he was God; and Solomon,
perhaps, owing this knowledge of magic! It is sublime—that science!
For the world—as a philosopher once explained it to me, forms a
whole, of which all parts mutually influence one another, like the
organs of one body. It is science which enables us to know the
natural loves and natural repulsions of all things, and to play upon
them?... Therefore, it is really possible to modify what appears to be
the immutable order of the universe?"
(Then the two shadows formed behind him by the arms of the cross,
suddenly lengthen and project themselves before him. They assume
the form of two great horns. Anthony cries out:—)
"Help me! O my God!"
Saint Anthony: Help me, O my God!

(The shadows shrink back to their former place.)


"Ah!... it was an illusion ... nothing more. It is needless for me to
torment my mind further! I can do nothing!—absolutely nothing."
(He sits down and folds his arms.)
"Nevertheless ... it seems to me that I felt the approach of.... But
why should He come? Besides, do I not know all his artifices? I
repulsed the monstrous anchorite who laughingly offered me little
loaves of warm, fresh bread, the centaur who sought to carry me
away upon his croup, and that black child who appeared to me in
the midst of the sands, who was very beautiful, and who told me
that he was called the Spirit of Lust!"
(Anthony rises and walks rapidly up and down, first to the right, then
to the left.)
"It was by my order that this multitude of holy retreats was
constructed—full of monks all wearing sackcloth of camel's hair
beneath their garments of goatskin, and numerous enough to form
an army. I have cured the sick from afar off; I have cast out
demons; I have passed the river in the midst of crocodiles; the
Emperor Constantine wrote me throe letters; Balacius, who had spat
upon mine, was torn to pieces by his own horses; when I
reappeared the people of Alexandria fought for the pleasure of
seeing me, and Athanasius himself escorted me on the way back.
But what works have I not accomplished Lo! for these thirty years
and more I have been dwelling and groaning unceasingly in the
desert! Like Eusebius, I have carried thirty-eight pounds of bronze
upon my loins; like Macarius, I have exposed my body to the stings
of insects; like Pacomus, I have passed fifty-three nights without
closing my eyes; and those who are decapitated, tortured with red
hot pincers, or burned alive, are perhaps less meritorious than I,
seeing that my whole life is but one prolonged martyrdom."
(Anthony slackens his pace.)
"Assuredly there is no human being in a condition of such
unutterable misery! Charitable hearts are becoming scarcer. I no
longer receive aught from any one. My mantle is worn out. I have no
sandals—I have not even a porringer!—for I have distributed all I
possessed to the poor and to my family, without retaining so much
as one obolus. Yet surely I ought to have a little money to obtain the
tools indispensable to my work? Oh, not much! a very small sum.... I
would be very saving of it....
"The fathers of Nicæa, clad in purple robes, sat like magi, upon
thrones ranged along the walls; and they were entertained at a
great banquet and overwhelmed with honours, especially
Paphnutius, because he is one-eyed and lame, since the persecution
of Diocletian! The Emperor kissed his blind eye several times; what
foolishness! Besides, there were such infamous men members of
that Council! A bishop of Scythia, Theophilus! another of Persia,
John! a keeper of beasts, Spiridion! Alexander was too old.
Athanasius ought to have shown more gentleness towards the
Arians, so as to have obtained concessions from them.
"Yet would they have made any? They would not hear me! The one
who spoke against me—a tall young man with a curly beard—uttered
the most captious objections to my argument; and while I was
seeking words to express my views they all stared at me with their
wicked faces, and barked like hyenas. Ah! why cannot I have them
all exiled by the Emperor! or rather have them beaten, crushed, and
see them suffer! I suffer enough myself."
(He leans against his cabin in a fainting condition.)
"It is because I have fasted too long; my strength is leaving me. If I
could eat—only once more—a piece of meat." (He half closes his
eyes with languor.)
"Ah! some red flesh—a bunch of grapes to bite into ... curdled milk
that trembles on a plate!...
"But what has come upon me? What is the matter with me? I feel
my heart enlarging like the sea, when it swells before the storm. An
unspeakable feebleness weighs down upon me, and the warm air
seems to waft me the perfume of a woman's hair. No woman has
approached this place; nevertheless?—"
(He gazes toward the little pathway between the rocks.)
"That is the path by which they come, rocked in their litters by the
black arms of the eunuchs. They descend and joining their hands,
heavy with rings, kneel down before me. They relate to me all their
troubles. The desire of human pleasure tortures them; they would
gladly die; they have seen in their dreams God calling to them ...
and all the while the hems of their robes fall upon my feet. I repel
them from me. 'Ah! no!' they cry, 'not yet! What shall I do?' They
gladly accept any penitence I impose on them. They ask for the
hardest of all; they beg to share mine and to live with me.
"It is now a long time since I have seen any of them! Perhaps some
of them will come! why not? If I could only hear again, all of a
sudden, the tinkling of mule-bells among the mountains. It seems to
me...."
(Anthony clambers upon a rock at the entrance of the pathway, and
leans over, darting his eyes into the darkness.)
"Yes! over there, far off I see a mass moving, like a band of
travellers seeking the way. She is there!... They are making a
mistake." (Calling.)
"This way! Come! Come!"
(Echo repeats: Come! Come! he lets his arms fall, stupefied.)
"What shame for me! Alas! poor Anthony."
(And all of a sudden he hears a whisper:—"Poor Anthony"!)
"Who is there? Speak!"
(The wind passing through the intervals between the rocks, makes
modulations; and in those confused sonorities he distinguishes
Voices, as though the air itself were speaking. They are low,
insinuating, hissing.)
The First: "Dost thou desire women?"
The Second: "Great heaps of money, rather!"
The Third: "A glittering sword?" (and)
The Others: "All the people admire thee! Sleep!"
"Thou shalt slay them all, aye, thou shalt slay them!"
(At the same moment objects become transformed. At the edge of
the cliff, the old palm tree with its tuft of yellow leaves, changes into
the torso of a woman leaning over the abyss, her long hair waving in
the wind.
Anthony turns toward his cabin; and the stool supporting the great
book whose pages are covered with black letters, seems to him
changed into a bush all covered with nightingales.)
"It must be the torch which is making this strange play of light.... Let
us put it out!"
(He extinguishes it; the obscurity becomes deeper, the darkness
profound.
And suddenly in the air above there appear and disappear
successively—first, a stretch of water; then the figure of a prostitute;
the corner of a temple, a soldier; a chariot with two white horses,
prancing.
These images appear suddenly, as in flashes—outlined against the
background of the night, like scarlet paintings executed upon ebony.
Their motion accelerates. They defile by with vertiginous rapidity.
Sometimes again, they pause and gradually pale and melt away; or
else float off out of sight, to be immediately succeeded by others.
Anthony closes his eyelids.
They multiply, surround him, besiege him. An unspeakable fear takes
possession of him; and he feels nothing more of living sensation,
save a burning contraction of the epigastrium. In spite of the tumult
in his brain, he is aware of an enormous silence which separates him
from the world. He tries to speak;—impossible! He feels as though
all the bands of his life were breaking and dissolving;—and, no
longer able to resist, Anthony falls prostrate upon his mat.)

[1] Acts X: 11-13—T.


[2] Esther IX: 5—T.
[3] Daniel II: 46.—T.
[4] Kings XX: 13 (Vulg.).—T.
[5] III Kings X: I (Vulg.).—T.

II

(Then a great shadow, subtler than any natural shadow, and


festooned by other shadows along its edges, defines itself upon the
ground.
It is the Devil, leaning upon the roof of the hut, and bearing beneath
his wings—like some gigantic bat suckling its little ones—the Seven
Deadly Sins, whose grimacing heads are dimly distinguishable.
With eyes still closed, Anthony yields to the pleasure of inaction; and
stretches his limbs upon the mat.
It seems to him quite soft, and yet softer—so that it becomes as if
padded; it rises up; it becomes a bed. The bed becomes a shallop;
water laps against its sides.
To right and left rise two long tongues of land, overlooking low
cultivated plains, with a sycamore tree here and there. In the
distance there is a tinkling of bells, a sound of drums and of singers.
It is a party going to Canopus to sleep upon the temple of Serapis,
in order to have dreams. Anthony knows this; and impelled by the
wind, his boat glides along between the banks. Papyrus-leaves and
the red flowers of the nymphæa, larger than the body of a man,
bend over him. He is lying at the bottom of the boat; one oar at the
stem, drags in the water. From time to time, a lukewarm wind blows;
and the slender reeds rub one against the other, and rustle. Then
the sobbing of the wavelets becomes indistinct. A heavy drowsiness
falls upon him. He dreams that he is a Solitary of Egypt.
Then he awakes with a start.)
"Did I dream? It was all so vivid that I can scarcely believe I was
dreaming! My tongue burns. I am thirsty."
(He enters the cabin, and gropes at random in the dark.)
"The ground is wet; can it have been raining? What can this mean!
My pitcher is broken into atoms! But the goatskin?" (He finds it.)
"Empty!—completely empty! In order to get down to the river, I
should have to walk for at least three hours; and the night is so dark
that I could not see my way.
"There is a gnawing in my entrails. Where is the bread!"
(After long searching, he picks up a crust not so large as an egg.)
"What? Have the jackals taken it? Ah! malediction!"
(And he flings the bread upon the ground with fury.
No sooner has the action occurred than a table makes its
appearance, covered with all things that are good to eat.
The byssus cloth, striated like the bandelets of the sphinx, produces
of itself luminous undulations. Upon it are enormous quarters of red
meats; huge fish; birds cooked in their plumage, and quadrupeds in
their skins; fruits with colors and tints almost human in appearance;
while fragments of cooling ice, and flagons of violet crystal reflect
each other's glittering. Anthony notices in the middle of the table a
boar smoking at every pore—with legs doubled up under its belly,
and eyes half closed—and the idea of being able to eat so
formidable an animal greatly delights him. Then many things appear
which he has never seen before—black hashes, jellies, the color of
gold, ragouts in which mushrooms float like nenuphars upon ponds,
dishes of whipt cream light as clouds.
And the aroma of all this comes to him together with the salt smell
of the ocean, the coolness of mountains, the great perfumes of the
woods. He dilates his nostrils to their fullest extent; his mouth
waters; he thinks to himself that he has enough before him for a
year, for ten years, for his whole life!
As he gazes with widely-opened eyes at all these viands, others
appear; they accumulate, forming a pyramid crumbling at all its
angles. The wines begin to flow over—the fish palpitate—the blood
seethes in the dishes—the pulp of the fruit protrudes like amorous
lips—and the table rises as high as his breast, up to his very chin at
last—now bearing only one plate and a single loaf of bread, placed
exactly in front of him.
He extends his hand to seize the loaf. Other loaves immediately
present themselves to his grasp.)
"For me!... all these! But ..." (Anthony suddenly draws back.)
"Instead of one which was there, lo! there are many! It must be a
miracle, then, the same as our Lord wrought!
"Yet for what purpose?... Ah! all the rest of these things are equally
incomprehensible! Demon, begone from me! depart! begone!"
(He kicks the table from him. It disappears.)
"Nothing more?—no!" (He draws a lung breath.)
"Ah! the temptation was strong! But how well I delivered myself
from it!"
(He lifts his head, and at the same time stumbles over some
sonorous object.)
"Why! what can that be?" (Anthony stoops down.)
"How! a cup! Some traveller must have lost it here. There is nothing
extraordinary...."
(He wets his finger, and rubs.)
"It glitters!—metal! Still, I cannot see very clearly...."
(He lights his torch, and examines the cup.)
"It is silver, ornamented with ovules about the rim, with a medal at
the bottom of it."
(He detaches the medal with his nail!)
"It is a piece of money worth about seven or eight drachmas—not
more! It matters not! even with that I could easily buy myself a
sheepskin."
(A sudden flash of the torch lights up the cup.)
"Impossible! gold? Yes, all gold, solid gold!"
(A still larger piece of money appears at the bottom. Under it he
perceives several others.)
"Why, this is a sum ... large enough to purchase three oxen ... and a
little field!"
(The cup is now filled with pieces of gold.)
"What! what!... a hundred slaves, soldiers, a host ... enough to
buy...."
(The granulations of the rim, detaching themselves form a necklace
of pearls.)
"With such a marvel of jewelry as that, one could win even the wife
of the Emperor!"
(By a sudden jerk, Anthony makes the necklace slip down over his
wrist. He holds the cup in his left hand, and with his right lifts up the
torch so as to throw the light upon it. As water streams overflowing
from the basin of a fountain, so diamonds, carbuncles, and
sapphires, all mingled with broad pieces of gold bearing the effigies
of Kings, overflow from the cup in never ceasing streams, to form a
glittering hillock upon the sand.)
"What! how! Staters, cycles, dariacs, aryandics; Alexander,
Demetrius, the Ptolemies, Cæsar!—yet not one of them all
possessed so much! Nothing is now impossible! no more suffering
for me! how these gleams dazzle my eyes! Ah! my heart overflows!
how delightful it is! yes—yes!—more yet! never could there be
enough! Vainly I might continually fling it into the sea, there would
always be plenty remaining for me. Why should I lose any of it? I
will keep all, and say nothing to any one about it; I will have a
chamber hollowed out for me in the rock, and lined with plates of
bronze, and I will come here from time to time to feel the gold
sinking down under the weight of my heel; I will plunge my arms
into it as into sacks of grain! I will rub my face with it, I will lie down
upon it!"
(He flings down the torch in order to embrace the glittering heap,
and falls flat upon the ground.
He rises to his feet. The place is wholly empty.)
"What have I done!
"Had I died during those moments, I should have gone to hell—to
irrevocable damnation."
(He trembles in every limb.)
"Am I, then, accursed? Ah! no; it is my own fault! I allow myself to
be caught in every snare! No man could be more imbecile, more
infamous! I should like to beat myself, or rather to tear myself out of
my own body! I have restrained myself too long. I feel the want of
vengeance—the necessity of striking, of killing!—as though I had a
pack of wild beasts within me! Would that I could hew my way with
an axe, through the midst of a multitude.... Ah, a poniard!..."
(He perceives his knife, and rushes to seize it. The knife slips from
his hand; and Anthony remains leaning against the wall of his hut,
with wide-open mouth, motionless, cataleptic.
Everything about him has disappeared.
He thinks himself at Alexandria, upon the Paneum—an artificial
mountain in the centre of the city, encircled by a winding stairway.
Before him lies Lake Mareolis; on his right hand is the sea, on his left
the country; and immediately beneath him a vast confusion of flat
roofs, traversed from north to south and from east to west by two
streets which intercross, and which offer throughout their entire
length the spectacle of files of porticoes with Corinthian columns.
The houses overhanging this double colonnade have windows of
stained glass. Some of them support exteriorly enormous wooden
cages, into which the fresh air rushes from without.
Monuments of various architecture tower up in close proximity.
Egyptian pylons dominate Greek temples. Obelisks appear like lances
above battlements of red brick. In the middle of public squares there
are figures of Hermes with pointed ears, and of Anubis with the
head of a dog. Anthony can distinguish the mosaic pavements of the
courtyards, and tapestries suspended from the beams of ceilings.
He beholds at one glance, the two ports (the Great Port and the
Eunostus), both round as circuses, and separated by a mole
connecting Alexandria with the craggy island upon which the Pharos-
tower rises—quadrangular, five hundred cubits high, nine storied,
having at its summit a smoking heap of black coals.
Small interior ports open into the larger ones. The mole terminates
at each end in a bridge supported upon marble columns planted in
the sea. Sailing vessels pass beneath it, while heavy lighters
overladen with merchandise, thalamegii[1] inlaid with ivory, gondolas
covered with awnings, triremes, biremes, and all sorts of vessels are
moving to and fro, or lie moored at the wharves.
About the Great Port extends an unbroken array of royal
construction: the palace of the Ptolomies, the Museum, the
Posidium, the Cæsareum, the Timonium where Mark Anthony sought
refuge, the Soma which contains the tomb of Alexander; while at the
other extremity of the city, beyond the Eunostus, the great glass
factories, perfume factories, and papyrus factories may be perceived
in a suburban quarter.
Strolling peddlers, porters, ass-drivers run and jostle together. Here
and there one observes some priest of Isis wearing a panther skin
on his shoulders, a Roman soldier with his bronze helmet, and many
negroes. At the thresholds of the shops women pause, artisans ply
their trades; and the grinding noise of chariot wheels puts to flight
the birds that devour the detritus of the butcher-shops and the
morsels of fish left upon the ground.
The general outline of the streets seems like a black network flung
upon the white uniformity of the houses. The markets stocked with
herbs make green bouquets in the midst of it; the drying-yards of
the dyers, blotches of color; the golden ornaments of the temple-
pediments, luminous points—all comprised within the oval enclosure
of the grey ramparts, under the vault of the blue heaven, beside the
motionless sea.
But suddenly the movement of the crowd ceases; all turn their eyes
toward the west, whence enormous whirlwinds of dust are seen
approaching.
It is the coming of the monks of the Thebaid, all clad in goatskins,
armed with cudgels, roaring a canticle of battle and of faith with the
refrain:
"Where are they? Where are they?"
Anthony understands that they are coming to kill the Arians.
The streets are suddenly emptied—only flying feet are visible.
The Solitaries are now in the city. Their formidable cudgels, studded
with nails, whirl in the air like suns of steel. The crash of things
broken in the houses is heard. There are intervals of silence. Then
great screams arise.
From one end of the street to the other there is a continual eddy of
terrified people.
Many grasp pikes. Sometimes two bands meet, rush into one; and
this mass of men slips upon the pavement—fighting, disjointing,
knocking down. But the men with the long hair always reappear.
Threads of smoke begin to escape from the corners of edifices!
folding doors burst open. Portions of walls crumble down.
Architraves fall.
Anthony finds all his enemies again, one after the other. He even
recognizes some whom he had altogether forgotten; before killing
them he outrages them. He disembowels—he severs throats—he
fells as in a slaughter house—he hales old men by the beard,
crushes children, smites the wounded. And vengeance is taken upon
luxury, those who do not know how to read tear up hooks; others
smash and deface the statues, paintings, furniture, caskets,—a
thousand dainty things the use of which they do not know, and
which simply for that reason exasperates them. At intervals they
pause, out of breath, in the work of destruction; then they
recommence.
The inhabitants moan in the courtyards where they have sought
refuge. The women raise their tearful eyes and lift their naked arms
to heaven. In hope of moving the Solitaries they embrace their
knees; the men cast them off and fling them down, and the blood
gushes to the ceilings, falls back upon the walls like sheets of rain,
streams from the trunks of decapitated corpses, fills the aqueducts,
forms huge red pools upon the ground.
Anthony is up to his knees in it. He wades in it; he sucks up the
blood-spray on his lips; he is thrilled with joy as he feels it upon his
limbs, under his hair-tunic which is soaked through with it.
Night comes. The immense uproar dies away.
The Solitaries have disappeared.
Suddenly, upon the outer galleries corresponding to each of the nine
stories of the Pharos, Anthony observes thick black lines forming,
like lines of crows perching. He hurries thither; and soon finds
himself at the summit.
A huge mirror of brass turned toward the open sea, reflects the
forms of the vessels in the offing.
Anthony amuses himself by watching them; and while he watches,
their number increases.
They are grouped together within a gulf which has the form of a
crescent. Upon a promontory in the background, towers a new city
of Roman architecture, with cupolas of stone, conical roofs, gleams
of pink and blue marbles, and a profusion of brazen ornamentation
applied to the volutes of the capitals, to the angles of the cornices,
to the summits of the edifices. A cypress-wood overhangs the city.
The line of the sea is greener, the air colder. The mountains lining
the horizon are capped with snow.
Anthony is trying to find his way, when a man approaches him, and
says:
"Come! they are waiting for you."
He traverses a forum, enters a great court, stoops beneath a low
door; and he arrives before the facade of the palace, decorated with
a group in wax, representing Constantine overcoming a dragon.
There is a porphyry basin, from the centre of which rises a golden
conch-shell full of nuts. His guide tells him that he may take some of
them. He does so. Then he is lost, as it were, in a long succession of
apartments.
There are mosaics upon the walls representing generals presenting
the Emperor with conquered cities, which they hold out upon the
palms of their hands. And there are columns of basalt everywhere,
trellis-work in silver filigree, ivory chairs, tapestries embroidered with
pearls. The light falls from the vaults above; Anthony still proceeds.
Warm exhalations circulate about him; occasionally he hears the
discreet clapping sound of sandals upon the pavement. Posted in the
anti-chambers are guards, who resemble automata, holding wands
of vermillion upon their shoulders.
And there are columns of basalt everywhere,... The light falls from the vaults
above
At last he finds himself in a great hall, with hyacinth-colored curtains
at the further end. They part, and display the Emperor seated on a
throne, clad in a violet tunic, and wearing red shoes striped with
bands of black.
A diadem of pearls surround his head; his locks are arranged
symmetrically in rouleaux. He has a straight nose, drooping eyelids,
a heavy and cunning physiognomy. At the four corners of the dais
stretched above his head are placed four golden doves; and at the
foot of the throne are two lions in enamel crouching. The doves
begin to sing, the lions to roar. The Emperor rolls his eyes; Anthony
advances; and forthwith, without preamble, they commence to
converse about recent events. In the cities of Antioch, Ephesus, and
Alexandria, the temples have been sacked, and the statues of the
gods converted into pots and cooking utensils; the Emperor laughs
heartily about it. Anthony reproaches him with his tolerance toward
the Novations. But the Emperor becomes vexed. Novations, Arians or
Meletians—he is sick of them all! Nevertheless, he admires the
episcopate; for inasmuch as the Christians maintain bishops, who
depend for their position upon five or six important personages, it is
only necessary to gain over the latter, in order to have all the rest on
one's side. Therefore he did not fail to furnish them with large sums.
But he detests the Fathers of the Council of Nicæa.
"Let us go and see them!"
Anthony follows him.
And they find themselves on a terrace, upon the same floor.
It overlooks a hippodrome thronged with people, and surmounted by
porticoes where other spectators are walking to and fro. From the
centre of the race-course rises a narrow platform of hewn stone,
supporting a little temple of Mercury, the statue of Constantine, and
three serpents of brass twisted into a column; there are three huge
wooden eggs at one end, and at the other a group of seven dolphins
with their tails in the air.
Behind the imperial pavilion sit the Prefects of the Chambers, the
Counts of the Domestics, and the Patricians—in ranks rising by tiers
to the first story of a church whose windows are thronged with
women. On the right is the tribune of the Blue Faction; on the left,
that of the Green; below, a picket of soldiers is stationed; and on a
level with the arena is a row of Corinthian arches, forming the
entrances to the stables.
The races are about to commence; the horses are drawn up in line.
Lofty plumes, fastened between their ears, bend to the wind like
saplings; and with every restive bound, they shake their chariots
violently, which are shell-shaped, and conducted by charioteers clad
in a sort of multi-colored cuirass, having sleeves tight at the wrist
and wide in the arms; their legs are bare; their beards, faces and
foreheads are shaven after the manner of the Huns.
Anthony is at first deafened by the billowy sound of voices. From the
summit of the hippodrome to its lowest tiers, he sees only faces
painted with rouge, garments checkered and variegated with many
colors, flashing jewelry; and the sand of the arena, all white, gleams
like a mirror.
The Emperor entertains him. He confides to him many matters of
high importance, many secrets; he confesses the assassination of his
son Criopus, and even asks Anthony for advice regarding his health.
Meanwhile Anthony notices some slaves in the rear portion of the
stables below. They are the Fathers of Nicæa, ragged and abject.
The martyr Paphnutius is brushing the mane of one horse;
Theophilus is washing the legs of another; John is painting the hoofs
of a third; Alexander is collecting dung in a basket.
Anthony passes through the midst of them. They range themselves
on either side respectfully; they beseech his intercession; they kiss
his hands. The whole assemblage of spectators hoots at them; and
he enjoys the spectacle with immeasurable pleasure. Lo! he is now
one of the grandees of the Court—the Emperor's confidant—the
prime minister! Constantine places his own diadem upon his brows.
Anthony allows it to remain upon his head, thinking this honor quite
natural.
And suddenly in the midst of the darkness a vast hall appears,
illuminated by golden candelabra.
Candles so lofty that they are half lost in the darkness, stretch away
in huge files beyond the lines of banquet-tables, which seem to
extend to the horizon, where through a luminous haze loom
superpositions of stairways, suites of arcades, colossi, towers, and
beyond all a vague border of palace walls, above which rise the
crests of cedars, making yet blacker masses of blackness against the
darkness.
The guests, crowned with violet wreaths, recline upon very low
couches and are leaning upon their elbows. Along the whole length
of this double line of couches, wine is being poured out from
amphoræ, and at the further end, all alone, coiffed with the tiara
and blazing with carbuncles, King Nebuchadnezzar eats and drinks.
On his right and left, two bands of priests in pointed caps are
swinging censers. On the pavement below crawl the captive kings
whose hands and feet have been cut off; from time to time he flings
them bones to gnaw. Further off sit his brothers, with bandages
across their eyes, being all blind.
From the depths of the ergastula arise moans of ceaseless pain.
Sweet slow sounds of a hydraulic organ alternate with choruses of
song; and one feels that all about the palace without extends an
immeasurable city—an ocean of human life whose waves break
against the walls. The slaves run hither and thither carrying dishes.
Women walk between the ranks of guests, offering drinks to all; the
baskets groan under their burthen of loaves; and a dromedary, laden
with perforated water-skins: passes and repasses through the hall,
sprinkling and cooling the pavement with vervain.
Lion tamers are leading tamed lions about. Dancing girls—their hair
confined in nets—balance themselves and turn upon their hands,
emitting fire through their nostrils; negro boatmen are juggling;
naked children pelt each other with pellets of snow, which burst
against the bright silverware. There is an awful clamor as of a
tempest; and a huge cloud hangs over the banquet—so numerous
are the meats and breaths. Sometimes a flake of fire torn from the
great flambeaux by the wind, traverses the night like a shooting star.
The king wipes the perfumes from his face with his arm. He eats
from the sacred vessels—then breaks them; and secretly reckons up
the number of his fleets, his armies, and his subjects. By and by, for
a new caprice, he will burn his palace with all its guests. He dreams
of rebuilding the tower of Babel, and dethroning God.
Anthony, from afar off, reads all these thoughts upon his brow. They
penetrate his own brain, and he becomes Nebuchadnezzar.
Immediately he is cloyed with orgiastic excesses, sated with fury of
extermination; and a great desire comes upon him to wallow in
vileness. For the degradation of that which terrifies men is an
outrage inflicted upon their minds—it affords yet one more way to
stupefy them; and as nothing is viler than a brute, Anthony goes
upon the table on all fours, and bellows like a bull.
He feels a sudden pain in his hand—a pebble has accidentally
wounded him—and he finds himself once more in front of his cabin.
The circle of the rocks is empty. The stars are glowing in the sky. All
is hushed.)
"Again have I allowed myself to be deceived! Why these things?
They come from the rebellion of the flesh. Ah! wretch!"
(He rushes into his cabin, and seizes a bunch of thongs, with
metallic hooks attached to their ends, strips himself to the waist and,
lifting his eyes to heaven exclaims:)
"Accept my penance, O my God: disdain it not for its feebleness.
Render it sharp, prolonged, excessive! It is time, indeed!—to the
work!"
(He gives himself a vigorous lash—and shrieks.)
"No! no!—without mercy it must be."
(He recommences.)
"Oh! oh! oh! each lash tears my skin, rends my limbs! It burns me
horribly!"
"Nay!—it is not so very terrible after all!—one becomes accustomed
to it. It even seems to me...."
(Anthony pauses.)
"Continue, coward! continue! Good! good!—upon the arms, on the
back, on the breast, on the belly—everywhere! Hiss, ye thongs! bite
me! tear me! I would that my blood could spurt to the stars!—let my
bones crack!—let my tendons be laid bare! O for pincers, racks, and
melted lead! The martyrs have endured far worse; have they not,
Ammonaria?"
(The shadow of the Devil's horns reappears.)
"I might have been bound to the column opposite to thine,—face to
face—under thy eyes—answering thy shrieks by my sighs; and our
pangs might have been interblended, our souls intermingled."
(He lashes himself with fury.)
"What! what! again. Take that!—But how strange a titillation thrills
me! What punishment! what pleasure! I feel as though receiving
invisible kisses; the very marrow of my bones seems to melt. I
die...."
And he sees before him three cavaliers, mounted upon onagers, clad
in robes of green—each holding a lily in his hand, and all resembling
each other in feature.
Anthony turns round, and beholds three other cavaliers exactly
similar, riding upon similar onagers, and preserving the same
attitude.
He draws back. Then all the onagers advance one pace at the same
time, and rub their noses against him, trying to bite his garment.
Voices shout:—

You might also like