Skip to content

Instantly share code, notes, and snippets.

View forstie's full-sized avatar

Scott Forstie forstie

  • IBM
View GitHub Profile
@forstie
forstie / Collection Services made easy with SQL.sql
Last active October 4, 2023 15:01
The Collection Services (CS) config and CS data hold a goldmine of operational insight about the IBM i. This Gist shows how a little bit of SQL can open the door to gaining insight and value from this data.
--
-- =======================================================================
-- Subject: See Collection Services (CS) config and query CS data with SQL
-- =======================================================================
--
-- Author: Scott Forstie
-- Date : September, 2023
-- Email : forstie@us.ibm.com
--
-- This Gist is a two-fer:
@forstie
forstie / Reactive index strategy.sql
Last active August 26, 2023 01:40
Database and SQL performance tuning is a persistent focus, which many times leads into the indexing strategy. In this gist, I show how some of the existing tools can be tied together to achieve an automated "DBE in a box".
-- Subject: DBE in a box
-- Author: Scott Forstie
-- Date : August, 2023
-- Features Used : This Gist uses SQL PL, qsys2.SYSTEM_STATUS_INFO_basic, systools.lprintf, qsys2.sysixadv, qsys2.sysindexes, QSYS2.SYSINDEXSTAT, SYSTOOLS.ACT_ON_INDEX_ADVICE, QSYS2.RESET_TABLE_INDEX_STATISTICS and SYSTOOLS.REMOVE_INDEXES
--
-- Note:
-- 1) Indexes are not free, and care should be taken when deciding whether an index is worth creating
-- 2) Indexes are not free, and care should be taken to understand whether adequate system resources are available before creating additional indexes
-- 3) Indexes are not free, and care should be taken to establish a well trained Database Engineer (DBE) responsible for your IBM i partitions
--
@forstie
forstie / PTF Cover Letters.sql
Last active August 25, 2023 15:36
The request... show how SQL can be used to narrow the field down to those PTFs that have special instructions, and only return the special instructions. The following example focuses on what an admin might do AFTER loading PTFs, but BEFORE applying them.
-- Subject: PTF Cover Letters
-- Author: Scott Forstie
-- Date : April, 2023
-- Features Used : This Gist uses qsys2.ptf_info, SQL Alias, ltrim, rtrim(), dynamic SQL, SQL PL, PIPE
--
-- Note:
-- 1) Whether PTF Cover Letters exist on your IBM i is up to whomever ordered the PTFs
-- Use the Copy PTF Cover Letter (CPYPTFCVR) command to bring in PTF cover letters
-- https://www.ibm.com/docs/en/i/7.5?topic=ssw_ibm_i_75/cl/cpyptfcvr.html
-- 2) The file QGPL/QAPZCOVER *FILE contains one member for each PTF Cover Letter
@forstie
forstie / Query Spooled File contents for a specific user.sql
Last active August 19, 2023 14:20
Query Spooled File contents for a specific user
--
-- description: What spooled files does the current user own?
--
select job_name, spooled_file_name, file_number, user_data,
create_timestamp
from qsys2.output_queue_entries_basic
where user_name = user;
stop;
--
-- description: Query the contents of RUNSQLSTM spooled files for the current user
@forstie
forstie / ZDA mystery solved
Last active August 18, 2023 17:37
This example shows several things worthy of attention. System managers can utilize exit program to establish improved auditing, understanding, and real time business rules using SQL. For QZDASOINIT jobs, it can be easily considered an unsolvable mystery. With the help of Db2 for i Client Special Registers, we can understand a great deal about ZD…
-- =============================================================
-- Author: Scott Forstie
-- Date : September 8, 2019
-- Revised: August 28, 2020
--
-- Description: Have you ever wondered what's driving all
-- those QZDASxINIT jobs?
-- This example shows how to establish an
-- exit program to capture client special register
-- and other detail for ZDA connections.
@forstie
forstie / prtprvaut.sql
Created May 14, 2021 14:02
Someone sent me an "SQL Challenge". Challenge accepted! #SQLcandoit
--
-- If you're not ready to use IFS_OBJECT_PRIVILEGES, this might help you...
-- Note: A special thanks to Sue Romano for her help with this Gist
--
-- https://www.ibm.com/docs/en/i/7.4?topic=services-ifs-object-privileges-table-function
cl:PRTPVTAUT OBJTYPE(*STMF) DIR('/') SCHSUBDIR(*YES);
--
-- create an alias over the most recent execution of PRTPRVAUT
@forstie
forstie / controlling adopted authority.sql
Last active August 17, 2023 15:36
If you use adopted authority, how do you avoid allowing code that you call from taking a free ride on your elevated authority? One answer lies within this gist...
-- =========================================================================================
--
-- Adopted authority... if you use it, how do you control it?
--
-- =========================================================================================
-- Q: How can you avoid propagating authority to code you need to call?
--
-- A: MODIFY INVOCATION AUTHORITY ATTRIBUTES
-- https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_74/rzatk/MODINVAU.htm
-- =========================================================================================
@forstie
forstie / Using LICOPT and initAuto.sql
Last active July 2, 2023 05:48
The inspiration for this Gist came from a client. A piece of code had a long-standing problem where a variable was not initialized. Given the unpredictable nature of uninitialized made the topic hard to approach. This Gist shows how the IBM i Optimizing Translator can be used to find such problems within a dev or test environment.
-- Subject: Using initAuto to find uninitialized variable problems in ILE code
-- Author: Scott Forstie
-- Date : June, 2023
-- Features Used : This Gist uses SQL PL, INCLUDE, Change Program (CHGPGM) CL command, qsys2.joblog_info, qsys2.program_info, qsys2.qcmdexc, and QSYS2.BOUND_MODULE_INFO
--
-- A) Programmers use declared variables and structures to facilitate program logic.
-- B) Programmers should always initialize or assign declared variables and structures to contain known and expected values.
-- C) Programmers sometime miss step B.
--
-- This Gist shows how the IBM i Optimizing Translator can help.
@forstie
forstie / SWAP_DYNUSRPRF.sql
Created November 24, 2019 18:37
With the latest Db2 PTF Groups for IBM i 7.3 and 7.4, you can now identify and fix those *PGM and *SRVPGM's that use SQL and were built incorrectly. This example finds those cases where *OWNER will be used for static SQL, but *USER will be used for dynamic SQL. The procedure swaps the dynamic user profile setting to *OWNER. This utility approach…
--
-- description: Which SQL programs or services have a mismatch between user profile and dynamic user profile (full)
--
select user_profile, dynamic_user_profile, program_schema, program_name, program_type,
module_name, program_owner, program_creator, creation_timestamp, default_schema,
"ISOLATION", concurrentaccessresolution, number_statements, program_used_size,
number_compressions, statement_contention_count, original_source_file,
original_source_file_ccsid, routine_type, routine_body, function_origin,
function_type, number_external_routines, extended_indicator, c_nul_required,
naming, target_release, earliest_possible_release, rdb, consistency_token,
@forstie
forstie / Library sizes and more.sql
Created November 27, 2020 18:15
With Db2 PTF Group SF99703 level 22 and Db2 PTF Group SF99704 level 10 (aka TR9 and TR3 timed enhancements), the LIBRARY_INFO UDTF has optional input paramters to provide better performing queries for library specific questions.
-- category: IBM i Services
-- description: Librarian - Library Info
-- minvrm: V7R3M0
--
create or replace variable coolstuff.library_report_stmt varchar(10000) for sbcs data default
'create or replace table coolstuff.library_sizes
(library_name, schema_name,
-- qsys2.library_info() columns