HowTo Reset Sequences in PostgreSQL

I use a lot of "serial" data type fields, sometimes the nextval of my sequence doesn't reflect the max() of my table.

Since I do a lot of moving data around these can get messed up.

The basic command that you want to use to reset a sequence is:

select setval("<sequence name>", <value>);

However, the cool thing about Postgres is that <value> can be a query too so I use:

select setval("<sequence name>", max(<column name>) from <table>);

So I wrote a little perl script to look through all of the columns that have a default like "nextval" (which if you look in the database is how a serial column actually looks) and sets the sequence to the max of the value in that table.

You can get it here

Usage: reset_requences.pl -d <database> [-s <schema>] [-u <user>] [-p <password>]

If you specify a database it will reset all of the columns in the database
If you specify a schema it will reset all of the columns within a specific schema.
If you specify a user it will reset all of the columns for a specific user.

Welcome to the new site

I recently decided that I wanted my site's backend to be on Postgres, since it's sort of my job and all. So I've ditched Wordpress for Habari. And am now able to uninstall MySQL, which suits me just fine.

Habari's permalinks are slightly different than the ones in wordpress, so I've put redirects for my most popular posts, but not everything. If something used to be here, just search for it or go to the (much simplified) tags you should be able to find it again.

Syncing Droid X with Banshee

Banshee (and apparently most linux media players) expects a file in the root of your Android device named ".is_media_player"

For the Droid X file shouldn't be empty, as many places on the 'net state, you need to add more information.

Mine looks like this:

name=droid
audio_folders=/media/serial number/music/
folder_depth=2
output_formats=audio/mpeg,audio/mp3,audio/x-aac
playlist_format=audio/x-mpegurl
cover_art_file_type=jpg

When my droid mounts in linux it mounts to /media/<what i assume to be a serial number>

UPDATE 4/28/11 New versions of banshee don't seem to recognize the is_media_player name, it's now called .is_audio_player.

PostgreSQL: Insert () ... Returning with Perl DBI

PostgreSQL provides the ability to return a value on insert. so for example:

create table foo (id serial, name text);
insert into foo('dave') returning id;

Will return the auto-generated value for "id" assigned to 'dave'

You can leverage this from within Perl by doing something like this:

my $foo_insert = << "EOH";
insert into foo(?) returning id
EOH

my $foo_cur = $dbh->prepare($foo_insert);
$foo_cur->execute('Dave');
my $foo_rec = $foo_cur->fetchrow_hashref();
print $$foo_rec{"id"};

Of course, you can use whichever fetch method you're comfortable with.

Installing Virtualbox Guest Addons for Fedora 13

You need to do 2 updates to make this work:

sudo yum update kernel
sudo yum install binutils gcc make patch libgomp glibc-headers \
glibc-devel kernel-headers kernel-devel

If that doesn't do it, check your installed kernel versions.

Remove any kernels that don't have kernel-devel/kernel-headers installed.


About

Random Database, OS or otherwise interesting tips and tricks.

User


Clicky Web Analytics