nothing ever works

Started by Gerard Matthewsalmost 9 years ago12 messagesdocs
Jump to latest
#1Gerard Matthews
gerardmatt@gmail.com

The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/9.6/static/plpgsql-statements.html
Description:

Your documentation although it seems straight forward actually never
works.

The dynamic sql section gives example code, it does not execute.

It does not exec in a script block or as plain sql.

Please explain limitations or where the script can actually be executed.

As a beginner starting out in postgre you can never rely on the
documentation because the same thing happens, you copy the code and make
your modifications and you get syntax error.

The same problem exists with the Perform statement. It does not work as
advertised, you cannot execute it in a function or in a script block.

Clearly define the limitations of your language so that developers don't
waste their time.

Coming from ms sql quite frankly I would never recommend postgress. The
barrior to entry into actualy writing code is too great.

--
Sent via pgsql-docs mailing list (pgsql-docs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-docs

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Gerard Matthews (#1)
Re: nothing ever works

Hi

2017-06-21 10:39 GMT+02:00 <gerardmatt@gmail.com>:

The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/9.6/static/plpgsql-statements.html
Description:

Your documentation although it seems straight forward actually never
works.

The dynamic sql section gives example code, it does not execute.

It does not exec in a script block or as plain sql.

Please explain limitations or where the script can actually be executed.

As a beginner starting out in postgre you can never rely on the
documentation because the same thing happens, you copy the code and make
your modifications and you get syntax error.

The same problem exists with the Perform statement. It does not work as
advertised, you cannot execute it in a function or in a script block.

Clearly define the limitations of your language so that developers
don&#39;t
waste their time.

Coming from ms sql quite frankly I would never recommend postgress. The
barrior to entry into actualy writing code is too great.

can you specify, please, what is wrong?

The storing procedures in ms and pg are two different worlds - and start
can be difficult.

Regards

Pavel

Show quoted text

--
Sent via pgsql-docs mailing list (pgsql-docs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-docs

#3Michael Paquier
michael@paquier.xyz
In reply to: Pavel Stehule (#2)
Re: nothing ever works

On Thu, Jun 22, 2017 at 1:00 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:

2017-06-21 10:39 GMT+02:00 <gerardmatt@gmail.com>:

The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/9.6/static/plpgsql-statements.html
Description:

Your documentation although it seems straight forward actually never
works.

The dynamic sql section gives example code, it does not execute.

It does not exec in a script block or as plain sql.

Please explain limitations or where the script can actually be executed.

As a beginner starting out in postgre you can never rely on the
documentation because the same thing happens, you copy the code and make
your modifications and you get syntax error.

The same problem exists with the Perform statement. It does not work as
advertised, you cannot execute it in a function or in a script block.

Clearly define the limitations of your language so that developers
don&#39;t
waste their time.

Coming from ms sql quite frankly I would never recommend postgress. The
barrior to entry into actualy writing code is too great.

Many people around the world praise Postgres for the quality of its
documentation. If you are not willing to spend time analyzing your
mistakes by yourself or what you are doing wrong, there are many
companies providing classes that could help you.

can you specify, please, what is wrong?

The storing procedures in ms and pg are two different worlds - and start can
be difficult.

Yes, and the queries specified in this portion of the docs need to be
executed within a plpgsql function block. Just copy-pasting them into
a psql terminal would give you nothing.
--
Michael

--
Sent via pgsql-docs mailing list (pgsql-docs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-docs

#4David G. Johnston
david.g.johnston@gmail.com
In reply to: Michael Paquier (#3)
Re: nothing ever works

2017-06-21 10:39 GMT+02:00 <gerardmatt@gmail.com>:

Your documentation although it seems straight forward actually never
works.

Most of it is not written as self-contained examples so this is not surprising.

Please explain limitations or where the script can actually be executed.

That would be the responsibility of chapters 36 and 41.2 (and maybe
some others); chapters prior to the one you are complaining about.
Again, this isn't a cookbook format where every section and example is
self-contained. This requires the reader to adopt their own
techniques for actually starting with functioning code and keeping it
functioning as new capabilities are introduced. For better and worse
I don't foresee any volunteering significant time to change the style
of the documentation - particularly without a large volume of specific
complaints and/or suggestions to work from.

As a beginner starting out in postgre you can never rely on the
documentation because the same thing happens, you copy the code and make
your modifications and you get syntax error.

Which means that some more fundamental aspect of the capability you
are trying to learn hasn't yet been figured out and so that code you
are writing is not operating in the context that the documentation
assumes it is. You need to go back earlier in the
process/documentation and get the fundamentals out of the way.

Clearly define the limitations of your language so that developers
don&#39;t
waste their time.

Either there is a distinct lack of others encountering the same
problems or they avail themselves of other solutions. In any case the
authors of the documentation cannot foresee every confusion or problem
that may arise. That's part of why the mailing lists exist. Ideally
you'd come here, get un-stumped, look back at why you got stumped in
the first place, and suggest documentation improvements that would
help prevent the next person from being stumped in a similar matter.
That flow would, IMO, be in the true spirit of this open source
community.

Coming from ms sql quite frankly I would never recommend postgress. The
barrior to entry into actualy writing code is too great.

--file: create-functions.sql
--might not work as-is but the structural components you require are here.
CREATE FUNCTION remove_all_users() RETURNS SETOF bigint AS $$
DELETE FROM users RETURNING user_id;
$$ LANGUAGE SQL;

CREATE FUNCTION do_it() RETURNS void AS $$
BEGIN
RAISE NOTICE 'Performing User Removal';
PERFORM remove_all_users(); -- using perform since we don't care about
returned user_id's
END;
$$ LANGUAGE plpgsql;

$psql

\i create-functions.sql

#now the two functions exist on the server

SELECT do_it();

%NOTICE: Performing User Removal
do_it
-------

--this would be roughly equivalent but "DO" won't return a result
which SELECT do_it() does.
--still in psql...
DO $$
BEGIN
PERFORM remove_all_users();
END;
$$; --implied pl/pgsql language

I cannot speak to learning MS SQL compared to PostgreSQL; but in some
ways having existing, but difference, experience hurts since you are
apt to make assumptions about how things should work that are not
true.

Your welcome to your venting but all I see here is a specific case of
learning having gone into spiral. The community here is great at
helping people get themselves out these kinds of spirals. That the
documentation cannot do so all by itself is not a failing of the
documentation or its authors.

David J.

--
Sent via pgsql-docs mailing list (pgsql-docs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-docs

#5Gerard Matthews
gerardmatt@gmail.com
In reply to: David G. Johnston (#4)
Re: nothing ever works

Hi Everyone,

Firstly I would like to thank everyone for your feedback. Secondly I would
like to apologise, I had spent some time trying to write some dynamic SQL
in postgres yesterday and got extremely frustrated at the the time I
wasted. I realize this a community and I'm sorry for not being more
constructive in my comment.

The reason for my frustration is this has happened before; where I look the
examples and think I know how to use it only to run into syntax errors. I
have tried the specific dynamic SQL examples inside script blocks and I get
syntax errors. If it only works in functions it would be good to know that.

If it's not that then I wonder if perhaps the problem is PG Admin.

Often the way I learn with a new language is by trying to do something that
I need. Hence I hit the docs at the point that I think will help me. If
this happens with other developers perhaps it would be helpful for the
examples to list where the specific language feature can be used.

I am not the only one as my colleagues have run into exactly the same thing.

Hopefully this input is a little more constructive and again thank you all
for taking the time to respond.

On Thu, 22 Jun 2017 at 02:47 David G. Johnston <david.g.johnston@gmail.com>
wrote:

Show quoted text

2017-06-21 10:39 GMT+02:00 <gerardmatt@gmail.com>:

Your documentation although it seems straight forward actually never
works.

Most of it is not written as self-contained examples so this is not
surprising.

Please explain limitations or where the script can actually be

executed.

That would be the responsibility of chapters 36 and 41.2 (and maybe
some others); chapters prior to the one you are complaining about.
Again, this isn't a cookbook format where every section and example is
self-contained. This requires the reader to adopt their own
techniques for actually starting with functioning code and keeping it
functioning as new capabilities are introduced. For better and worse
I don't foresee any volunteering significant time to change the style
of the documentation - particularly without a large volume of specific
complaints and/or suggestions to work from.

As a beginner starting out in postgre you can never rely on the
documentation because the same thing happens, you copy the code and

make

your modifications and you get syntax error.

Which means that some more fundamental aspect of the capability you
are trying to learn hasn't yet been figured out and so that code you
are writing is not operating in the context that the documentation
assumes it is. You need to go back earlier in the
process/documentation and get the fundamentals out of the way.

Clearly define the limitations of your language so that developers
don&#39;t
waste their time.

Either there is a distinct lack of others encountering the same
problems or they avail themselves of other solutions. In any case the
authors of the documentation cannot foresee every confusion or problem
that may arise. That's part of why the mailing lists exist. Ideally
you'd come here, get un-stumped, look back at why you got stumped in
the first place, and suggest documentation improvements that would
help prevent the next person from being stumped in a similar matter.
That flow would, IMO, be in the true spirit of this open source
community.

Coming from ms sql quite frankly I would never recommend postgress. The
barrior to entry into actualy writing code is too great.

--file: create-functions.sql
--might not work as-is but the structural components you require are here.
CREATE FUNCTION remove_all_users() RETURNS SETOF bigint AS $$
DELETE FROM users RETURNING user_id;
$$ LANGUAGE SQL;

CREATE FUNCTION do_it() RETURNS void AS $$
BEGIN
RAISE NOTICE 'Performing User Removal';
PERFORM remove_all_users(); -- using perform since we don't care about
returned user_id's
END;
$$ LANGUAGE plpgsql;

$psql

\i create-functions.sql

#now the two functions exist on the server

SELECT do_it();

%NOTICE: Performing User Removal
do_it
-------

--this would be roughly equivalent but "DO" won't return a result
which SELECT do_it() does.
--still in psql...
DO $$
BEGIN
PERFORM remove_all_users();
END;
$$; --implied pl/pgsql language

I cannot speak to learning MS SQL compared to PostgreSQL; but in some
ways having existing, but difference, experience hurts since you are
apt to make assumptions about how things should work that are not
true.

Your welcome to your venting but all I see here is a specific case of
learning having gone into spiral. The community here is great at
helping people get themselves out these kinds of spirals. That the
documentation cannot do so all by itself is not a failing of the
documentation or its authors.

David J.

#6Gerard Matthews
gerardmatt@gmail.com
In reply to: Gerard Matthews (#5)
Re: nothing ever works

Here's an example. this code does not exec for me, I get syntax error. I
have PG version 9.5

DO $$
BEGIN

EXEC SQL BEGIN DECLARE SECTION;
const char *stmt = "CREATE TABLE test1 (...);";
EXEC SQL END DECLARE SECTION;
EXEC SQL EXECUTE IMMEDIATE :stmt;

END;
$$;

[image: image.png]

On Thu, 22 Jun 2017 at 09:21 Gerard Matthews <gerardmatt@gmail.com> wrote:

Show quoted text

Hi Everyone,

Firstly I would like to thank everyone for your feedback. Secondly I would
like to apologise, I had spent some time trying to write some dynamic SQL
in postgres yesterday and got extremely frustrated at the the time I
wasted. I realize this a community and I'm sorry for not being more
constructive in my comment.

The reason for my frustration is this has happened before; where I look
the examples and think I know how to use it only to run into syntax errors.
I have tried the specific dynamic SQL examples inside script blocks and I
get syntax errors. If it only works in functions it would be good to know
that.

If it's not that then I wonder if perhaps the problem is PG Admin.

Often the way I learn with a new language is by trying to do something
that I need. Hence I hit the docs at the point that I think will help me.
If this happens with other developers perhaps it would be helpful for the
examples to list where the specific language feature can be used.

I am not the only one as my colleagues have run into exactly the same
thing.

Hopefully this input is a little more constructive and again thank you all
for taking the time to respond.

On Thu, 22 Jun 2017 at 02:47 David G. Johnston <david.g.johnston@gmail.com>
wrote:

2017-06-21 10:39 GMT+02:00 <gerardmatt@gmail.com>:

Your documentation although it seems straight forward actually never
works.

Most of it is not written as self-contained examples so this is not
surprising.

Please explain limitations or where the script can actually be

executed.

That would be the responsibility of chapters 36 and 41.2 (and maybe
some others); chapters prior to the one you are complaining about.
Again, this isn't a cookbook format where every section and example is
self-contained. This requires the reader to adopt their own
techniques for actually starting with functioning code and keeping it
functioning as new capabilities are introduced. For better and worse
I don't foresee any volunteering significant time to change the style
of the documentation - particularly without a large volume of specific
complaints and/or suggestions to work from.

As a beginner starting out in postgre you can never rely on the
documentation because the same thing happens, you copy the code and

make

your modifications and you get syntax error.

Which means that some more fundamental aspect of the capability you
are trying to learn hasn't yet been figured out and so that code you
are writing is not operating in the context that the documentation
assumes it is. You need to go back earlier in the
process/documentation and get the fundamentals out of the way.

Clearly define the limitations of your language so that developers
don&#39;t
waste their time.

Either there is a distinct lack of others encountering the same
problems or they avail themselves of other solutions. In any case the
authors of the documentation cannot foresee every confusion or problem
that may arise. That's part of why the mailing lists exist. Ideally
you'd come here, get un-stumped, look back at why you got stumped in
the first place, and suggest documentation improvements that would
help prevent the next person from being stumped in a similar matter.
That flow would, IMO, be in the true spirit of this open source
community.

Coming from ms sql quite frankly I would never recommend postgress.

The

barrior to entry into actualy writing code is too great.

--file: create-functions.sql
--might not work as-is but the structural components you require are here.
CREATE FUNCTION remove_all_users() RETURNS SETOF bigint AS $$
DELETE FROM users RETURNING user_id;
$$ LANGUAGE SQL;

CREATE FUNCTION do_it() RETURNS void AS $$
BEGIN
RAISE NOTICE 'Performing User Removal';
PERFORM remove_all_users(); -- using perform since we don't care about
returned user_id's
END;
$$ LANGUAGE plpgsql;

$psql

\i create-functions.sql

#now the two functions exist on the server

SELECT do_it();

%NOTICE: Performing User Removal
do_it
-------

--this would be roughly equivalent but "DO" won't return a result
which SELECT do_it() does.
--still in psql...
DO $$
BEGIN
PERFORM remove_all_users();
END;
$$; --implied pl/pgsql language

I cannot speak to learning MS SQL compared to PostgreSQL; but in some
ways having existing, but difference, experience hurts since you are
apt to make assumptions about how things should work that are not
true.

Your welcome to your venting but all I see here is a specific case of
learning having gone into spiral. The community here is great at
helping people get themselves out these kinds of spirals. That the
documentation cannot do so all by itself is not a failing of the
documentation or its authors.

David J.

Attachments:

image.pngimage/png; name=image.pngDownload
#7Gerard Matthews
gerardmatt@gmail.com
In reply to: Gerard Matthews (#6)
Re: nothing ever works

It's the same with this as well. All the dynamic SQL examples use Exec SQL
declare begin and end section which return syntax errors for me.

CREATE FUNCTION exec() RETURNS void AS $$
BEGIN
RAISE NOTICE 'Performing Exec';

EXEC SQL BEGIN DECLARE SECTION;
const char *stmt = "CREATE TABLE test1 (...);";
EXEC SQL END DECLARE SECTION;
EXEC SQL EXECUTE IMMEDIATE :stmt;

END;
$$ LANGUAGE plpgsql;

[image: image.png]

On Thu, 22 Jun 2017 at 09:58 Gerard Matthews <gerardmatt@gmail.com> wrote:

Show quoted text

Here's an example. this code does not exec for me, I get syntax error. I
have PG version 9.5

DO $$
BEGIN

EXEC SQL BEGIN DECLARE SECTION;
const char *stmt = "CREATE TABLE test1 (...);";
EXEC SQL END DECLARE SECTION;
EXEC SQL EXECUTE IMMEDIATE :stmt;

END;
$$;

[image: image.png]

On Thu, 22 Jun 2017 at 09:21 Gerard Matthews <gerardmatt@gmail.com> wrote:

Hi Everyone,

Firstly I would like to thank everyone for your feedback. Secondly I
would like to apologise, I had spent some time trying to write some dynamic
SQL in postgres yesterday and got extremely frustrated at the the time I
wasted. I realize this a community and I'm sorry for not being more
constructive in my comment.

The reason for my frustration is this has happened before; where I look
the examples and think I know how to use it only to run into syntax errors.
I have tried the specific dynamic SQL examples inside script blocks and I
get syntax errors. If it only works in functions it would be good to know
that.

If it's not that then I wonder if perhaps the problem is PG Admin.

Often the way I learn with a new language is by trying to do something
that I need. Hence I hit the docs at the point that I think will help me.
If this happens with other developers perhaps it would be helpful for the
examples to list where the specific language feature can be used.

I am not the only one as my colleagues have run into exactly the same
thing.

Hopefully this input is a little more constructive and again thank you
all for taking the time to respond.

On Thu, 22 Jun 2017 at 02:47 David G. Johnston <
david.g.johnston@gmail.com> wrote:

2017-06-21 10:39 GMT+02:00 <gerardmatt@gmail.com>:

Your documentation although it seems straight forward actually never
works.

Most of it is not written as self-contained examples so this is not
surprising.

Please explain limitations or where the script can actually be

executed.

That would be the responsibility of chapters 36 and 41.2 (and maybe
some others); chapters prior to the one you are complaining about.
Again, this isn't a cookbook format where every section and example is
self-contained. This requires the reader to adopt their own
techniques for actually starting with functioning code and keeping it
functioning as new capabilities are introduced. For better and worse
I don't foresee any volunteering significant time to change the style
of the documentation - particularly without a large volume of specific
complaints and/or suggestions to work from.

As a beginner starting out in postgre you can never rely on the
documentation because the same thing happens, you copy the code and

make

your modifications and you get syntax error.

Which means that some more fundamental aspect of the capability you
are trying to learn hasn't yet been figured out and so that code you
are writing is not operating in the context that the documentation
assumes it is. You need to go back earlier in the
process/documentation and get the fundamentals out of the way.

Clearly define the limitations of your language so that developers
don&#39;t
waste their time.

Either there is a distinct lack of others encountering the same
problems or they avail themselves of other solutions. In any case the
authors of the documentation cannot foresee every confusion or problem
that may arise. That's part of why the mailing lists exist. Ideally
you'd come here, get un-stumped, look back at why you got stumped in
the first place, and suggest documentation improvements that would
help prevent the next person from being stumped in a similar matter.
That flow would, IMO, be in the true spirit of this open source
community.

Coming from ms sql quite frankly I would never recommend postgress.

The

barrior to entry into actualy writing code is too great.

--file: create-functions.sql
--might not work as-is but the structural components you require are
here.
CREATE FUNCTION remove_all_users() RETURNS SETOF bigint AS $$
DELETE FROM users RETURNING user_id;
$$ LANGUAGE SQL;

CREATE FUNCTION do_it() RETURNS void AS $$
BEGIN
RAISE NOTICE 'Performing User Removal';
PERFORM remove_all_users(); -- using perform since we don't care about
returned user_id's
END;
$$ LANGUAGE plpgsql;

$psql

\i create-functions.sql

#now the two functions exist on the server

SELECT do_it();

%NOTICE: Performing User Removal
do_it
-------

--this would be roughly equivalent but "DO" won't return a result
which SELECT do_it() does.
--still in psql...
DO $$
BEGIN
PERFORM remove_all_users();
END;
$$; --implied pl/pgsql language

I cannot speak to learning MS SQL compared to PostgreSQL; but in some
ways having existing, but difference, experience hurts since you are
apt to make assumptions about how things should work that are not
true.

Your welcome to your venting but all I see here is a specific case of
learning having gone into spiral. The community here is great at
helping people get themselves out these kinds of spirals. That the
documentation cannot do so all by itself is not a failing of the
documentation or its authors.

David J.

Attachments:

image.pngimage/png; name=image.pngDownload
image.pngimage/png; name=image.pngDownload+1-1
#8Pavel Stehule
pavel.stehule@gmail.com
In reply to: Gerard Matthews (#6)
Re: nothing ever works

2017-06-22 10:00 GMT+02:00 Gerard Matthews <gerardmatt@gmail.com>:

Here's an example. this code does not exec for me, I get syntax error. I
have PG version 9.5

DO $$
BEGIN

EXEC SQL BEGIN DECLARE SECTION;
const char *stmt = "CREATE TABLE test1 (...);";
EXEC SQL END DECLARE SECTION;
EXEC SQL EXECUTE IMMEDIATE :stmt;

END;
$$;

where you see this code?

It is part of embedded C, what is client side only code. You cannot to use
embedded C on server side ever.

DO command is designed for execution server side procedures - you can use
PLpgSQL, PLPythonu or PLPerl languages.

The functional example:

DO $$
DECLARE stmt text;
BEGIN
stmt := 'CREATE TABLE ...()';
EXECUTE stmt;
END;
$$ LANGUAGE plpgsql;

Related documentation:
https://www.postgresql.org/docs/9.6/static/plpgsql.html

Maybe you are missing difference between client side and server side
coding. Client side codes cannot to work on server side.

Regards

Pavel

Show quoted text

[image: image.png]

On Thu, 22 Jun 2017 at 09:21 Gerard Matthews <gerardmatt@gmail.com> wrote:

Hi Everyone,

Firstly I would like to thank everyone for your feedback. Secondly I
would like to apologise, I had spent some time trying to write some dynamic
SQL in postgres yesterday and got extremely frustrated at the the time I
wasted. I realize this a community and I'm sorry for not being more
constructive in my comment.

The reason for my frustration is this has happened before; where I look
the examples and think I know how to use it only to run into syntax errors.
I have tried the specific dynamic SQL examples inside script blocks and I
get syntax errors. If it only works in functions it would be good to know
that.

If it's not that then I wonder if perhaps the problem is PG Admin.

Often the way I learn with a new language is by trying to do something
that I need. Hence I hit the docs at the point that I think will help me.
If this happens with other developers perhaps it would be helpful for the
examples to list where the specific language feature can be used.

I am not the only one as my colleagues have run into exactly the same
thing.

Hopefully this input is a little more constructive and again thank you
all for taking the time to respond.

On Thu, 22 Jun 2017 at 02:47 David G. Johnston <
david.g.johnston@gmail.com> wrote:

2017-06-21 10:39 GMT+02:00 <gerardmatt@gmail.com>:

Your documentation although it seems straight forward actually never
works.

Most of it is not written as self-contained examples so this is not
surprising.

Please explain limitations or where the script can actually be

executed.

That would be the responsibility of chapters 36 and 41.2 (and maybe
some others); chapters prior to the one you are complaining about.
Again, this isn't a cookbook format where every section and example is
self-contained. This requires the reader to adopt their own
techniques for actually starting with functioning code and keeping it
functioning as new capabilities are introduced. For better and worse
I don't foresee any volunteering significant time to change the style
of the documentation - particularly without a large volume of specific
complaints and/or suggestions to work from.

As a beginner starting out in postgre you can never rely on the
documentation because the same thing happens, you copy the code and

make

your modifications and you get syntax error.

Which means that some more fundamental aspect of the capability you
are trying to learn hasn't yet been figured out and so that code you
are writing is not operating in the context that the documentation
assumes it is. You need to go back earlier in the
process/documentation and get the fundamentals out of the way.

Clearly define the limitations of your language so that developers
don&#39;t
waste their time.

Either there is a distinct lack of others encountering the same
problems or they avail themselves of other solutions. In any case the
authors of the documentation cannot foresee every confusion or problem
that may arise. That's part of why the mailing lists exist. Ideally
you'd come here, get un-stumped, look back at why you got stumped in
the first place, and suggest documentation improvements that would
help prevent the next person from being stumped in a similar matter.
That flow would, IMO, be in the true spirit of this open source
community.

Coming from ms sql quite frankly I would never recommend postgress.

The

barrior to entry into actualy writing code is too great.

--file: create-functions.sql
--might not work as-is but the structural components you require are
here.
CREATE FUNCTION remove_all_users() RETURNS SETOF bigint AS $$
DELETE FROM users RETURNING user_id;
$$ LANGUAGE SQL;

CREATE FUNCTION do_it() RETURNS void AS $$
BEGIN
RAISE NOTICE 'Performing User Removal';
PERFORM remove_all_users(); -- using perform since we don't care about
returned user_id's
END;
$$ LANGUAGE plpgsql;

$psql

\i create-functions.sql

#now the two functions exist on the server

SELECT do_it();

%NOTICE: Performing User Removal
do_it
-------

--this would be roughly equivalent but "DO" won't return a result
which SELECT do_it() does.
--still in psql...
DO $$
BEGIN
PERFORM remove_all_users();
END;
$$; --implied pl/pgsql language

I cannot speak to learning MS SQL compared to PostgreSQL; but in some
ways having existing, but difference, experience hurts since you are
apt to make assumptions about how things should work that are not
true.

Your welcome to your venting but all I see here is a specific case of
learning having gone into spiral. The community here is great at
helping people get themselves out these kinds of spirals. That the
documentation cannot do so all by itself is not a failing of the
documentation or its authors.

David J.

Attachments:

image.pngimage/png; name=image.pngDownload
#9Gerard Matthews
gerardmatt@gmail.com
In reply to: Pavel Stehule (#8)
Re: nothing ever works

It's under this page on the docs
https://www.postgresql.org/docs/9.1/static/ecpg-dynamic.html.

It does not make it clear where this code can be executed. It's in the
documentation under chapter 33.5. Dynamic SQL. If you search in google for
postgres dynamic sql it's the first link. Here is link to google search.
https://www.google.co.za/search?q=postgresql+dynamic+sql&amp;rlz=1C1CHZL_enZA685ZA685&amp;oq=postgresql+dynamic+&amp;aqs=chrome.0.0j69i57j0l4.8833j0j7&amp;sourceid=chrome&amp;ie=UTF-8

On Thu, 22 Jun 2017 at 12:37 Pavel Stehule <pavel.stehule@gmail.com> wrote:

Show quoted text

2017-06-22 10:00 GMT+02:00 Gerard Matthews <gerardmatt@gmail.com>:

Here's an example. this code does not exec for me, I get syntax error. I
have PG version 9.5

DO $$
BEGIN

EXEC SQL BEGIN DECLARE SECTION;
const char *stmt = "CREATE TABLE test1 (...);";
EXEC SQL END DECLARE SECTION;
EXEC SQL EXECUTE IMMEDIATE :stmt;

END;
$$;

where you see this code?

It is part of embedded C, what is client side only code. You cannot to use
embedded C on server side ever.

DO command is designed for execution server side procedures - you can use
PLpgSQL, PLPythonu or PLPerl languages.

The functional example:

DO $$
DECLARE stmt text;
BEGIN
stmt := 'CREATE TABLE ...()';
EXECUTE stmt;
END;
$$ LANGUAGE plpgsql;

Related documentation:
https://www.postgresql.org/docs/9.6/static/plpgsql.html

Maybe you are missing difference between client side and server side
coding. Client side codes cannot to work on server side.

Regards

Pavel

[image: image.png]

On Thu, 22 Jun 2017 at 09:21 Gerard Matthews <gerardmatt@gmail.com>
wrote:

Hi Everyone,

Firstly I would like to thank everyone for your feedback. Secondly I
would like to apologise, I had spent some time trying to write some dynamic
SQL in postgres yesterday and got extremely frustrated at the the time I
wasted. I realize this a community and I'm sorry for not being more
constructive in my comment.

The reason for my frustration is this has happened before; where I look
the examples and think I know how to use it only to run into syntax errors.
I have tried the specific dynamic SQL examples inside script blocks and I
get syntax errors. If it only works in functions it would be good to know
that.

If it's not that then I wonder if perhaps the problem is PG Admin.

Often the way I learn with a new language is by trying to do something
that I need. Hence I hit the docs at the point that I think will help me.
If this happens with other developers perhaps it would be helpful for the
examples to list where the specific language feature can be used.

I am not the only one as my colleagues have run into exactly the same
thing.

Hopefully this input is a little more constructive and again thank you
all for taking the time to respond.

On Thu, 22 Jun 2017 at 02:47 David G. Johnston <
david.g.johnston@gmail.com> wrote:

2017-06-21 10:39 GMT+02:00 <gerardmatt@gmail.com>:

Your documentation although it seems straight forward actually never
works.

Most of it is not written as self-contained examples so this is not
surprising.

Please explain limitations or where the script can actually be

executed.

That would be the responsibility of chapters 36 and 41.2 (and maybe
some others); chapters prior to the one you are complaining about.
Again, this isn't a cookbook format where every section and example is
self-contained. This requires the reader to adopt their own
techniques for actually starting with functioning code and keeping it
functioning as new capabilities are introduced. For better and worse
I don't foresee any volunteering significant time to change the style
of the documentation - particularly without a large volume of specific
complaints and/or suggestions to work from.

As a beginner starting out in postgre you can never rely on the
documentation because the same thing happens, you copy the code and

make

your modifications and you get syntax error.

Which means that some more fundamental aspect of the capability you
are trying to learn hasn't yet been figured out and so that code you
are writing is not operating in the context that the documentation
assumes it is. You need to go back earlier in the
process/documentation and get the fundamentals out of the way.

Clearly define the limitations of your language so that developers
don&#39;t
waste their time.

Either there is a distinct lack of others encountering the same
problems or they avail themselves of other solutions. In any case the
authors of the documentation cannot foresee every confusion or problem
that may arise. That's part of why the mailing lists exist. Ideally
you'd come here, get un-stumped, look back at why you got stumped in
the first place, and suggest documentation improvements that would
help prevent the next person from being stumped in a similar matter.
That flow would, IMO, be in the true spirit of this open source
community.

Coming from ms sql quite frankly I would never recommend postgress.

The

barrior to entry into actualy writing code is too great.

--file: create-functions.sql
--might not work as-is but the structural components you require are
here.
CREATE FUNCTION remove_all_users() RETURNS SETOF bigint AS $$
DELETE FROM users RETURNING user_id;
$$ LANGUAGE SQL;

CREATE FUNCTION do_it() RETURNS void AS $$
BEGIN
RAISE NOTICE 'Performing User Removal';
PERFORM remove_all_users(); -- using perform since we don't care about
returned user_id's
END;
$$ LANGUAGE plpgsql;

$psql

\i create-functions.sql

#now the two functions exist on the server

SELECT do_it();

%NOTICE: Performing User Removal
do_it
-------

--this would be roughly equivalent but "DO" won't return a result
which SELECT do_it() does.
--still in psql...
DO $$
BEGIN
PERFORM remove_all_users();
END;
$$; --implied pl/pgsql language

I cannot speak to learning MS SQL compared to PostgreSQL; but in some
ways having existing, but difference, experience hurts since you are
apt to make assumptions about how things should work that are not
true.

Your welcome to your venting but all I see here is a specific case of
learning having gone into spiral. The community here is great at
helping people get themselves out these kinds of spirals. That the
documentation cannot do so all by itself is not a failing of the
documentation or its authors.

David J.

Attachments:

image.pngimage/png; name=image.pngDownload
#10Pavel Stehule
pavel.stehule@gmail.com
In reply to: Gerard Matthews (#9)
Re: nothing ever works

2017-06-22 13:29 GMT+02:00 Gerard Matthews <gerardmatt@gmail.com>:

It's under this page on the docs https://www.postgresql.
org/docs/9.1/static/ecpg-dynamic.html.

It does not make it clear where this code can be executed. It's in the
documentation under chapter 33.5. Dynamic SQL. If you search in google for
postgres dynamic sql it's the first link. Here is link to google search.
https://www.google.co.za/search?q=postgresql+dynamic+sql&amp;rlz=1C1CHZL_
enZA685ZA685&oq=postgresql+dynamic+&aqs=chrome.0.
0j69i57j0l4.8833j0j7&sourceid=chrome&ie=UTF-8

Unfortunately we have not control of Google. I understand so every body can
be confused from this information. ecpg is old very specific feature of old
databases. Few people uses it today.

For you interesting page is
https://www.postgresql.org/docs/9.6/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

but you need to use keyword "plpgsql" what is preferred language for stored
procedures in Postgres. I don't know why Google is thinking so ecpg is more
than plpgsql :(.

Regards

Pavel

Show quoted text

On Thu, 22 Jun 2017 at 12:37 Pavel Stehule <pavel.stehule@gmail.com>
wrote:

2017-06-22 10:00 GMT+02:00 Gerard Matthews <gerardmatt@gmail.com>:

Here's an example. this code does not exec for me, I get syntax error. I
have PG version 9.5

DO $$
BEGIN

EXEC SQL BEGIN DECLARE SECTION;
const char *stmt = "CREATE TABLE test1 (...);";
EXEC SQL END DECLARE SECTION;
EXEC SQL EXECUTE IMMEDIATE :stmt;

END;
$$;

where you see this code?

It is part of embedded C, what is client side only code. You cannot to
use embedded C on server side ever.

DO command is designed for execution server side procedures - you can use
PLpgSQL, PLPythonu or PLPerl languages.

The functional example:

DO $$
DECLARE stmt text;
BEGIN
stmt := 'CREATE TABLE ...()';
EXECUTE stmt;
END;
$$ LANGUAGE plpgsql;

Related documentation: https://www.postgresql.org/
docs/9.6/static/plpgsql.html

Maybe you are missing difference between client side and server side
coding. Client side codes cannot to work on server side.

Regards

Pavel

[image: image.png]

On Thu, 22 Jun 2017 at 09:21 Gerard Matthews <gerardmatt@gmail.com>
wrote:

Hi Everyone,

Firstly I would like to thank everyone for your feedback. Secondly I
would like to apologise, I had spent some time trying to write some dynamic
SQL in postgres yesterday and got extremely frustrated at the the time I
wasted. I realize this a community and I'm sorry for not being more
constructive in my comment.

The reason for my frustration is this has happened before; where I look
the examples and think I know how to use it only to run into syntax errors.
I have tried the specific dynamic SQL examples inside script blocks and I
get syntax errors. If it only works in functions it would be good to know
that.

If it's not that then I wonder if perhaps the problem is PG Admin.

Often the way I learn with a new language is by trying to do something
that I need. Hence I hit the docs at the point that I think will help me.
If this happens with other developers perhaps it would be helpful for the
examples to list where the specific language feature can be used.

I am not the only one as my colleagues have run into exactly the same
thing.

Hopefully this input is a little more constructive and again thank you
all for taking the time to respond.

On Thu, 22 Jun 2017 at 02:47 David G. Johnston <
david.g.johnston@gmail.com> wrote:

2017-06-21 10:39 GMT+02:00 <gerardmatt@gmail.com>:

Your documentation although it seems straight forward actually

never

works.

Most of it is not written as self-contained examples so this is not
surprising.

Please explain limitations or where the script can actually be

executed.

That would be the responsibility of chapters 36 and 41.2 (and maybe
some others); chapters prior to the one you are complaining about.
Again, this isn't a cookbook format where every section and example is
self-contained. This requires the reader to adopt their own
techniques for actually starting with functioning code and keeping it
functioning as new capabilities are introduced. For better and worse
I don't foresee any volunteering significant time to change the style
of the documentation - particularly without a large volume of specific
complaints and/or suggestions to work from.

As a beginner starting out in postgre you can never rely on the
documentation because the same thing happens, you copy the code

and make

your modifications and you get syntax error.

Which means that some more fundamental aspect of the capability you
are trying to learn hasn't yet been figured out and so that code you
are writing is not operating in the context that the documentation
assumes it is. You need to go back earlier in the
process/documentation and get the fundamentals out of the way.

Clearly define the limitations of your language so that developers
don&#39;t
waste their time.

Either there is a distinct lack of others encountering the same
problems or they avail themselves of other solutions. In any case the
authors of the documentation cannot foresee every confusion or problem
that may arise. That's part of why the mailing lists exist. Ideally
you'd come here, get un-stumped, look back at why you got stumped in
the first place, and suggest documentation improvements that would
help prevent the next person from being stumped in a similar matter.
That flow would, IMO, be in the true spirit of this open source
community.

Coming from ms sql quite frankly I would never recommend

postgress. The

barrior to entry into actualy writing code is too great.

--file: create-functions.sql
--might not work as-is but the structural components you require are
here.
CREATE FUNCTION remove_all_users() RETURNS SETOF bigint AS $$
DELETE FROM users RETURNING user_id;
$$ LANGUAGE SQL;

CREATE FUNCTION do_it() RETURNS void AS $$
BEGIN
RAISE NOTICE 'Performing User Removal';
PERFORM remove_all_users(); -- using perform since we don't care about
returned user_id's
END;
$$ LANGUAGE plpgsql;

$psql

\i create-functions.sql

#now the two functions exist on the server

SELECT do_it();

%NOTICE: Performing User Removal
do_it
-------

--this would be roughly equivalent but "DO" won't return a result
which SELECT do_it() does.
--still in psql...
DO $$
BEGIN
PERFORM remove_all_users();
END;
$$; --implied pl/pgsql language

I cannot speak to learning MS SQL compared to PostgreSQL; but in some
ways having existing, but difference, experience hurts since you are
apt to make assumptions about how things should work that are not
true.

Your welcome to your venting but all I see here is a specific case of
learning having gone into spiral. The community here is great at
helping people get themselves out these kinds of spirals. That the
documentation cannot do so all by itself is not a failing of the
documentation or its authors.

David J.

Attachments:

image.pngimage/png; name=image.pngDownload
#11Joe Conway
mail@joeconway.com
In reply to: Gerard Matthews (#9)
Re: nothing ever works

On 06/22/2017 04:29 AM, Gerard Matthews wrote:

It's under this page on the
docs https://www.postgresql.org/docs/9.1/static/ecpg-dynamic.html.

Perhaps a bit confusing but notice the title in the top middle of the
page: "Chapter 33. ECPG - Embedded SQL in C"

That is definitely not what you are looking for by the sound of it. As
others have pointed out, most likely you want plpgsql:

https://www.postgresql.org/docs/current/static/plpgsql.html

although you can also write dynamic SQL in other Procedural Languages
which ship with Postgres, for example plpython and plperl:

https://www.postgresql.org/docs/current/static/plpython.html
https://www.postgresql.org/docs/current/static/plperl.html

Note however, while plpgsql is installed into your database by default,
plperl and plpython are not, so you would have to log into the database
of interest and run the following (once), e.g. (only do for the language
of interest, not all three; also note that depending on how you
installed Postgres, you might need to also install extra packages):

CREATE EXTENSION plperl;
CREATE EXTENSION plpythonu;
CREATE EXTENSION plperlu;

Also note the difference between "trusted" and "untrusted" procedural
languages. plpgsql and plperl are "trusted" while plperlu and plpythonu
are "untrusted". See this link for an explanation:

https://www.postgresql.org/docs/current/static/plperl-trusted.html

Hope this helps.

Joe

--
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development

#12Gerard Matthews
gerardmatt@gmail.com
In reply to: Joe Conway (#11)
Re: nothing ever works

Thank you very much guys. I'm on the right path now.

On Fri, 23 Jun 2017 at 01:25 Joe Conway <mail@joeconway.com> wrote:

Show quoted text

On 06/22/2017 04:29 AM, Gerard Matthews wrote:

It's under this page on the
docs https://www.postgresql.org/docs/9.1/static/ecpg-dynamic.html.

Perhaps a bit confusing but notice the title in the top middle of the
page: "Chapter 33. ECPG - Embedded SQL in C"

That is definitely not what you are looking for by the sound of it. As
others have pointed out, most likely you want plpgsql:

https://www.postgresql.org/docs/current/static/plpgsql.html

although you can also write dynamic SQL in other Procedural Languages
which ship with Postgres, for example plpython and plperl:

https://www.postgresql.org/docs/current/static/plpython.html
https://www.postgresql.org/docs/current/static/plperl.html

Note however, while plpgsql is installed into your database by default,
plperl and plpython are not, so you would have to log into the database
of interest and run the following (once), e.g. (only do for the language
of interest, not all three; also note that depending on how you
installed Postgres, you might need to also install extra packages):

CREATE EXTENSION plperl;
CREATE EXTENSION plpythonu;
CREATE EXTENSION plperlu;

Also note the difference between "trusted" and "untrusted" procedural
languages. plpgsql and plperl are "trusted" while plperlu and plpythonu
are "untrusted". See this link for an explanation:

https://www.postgresql.org/docs/current/static/plperl-trusted.html

Hope this helps.

Joe

--
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development