This file is indexed.

/usr/lib/postgresql/9.3/bin/pltcl_loadmod is in postgresql-pltcl-9.3 9.3.4-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#! /bin/sh
# Start tclsh \
exec /usr/bin/tclsh8.6 "$0" "$@"

#
# Code still has to be documented
#

#load /usr/local/pgsql/lib/libpgtcl.so
package require Pgtcl


#
# Check for minimum arguments
#
if {$argc < 2} {
    puts stderr ""
    puts stderr "usage: pltcl_loadmod dbname \[options\] file \[...\]"
    puts stderr ""
    puts stderr "options:"
    puts stderr "    -host hostname"
    puts stderr "    -port portnumber"
    puts stderr ""
    exit 1
}

#
# Remember database name and initialize options
#
set dbname [lindex $argv 0]
set options ""
set errors 0
set opt ""
set val ""

set i 1
while {$i < $argc} {
    if {[string compare [string index [lindex $argv $i] 0] "-"] != 0} {
        break;
    }

    set opt [lindex $argv $i]
    incr i
    if {$i >= $argc} {
        puts stderr "no value given for option $opt"
	incr errors
	continue
    }
    set val [lindex $argv $i]
    incr i

    switch -- $opt {
        -host {
	    append options "-host \"$val\" "
	}
	-port {
	    append options "-port $val "
	}
	default {
	    puts stderr "unknown option '$opt'"
	    incr errors
	}
    }
}

#
# Final syntax check
#
if {$i >= $argc || $errors > 0} {
    puts stderr ""
    puts stderr "usage: pltcl_loadmod dbname \[options\] file \[...\]"
    puts stderr ""
    puts stderr "options:"
    puts stderr "    -host hostname"
    puts stderr "    -port portnumber"
    puts stderr ""
    exit 1
}


proc __PLTcl_loadmod_check_table {conn tabname expnames exptypes} {
    set attrs [expr [llength $expnames] - 1]
    set error 0
    set found 0

    pg_select $conn "select C.relname, A.attname, A.attnum, T.typname 	\
    		from pg_catalog.pg_class C, pg_catalog.pg_attribute A, pg_catalog.pg_type T		\
		where C.relname = '$tabname'				\
		  and A.attrelid = C.oid				\
		  and A.attnum > 0					\
		  and T.oid = A.atttypid				\
		order by attnum" tup {

	incr found
	set i $tup(attnum)

	if {$i > $attrs} {
	    puts stderr "Table $tabname has extra field '$tup(attname)'"
	    incr error
	    continue
	}

	set xname [lindex $expnames $i]
	set xtype [lindex $exptypes $i]

	if {[string compare $tup(attname) $xname] != 0} {
	    puts stderr "Attribute $i of $tabname has wrong name"
	    puts stderr "    got '$tup(attname)' expected '$xname'"
	    incr error
	}
	if {[string compare $tup(typname) $xtype] != 0} {
	    puts stderr "Attribute $i of $tabname has wrong type"
	    puts stderr "    got '$tup(typname)' expected '$xtype'"
	    incr error
	}
    }

    if {$found == 0} {
        return 0
    }

    if {$found < $attrs} {
	incr found
	set miss [lrange $expnames $found end]
        puts "Table $tabname doesn't have field(s) $miss"
	incr error
    }

    if {$error > 0} {
        return 2
    }

    return 1
}


proc __PLTcl_loadmod_check_tables {conn} {
    upvar #0	__PLTcl_loadmod_status	status

    set error 0

    set names {{} modname modseq modsrc}
    set types {{} name int2 text}

    switch [__PLTcl_loadmod_check_table $conn pltcl_modules $names $types] {
        0 {
	    set status(create_table_modules) 1
	}
	1 {
	    set status(create_table_modules) 0
	}
	2 {
	    puts "Error(s) in table pltcl_modules"
	    incr error
	}
    }

    set names {{} funcname modname}
    set types {{} name name}

    switch [__PLTcl_loadmod_check_table $conn pltcl_modfuncs $names $types] {
        0 {
	    set status(create_table_modfuncs) 1
	}
	1 {
	    set status(create_table_modfuncs) 0
	}
	2 {
	    puts "Error(s) in table pltcl_modfuncs"
	    incr error
	}
    }

    if {$status(create_table_modfuncs) && !$status(create_table_modules)} {
        puts stderr "Table pltcl_modfuncs doesn't exist but pltcl_modules does"
	puts stderr "Either both tables must be present or none."
	incr error
    }

    if {$status(create_table_modules) && !$status(create_table_modfuncs)} {
        puts stderr "Table pltcl_modules doesn't exist but pltcl_modfuncs does"
	puts stderr "Either both tables must be present or none."
	incr error
    }

    if {$error} {
        puts stderr ""
	puts stderr "Abort"
	exit 1
    }

    if {!$status(create_table_modules)} {
        __PLTcl_loadmod_read_current $conn
    }
}


proc __PLTcl_loadmod_read_current {conn} {
    upvar #0	__PLTcl_loadmod_status		status
    upvar #0	__PLTcl_loadmod_modsrc		modsrc
    upvar #0	__PLTcl_loadmod_funclist	funcs
    upvar #0	__PLTcl_loadmod_globlist	globs

    set errors 0

    set curmodlist ""
    pg_select $conn "select distinct modname from pltcl_modules" mtup {
	set mname $mtup(modname);
        lappend curmodlist $mname
    }

    foreach mname $curmodlist {
	set srctext ""
        pg_select $conn "select * from pltcl_modules		\
		where modname = '$mname'			\
		order by modseq" tup {
	    append srctext $tup(modsrc)
        }

	if {[catch {
	        __PLTcl_loadmod_analyze 			\
			"Current $mname"			\
			$mname					\
			$srctext new_globals new_functions
	    }]} {
	    incr errors
        }
	set modsrc($mname) $srctext
	set funcs($mname) $new_functions
	set globs($mname) $new_globals
    }

    if {$errors} {
        puts stderr ""
        puts stderr "Abort"
	exit 1
    }
}


proc __PLTcl_loadmod_analyze {modinfo modname srctext v_globals v_functions} {
    upvar 1	$v_globals new_g
    upvar 1	$v_functions new_f
    upvar #0	__PLTcl_loadmod_allfuncs	allfuncs
    upvar #0	__PLTcl_loadmod_allglobs	allglobs

    set errors 0

    set old_g [info globals]
    set old_f [info procs]
    set new_g ""
    set new_f ""

    if {[catch {
	    uplevel #0 "$srctext"
        } msg]} {
        puts "$modinfo: $msg"
	incr errors
    }

    set cur_g [info globals]
    set cur_f [info procs]

    foreach glob $cur_g {
        if {[lsearch -exact $old_g $glob] >= 0} {
	    continue
	}
	if {[info exists allglobs($glob)]} {
	    puts stderr "$modinfo: Global $glob previously used in module $allglobs($glob)"
	    incr errors
	} else {
	    set allglobs($glob) $modname
	}
	lappend new_g $glob
	uplevel #0 unset $glob
    }
    foreach func $cur_f {
        if {[lsearch -exact $old_f $func] >= 0} {
	    continue
	}
	if {[info exists allfuncs($func)]} {
	    puts stderr "$modinfo: Function $func previously defined in module $allfuncs($func)"
	    incr errors
	} else {
	    set allfuncs($func) $modname
	}
	lappend new_f $func
	rename $func {}
    }

    if {$errors} {
        return -code error
    }
    #puts "globs in $modname: $new_g"
    #puts "funcs in $modname: $new_f"
}


proc __PLTcl_loadmod_create_tables {conn} {
    upvar #0	__PLTcl_loadmod_status	status

    if {$status(create_table_modules)} {
        if {[catch {
	        set res [pg_exec $conn				\
		    "create table pltcl_modules (		\
		        modname	name,				\
			modseq	int2,				\
			modsrc	text);"]
	    } msg]} {
	    puts stderr "Error creating table pltcl_modules"
	    puts stderr "    $msg"
	    exit 1
	}
        if {[catch {
	        set res [pg_exec $conn				\
		    "create index pltcl_modules_i 		\
		        on pltcl_modules using btree		\
			(modname name_ops);"]
	    } msg]} {
	    puts stderr "Error creating index pltcl_modules_i"
	    puts stderr "    $msg"
	    exit 1
	}
	puts "Table pltcl_modules created"
	pg_result $res -clear
    }

    if {$status(create_table_modfuncs)} {
        if {[catch {
	        set res [pg_exec $conn				\
		    "create table pltcl_modfuncs (		\
		        funcname name,				\
			modname	 name);"]
	    } msg]} {
	    puts stderr "Error creating table pltcl_modfuncs"
	    puts stderr "    $msg"
	    exit 1
	}
        if {[catch {
	        set res [pg_exec $conn				\
		    "create index pltcl_modfuncs_i 		\
		        on pltcl_modfuncs using hash		\
			(funcname name_ops);"]
	    } msg]} {
	    puts stderr "Error creating index pltcl_modfuncs_i"
	    puts stderr "    $msg"
	    exit 1
	}
	puts "Table pltcl_modfuncs created"
	pg_result $res -clear
    }
}


proc __PLTcl_loadmod_read_new {conn} {
    upvar #0	__PLTcl_loadmod_status		status
    upvar #0	__PLTcl_loadmod_modsrc		modsrc
    upvar #0	__PLTcl_loadmod_funclist	funcs
    upvar #0	__PLTcl_loadmod_globlist	globs
    upvar #0	__PLTcl_loadmod_allfuncs	allfuncs
    upvar #0	__PLTcl_loadmod_allglobs	allglobs
    upvar #0	__PLTcl_loadmod_modlist		modlist

    set errors 0

    set new_modlist ""
    foreach modfile $modlist {
        set modname [file rootname [file tail $modfile]]
	if {[catch {
	        set fid [open $modfile "r"]
	    } msg]} {
	    puts stderr $msg
	    incr errors
	    continue
        }
	set srctext [read $fid]
	close $fid

	if {[info exists modsrc($modname)]} {
	    if {[string compare $modsrc($modname) $srctext] == 0} {
	        puts "Module $modname unchanged - ignored"
		continue
	    }
	    foreach func $funcs($modname) {
	        unset allfuncs($func)
	    }
	    foreach glob $globs($modname) {
	        unset allglobs($glob)
	    }
	    unset funcs($modname)
	    unset globs($modname)
	    set modsrc($modname) $srctext
	    lappend new_modlist $modname
	} else {
	    set modsrc($modname) $srctext
	    lappend new_modlist $modname
	}

	if {[catch {
	        __PLTcl_loadmod_analyze "New/updated $modname"	\
			$modname $srctext new_globals new_funcs
	    }]} {
	    incr errors
	}

	set funcs($modname) $new_funcs
	set globs($modname) $new_globals
    }

    if {$errors} {
        puts stderr ""
        puts stderr "Abort"
	exit 1
    }

    set modlist $new_modlist
}


proc __PLTcl_loadmod_load_modules {conn} {
    upvar #0	__PLTcl_loadmod_modsrc		modsrc
    upvar #0	__PLTcl_loadmod_funclist	funcs
    upvar #0	__PLTcl_loadmod_modlist		modlist

    set errors 0

    foreach modname $modlist {
	set xname [__PLTcl_loadmod_quote $modname]

        pg_result [pg_exec $conn "begin;"] -clear

	pg_result [pg_exec $conn 				\
		"delete from pltcl_modules where modname = '$xname'"] -clear
	pg_result [pg_exec $conn 				\
		"delete from pltcl_modfuncs where modname = '$xname'"] -clear

	foreach func $funcs($modname) {
	    set xfunc [__PLTcl_loadmod_quote $func]
	    pg_result [							\
	        pg_exec $conn "insert into pltcl_modfuncs values (	\
			'$xfunc', '$xname')"				\
	    ] -clear
	}
	set i 0
	set srctext $modsrc($modname)
	while {[string compare $srctext ""] != 0} {
	    set xpart [string range $srctext 0 3999]
	    set xpart [__PLTcl_loadmod_quote $xpart]
	    set srctext [string range $srctext 4000 end]

	    pg_result [							\
	    	pg_exec $conn "insert into pltcl_modules values (	\
			'$xname', $i, '$xpart')"			\
	    ] -clear
	    incr i
	}

        pg_result [pg_exec $conn "commit;"] -clear

	puts "Successfully loaded/updated module $modname"
    }
}


proc __PLTcl_loadmod_quote {s} {
    regsub -all {\\} $s {\\\\} s
    regsub -all {'}  $s {''} s
    return $s
}


set __PLTcl_loadmod_modlist [lrange $argv $i end]
set __PLTcl_loadmod_modsrc(dummy) ""
set __PLTcl_loadmod_funclist(dummy) ""
set __PLTcl_loadmod_globlist(dummy) ""
set __PLTcl_loadmod_allfuncs(dummy) ""
set __PLTcl_loadmod_allglobs(dummy) ""

unset __PLTcl_loadmod_modsrc(dummy)
unset __PLTcl_loadmod_funclist(dummy)
unset __PLTcl_loadmod_globlist(dummy)
unset __PLTcl_loadmod_allfuncs(dummy)
unset __PLTcl_loadmod_allglobs(dummy)


puts ""

set __PLTcl_loadmod_conn [eval pg_connect $dbname $options]

unset i dbname options errors opt val

__PLTcl_loadmod_check_tables $__PLTcl_loadmod_conn

__PLTcl_loadmod_read_new $__PLTcl_loadmod_conn

__PLTcl_loadmod_create_tables $__PLTcl_loadmod_conn
__PLTcl_loadmod_load_modules $__PLTcl_loadmod_conn

pg_disconnect $__PLTcl_loadmod_conn

puts ""