Unix Technical Forum

Setting sequence ids after importing data

This is a discussion on Setting sequence ids after importing data within the pgsql Novice forums, part of the PostgreSQL category; --> Hi: I am importing data from sqlite db into postgres. The tables have an id column that is obtained ...


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, 09:51 PM
Hari Patel
 
Posts: n/a
Default Setting sequence ids after importing data

Hi:

I am importing data from sqlite db into postgres. The tables have an id
column that is obtained from a sequence.

To preserve the foreign key relationships, I need to preserve the ids while
importing into postgres.

The problem is that after import, the sequences don't get incremented which
leads to unique constraint violation errors further down.
How do I fix this ?

Thanks,
Hari

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 09:51 PM
Sean Davis
 
Posts: n/a
Default Re: Setting sequence ids after importing data

Hari Patel wrote:
> Hi:
>
> I am importing data from sqlite db into postgres. The tables have an id
> column that is obtained from a sequence.
>
> To preserve the foreign key relationships, I need to preserve the ids
> while importing into postgres.
>
> The problem is that after import, the sequences don't get incremented
> which leads to unique constraint violation errors further down.
> How do I fix this ?


See here in the documentation:

http://www.postgresql.org/docs/8.1/s...-sequence.html

SEan

---------------------------(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
  #3 (permalink)  
Old 04-17-2008, 09:51 PM
Hari Patel
 
Posts: n/a
Default Re: Setting sequence ids after importing data

Thanks Sean. I'm trying the following after having done the import.

CREATE function set_sequence_ids() RETURNS INTEGER AS '
DECLARE
arg INTEGER;
BEGIN
select into arg max(id) from foo;
select setval('foo_id_seq', 10);
return arg;
END;
' LANGUAGE 'plpgsql';

I get the following error in psql.

psql:func.sql:9: ERROR: syntax error at or near "foo_id_seq" at charact
er 152
psql:func.sql:9: LINE 6: select setval('foo_id_seq', 10);
psql:func.sql:9: ^


setval seems to work okay outside plpgsql but I need it to do the select
into.

Any help is greatly appreciated.

Thanks,
Hari

On 5/8/06, Sean Davis <sdavis2@mail.nih.gov> wrote:
>
> Hari Patel wrote:
> > Hi:
> >
> > I am importing data from sqlite db into postgres. The tables have an id
> > column that is obtained from a sequence.
> >
> > To preserve the foreign key relationships, I need to preserve the ids
> > while importing into postgres.
> >
> > The problem is that after import, the sequences don't get incremented
> > which leads to unique constraint violation errors further down.
> > How do I fix this ?

>
> See here in the documentation:
>
> http://www.postgresql.org/docs/8.1/s...-sequence.html
>
> SEan
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 09:51 PM
Michael Glaesemann
 
Posts: n/a
Default Re: Setting sequence ids after importing data


On May 9, 2006, at 10:59 , Hari Patel wrote:

> CREATE function set_sequence_ids() RETURNS INTEGER AS '
> DECLARE
> arg INTEGER;
> BEGIN
> select into arg max(id) from foo;
> select setval('foo_id_seq', 10);
> return arg;
> END;
> ' LANGUAGE 'plpgsql';
>
> I get the following error in psql.
>
> psql:func.sql:9: ERROR: syntax error at or near "foo_id_seq" at
> charact
> er 152
> psql:func.sql:9: LINE 6: select setval('foo_id_seq', 10);
> psql:func.sql:9: ^


Try just
setval('foo_id_seq',10)
or
PEFORM setval('foo_id_seq', 10)

It's just a function call, and since you're not saving the return
value, you don't use SELECT. SELECT has different syntax in PL/pgSQL
than in SQL.

Hope this helps.

Michael Glaesemann
grzm seespotcode net




---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-17-2008, 09:51 PM
Tom Lane
 
Posts: n/a
Default Re: Setting sequence ids after importing data

"Hari Patel" <hariwise@gmail.com> writes:
> CREATE function set_sequence_ids() RETURNS INTEGER AS '
> DECLARE
> arg INTEGER;
> BEGIN
> select into arg max(id) from foo;
> select setval('foo_id_seq', 10);
> return arg;
> END;
> ' LANGUAGE 'plpgsql';


> I get the following error in psql.


> psql:func.sql:9: ERROR: syntax error at or near "foo_id_seq" at charact
> er 152
> psql:func.sql:9: LINE 6: select setval('foo_id_seq', 10);
> psql:func.sql:9: ^


You've got a quoting problem: either double the quotes embedded in the
function body, or use dollar quotes ($$) to surround the function body
instead of plain quotes (').

BTW, seems like the above is wrong anyway: don't you want to set the
sequence to arg+1, rather than constant 10?

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:38 PM.


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