Are PL/pgSQL and SQL in PostgreSQL both at the same level as SQL/PSM standard, instead of as SQL standard only? [closed]

馋奶兔 提交于 2019-12-31 05:11:48

问题


Terminology: In the following, there are two kinds of languages understood by PostgreSQL server

  • PL/pgSQL;
  • I use both "PostgreSQL SQL" and "SQL in PostgreSQL" to refer to the same thing, the default language of the commands received by PostgreSQL server. "PostgreSQL SQL" i.e. "SQL in PostgreSQL" is not the same thing as SQL per SQL standard.

I have found the following observations:

  • SQL in PostgreSQL and PL/pgSQL both can create functions (actually stored procedures) by their own CREATE FUNCTION statements. I read Difference between language sql and language plpgsql in PostgreSQL functions

  • But PL/pgSQL allows variable assignment, while I don't find that SQL in PostgreSQL allows variable assignment.

Since PostgreSQL SQL has CREATE FUNCTION and SQL standard doesn't have stored procedures, is SQL in PostgreSQL at the same level as SQL/PSM standard, instead of as SQL standard only?

Are SQL in PostgreSQL and PL/pgSQL both at the same level as SQL/PSM standard?

What is the relation betw PL/pgSQL and PostgreSQL SQL?

  • Is PL/pgSQL a procedural extension to SQL in PostgreSQL, but PostgreSQL SQL already has CREATE FUNCTION?

  • Is PL/pgSQL an alternative to SQL in PostgreSQL? Can most of what PL/pgSQL can do also be done in PostgreSQL SQL?

If both PL/pgSQL and SQL in PostgreSQL are at the same level as SQL/PSM, which one more closely follows SQL/PSM standard?

Thanks.


More background:

I learned that we have to use DO command in PostgreSQL SQL to specify using PL/pgSQL, otherwise the commands are just in PostgreSQL SQL. In MySQL, I never have to do similar things, but just write MySQL commands. So I am not clear why we have both PL/pgSQL and PostgreSQL SQL, and have to use PL/pgSQL in PostgreSQL SQL via DO command, instead of just using one of them without the awkwardness of mixing PL/pgSQL in PostgreSQL SQL.


回答1:


To clear up the terminology:

SQL is a query language that is used to select, update, delete or create data in a relational database. It has no procedural elements like loops (FOR, WHILE) or conditional statements (IF, ELSE) or variables or cursors.

CREATE FUNCTION is indeed a "SQL statement" but is is merely a "wrapper" to specify a block of code that is executed by something different than the SQL query "engine". Postgres (unlike other DBMS) supports multiple "runtime engines" that can execute the block of code that was passed to the "CREATE FUNCTION" statement - one artifact of that is that the code is actually a string so CREATE FUNCTION only sees a string, nothing else.

Because SQL has no procedural elements, you can't mix procedural code and SQL code. If you want to run procedural code you need to tell the server that you are switching "engines" somehow. This is done through the (SQL) DO command which again takes a string that it doesn't know what to do with, sends it to the server and says "here is a piece of code where the user claimed that the engine 'xyz' can execute" - xyz is either PL/pgSQL, Python, Perl or something entirely different.

This is the same as an anonymous PL/SQL block in Oracle that you start with DECLARE - everything after that is executed by a different runtime engine on the server. MySQL has no such feature. The only way to run procedural code is to create a procedure (or function), then run that. That's why there is no such thing as DO in MySQL.

The only DBMS product which does not clearly distinguish between procedural code and "plain SQL" is SQL Server: T-SQL is an extension to the SQL language which allows you to mix "regular SQL" and procedural SQL without telling the backend that the code needs a different engine to run (which is a source of great confusion for people migrating from SQL Server to Postgres or Oracle).

SQL/PSM is a standard that defines procedural elements that can be embedded into a database engine that uses SQL as its query language. I know of no DBMS product that actually implements SQL/PSM. Postgres' PL/pgSQL, Oracle's PL/SQL, MySQL's procedural dialect are somewhat similar to that, but far from being compliant with the SQL/PSM standard. I think the closest to the SQL/PSM standard is DB2 and maybe HSQLDB


SQL in PostgreSQL" is not the same thing as SQL per SQL standard.

That is true. But then, no DBMS fully implements the SQL standard - but Postgres' implementation is probably one closest to the standard.




回答2:


SQL is a query language, while PL/pgSQL is a procedural language (it has statements like LOOP and IF).

Procedural languages are used to write functions; the body of a function can be written in PL/pgSQL.

CREATE FUNCTION is an SQL statement that defines a function object in the database. Such a function can be used in SQL expressions.

SQL/PSM is the part of the standard that defines the stored procedure language, so SQL/PSM would be applicable to PL/pgSQL, but not to SQL.

However, PL/pgSQL does not follow the SQL/PSM standard.

Nobody tells you that you have to use PL/pgSQL, and indeed it makes your application more portable (on the database end) not to use it. But SQL and procedural languages are something different (in PostgreSQL as well as in MySQL), and you may find that both have their uses.



来源:https://stackoverflow.com/questions/50858872/are-pl-pgsql-and-sql-in-postgresql-both-at-the-same-level-as-sql-psm-standard-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!