pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedSnippets
7+ messages / 4 participants
[nested] [flat]
* Snippets
@ 2003-12-14 20:07 Uwe C. Schroeder <uwe@oss4u.com>
0 siblings, 0 replies; 7+ messages in thread
From: Uwe C. Schroeder @ 2003-12-14 20:07 UTC (permalink / raw)
To: pgsql-general
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi Guys,
since there isn't too much documentation/samples and stuff out for stored
procedures etc, I decided to put up a quickhack on the web for code snippets
like functions etc.
I added some functions from the mailinglist I found interesting. I'd like to
keep adding functions from the mailinglist, so one doesn't have to search the
archives. In the end It's omething like the well known cookbooks at
activestate.
I leave the edit functions open to the public for now. However I'll delete
inappropriate stuff someone put there.
If someone knows of a better "cookbook" script than the one I'm using there
I'd like to hear about it.
You find the thing on
http://www.oss4u.com/snippets
UC
- --
Open Source Solutions 4U, LLC 2570 Fleetwood Drive
Phone: +1 650 872 2425 San Bruno, CA 94066
Cell: +1 650 302 2405 United States
Fax: +1 650 872 2417
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
iD8DBQE/3MMHjqGXBvRToM4RApW7AKCC6nkSXGI6Qbjash4AyzsBt86tvQCfReN+
c2WqPIpzm8m0JgG9Wf7z3S4=
=NrHh
-----END PGP SIGNATURE-----
^ permalink raw reply [nested|flat] 7+ messages in thread
* Snippets?
@ 2019-05-24 18:15 Ken Lacrosse <klacrosse@pkwillis.com>
0 siblings, 2 replies; 7+ messages in thread
From: Ken Lacrosse @ 2019-05-24 18:15 UTC (permalink / raw)
To: 'pgsql-general@lists.postgresql.org' <pgsql-general@lists.postgresql.org>
Is there any way in postgresql to have a "snippet" of SQL code which you could apply to all tables. Something you could add which would ensure that every table always has a Created, Changed and Deleted column for example. Sort of like a C include I suppose. Of course if I'm building a DB schema by hand I could just copy and paste those columns but it still seems a bit too, darn humans!, error prone.
Ken LaCrosse
Senior IT InfoSec and Infrastructure Monitoring Specialist
I.T. | American Recovery Service & Skipbusters
Phone: (800)398-6480 x3758
Email: KLacrosse@pkwillis.com<mailto:KLacrosse@pkwillis.com>
www.pkwillis.com<http://www.pkwillis.com/;
* Visibility is key. *
Without visibility you can't see.
What you can't see you can't measure.
What you can't measure you can't planfully change.
What you can't planfully change you can't manage.
And if we can't manage then why are we here?
NOTICE: The information contained in this transmission, including attachments, may contain confidential information that is privileged, confidential and/or exempt from disclosure by applicable law. It is intended only for the use of the person(s) or entity to which it is addressed. If the reader of this transmission is not the intended recipient, the reader is hereby notified that any review, use, dissemination, distribution or duplication of this communication (including any reliance thereon) is strictly prohibited. If you have received this transmission in error, please contact the sender by reply email, then delete and destroy the material in its entirety, whether in electronic or hard copy format. WE SPECIFICALLY DISCLAIM RESPONSIBILITY FOR ANY UNAUTHORIZED USE OF THIS COMMUNICATION OR ANY ATTACHMENTS TO IT. Thank you.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Snippets?
@ 2019-05-24 18:20 Ron <ronljohnsonjr@gmail.com>
parent: Ken Lacrosse <klacrosse@pkwillis.com>
1 sibling, 0 replies; 7+ messages in thread
From: Ron @ 2019-05-24 18:20 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 5/24/19 1:15 PM, Ken Lacrosse wrote:
>
> Is there any way in postgresql to have a "snippet" of SQL code which you
> could apply to all tables. Something you could add which would ensure
> that every table always has a Created, Changed and Deleted column for
> example. Sort of like a C include I suppose. Of course if I’m building a
> DB schema by hand I could just copy and paste those columns but it still
> seems a bit too, darn humans!, error prone.
>
Inheritance might be what you want.
--
Angular momentum makes the world go 'round.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Snippets?
@ 2019-05-24 18:27 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Ken Lacrosse <klacrosse@pkwillis.com>
1 sibling, 1 reply; 7+ messages in thread
From: Adrian Klaver @ 2019-05-24 18:27 UTC (permalink / raw)
To: Ken Lacrosse <klacrosse@pkwillis.com>; 'pgsql-general@lists.postgresql.org' <pgsql-general@lists.postgresql.org>
On 5/24/19 11:15 AM, Ken Lacrosse wrote:
> Is there any way in postgresql to have a "snippet" of SQL code which you
> could apply to all tables. Something you could add which would ensure
> that every table always has a Created, Changed and Deleted column for
> example. Sort of like a C include I suppose. Of course if I’m building
> a DB schema by hand I could just copy and paste those columns but it
> still seems a bit too, darn humans!, error prone.
Not that I know of.
Things I have done:
1) Template table that I includes those fields that I then add to.
FYI the Sqitch schema management system allows you to do the above:
https://sqitch.org/docs/manual/sqitch-add/#templates
2) A script that I run over table definition to add the fields.
>
> Ken LaCrosse
>
> Senior IT InfoSec and Infrastructure Monitoring Specialist
>
> I.T. | American Recovery Service & Skipbusters
>
> Phone: (800)398-6480 x3758
>
> Email: KLacrosse@pkwillis.com <mailto:KLacrosse@pkwillis.com>
>
> www.pkwillis.com <http://www.pkwillis.com/;
>
> * Visibility is key. *
>
> Without visibility you can't see.
>
> What you can't see you can't measure.
>
> What you can't measure you can’t planfully change.
>
> What you can’t planfully change you can't manage.
>
> And if we can’t manage then why are we here?
>
> NOTICE: The information contained in this transmission, including
> attachments, may contain confidential information that is privileged,
> confidential and/or exempt from disclosure by applicable law. It is
> intended only for the use of the person(s) or entity to which it is
> addressed. If the reader of this transmission is not the intended
> recipient, the reader is hereby notified that any review, use,
> dissemination, distribution or duplication of this communication
> (including any reliance thereon) is strictly prohibited. If you have
> received this transmission in error, please contact the sender by reply
> email, then delete and destroy the material in its entirety, whether in
> electronic or hard copy format. WE SPECIFICALLY DISCLAIM RESPONSIBILITY
> FOR ANY UNAUTHORIZED USE OF THIS COMMUNICATION OR ANY ATTACHMENTS TO IT.
> Thank you.
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Snippets?
@ 2019-05-24 19:24 Ron <ronljohnsonjr@gmail.com>
parent: Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 1 reply; 7+ messages in thread
From: Ron @ 2019-05-24 19:24 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 5/24/19 1:27 PM, Adrian Klaver wrote:
> On 5/24/19 11:15 AM, Ken Lacrosse wrote:
>> Is there any way in postgresql to have a "snippet" of SQL code which you
>> could apply to all tables. Something you could add which would ensure
>> that every table always has a Created, Changed and Deleted column for
>> example. Sort of like a C include I suppose. Of course if I’m building a
>> DB schema by hand I could just copy and paste those columns but it still
>> seems a bit too, darn humans!, error prone.
>
> Not that I know of.
>
> Things I have done:
> 1) Template table that I includes those fields that I then add to.
>
> FYI the Sqitch schema management system allows you to do the above:
> https://sqitch.org/docs/manual/sqitch-add/#templates
>
> 2) A script that I run over table definition to add the fields.
What about INHERITS?
test=# create table CCD_template (
test(# Created timestamptz,
test(# Changed timestamptz,
test(# Deleted timestamptz );
CREATE TABLE
test=# create table foobar (
field1 integer,
field2 bytea
) inherits (CCD_template);
CREATE TABLE
test=#
test=# \d foobar
Table "public.foobar"
Column | Type | Modifiers
---------+--------------------------+-----------
created | timestamp with time zone |
changed | timestamp with time zone |
deleted | timestamp with time zone |
field1 | integer |
field2 | bytea |
Inherits: ccd_template
>
>>
>> Ken LaCrosse
>>
>> Senior IT InfoSec and Infrastructure Monitoring Specialist
>>
>> I.T. | American Recovery Service & Skipbusters
>>
>> Phone: (800)398-6480 x3758
>>
>> Email: KLacrosse@pkwillis.com <mailto:KLacrosse@pkwillis.com>
>>
>> www.pkwillis.com <http://www.pkwillis.com/;
>>
>> * Visibility is key. *
>>
>> Without visibility you can't see.
>>
>> What you can't see you can't measure.
>>
>> What you can't measure you can’t planfully change.
>>
>> What you can’t planfully change you can't manage.
>>
>> And if we can’t manage then why are we here?
>>
>> NOTICE: The information contained in this transmission, including
>> attachments, may contain confidential information that is privileged,
>> confidential and/or exempt from disclosure by applicable law. It is
>> intended only for the use of the person(s) or entity to which it is
>> addressed. If the reader of this transmission is not the intended
>> recipient, the reader is hereby notified that any review, use,
>> dissemination, distribution or duplication of this communication
>> (including any reliance thereon) is strictly prohibited. If you have
>> received this transmission in error, please contact the sender by reply
>> email, then delete and destroy the material in its entirety, whether in
>> electronic or hard copy format. WE SPECIFICALLY DISCLAIM RESPONSIBILITY
>> FOR ANY UNAUTHORIZED USE OF THIS COMMUNICATION OR ANY ATTACHMENTS TO IT.
>> Thank you.
>
>
--
Angular momentum makes the world go 'round.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Snippets?
@ 2019-05-24 19:29 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Ron <ronljohnsonjr@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Adrian Klaver @ 2019-05-24 19:29 UTC (permalink / raw)
To: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org
On 5/24/19 12:24 PM, Ron wrote:
> On 5/24/19 1:27 PM, Adrian Klaver wrote:
>> On 5/24/19 11:15 AM, Ken Lacrosse wrote:
>>> Is there any way in postgresql to have a "snippet" of SQL code which
>>> you could apply to all tables. Something you could add which would
>>> ensure that every table always has a Created, Changed and Deleted
>>> column for example. Sort of like a C include I suppose. Of course if
>>> I’m building a DB schema by hand I could just copy and paste those
>>> columns but it still seems a bit too, darn humans!, error prone.
>>
>> Not that I know of.
>>
>> Things I have done:
>> 1) Template table that I includes those fields that I then add to.
>>
>> FYI the Sqitch schema management system allows you to do the above:
>> https://sqitch.org/docs/manual/sqitch-add/#templates
>>
>> 2) A script that I run over table definition to add the fields.
>
> What about INHERITS?
It is an option, though it does have baggage:
https://www.postgresql.org/docs/11/sql-createtable.html
"INHERITS ( parent_table [, ... ] )
The optional INHERITS clause specifies a list of tables from which
the new table automatically inherits all columns. Parent tables can be
plain tables or foreign tables.
Use of INHERITS creates a persistent relationship between the new
child table and its parent table(s). Schema modifications to the
parent(s) normally propagate to children as well, and by default the
data of the child table is included in scans of the parent(s).
..."""
There is more.
If I was to go that route I would use LIKE:
"LIKE source_table [ like_option ... ]
The LIKE clause specifies a table from which the new table
automatically copies all column names, their data types, and their
not-null constraints.
Unlike INHERITS, the new table and original table are completely
decoupled after creation is complete. Changes to the original table will
not be applied to the new table, and it is not possible to include data
of the new table in scans of the original table.
..."
>
> test=# create table CCD_template (
> test(# Created timestamptz,
> test(# Changed timestamptz,
> test(# Deleted timestamptz );
> CREATE TABLE
>
> test=# create table foobar (
> field1 integer,
> field2 bytea
> ) inherits (CCD_template);
> CREATE TABLE
> test=#
> test=# \d foobar
> Table "public.foobar"
> Column | Type | Modifiers
> ---------+--------------------------+-----------
> created | timestamp with time zone |
> changed | timestamp with time zone |
> deleted | timestamp with time zone |
> field1 | integer |
> field2 | bytea |
> Inherits: ccd_template
>
>
>>
>>>
>>> Ken LaCrosse
>>>
>>> Senior IT InfoSec and Infrastructure Monitoring Specialist
>>>
>>> I.T. | American Recovery Service & Skipbusters
>>>
>>> Phone: (800)398-6480 x3758
>>>
>>> Email: KLacrosse@pkwillis.com <mailto:KLacrosse@pkwillis.com>
>>>
>>> www.pkwillis.com <http://www.pkwillis.com/;
>>>
>>> * Visibility is key. *
>>>
>>> Without visibility you can't see.
>>>
>>> What you can't see you can't measure.
>>>
>>> What you can't measure you can’t planfully change.
>>>
>>> What you can’t planfully change you can't manage.
>>>
>>> And if we can’t manage then why are we here?
>>>
>>> NOTICE: The information contained in this transmission, including
>>> attachments, may contain confidential information that is privileged,
>>> confidential and/or exempt from disclosure by applicable law. It is
>>> intended only for the use of the person(s) or entity to which it is
>>> addressed. If the reader of this transmission is not the intended
>>> recipient, the reader is hereby notified that any review, use,
>>> dissemination, distribution or duplication of this communication
>>> (including any reliance thereon) is strictly prohibited. If you have
>>> received this transmission in error, please contact the sender by
>>> reply email, then delete and destroy the material in its entirety,
>>> whether in electronic or hard copy format. WE SPECIFICALLY DISCLAIM
>>> RESPONSIBILITY FOR ANY UNAUTHORIZED USE OF THIS COMMUNICATION OR ANY
>>> ATTACHMENTS TO IT. Thank you.
>>
>>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 7+ messages in thread
* RE: Snippets?
@ 2019-05-24 19:55 Ken Lacrosse <klacrosse@pkwillis.com>
parent: Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 0 replies; 7+ messages in thread
From: Ken Lacrosse @ 2019-05-24 19:55 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Thanks guys. VERY informative. I'll be looking at all 3 approaches: INHERITS, LIKE and Sqitch.
Ken LaCrosse
Senior IT InfoSec and Infrastructure Monitoring Specialist
I.T. | American Recovery Service & Skipbusters
Phone: (800)398-6480 x3758
Email: KLacrosse@pkwillis.com
www.pkwillis.com
* Visibility is key. *
Without visibility you can't see.
What you can't see you can't measure.
What you can't measure you can’t planfully change.
What you can’t planfully change you can't manage.
And if we can’t manage then why are we here?
-----Original Message-----
From: Adrian Klaver [mailto:adrian.klaver@aklaver.com]
Sent: Friday, May 24, 2019 12:30
To: Ron; pgsql-general@lists.postgresql.org
Subject: Re: Snippets?
On 5/24/19 12:24 PM, Ron wrote:
> On 5/24/19 1:27 PM, Adrian Klaver wrote:
>> On 5/24/19 11:15 AM, Ken Lacrosse wrote:
>>> Is there any way in postgresql to have a "snippet" of SQL code which
>>> you could apply to all tables. Something you could add which would
>>> ensure that every table always has a Created, Changed and Deleted
>>> column for example. Sort of like a C include I suppose. Of course if
>>> I’m building a DB schema by hand I could just copy and paste those
>>> columns but it still seems a bit too, darn humans!, error prone.
>>
>> Not that I know of.
>>
>> Things I have done:
>> 1) Template table that I includes those fields that I then add to.
>>
>> FYI the Sqitch schema management system allows you to do the above:
>> https://sqitch.org/docs/manual/sqitch-add/#templates
>>
>> 2) A script that I run over table definition to add the fields.
>
> What about INHERITS?
It is an option, though it does have baggage:
https://www.postgresql.org/docs/11/sql-createtable.html
"INHERITS ( parent_table [, ... ] )
The optional INHERITS clause specifies a list of tables from which
the new table automatically inherits all columns. Parent tables can be
plain tables or foreign tables.
Use of INHERITS creates a persistent relationship between the new
child table and its parent table(s). Schema modifications to the
parent(s) normally propagate to children as well, and by default the
data of the child table is included in scans of the parent(s).
..."""
There is more.
If I was to go that route I would use LIKE:
"LIKE source_table [ like_option ... ]
The LIKE clause specifies a table from which the new table
automatically copies all column names, their data types, and their
not-null constraints.
Unlike INHERITS, the new table and original table are completely
decoupled after creation is complete. Changes to the original table will
not be applied to the new table, and it is not possible to include data
of the new table in scans of the original table.
..."
>
> test=# create table CCD_template (
> test(# Created timestamptz,
> test(# Changed timestamptz,
> test(# Deleted timestamptz );
> CREATE TABLE
>
> test=# create table foobar (
> field1 integer,
> field2 bytea
> ) inherits (CCD_template);
> CREATE TABLE
> test=#
> test=# \d foobar
> Table "public.foobar"
> Column | Type | Modifiers
> ---------+--------------------------+-----------
> created | timestamp with time zone |
> changed | timestamp with time zone |
> deleted | timestamp with time zone |
> field1 | integer |
> field2 | bytea |
> Inherits: ccd_template
>
>
>>
>>>
>>> Ken LaCrosse
>>>
>>> Senior IT InfoSec and Infrastructure Monitoring Specialist
>>>
>>> I.T. | American Recovery Service & Skipbusters
>>>
>>> Phone: (800)398-6480 x3758
>>>
>>> Email: KLacrosse@pkwillis.com <mailto:KLacrosse@pkwillis.com>
>>>
>>> www.pkwillis.com <http://www.pkwillis.com/;
>>>
>>> * Visibility is key. *
>>>
>>> Without visibility you can't see.
>>>
>>> What you can't see you can't measure.
>>>
>>> What you can't measure you can’t planfully change.
>>>
>>> What you can’t planfully change you can't manage.
>>>
>>> And if we can’t manage then why are we here?
>>>
>>> NOTICE: The information contained in this transmission, including
>>> attachments, may contain confidential information that is privileged,
>>> confidential and/or exempt from disclosure by applicable law. It is
>>> intended only for the use of the person(s) or entity to which it is
>>> addressed. If the reader of this transmission is not the intended
>>> recipient, the reader is hereby notified that any review, use,
>>> dissemination, distribution or duplication of this communication
>>> (including any reliance thereon) is strictly prohibited. If you have
>>> received this transmission in error, please contact the sender by
>>> reply email, then delete and destroy the material in its entirety,
>>> whether in electronic or hard copy format. WE SPECIFICALLY DISCLAIM
>>> RESPONSIBILITY FOR ANY UNAUTHORIZED USE OF THIS COMMUNICATION OR ANY
>>> ATTACHMENTS TO IT. Thank you.
>>
>>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
NOTICE: The information contained in this transmission, including attachments, may contain confidential information that is privileged, confidential and/or exempt from disclosure by applicable law. It is intended only for the use of the person(s) or entity to which it is addressed. If the reader of this transmission is not the intended recipient, the reader is hereby notified that any review, use, dissemination, distribution or duplication of this communication (including any reliance thereon) is strictly prohibited. If you have received this transmission in error, please contact the sender by reply email, then delete and destroy the material in its entirety, whether in electronic or hard copy format. WE SPECIFICALLY DISCLAIM RESPONSIBILITY FOR ANY UNAUTHORIZED USE OF THIS COMMUNICATION OR ANY ATTACHMENTS TO IT. Thank you.
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2019-05-24 19:55 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-12-14 20:07 Snippets Uwe C. Schroeder <uwe@oss4u.com>
2019-05-24 18:15 Snippets? Ken Lacrosse <klacrosse@pkwillis.com>
2019-05-24 18:20 ` Re: Snippets? Ron <ronljohnsonjr@gmail.com>
2019-05-24 18:27 ` Re: Snippets? Adrian Klaver <adrian.klaver@aklaver.com>
2019-05-24 19:24 ` Re: Snippets? Ron <ronljohnsonjr@gmail.com>
2019-05-24 19:29 ` Re: Snippets? Adrian Klaver <adrian.klaver@aklaver.com>
2019-05-24 19:55 ` RE: Snippets? Ken Lacrosse <klacrosse@pkwillis.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox