Tuesday, July 01, 2008

Sequence Number Allocation in PeopleSoft

In PeopleSoft for the Oracle DBA Chapter 8 (p168-170) in a section called Sequence Numbers and Concurrency I discuss the implications of how this is done in HR8.4 as a part of the save time processing in PeopleCode FUNCLIB_HR.EMPLID.FieldFormula.


The problem is that while this update remains uncommitted, nobody else can obtain a new employee ID because the row is locked. This design has scalability issues. PeopleSoft applications generally get away with this because the update is performed as a part of save time processing which is committed when that is complete. Thus the lock is held for only a short time, and never waits for a human response. It executes in the application server which should be close to the database, so there is no network latency.

However, I was looking at this same PeopleCode in HR9.0 and I have noticed that is has changed. The change appears to have been introduced in HR8.8, and uses a new built-in PeopleCode function, GetNextNumberWithGapsCommit(record.field, max_number, increment [, WHERE_Clause, paramlist]), that was introduced in PeopleTools 8.44.

PeopleBooks describes the function thus:
"Use the GetNextNumberWithGapsCommit function to return the sequence number value plus increment for the given field residing in the given record. This function also enables you to specify a SQL Where clause as part of the function for maintaining multiple sequence numbers in a single record.

This function is typically used for obtaining a new sequence number for the application ... The sequence number (record.field ) is incremented right away and it doesn't hold any database internal row lock beyond the execution of this function.

Note. A secondary database connection is used to increment and retrieve record.field. The default behaviour is to keep the secondary database connection persistent in order to improve performance for the next GetNextNumberWithGapsCommit usage. If the database administrator finds the persistent connection too high an overhead for the production environment (which should not be the case since PeopleSoft uses application server to multiplex the database connection), the database administrator can change the default behaviour to use an on-demand connection method. The persistent second connection is disabled using DbFlags bit eight in the application server and process scheduler configuration files. The second connection can be completely disabled using DbFlags bit four in the application server and process scheduler configuration files

The following restrictions apply to the GetNextNumberWithGapsCommit function:
  • The function is enabled for Application Engine programs running in stand-alone mode (via PSAE). This function is disabled for Application Engine programs that execute through the PeopleSoft Application Engine server (PSAESRV).
  • This function is enabled for PeopleSoft Application Engine programs running under Unix System Services on z/OS.
  • PeopleSoft does not recommend Using both the GetNextNumberWithGapsCommit function and the GetNextNumber function in the same application, on the same table, in the same unit of work. This can lead to lock contention or deadlocking.
  • For a DB2 z/OS database, isolate the table that contains the sequence number to its own tablespace and set the locksize parameter to row."

Hence, if migrating data into a system via a Component Interface (eg. employees from a legacy system where new EMPLIDs will be allocated), and you need to do this with multiple concurrent process to achieve throughput, it is advantageous to use PSAE rather than PSAESRV processes, otherwise they are likely to serialise.

This is the PeopleTools trace produced when I added an new person to an HR system. You can see the second connection to the database is created at line 51935, and then the row is updated, queried and immediately committed. The lock is only held for only 35ms (on my laptop), instead of holding it until all the save-time processing completes.

PSAPPSRV.4180 1-51932 23.26.14 0.000000 >>> start-ext Nest=01 assign_employee_id FUNCLIB_HR.EMPLID.FieldFormula
PSAPPSRV.4180 1-51933 23.26.14 0.019000 Cur#2.4180.HCM89 RC=0 Dur=0.015000 Connect=Secondry/HCM89/SYSADM/
PSAPPSRV.4180 1-51934 23.26.14 0.000000 SamCreateSecondary ---- Successful obtain Second DB connection
PSAPPSRV.4180 1-51935 23.26.14 0.000000 Cur#2.4180.HCM89 RC=0 Dur=0.000000 COM Stmt=UPDATE PS_INSTALLATION SET EMPLID_LAST_EMPL = EMPLID_LAST_EMPL + 1
PSAPPSRV.4180 1-51936 23.26.14 0.028000 Cur#2.4180.HCM89 RC=0 Dur=0.028000 EXE
PSAPPSRV.4180 1-51937 23.26.14 0.000000 Cur#2.4180.HCM89 RC=0 Dur=0.000000 COM Stmt=SELECT EMPLID_LAST_EMPL FROM PS_INSTALLATION
PSAPPSRV.4180 1-51938 23.26.14 0.005000 Cur#2.4180.HCM89 RC=0 Dur=0.005000 EXE
PSAPPSRV.4180 1-51939 23.26.14 0.000000 Cur#2.4180.HCM89 RC=0 Dur=0.000000 Fetch
PSAPPSRV.4180 1-51940 23.26.14 0.002000 Cur#2.4180.HCM89 RC=0 Dur=0.002000 Commit
PSAPPSRV.4180 1-51941 23.26.14 0.000000 > GetNextNumberWithGapsCommit info ( INSTALLATION, EMPLID_LAST_EMPL, (null) )

This approach is certainly an improvement over the previous behaviour. It is similar to an Oracle Sequence without any caching which increments a value stored on SYS.SEQ$ in an autonomous transaction which it immediately commits without affecting the main transaction (except that Oracle does this in the same session, and here PeopleSoft have created a separate session).

Also, like an Oracle sequence, if the main transaction fails you are left with a gap in your numbers. Some of the dictionary definitions of the word sequence imply a fixed increment or decrement between values (usually 1). In some ways, it would be more helpful to think of this a way of generating a unique identifier rather than a sequence number.

By looking at v$session you can see that a second session is created for the same client process.
Session  Serial Program                          Client    Shadow
ID Number Name Process Proces
------- ------- -------------------------------- --------- ------
Client Info LOGON_TIME
----------------------------------------------------------- -------------------
147 35 PSAPPSRV.exe 4180:7720 5824
PS,,GO-FASTER-4.london.go-faster.co.uk,HCM89,PSAPPSRV.exe, 23:08:40 18/06/2008

139 367 PSAPPSRV.exe 4180:7720 5824
23:26:14 18/06/2008

The DbFlags field in the Application Server and Process Scheduler parameter filess controls how PSAPPSRV and PSAE in creates and terminates the secondary session.
; DbFlags Bitfield
; Bit Flag
; --- ----
; 4 - Disable Second DB Connection
; 8 - Disable Persistent Secondary DB Connection

By default, the second session persists until the PSAPPSRV session terminates when either the application server is shut down or the PSAPPSRV process recycles when the number of services reaches the recycle_count specified in the application server configuration file.

Obviously, disabling the second database connection would effectively negate the advantages of this new functionality.

There is additional overhead to the second connection, and if DbFlags is set to 8 the secondary session would be shut down after the incremented sequence number has been returned. However, in a large, highly active system, where sequence numbers are requested frequently, this would lead to frequent creation of secondary sessions. The process of allocating new sequence numbers would also have to wait for the new session to be created.

Note, CLIENT_INFO is not set in the secondary session. However, it is possible to copy the CLIENT_INFO from the primary session using an ON LOGON trigger (which can also be dowloaded from the Go-Faster website).
REM second_client_info.sql
rem (c)Go-Faster Consultancy 2008

spool second_client_info

rem requires following privileges to be granted explicitly
GRANT EXECUTE ON sys.dbms_application_info TO sysadm;
GRANT SELECT ON sys.v_$session TO sysadm;
GRANT SELECT ON sys.v_$process TO sysadm;
GRANT SELECT ON sys.v_$mystat TO sysadm;

CREATE OR REPLACE TRIGGER sysadm.connect_secondary_session
ON sysadm.schema
l_client_info VARCHAR2(64);
SELECT SUBSTR(p.client_info||'Secondary Session',1,64)
INTO l_client_info
FROM v$session p --primary session
, v$session s --secondary session
WHERE s.program = p.program --same program
AND s.process = p.process --same process
AND s.machine = p.machine --same machine
AND s.user# = p.user# --same user
AND s.sid != p.sid --different session
AND s.client_info IS NULL --client info not already set
AND s.sid = (SELECT m.sid
FROM v$mystat m
WHERE rownum = 1)
AND p.logon_time < client_info="">l_client_info);
sys.dbms_application_info.set_action(action_name=>'Secondary Session');


show errors
The result is that when the second session is created the trigger fires and copies the client info from the first session and appends the string ',Second'.
LOGON_TIME          Client Info
------------------- ----------------------------------------------------------------
21:44:37 26/06/2008 PS,,GO-FASTER-4.london.go-faster.co.uk,HCM89,PSAPPSRV.exe,
08:01:29 01/07/2008 PS,,GO-FASTER-4.london.go-faster.co.uk,HCM89,PSAPPSRV.exe,Second


Aniruddha said...

Nice article..
I am facing the same scenario..during my conversion using Component interface and peoplecode, the process does not progress and kind of stucks..Our DBA figured out that there are two sessions that are locking the same table...and we are on Informix-UNIX..i.e. the secondary connection will be established even if I use PSAE...I am confused how to resolve this issue...

David Kurtz said...

You could try disabling the second session by setting DBFlags.

; DbFlags Bitfield
; Bit Flag
; --- ----
; 4 - Disable Second DB Connection
; 8 - Disable Persistent Secondary DB Connection

Thomas said...


I can contact you because I am peoplesoft developer and I have a huge problem.
Randomly, the PS_INSTALLATION table data are deleted. (DELETE FROM PE_INSTALLATION)
The analysis of our DBA is: module PSAPPSRV @ gmapwas02 (SYSADM.PSUPSWORKLIST) deletes the data.

Do you have an idea, where it could be executed DELETE?

Thank you in advance for your reply

David Kurtz said...

This sort of thing is always difficult. I don't know what PeopleTools version you are using.

If you are running 8.50 or higher, then the module will be set to the PeopleSoft component name. However, the module is still set to PSAPPSRV when the application server process starts which would suggest this is happening during server startup.

If you are using 8.49 or lower. module is only set to PSAPPSRV at startup, and delete on PSINSTALLATION could be anywhere.

If you can't reproduce it while running trace, then a possible option is a technique (I have this heard called 'intelligence by return fire') is to stop it happening and create an error when something tries. Create a database trigger that fires on delete on this table and raises an error that will in turn cause the PeopleSoft transaction to fail. Hopefully, somebody will then report the error in a support call.

Unknown said...

How can we implement the same logic in SQR.
If we run the sqr concurrently,weather it will give same Id twice.

David Kurtz said...

I ought first to say that PeopleSoft has moved away from SQR. So this is not a problem that they will address.
The only suggestion I can make for SQR (and I have NOT tried this myself) is to encapsulate the logic in PL/SQL and use an autonomous transaction so you can commit it independently.