Unix Technical Forum

Indexes and sequences

This is a discussion on Indexes and sequences within the pgsql Novice forums, part of the PostgreSQL category; --> I'm cleaning up a database that someone else made and have a couple questions. When I look at a ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 10:51 PM
Jeff Willden
 
Posts: n/a
Default Indexes and sequences

I'm cleaning up a database that someone else made and have a couple
questions. When I look at a table in pgAdmin it shows the DDL below.
Do I need an index on the groupid_seq sequence? Doesn't the sequence
already include one? Also, even if I need it, it doesn't need to be a
unique index because the sequence already ensures uniqueness, right?
On top of that there's a primary key constraint that also ensures
uniqueness. Isn't there a bunch of redundant stuff here?

CREATE TABLE buddygroup
(
groupid integer NOT NULL DEFAULT nextval('groupid_seq'::text),
userid integer NOT NULL,
title character varying(255) NOT NULL,
CONSTRAINT buddygroup_pkey PRIMARY KEY (groupid)
)
WITH OIDS;
ALTER TABLE buddygroup OWNER TO postgres;

-- Index: bg_groupid_idx

-- DROP INDEX bg_groupid_idx;

CREATE UNIQUE INDEX bg_groupid_idx
ON buddygroup
USING btree
(groupid);

-- Index: bg_userid_idx

-- DROP INDEX bg_userid_idx;

CREATE INDEX bg_userid_idx
ON buddygroup
USING btree
(userid);

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 10:51 PM
Sean Davis
 
Posts: n/a
Default Re: Indexes and sequences

On Jan 19, 2008 5:04 PM, Jeff Willden <jeff@pavanell.com> wrote:

> I'm cleaning up a database that someone else made and have a couple
> questions. When I look at a table in pgAdmin it shows the DDL below.
> Do I need an index on the groupid_seq sequence? Doesn't the sequence
> already include one? Also, even if I need it, it doesn't need to be a
> unique index because the sequence already ensures uniqueness, right?
> On top of that there's a primary key constraint that also ensures
> uniqueness. Isn't there a bunch of redundant stuff here?
>


A "sequence" does not include an index, no. A sequence is NOT a column, but
rather a separate data entity that increments. The value of a sequence can
be inserted into a column, but the column and the sequence are NOT the same
thing. Indexes are created on columns.

You do need to create an index, and it should be a unique index if you want
uniqueness. Since the DEFAULT value is taken from the sequence, the values
will be unique as long as you do not EVER specify the value of the column in
an insert or update. That is a dangerous assumption, so specify a unique
index.

The primary key will automatically create a unique index if one does not
exist.

There is a section in the manual about "serial" data types that will answer
many of these questions in more detail.

Hopefully that helps.

Sean



>
> CREATE TABLE buddygroup
> (
> groupid integer NOT NULL DEFAULT nextval('groupid_seq'::text),
> userid integer NOT NULL,
> title character varying(255) NOT NULL,
> CONSTRAINT buddygroup_pkey PRIMARY KEY (groupid)
> )
> WITH OIDS;
> ALTER TABLE buddygroup OWNER TO postgres;
>
> -- Index: bg_groupid_idx
>
> -- DROP INDEX bg_groupid_idx;
>
> CREATE UNIQUE INDEX bg_groupid_idx
> ON buddygroup
> USING btree
> (groupid);
>
> -- Index: bg_userid_idx
>
> -- DROP INDEX bg_userid_idx;
>
> CREATE INDEX bg_userid_idx
> ON buddygroup
> USING btree
> (userid);
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
> http://archives.postgresql.org
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 10:51 PM
Lukas
 
Posts: n/a
Default Function in function

Hello,

If I have two PostgreSQL functions:
Function_one()
and
Function_two()

I call one from other:

Function_one
Begin
return function_two()
end

Now if I change something in body of Function_two and will NOT
recreate/replace Function_one I will still get old value.
My question is: Is it possible somehow recreate Function_two that it
changed in every function it was used?

thx
Lukas


--
This message has been scanned for viruses and
dangerous content by OpenProtect(http://www.openprotect.com), and is
believed to be clean.


---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 10:51 PM
Tom Lane
 
Posts: n/a
Default Re: Function in function

"Lukas" <lukas@fmf.vtu.lt> writes:
> Now if I change something in body of Function_two and will NOT
> recreate/replace Function_one I will still get old value.


Really? Works fine for me. Please show a complete test case.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:08 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com