PostgreSQL: Why we should use ‘$$’ double dollar in PG/pgSQL Block

Actually, The funda of $$ – double dollar is very simple, but Postgres new comers have always doubted about it.

When you are writing a PL/pgSQL function, the body of the function has to pass in the form of a string literal.

For example:

Create a sample table:

Create a function without $$ – double dollar:

We can create a function using single quote and We do not require $$ – double dollar in above example.
But we should not do this, because It is not suggestible to write your function code in single quotes.

When you create your function using single quote, you have to escape all internal single quote in the body.

$$ – Double Dollar:

It is nothing, but just a substitute of a single quote. Using $$ – Double Dollar we can avoid all types of quoting issues.
We can also put token between $$ Double Dollar like: $rnd$ which makes it unique.

For Example:
Create a function with $$ – double dollar:

Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.

If you like this post, then please share it with others.
Please follow, I will share my experience towards the success of Database Research and Development Activity.

I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.

More from

Leave a Reply

Be the First to Comment!

Notify of