Adding a crucial element to an example

Lists: pgsql-docs
From: John Gage <jsmgage(at)numericable(dot)fr>
To: pgsql-docs(at)postgresql(dot)org
Subject: Adding a crucial element to an example
Date: 2010-07-23 10:02:02
Message-ID: 9A88E54D-2CF7-403F-A0D8-35B9ECDC7103@numericable.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-docs

The examples in the documentation are the most valuable part.

In this section, you create the table for the example, but you do not
populate it, although the example select statements are against the
phantom population that has not been inserted.

I suggest strongly including the minimal code necessary to populate
the table, so that the user doesn't have to populate it himself. Call
me lazy, but I did go into pgAdmin and insert values after creating a
primary key.

Cutting and pasting examples and then playing around with them is by
far, by far the most productive way to learn the system.

John

35.4.2. SQL Functions on Composite Types
When writing functions with arguments of composite types, we must not
only specify which argument we want (as we did above with $1 and $2)
but also the desired attribute (field) of that argument. For example,
suppose that emp is a table containing employee data, and therefore
also the name of the composite type of each row of the table. Here is
a function double_salary that computes what someone's salary would be
if it were doubled:

CREATE TABLE emp ( name text, salary numeric, age integer, cubicle
point );

CREATE FUNCTION double_salary(emp) RETURNS numeric AS $$
SELECT $1.salary * 2 AS salary;
$$ LANGUAGE SQL;

SELECT name, double_salary(emp.*) AS dream FROM emp WHERE emp.cubicle
~= point '(2,1)';

name | dream
------+-------
Bill | 8400


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: John Gage <jsmgage(at)numericable(dot)fr>
Cc: pgsql-docs(at)postgresql(dot)org
Subject: Re: Adding a crucial element to an example
Date: 2010-07-24 12:21:29
Message-ID: 1279974089.22066.3.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-docs

On fre, 2010-07-23 at 12:02 +0200, John Gage wrote:
> In this section, you create the table for the example, but you do
> not
> populate it, although the example select statements are against the
> phantom population that has not been inserted.
>
> I suggest strongly including the minimal code necessary to populate
> the table, so that the user doesn't have to populate it himself.
> Call
> me lazy, but I did go into pgAdmin and insert values after creating
> a
> primary key.

Could you send a patch, or a list of the statements that need to be
inserted?


From: John Gage <jsmgage(at)numericable(dot)fr>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-docs(at)postgresql(dot)org
Subject: Re: Adding a crucial element to an example
Date: 2010-07-24 14:26:11
Message-ID: DE129122-F02D-4985-A32A-A18DE7A81F09@numericable.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-docs

After the CREATE statement I would simply put:

INSERT INTO emp VALUES ('Bill', 4200, 45, '(2,1)');

What that accomplishes is making it so the user can simply copy and
paste the entire section and run it in pg_admin. Then, the user can
start fiddling with it as he wants to.

I know this sounds terribly simple, but simplicity is good,
particularly when you're climbing a steep learning curve. And there
is certainly, unequivocally, no harm in simplicity.

Thanks,

John

On Jul 24, 2010, at 2:21 PM, Peter Eisentraut wrote:

> On fre, 2010-07-23 at 12:02 +0200, John Gage wrote:
>> In this section, you create the table for the example, but you do
>> not
>> populate it, although the example select statements are against the
>> phantom population that has not been inserted.
>>
>> I suggest strongly including the minimal code necessary to populate
>> the table, so that the user doesn't have to populate it himself.
>> Call
>> me lazy, but I did go into pgAdmin and insert values after creating
>> a
>> primary key.
>
> Could you send a patch, or a list of the statements that need to be
> inserted?
>
>
> --
> Sent via pgsql-docs mailing list (pgsql-docs(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-docs


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: John Gage <jsmgage(at)numericable(dot)fr>
Cc: pgsql-docs(at)postgresql(dot)org
Subject: Re: Adding a crucial element to an example
Date: 2010-07-25 08:31:03
Message-ID: 1280046663.28006.0.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-docs

On lör, 2010-07-24 at 16:26 +0200, John Gage wrote:
> After the CREATE statement I would simply put:
>
> INSERT INTO emp VALUES ('Bill', 4200, 45, '(2,1)');

done


From: John Gage <jsmgage(at)numericable(dot)fr>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-docs(at)postgresql(dot)org
Subject: Re: Another change to the same section once again to facilitate copying and pasting
Date: 2010-07-25 09:34:07
Message-ID: B292CCAB-124A-475D-8075-D7511395E465@numericable.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-docs

In 34.4.3. SQL Functions with Output Parameters we have:

CREATE FUNCTION sum_n_product (x int, y int, OUT sum int, OUT product
int) AS
'SELECT $1 + $2, $1 * $2'
LANGUAGE SQL;

SELECT * FROM sum_n_product(11,42);

sum | product
-----+---------
53 | 462
(1 row)

Then in 34.4.7. SQL Functions Returning Sets we have (without
an example SELECT statement):

CREATE FUNCTION sum_n_product_with_tab (x int, OUT sum int, OUT
product int) RETURNS SETOF record AS $$
SELECT $1 + tab.y, $1 * tab.y FROM tab;
$$ LANGUAGE SQL;

The problem is that if the reader copies and pastes the last function
definition and then attempts to run it, quite obviously he gets an
error because there is no table "tab".
I would add a table definition and fill it with dummy data and then
give two example SELECT statements after the function definition:

CREATE TABLE tab ( y integer, z integer );
INSERT INTO tab VALUES (1, 2), (3,4), (5,6), (7,8);

CREATE FUNCTION sum_n_product_with_tab (x int, OUT sum int, OUT
product int) RETURNS SETOF record AS $$
SELECT $1 + tab.y, $1 * tab.y FROM tab;
$$ LANGUAGE SQL;

SELECT sum_n_product_with_tab(10);
SELECT * from sum_n_product_with_tab(10);

I think one of the things this presentation accomplishes is to
emphasize that a table definition is really not much more than a type
definition or object definition or what you will. That is not said
derogatorily at all. Rather, I believe it emphasizes the
extraordinary power of Postgresql.

John


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: John Gage <jsmgage(at)numericable(dot)fr>
Cc: pgsql-docs(at)postgresql(dot)org
Subject: Re: Another change to the same section once again to facilitate copying and pasting
Date: 2010-07-26 20:15:04
Message-ID: 1280175304.17392.0.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-docs

On sön, 2010-07-25 at 11:34 +0200, John Gage wrote:
> I would add a table definition and fill it with dummy data and then
> give two example SELECT statements after the function definition:

Done


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: John Gage <jsmgage(at)numericable(dot)fr>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, pgsql-docs(at)postgresql(dot)org
Subject: Re: Adding a crucial element to an example
Date: 2010-08-11 21:22:20
Message-ID: 201008112122.o7BLMKT13539@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-docs

John Gage wrote:
> After the CREATE statement I would simply put:
>
> INSERT INTO emp VALUES ('Bill', 4200, 45, '(2,1)');
>
> What that accomplishes is making it so the user can simply copy and
> paste the entire section and run it in pg_admin. Then, the user can
> start fiddling with it as he wants to.
>
> I know this sounds terribly simple, but simplicity is good,
> particularly when you're climbing a steep learning curve. And there
> is certainly, unequivocally, no harm in simplicity.

Our documentation got this good only through many small improvements
like this. :-)

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +