Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6989077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:09:55+00:00 2026-05-27T19:09:55+00:00

In PostgreSQL 8.4.9 I have a small game, where users can purchase a VIP

  • 0

In PostgreSQL 8.4.9 I have a small game, where users can purchase a VIP (“very important person”) status:

# \d pref_users;
                Table "public.pref_users"
   Column   |            Type             |   Modifiers
------------+-----------------------------+---------------
 id         | character varying(32)       | not null
 vip        | timestamp without time zone |

If vip has never been purchased it will be NULL.

If vip has expired it will be < CURRENT_TIMESTAMP.

I’m trying to create PL/pgSQL procedure allowing users with enough vip status left to give a week of it to other users, as a “gift”:

create or replace function pref_move_week(_from varchar,
    _to varchar) returns void as $BODY$
        declare
                has_vip boolean;
        begin

        select vip > current_timestamp + interval '1 week'
        into has_vip from pref_users where id=_from;

        if (not has_vip) then
                return;
        end if;

        update pref_users set vip = current_timestamp - interval '1 week' where id=_from;
        update pref_users set vip = current_timestamp + interval '1 week' where id=_to;

        end;
$BODY$ language plpgsql;

Unfortunately this procedure does not work properly if vip of the giving user is NULL:

# update pref_users set vip=null where id='DE16290';
UPDATE 1

# select id,vip from pref_users where id in ('DE16290', 'DE1');
   id    |            vip
---------+----------------------------
 DE1     | 2012-01-05 17:35:17.772043
 DE16290 |
(2 rows)

# select pref_move_week('DE16290', 'DE1');
 pref_move_week
----------------

(1 row)

# select id,vip from pref_users where id in ('DE16290', 'DE1');
   id    |            vip
---------+----------------------------
 DE1     | 2012-01-05 17:43:11.589922
 DE16290 | 2011-12-22 17:43:11.589922
(2 rows)

I.e. the IF-statement above doesn’t seem to work and falls through.

Also I wonder if the has_vip variable is needed here at all?

And how could I ensure that the primary keys _from and _to are really present in the pref_users table or is this already taken care of (because one of the UPDATE statements will “throw an exception” and the transaction will be rollbacked)?

UPDATE:

Thank you for all the replies and I’ve also got a tip to use:

          if (not coalesce(has_vip, false)) then
                   return;
          end if;

But now I have a new problem:

# select id,vip from pref_users where id in ('DE16290', 'DE1');
  id    |            vip
---------+----------------------------
 DE1     | 2012-01-05 17:43:11.589922
 DE16290 |
(2 rows)

(i.e. DE1 has vip until May and should be able to give a week to DE16290, but):

# select pref_move_week('DE1', 'DE16290');
 pref_move_week
----------------

(1 row)

# select id,vip from pref_users where id in ('DE16290', 'DE1');
  id    |            vip
---------+----------------------------
 DE1     | 2012-01-05 17:43:11.589922
 DE16290 |
(2 rows)

(For some reason nothing has changed?)

UPDATE 2: The final solution –

    create or replace function pref_move_week(_from varchar,
        _to varchar) returns void as $BODY$
            begin

            select 1 from pref_users
                where id=_from and
                vip > current_timestamp + interval '1 week';

            if not found then
                    return;
            end if;

            update pref_users set
                vip = vip - interval '1 week'
                where id=_from;

            update pref_users set
                vip = greatest(vip, current_timestamp) + interval '1 week'
                where id=_to;

            end;
    $BODY$ language plpgsql;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:09:55+00:00Added an answer on May 27, 2026 at 7:09 pm

    Null values return false on any compare short of is null (so where null <> 1 is false and null = 1 is false…null never equals or not equals anything). Instead of

    if (not has_vip) then return
    

    go with

    if (not has_vip) or has_vip is null then return
    

    The syntax you are using is a bit unique here and I’m not used to reading it…guessing you’ve come from a different background than SQL and haven’t encountered null fun yet. Null is a special concept in SQL that you’ll need to handle differently. Remember it is not equal to blank or 0, nor is it less than or greater than any number.

    Edit in:
    I’m a bit confused by this:

      select vip > current_timestamp + interval '1 week'
      into has_vip from pref_users where id=_from;
    

    I believe this is equivalent to:

      select vip 
      into has_vip from pref_users 
      where id=_from
      and vip > current_timestamp + interval '1 week';
    

    I hope anyway. Now this is going to return a null if no record is found or if the VIP value is null. can you just go if has_vip is null then return?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd need advice on following situation with Oracle/PostgreSQL: I have a db table with
I have a table in PostgreSQL where the schema looks like this: CREATE TABLE
I have started to use NHibernate 3.0 and PostgreSQL for a small project, so
I have postgresql running (/opt/local/lib/postgresql90/bin). The data base is set up @ /Users/demet8/postgres/data. I
I have a table in PostgreSQL that I need read into memory. It is
I have postgresql (in perlu) function getTravelTime(integer, timestamp), which tries to select data for
I have a Postgresql database on which I want to do a few cascading
Background: I have a PostgreSQL (v8.3) database that is heavily optimized for OLTP. I
I'm new to postgreSQL and I have a simple question: I'm trying to create
I've got two PostgreSQL databases that have been created using the same sql file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.